8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.util;
27
28 /**
29 * A comparison function, which imposes a <i>total ordering</i> on some
30 * collection of objects. Comparators can be passed to a sort method (such
31 * as {@link Collections#sort(List,Comparator) Collections.sort} or {@link
32 * Arrays#sort(Object[],Comparator) Arrays.sort}) to allow precise control
33 * over the sort order. Comparators can also be used to control the order of
34 * certain data structures (such as {@link SortedSet sorted sets} or {@link
35 * SortedMap sorted maps}), or to provide an ordering for collections of
36 * objects that don't have a {@link Comparable natural ordering}.<p>
37 *
38 * The ordering imposed by a comparator <tt>c</tt> on a set of elements
39 * <tt>S</tt> is said to be <i>consistent with equals</i> if and only if
40 * <tt>c.compare(e1, e2)==0</tt> has the same boolean value as
41 * <tt>e1.equals(e2)</tt> for every <tt>e1</tt> and <tt>e2</tt> in
42 * <tt>S</tt>.<p>
43 *
44 * Caution should be exercised when using a comparator capable of imposing an
45 * ordering inconsistent with equals to order a sorted set (or sorted map).
46 * Suppose a sorted set (or sorted map) with an explicit comparator <tt>c</tt>
47 * is used with elements (or keys) drawn from a set <tt>S</tt>. If the
148 * {@link Object#equals(Object)}. Additionally, this method can return
149 * <tt>true</tt> <i>only</i> if the specified object is also a comparator
150 * and it imposes the same ordering as this comparator. Thus,
151 * <code>comp1.equals(comp2)</code> implies that <tt>sgn(comp1.compare(o1,
152 * o2))==sgn(comp2.compare(o1, o2))</tt> for every object reference
153 * <tt>o1</tt> and <tt>o2</tt>.<p>
154 *
155 * Note that it is <i>always</i> safe <i>not</i> to override
156 * <tt>Object.equals(Object)</tt>. However, overriding this method may,
157 * in some cases, improve performance by allowing programs to determine
158 * that two distinct comparators impose the same order.
159 *
160 * @param obj the reference object with which to compare.
161 * @return <code>true</code> only if the specified object is also
162 * a comparator and it imposes the same ordering as this
163 * comparator.
164 * @see Object#equals(Object)
165 * @see Object#hashCode()
166 */
167 boolean equals(Object obj);
168 }
|
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.util;
27
28 import java.util.function.Function;
29 import java.util.function.ToIntFunction;
30 import java.util.function.ToLongFunction;
31 import java.util.function.ToDoubleFunction;
32
33 /**
34 * A comparison function, which imposes a <i>total ordering</i> on some
35 * collection of objects. Comparators can be passed to a sort method (such
36 * as {@link Collections#sort(List,Comparator) Collections.sort} or {@link
37 * Arrays#sort(Object[],Comparator) Arrays.sort}) to allow precise control
38 * over the sort order. Comparators can also be used to control the order of
39 * certain data structures (such as {@link SortedSet sorted sets} or {@link
40 * SortedMap sorted maps}), or to provide an ordering for collections of
41 * objects that don't have a {@link Comparable natural ordering}.<p>
42 *
43 * The ordering imposed by a comparator <tt>c</tt> on a set of elements
44 * <tt>S</tt> is said to be <i>consistent with equals</i> if and only if
45 * <tt>c.compare(e1, e2)==0</tt> has the same boolean value as
46 * <tt>e1.equals(e2)</tt> for every <tt>e1</tt> and <tt>e2</tt> in
47 * <tt>S</tt>.<p>
48 *
49 * Caution should be exercised when using a comparator capable of imposing an
50 * ordering inconsistent with equals to order a sorted set (or sorted map).
51 * Suppose a sorted set (or sorted map) with an explicit comparator <tt>c</tt>
52 * is used with elements (or keys) drawn from a set <tt>S</tt>. If the
153 * {@link Object#equals(Object)}. Additionally, this method can return
154 * <tt>true</tt> <i>only</i> if the specified object is also a comparator
155 * and it imposes the same ordering as this comparator. Thus,
156 * <code>comp1.equals(comp2)</code> implies that <tt>sgn(comp1.compare(o1,
157 * o2))==sgn(comp2.compare(o1, o2))</tt> for every object reference
158 * <tt>o1</tt> and <tt>o2</tt>.<p>
159 *
160 * Note that it is <i>always</i> safe <i>not</i> to override
161 * <tt>Object.equals(Object)</tt>. However, overriding this method may,
162 * in some cases, improve performance by allowing programs to determine
163 * that two distinct comparators impose the same order.
164 *
165 * @param obj the reference object with which to compare.
166 * @return <code>true</code> only if the specified object is also
167 * a comparator and it imposes the same ordering as this
168 * comparator.
169 * @see Object#equals(Object)
170 * @see Object#hashCode()
171 */
172 boolean equals(Object obj);
173
174 /**
175 * Returns a comparator that imposes the reverse ordering of this
176 * comparator.
177 *
178 * @return A comparator that imposes the reverse ordering of this
179 * comparator.
180 * @since 1.8
181 */
182 default Comparator<T> reverseOrder() {
183 return Collections.reverseOrder(this);
184 }
185
186 /**
187 * Constructs a lexicographic order comparator with another comparator.
188 * For example, a {@code Comparator<Person> byLastName} can be composed
189 * with another {@code Comparator<Person> byFirstName}, then {@code
190 * byLastName.thenComparing(byFirstName)} creates a {@code
191 * Comparator<Person>} which sorts by last name, and for equal last names
192 * sorts by first name.
193 *
194 * @param other the other comparator used when equals on this.
195 * @throws NullPointerException if the argument is null.
196 * @since 1.8
197 */
198 default Comparator<T> thenComparing(Comparator<? super T> other) {
199 return Comparators.compose(this, other);
200 }
201
202 /**
203 * Constructs a lexicographic order comparator with a function that
204 * extracts a {@code Comparable} key. This default implementation calls
205 * {@code thenComparing(this, Comparators.comparing(keyExtractor))}.
206 *
207 * @param <U> the {@link Comparable} type for comparison
208 * @param keyExtractor the function used to extract the {@link Comparable} sort key
209 * @throws NullPointerException if the argument is null.
210 * @see Comparators#comparing(Function)
211 * @see #thenComparing(Comparator)
212 * @since 1.8
213 */
214 default <U extends Comparable<? super U>> Comparator<T> thenComparing(Function<? super T, ? extends U> keyExtractor) {
215 return thenComparing(Comparators.comparing(keyExtractor));
216 }
217
218 /**
219 * Constructs a lexicographic order comparator with a function that
220 * extracts a {@code int} value. This default implementation calls {@code
221 * thenComparing(this, Comparators.comparing(keyExtractor))}.
222 *
223 * @param keyExtractor the function used to extract the integer value
224 * @throws NullPointerException if the argument is null.
225 * @see Comparators#comparing(ToIntFunction)
226 * @see #thenComparing(Comparator)
227 * @since 1.8
228 */
229 default Comparator<T> thenComparing(ToIntFunction<? super T> keyExtractor) {
230 return thenComparing(Comparators.comparing(keyExtractor));
231 }
232
233 /**
234 * Constructs a lexicographic order comparator with a function that
235 * extracts a {@code long} value. This default implementation calls
236 * {@code thenComparing(this, Comparators.comparing(keyExtractor))}.
237 *
238 * @param keyExtractor the function used to extract the long value
239 * @throws NullPointerException if the argument is null.
240 * @see Comparators#comparing(ToLongFunction)
241 * @see #thenComparing(Comparator)
242 * @since 1.8
243 */
244 default Comparator<T> thenComparing(ToLongFunction<? super T> keyExtractor) {
245 return thenComparing(Comparators.comparing(keyExtractor));
246 }
247
248 /**
249 * Constructs a lexicographic order comparator with a function that
250 * extracts a {@code double} value. This default implementation calls
251 * {@code thenComparing(this, Comparators.comparing(keyExtractor))}.
252 *
253 * @param keyExtractor the function used to extract the double value
254 * @throws NullPointerException if the argument is null.
255 * @see Comparators#comparing(ToDoubleFunction)
256 * @see #thenComparing(Comparator)
257 * @since 1.8
258 */
259 default Comparator<T> thenComparing(ToDoubleFunction<? super T> keyExtractor) {
260 return thenComparing(Comparators.comparing(keyExtractor));
261 }
262 }
|