author | henryjen |
Tue, 22 Oct 2013 15:12:22 -0700 | |
changeset 21339 | 20e8b81964d5 |
parent 19867 | 9918d2d14f78 |
child 22944 | a9e7afb60f4f |
permissions | -rw-r--r-- |
2 | 1 |
/* |
15647
314007859004
8005623: Retrofit FunctionalInterface annotations to core platform interfaces
darcy
parents:
5506
diff
changeset
|
2 |
* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.util; |
|
27 |
||
18571 | 28 |
import java.io.Serializable; |
16064
ef93558b0d63
8001667: Comparator combinators and extension methods
mduigou
parents:
15647
diff
changeset
|
29 |
import java.util.function.Function; |
ef93558b0d63
8001667: Comparator combinators and extension methods
mduigou
parents:
15647
diff
changeset
|
30 |
import java.util.function.ToIntFunction; |
ef93558b0d63
8001667: Comparator combinators and extension methods
mduigou
parents:
15647
diff
changeset
|
31 |
import java.util.function.ToLongFunction; |
ef93558b0d63
8001667: Comparator combinators and extension methods
mduigou
parents:
15647
diff
changeset
|
32 |
import java.util.function.ToDoubleFunction; |
18571 | 33 |
import java.util.Comparators; |
16064
ef93558b0d63
8001667: Comparator combinators and extension methods
mduigou
parents:
15647
diff
changeset
|
34 |
|
2 | 35 |
/** |
36 |
* A comparison function, which imposes a <i>total ordering</i> on some |
|
37 |
* collection of objects. Comparators can be passed to a sort method (such |
|
38 |
* as {@link Collections#sort(List,Comparator) Collections.sort} or {@link |
|
39 |
* Arrays#sort(Object[],Comparator) Arrays.sort}) to allow precise control |
|
40 |
* over the sort order. Comparators can also be used to control the order of |
|
41 |
* certain data structures (such as {@link SortedSet sorted sets} or {@link |
|
42 |
* SortedMap sorted maps}), or to provide an ordering for collections of |
|
43 |
* objects that don't have a {@link Comparable natural ordering}.<p> |
|
44 |
* |
|
45 |
* The ordering imposed by a comparator <tt>c</tt> on a set of elements |
|
46 |
* <tt>S</tt> is said to be <i>consistent with equals</i> if and only if |
|
47 |
* <tt>c.compare(e1, e2)==0</tt> has the same boolean value as |
|
48 |
* <tt>e1.equals(e2)</tt> for every <tt>e1</tt> and <tt>e2</tt> in |
|
49 |
* <tt>S</tt>.<p> |
|
50 |
* |
|
51 |
* Caution should be exercised when using a comparator capable of imposing an |
|
52 |
* ordering inconsistent with equals to order a sorted set (or sorted map). |
|
53 |
* Suppose a sorted set (or sorted map) with an explicit comparator <tt>c</tt> |
|
54 |
* is used with elements (or keys) drawn from a set <tt>S</tt>. If the |
|
55 |
* ordering imposed by <tt>c</tt> on <tt>S</tt> is inconsistent with equals, |
|
56 |
* the sorted set (or sorted map) will behave "strangely." In particular the |
|
57 |
* sorted set (or sorted map) will violate the general contract for set (or |
|
58 |
* map), which is defined in terms of <tt>equals</tt>.<p> |
|
59 |
* |
|
60 |
* For example, suppose one adds two elements {@code a} and {@code b} such that |
|
61 |
* {@code (a.equals(b) && c.compare(a, b) != 0)} |
|
62 |
* to an empty {@code TreeSet} with comparator {@code c}. |
|
63 |
* The second {@code add} operation will return |
|
64 |
* true (and the size of the tree set will increase) because {@code a} and |
|
65 |
* {@code b} are not equivalent from the tree set's perspective, even though |
|
66 |
* this is contrary to the specification of the |
|
67 |
* {@link Set#add Set.add} method.<p> |
|
68 |
* |
|
69 |
* Note: It is generally a good idea for comparators to also implement |
|
70 |
* <tt>java.io.Serializable</tt>, as they may be used as ordering methods in |
|
71 |
* serializable data structures (like {@link TreeSet}, {@link TreeMap}). In |
|
72 |
* order for the data structure to serialize successfully, the comparator (if |
|
73 |
* provided) must implement <tt>Serializable</tt>.<p> |
|
74 |
* |
|
75 |
* For the mathematically inclined, the <i>relation</i> that defines the |
|
76 |
* <i>imposed ordering</i> that a given comparator <tt>c</tt> imposes on a |
|
77 |
* given set of objects <tt>S</tt> is:<pre> |
|
78 |
* {(x, y) such that c.compare(x, y) <= 0}. |
|
79 |
* </pre> The <i>quotient</i> for this total order is:<pre> |
|
80 |
* {(x, y) such that c.compare(x, y) == 0}. |
|
81 |
* </pre> |
|
82 |
* |
|
83 |
* It follows immediately from the contract for <tt>compare</tt> that the |
|
84 |
* quotient is an <i>equivalence relation</i> on <tt>S</tt>, and that the |
|
85 |
* imposed ordering is a <i>total order</i> on <tt>S</tt>. When we say that |
|
86 |
* the ordering imposed by <tt>c</tt> on <tt>S</tt> is <i>consistent with |
|
87 |
* equals</i>, we mean that the quotient for the ordering is the equivalence |
|
88 |
* relation defined by the objects' {@link Object#equals(Object) |
|
89 |
* equals(Object)} method(s):<pre> |
|
90 |
* {(x, y) such that x.equals(y)}. </pre> |
|
91 |
* |
|
92 |
* <p>Unlike {@code Comparable}, a comparator may optionally permit |
|
93 |
* comparison of null arguments, while maintaining the requirements for |
|
94 |
* an equivalence relation. |
|
95 |
* |
|
96 |
* <p>This interface is a member of the |
|
97 |
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
|
98 |
* Java Collections Framework</a>. |
|
99 |
* |
|
100 |
* @param <T> the type of objects that may be compared by this comparator |
|
101 |
* |
|
102 |
* @author Josh Bloch |
|
103 |
* @author Neal Gafter |
|
104 |
* @see Comparable |
|
105 |
* @see java.io.Serializable |
|
106 |
* @since 1.2 |
|
107 |
*/ |
|
15647
314007859004
8005623: Retrofit FunctionalInterface annotations to core platform interfaces
darcy
parents:
5506
diff
changeset
|
108 |
@FunctionalInterface |
2 | 109 |
public interface Comparator<T> { |
110 |
/** |
|
111 |
* Compares its two arguments for order. Returns a negative integer, |
|
112 |
* zero, or a positive integer as the first argument is less than, equal |
|
113 |
* to, or greater than the second.<p> |
|
114 |
* |
|
115 |
* In the foregoing description, the notation |
|
116 |
* <tt>sgn(</tt><i>expression</i><tt>)</tt> designates the mathematical |
|
117 |
* <i>signum</i> function, which is defined to return one of <tt>-1</tt>, |
|
118 |
* <tt>0</tt>, or <tt>1</tt> according to whether the value of |
|
119 |
* <i>expression</i> is negative, zero or positive.<p> |
|
120 |
* |
|
121 |
* The implementor must ensure that <tt>sgn(compare(x, y)) == |
|
122 |
* -sgn(compare(y, x))</tt> for all <tt>x</tt> and <tt>y</tt>. (This |
|
123 |
* implies that <tt>compare(x, y)</tt> must throw an exception if and only |
|
124 |
* if <tt>compare(y, x)</tt> throws an exception.)<p> |
|
125 |
* |
|
126 |
* The implementor must also ensure that the relation is transitive: |
|
127 |
* <tt>((compare(x, y)>0) && (compare(y, z)>0))</tt> implies |
|
128 |
* <tt>compare(x, z)>0</tt>.<p> |
|
129 |
* |
|
130 |
* Finally, the implementor must ensure that <tt>compare(x, y)==0</tt> |
|
131 |
* implies that <tt>sgn(compare(x, z))==sgn(compare(y, z))</tt> for all |
|
132 |
* <tt>z</tt>.<p> |
|
133 |
* |
|
134 |
* It is generally the case, but <i>not</i> strictly required that |
|
135 |
* <tt>(compare(x, y)==0) == (x.equals(y))</tt>. Generally speaking, |
|
136 |
* any comparator that violates this condition should clearly indicate |
|
137 |
* this fact. The recommended language is "Note: this comparator |
|
138 |
* imposes orderings that are inconsistent with equals." |
|
139 |
* |
|
140 |
* @param o1 the first object to be compared. |
|
141 |
* @param o2 the second object to be compared. |
|
142 |
* @return a negative integer, zero, or a positive integer as the |
|
143 |
* first argument is less than, equal to, or greater than the |
|
144 |
* second. |
|
145 |
* @throws NullPointerException if an argument is null and this |
|
146 |
* comparator does not permit null arguments |
|
147 |
* @throws ClassCastException if the arguments' types prevent them from |
|
148 |
* being compared by this comparator. |
|
149 |
*/ |
|
150 |
int compare(T o1, T o2); |
|
151 |
||
152 |
/** |
|
153 |
* Indicates whether some other object is "equal to" this |
|
154 |
* comparator. This method must obey the general contract of |
|
155 |
* {@link Object#equals(Object)}. Additionally, this method can return |
|
156 |
* <tt>true</tt> <i>only</i> if the specified object is also a comparator |
|
157 |
* and it imposes the same ordering as this comparator. Thus, |
|
158 |
* <code>comp1.equals(comp2)</code> implies that <tt>sgn(comp1.compare(o1, |
|
159 |
* o2))==sgn(comp2.compare(o1, o2))</tt> for every object reference |
|
160 |
* <tt>o1</tt> and <tt>o2</tt>.<p> |
|
161 |
* |
|
162 |
* Note that it is <i>always</i> safe <i>not</i> to override |
|
163 |
* <tt>Object.equals(Object)</tt>. However, overriding this method may, |
|
164 |
* in some cases, improve performance by allowing programs to determine |
|
165 |
* that two distinct comparators impose the same order. |
|
166 |
* |
|
167 |
* @param obj the reference object with which to compare. |
|
168 |
* @return <code>true</code> only if the specified object is also |
|
169 |
* a comparator and it imposes the same ordering as this |
|
170 |
* comparator. |
|
171 |
* @see Object#equals(Object) |
|
172 |
* @see Object#hashCode() |
|
173 |
*/ |
|
174 |
boolean equals(Object obj); |
|
16064
ef93558b0d63
8001667: Comparator combinators and extension methods
mduigou
parents:
15647
diff
changeset
|
175 |
|
ef93558b0d63
8001667: Comparator combinators and extension methods
mduigou
parents:
15647
diff
changeset
|
176 |
/** |
ef93558b0d63
8001667: Comparator combinators and extension methods
mduigou
parents:
15647
diff
changeset
|
177 |
* Returns a comparator that imposes the reverse ordering of this |
ef93558b0d63
8001667: Comparator combinators and extension methods
mduigou
parents:
15647
diff
changeset
|
178 |
* comparator. |
ef93558b0d63
8001667: Comparator combinators and extension methods
mduigou
parents:
15647
diff
changeset
|
179 |
* |
18571 | 180 |
* @return a comparator that imposes the reverse ordering of this |
16064
ef93558b0d63
8001667: Comparator combinators and extension methods
mduigou
parents:
15647
diff
changeset
|
181 |
* comparator. |
ef93558b0d63
8001667: Comparator combinators and extension methods
mduigou
parents:
15647
diff
changeset
|
182 |
* @since 1.8 |
ef93558b0d63
8001667: Comparator combinators and extension methods
mduigou
parents:
15647
diff
changeset
|
183 |
*/ |
18571 | 184 |
default Comparator<T> reversed() { |
16064
ef93558b0d63
8001667: Comparator combinators and extension methods
mduigou
parents:
15647
diff
changeset
|
185 |
return Collections.reverseOrder(this); |
ef93558b0d63
8001667: Comparator combinators and extension methods
mduigou
parents:
15647
diff
changeset
|
186 |
} |
ef93558b0d63
8001667: Comparator combinators and extension methods
mduigou
parents:
15647
diff
changeset
|
187 |
|
ef93558b0d63
8001667: Comparator combinators and extension methods
mduigou
parents:
15647
diff
changeset
|
188 |
/** |
18571 | 189 |
* Returns a lexicographic-order comparator with another comparator. |
190 |
* If this {@code Comparator} considers two elements equal, i.e. |
|
191 |
* {@code compare(a, b) == 0}, {@code other} is used to determine the order. |
|
192 |
* |
|
193 |
* <p>The returned comparator is serializable if the specified comparator |
|
194 |
* is also serializable. |
|
16064
ef93558b0d63
8001667: Comparator combinators and extension methods
mduigou
parents:
15647
diff
changeset
|
195 |
* |
18571 | 196 |
* @apiNote |
197 |
* For example, to sort a collection of {@code String} based on the length |
|
198 |
* and then case-insensitive natural ordering, the comparator can be |
|
199 |
* composed using following code, |
|
200 |
* |
|
201 |
* <pre>{@code |
|
19605
0f98f8e3bdcb
8023528: Rename Comparator combinators to disambiguate overloading methods
henryjen
parents:
19208
diff
changeset
|
202 |
* Comparator<String> cmp = Comparator.comparingInt(String::length) |
18571 | 203 |
* .thenComparing(String.CASE_INSENSITIVE_ORDER); |
204 |
* }</pre> |
|
205 |
* |
|
206 |
* @param other the other comparator to be used when this comparator |
|
207 |
* compares two objects that are equal. |
|
208 |
* @return a lexicographic-order comparator composed of this and then the |
|
209 |
* other comparator |
|
16064
ef93558b0d63
8001667: Comparator combinators and extension methods
mduigou
parents:
15647
diff
changeset
|
210 |
* @throws NullPointerException if the argument is null. |
ef93558b0d63
8001667: Comparator combinators and extension methods
mduigou
parents:
15647
diff
changeset
|
211 |
* @since 1.8 |
ef93558b0d63
8001667: Comparator combinators and extension methods
mduigou
parents:
15647
diff
changeset
|
212 |
*/ |
ef93558b0d63
8001667: Comparator combinators and extension methods
mduigou
parents:
15647
diff
changeset
|
213 |
default Comparator<T> thenComparing(Comparator<? super T> other) { |
18571 | 214 |
Objects.requireNonNull(other); |
215 |
return (Comparator<T> & Serializable) (c1, c2) -> { |
|
216 |
int res = compare(c1, c2); |
|
217 |
return (res != 0) ? res : other.compare(c1, c2); |
|
218 |
}; |
|
219 |
} |
|
220 |
||
221 |
/** |
|
222 |
* Returns a lexicographic-order comparator with a function that |
|
223 |
* extracts a key to be compared with the given {@code Comparator}. |
|
224 |
* |
|
225 |
* @implSpec This default implementation behaves as if {@code |
|
226 |
* thenComparing(comparing(keyExtractor, cmp))}. |
|
227 |
* |
|
228 |
* @param <U> the type of the sort key |
|
229 |
* @param keyExtractor the function used to extract the sort key |
|
230 |
* @param keyComparator the {@code Comparator} used to compare the sort key |
|
231 |
* @return a lexicographic-order comparator composed of this comparator |
|
232 |
* and then comparing on the key extracted by the keyExtractor function |
|
19867
9918d2d14f78
8024874: Copy-paste typo in the spec for j.u.Comparator.thenComparing(Function, Comparator)
henryjen
parents:
19605
diff
changeset
|
233 |
* @throws NullPointerException if either argument is null. |
18571 | 234 |
* @see #comparing(Function, Comparator) |
235 |
* @see #thenComparing(Comparator) |
|
236 |
* @since 1.8 |
|
237 |
*/ |
|
238 |
default <U extends Comparable<? super U>> Comparator<T> thenComparing( |
|
239 |
Function<? super T, ? extends U> keyExtractor, |
|
240 |
Comparator<? super U> keyComparator) |
|
241 |
{ |
|
242 |
return thenComparing(comparing(keyExtractor, keyComparator)); |
|
16064
ef93558b0d63
8001667: Comparator combinators and extension methods
mduigou
parents:
15647
diff
changeset
|
243 |
} |
ef93558b0d63
8001667: Comparator combinators and extension methods
mduigou
parents:
15647
diff
changeset
|
244 |
|
ef93558b0d63
8001667: Comparator combinators and extension methods
mduigou
parents:
15647
diff
changeset
|
245 |
/** |
18571 | 246 |
* Returns a lexicographic-order comparator with a function that |
247 |
* extracts a {@code Comparable} sort key. |
|
248 |
* |
|
249 |
* @implSpec This default implementation behaves as if {@code |
|
250 |
* thenComparing(comparing(keyExtractor))}. |
|
16064
ef93558b0d63
8001667: Comparator combinators and extension methods
mduigou
parents:
15647
diff
changeset
|
251 |
* |
18571 | 252 |
* @param <U> the type of the {@link Comparable} sort key |
253 |
* @param keyExtractor the function used to extract the {@link |
|
254 |
* Comparable} sort key |
|
255 |
* @return a lexicographic-order comparator composed of this and then the |
|
256 |
* {@link Comparable} sort key. |
|
16064
ef93558b0d63
8001667: Comparator combinators and extension methods
mduigou
parents:
15647
diff
changeset
|
257 |
* @throws NullPointerException if the argument is null. |
18571 | 258 |
* @see #comparing(Function) |
16064
ef93558b0d63
8001667: Comparator combinators and extension methods
mduigou
parents:
15647
diff
changeset
|
259 |
* @see #thenComparing(Comparator) |
ef93558b0d63
8001667: Comparator combinators and extension methods
mduigou
parents:
15647
diff
changeset
|
260 |
* @since 1.8 |
ef93558b0d63
8001667: Comparator combinators and extension methods
mduigou
parents:
15647
diff
changeset
|
261 |
*/ |
18571 | 262 |
default <U extends Comparable<? super U>> Comparator<T> thenComparing( |
263 |
Function<? super T, ? extends U> keyExtractor) |
|
264 |
{ |
|
265 |
return thenComparing(comparing(keyExtractor)); |
|
16064
ef93558b0d63
8001667: Comparator combinators and extension methods
mduigou
parents:
15647
diff
changeset
|
266 |
} |
ef93558b0d63
8001667: Comparator combinators and extension methods
mduigou
parents:
15647
diff
changeset
|
267 |
|
ef93558b0d63
8001667: Comparator combinators and extension methods
mduigou
parents:
15647
diff
changeset
|
268 |
/** |
18571 | 269 |
* Returns a lexicographic-order comparator with a function that |
270 |
* extracts a {@code int} sort key. |
|
271 |
* |
|
272 |
* @implSpec This default implementation behaves as if {@code |
|
19605
0f98f8e3bdcb
8023528: Rename Comparator combinators to disambiguate overloading methods
henryjen
parents:
19208
diff
changeset
|
273 |
* thenComparing(comparingInt(keyExtractor))}. |
16064
ef93558b0d63
8001667: Comparator combinators and extension methods
mduigou
parents:
15647
diff
changeset
|
274 |
* |
18571 | 275 |
* @param keyExtractor the function used to extract the integer sort key |
276 |
* @return a lexicographic-order comparator composed of this and then the |
|
277 |
* {@code int} sort key |
|
16064
ef93558b0d63
8001667: Comparator combinators and extension methods
mduigou
parents:
15647
diff
changeset
|
278 |
* @throws NullPointerException if the argument is null. |
19605
0f98f8e3bdcb
8023528: Rename Comparator combinators to disambiguate overloading methods
henryjen
parents:
19208
diff
changeset
|
279 |
* @see #comparingInt(ToIntFunction) |
16064
ef93558b0d63
8001667: Comparator combinators and extension methods
mduigou
parents:
15647
diff
changeset
|
280 |
* @see #thenComparing(Comparator) |
ef93558b0d63
8001667: Comparator combinators and extension methods
mduigou
parents:
15647
diff
changeset
|
281 |
* @since 1.8 |
ef93558b0d63
8001667: Comparator combinators and extension methods
mduigou
parents:
15647
diff
changeset
|
282 |
*/ |
19605
0f98f8e3bdcb
8023528: Rename Comparator combinators to disambiguate overloading methods
henryjen
parents:
19208
diff
changeset
|
283 |
default Comparator<T> thenComparingInt(ToIntFunction<? super T> keyExtractor) { |
0f98f8e3bdcb
8023528: Rename Comparator combinators to disambiguate overloading methods
henryjen
parents:
19208
diff
changeset
|
284 |
return thenComparing(comparingInt(keyExtractor)); |
16064
ef93558b0d63
8001667: Comparator combinators and extension methods
mduigou
parents:
15647
diff
changeset
|
285 |
} |
ef93558b0d63
8001667: Comparator combinators and extension methods
mduigou
parents:
15647
diff
changeset
|
286 |
|
ef93558b0d63
8001667: Comparator combinators and extension methods
mduigou
parents:
15647
diff
changeset
|
287 |
/** |
18571 | 288 |
* Returns a lexicographic-order comparator with a function that |
289 |
* extracts a {@code long} sort key. |
|
290 |
* |
|
291 |
* @implSpec This default implementation behaves as if {@code |
|
19605
0f98f8e3bdcb
8023528: Rename Comparator combinators to disambiguate overloading methods
henryjen
parents:
19208
diff
changeset
|
292 |
* thenComparing(comparingLong(keyExtractor))}. |
16064
ef93558b0d63
8001667: Comparator combinators and extension methods
mduigou
parents:
15647
diff
changeset
|
293 |
* |
18571 | 294 |
* @param keyExtractor the function used to extract the long sort key |
295 |
* @return a lexicographic-order comparator composed of this and then the |
|
296 |
* {@code long} sort key |
|
16064
ef93558b0d63
8001667: Comparator combinators and extension methods
mduigou
parents:
15647
diff
changeset
|
297 |
* @throws NullPointerException if the argument is null. |
19605
0f98f8e3bdcb
8023528: Rename Comparator combinators to disambiguate overloading methods
henryjen
parents:
19208
diff
changeset
|
298 |
* @see #comparingLong(ToLongFunction) |
16064
ef93558b0d63
8001667: Comparator combinators and extension methods
mduigou
parents:
15647
diff
changeset
|
299 |
* @see #thenComparing(Comparator) |
ef93558b0d63
8001667: Comparator combinators and extension methods
mduigou
parents:
15647
diff
changeset
|
300 |
* @since 1.8 |
ef93558b0d63
8001667: Comparator combinators and extension methods
mduigou
parents:
15647
diff
changeset
|
301 |
*/ |
19605
0f98f8e3bdcb
8023528: Rename Comparator combinators to disambiguate overloading methods
henryjen
parents:
19208
diff
changeset
|
302 |
default Comparator<T> thenComparingLong(ToLongFunction<? super T> keyExtractor) { |
0f98f8e3bdcb
8023528: Rename Comparator combinators to disambiguate overloading methods
henryjen
parents:
19208
diff
changeset
|
303 |
return thenComparing(comparingLong(keyExtractor)); |
16064
ef93558b0d63
8001667: Comparator combinators and extension methods
mduigou
parents:
15647
diff
changeset
|
304 |
} |
ef93558b0d63
8001667: Comparator combinators and extension methods
mduigou
parents:
15647
diff
changeset
|
305 |
|
ef93558b0d63
8001667: Comparator combinators and extension methods
mduigou
parents:
15647
diff
changeset
|
306 |
/** |
18571 | 307 |
* Returns a lexicographic-order comparator with a function that |
308 |
* extracts a {@code double} sort key. |
|
309 |
* |
|
310 |
* @implSpec This default implementation behaves as if {@code |
|
19605
0f98f8e3bdcb
8023528: Rename Comparator combinators to disambiguate overloading methods
henryjen
parents:
19208
diff
changeset
|
311 |
* thenComparing(comparingDouble(keyExtractor))}. |
16064
ef93558b0d63
8001667: Comparator combinators and extension methods
mduigou
parents:
15647
diff
changeset
|
312 |
* |
18571 | 313 |
* @param keyExtractor the function used to extract the double sort key |
314 |
* @return a lexicographic-order comparator composed of this and then the |
|
315 |
* {@code double} sort key |
|
16064
ef93558b0d63
8001667: Comparator combinators and extension methods
mduigou
parents:
15647
diff
changeset
|
316 |
* @throws NullPointerException if the argument is null. |
19605
0f98f8e3bdcb
8023528: Rename Comparator combinators to disambiguate overloading methods
henryjen
parents:
19208
diff
changeset
|
317 |
* @see #comparingDouble(ToDoubleFunction) |
16064
ef93558b0d63
8001667: Comparator combinators and extension methods
mduigou
parents:
15647
diff
changeset
|
318 |
* @see #thenComparing(Comparator) |
ef93558b0d63
8001667: Comparator combinators and extension methods
mduigou
parents:
15647
diff
changeset
|
319 |
* @since 1.8 |
ef93558b0d63
8001667: Comparator combinators and extension methods
mduigou
parents:
15647
diff
changeset
|
320 |
*/ |
19605
0f98f8e3bdcb
8023528: Rename Comparator combinators to disambiguate overloading methods
henryjen
parents:
19208
diff
changeset
|
321 |
default Comparator<T> thenComparingDouble(ToDoubleFunction<? super T> keyExtractor) { |
0f98f8e3bdcb
8023528: Rename Comparator combinators to disambiguate overloading methods
henryjen
parents:
19208
diff
changeset
|
322 |
return thenComparing(comparingDouble(keyExtractor)); |
18571 | 323 |
} |
324 |
||
325 |
/** |
|
326 |
* Returns a comparator that imposes the reverse of the <em>natural |
|
327 |
* ordering</em>. |
|
328 |
* |
|
329 |
* <p>The returned comparator is serializable and throws {@link |
|
330 |
* NullPointerException} when comparing {@code null}. |
|
331 |
* |
|
332 |
* @param <T> the {@link Comparable} type of element to be compared |
|
333 |
* @return a comparator that imposes the reverse of the <i>natural |
|
334 |
* ordering</i> on {@code Comparable} objects. |
|
335 |
* @see Comparable |
|
336 |
* @since 1.8 |
|
337 |
*/ |
|
338 |
public static <T extends Comparable<? super T>> Comparator<T> reverseOrder() { |
|
339 |
return Collections.reverseOrder(); |
|
340 |
} |
|
341 |
||
342 |
/** |
|
343 |
* Returns a comparator that compares {@link Comparable} objects in natural |
|
344 |
* order. |
|
345 |
* |
|
346 |
* <p>The returned comparator is serializable and throws {@link |
|
347 |
* NullPointerException} when comparing {@code null}. |
|
348 |
* |
|
349 |
* @param <T> the {@link Comparable} type of element to be compared |
|
350 |
* @return a comparator that imposes the <i>natural ordering</i> on {@code |
|
351 |
* Comparable} objects. |
|
352 |
* @see Comparable |
|
353 |
* @since 1.8 |
|
354 |
*/ |
|
19208
1e3d351eba80
8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents:
18571
diff
changeset
|
355 |
@SuppressWarnings("unchecked") |
18571 | 356 |
public static <T extends Comparable<? super T>> Comparator<T> naturalOrder() { |
357 |
return (Comparator<T>) Comparators.NaturalOrderComparator.INSTANCE; |
|
358 |
} |
|
359 |
||
360 |
/** |
|
361 |
* Returns a null-friendly comparator that considers {@code null} to be |
|
362 |
* less than non-null. When both are {@code null}, they are considered |
|
363 |
* equal. If both are non-null, the specified {@code Comparator} is used |
|
364 |
* to determine the order. If the specified comparator is {@code null}, |
|
365 |
* then the returned comparator considers all non-null values to be equal. |
|
366 |
* |
|
367 |
* <p>The returned comparator is serializable if the specified comparator |
|
368 |
* is serializable. |
|
369 |
* |
|
370 |
* @param <T> the type of the elements to be compared |
|
371 |
* @param comparator a {@code Comparator} for comparing non-null values |
|
372 |
* @return a comparator that considers {@code null} to be less than |
|
373 |
* non-null, and compares non-null objects with the supplied |
|
374 |
* {@code Comparator}. |
|
375 |
* @since 1.8 |
|
376 |
*/ |
|
377 |
public static <T> Comparator<T> nullsFirst(Comparator<? super T> comparator) { |
|
19208
1e3d351eba80
8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents:
18571
diff
changeset
|
378 |
return new Comparators.NullComparator<>(true, comparator); |
18571 | 379 |
} |
380 |
||
381 |
/** |
|
382 |
* Returns a null-friendly comparator that considers {@code null} to be |
|
383 |
* greater than non-null. When both are {@code null}, they are considered |
|
384 |
* equal. If both are non-null, the specified {@code Comparator} is used |
|
385 |
* to determine the order. If the specified comparator is {@code null}, |
|
386 |
* then the returned comparator considers all non-null values to be equal. |
|
387 |
* |
|
388 |
* <p>The returned comparator is serializable if the specified comparator |
|
389 |
* is serializable. |
|
390 |
* |
|
391 |
* @param <T> the type of the elements to be compared |
|
392 |
* @param comparator a {@code Comparator} for comparing non-null values |
|
393 |
* @return a comparator that considers {@code null} to be greater than |
|
394 |
* non-null, and compares non-null objects with the supplied |
|
395 |
* {@code Comparator}. |
|
396 |
* @since 1.8 |
|
397 |
*/ |
|
398 |
public static <T> Comparator<T> nullsLast(Comparator<? super T> comparator) { |
|
19208
1e3d351eba80
8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents:
18571
diff
changeset
|
399 |
return new Comparators.NullComparator<>(false, comparator); |
18571 | 400 |
} |
401 |
||
402 |
/** |
|
403 |
* Accepts a function that extracts a sort key from a type {@code T}, and |
|
404 |
* returns a {@code Comparator<T>} that compares by that sort key using |
|
405 |
* the specified {@link Comparator}. |
|
406 |
* |
|
407 |
* <p>The returned comparator is serializable if the specified function |
|
408 |
* and comparator are both serializable. |
|
409 |
* |
|
410 |
* @apiNote |
|
411 |
* For example, to obtain a {@code Comparator} that compares {@code |
|
412 |
* Person} objects by their last name ignoring case differences, |
|
413 |
* |
|
414 |
* <pre>{@code |
|
415 |
* Comparator<Person> cmp = Comparator.comparing( |
|
416 |
* Person::getLastName, |
|
417 |
* String.CASE_INSENSITIVE_ORDER); |
|
418 |
* }</pre> |
|
419 |
* |
|
420 |
* @param <T> the type of element to be compared |
|
421 |
* @param <U> the type of the sort key |
|
422 |
* @param keyExtractor the function used to extract the sort key |
|
423 |
* @param keyComparator the {@code Comparator} used to compare the sort key |
|
424 |
* @return a comparator that compares by an extracted key using the |
|
425 |
* specified {@code Comparator} |
|
426 |
* @throws NullPointerException if either argument is null |
|
427 |
* @since 1.8 |
|
428 |
*/ |
|
429 |
public static <T, U> Comparator<T> comparing( |
|
430 |
Function<? super T, ? extends U> keyExtractor, |
|
431 |
Comparator<? super U> keyComparator) |
|
432 |
{ |
|
433 |
Objects.requireNonNull(keyExtractor); |
|
434 |
Objects.requireNonNull(keyComparator); |
|
435 |
return (Comparator<T> & Serializable) |
|
436 |
(c1, c2) -> keyComparator.compare(keyExtractor.apply(c1), |
|
437 |
keyExtractor.apply(c2)); |
|
438 |
} |
|
439 |
||
440 |
/** |
|
441 |
* Accepts a function that extracts a {@link java.lang.Comparable |
|
442 |
* Comparable} sort key from a type {@code T}, and returns a {@code |
|
443 |
* Comparator<T>} that compares by that sort key. |
|
444 |
* |
|
445 |
* <p>The returned comparator is serializable if the specified function |
|
446 |
* is also serializable. |
|
447 |
* |
|
448 |
* @apiNote |
|
449 |
* For example, to obtain a {@code Comparator} that compares {@code |
|
450 |
* Person} objects by their last name, |
|
451 |
* |
|
452 |
* <pre>{@code |
|
453 |
* Comparator<Person> byLastName = Comparator.comparing(Person::getLastName); |
|
454 |
* }</pre> |
|
455 |
* |
|
456 |
* @param <T> the type of element to be compared |
|
457 |
* @param <U> the type of the {@code Comparable} sort key |
|
458 |
* @param keyExtractor the function used to extract the {@link |
|
459 |
* Comparable} sort key |
|
460 |
* @return a comparator that compares by an extracted key |
|
461 |
* @throws NullPointerException if the argument is null |
|
462 |
* @since 1.8 |
|
463 |
*/ |
|
464 |
public static <T, U extends Comparable<? super U>> Comparator<T> comparing( |
|
465 |
Function<? super T, ? extends U> keyExtractor) |
|
466 |
{ |
|
467 |
Objects.requireNonNull(keyExtractor); |
|
468 |
return (Comparator<T> & Serializable) |
|
469 |
(c1, c2) -> keyExtractor.apply(c1).compareTo(keyExtractor.apply(c2)); |
|
470 |
} |
|
471 |
||
472 |
/** |
|
473 |
* Accepts a function that extracts an {@code int} sort key from a type |
|
474 |
* {@code T}, and returns a {@code Comparator<T>} that compares by that |
|
475 |
* sort key. |
|
476 |
* |
|
477 |
* <p>The returned comparator is serializable if the specified function |
|
478 |
* is also serializable. |
|
479 |
* |
|
480 |
* @param <T> the type of element to be compared |
|
481 |
* @param keyExtractor the function used to extract the integer sort key |
|
482 |
* @return a comparator that compares by an extracted key |
|
483 |
* @see #comparing(Function) |
|
484 |
* @throws NullPointerException if the argument is null |
|
485 |
* @since 1.8 |
|
486 |
*/ |
|
19605
0f98f8e3bdcb
8023528: Rename Comparator combinators to disambiguate overloading methods
henryjen
parents:
19208
diff
changeset
|
487 |
public static <T> Comparator<T> comparingInt(ToIntFunction<? super T> keyExtractor) { |
18571 | 488 |
Objects.requireNonNull(keyExtractor); |
489 |
return (Comparator<T> & Serializable) |
|
490 |
(c1, c2) -> Integer.compare(keyExtractor.applyAsInt(c1), keyExtractor.applyAsInt(c2)); |
|
491 |
} |
|
492 |
||
493 |
/** |
|
494 |
* Accepts a function that extracts a {@code long} sort key from a type |
|
495 |
* {@code T}, and returns a {@code Comparator<T>} that compares by that |
|
496 |
* sort key. |
|
497 |
* |
|
498 |
* <p>The returned comparator is serializable if the specified function is |
|
499 |
* also serializable. |
|
500 |
* |
|
501 |
* @param <T> the type of element to be compared |
|
502 |
* @param keyExtractor the function used to extract the long sort key |
|
503 |
* @return a comparator that compares by an extracted key |
|
504 |
* @see #comparing(Function) |
|
505 |
* @throws NullPointerException if the argument is null |
|
506 |
* @since 1.8 |
|
507 |
*/ |
|
19605
0f98f8e3bdcb
8023528: Rename Comparator combinators to disambiguate overloading methods
henryjen
parents:
19208
diff
changeset
|
508 |
public static <T> Comparator<T> comparingLong(ToLongFunction<? super T> keyExtractor) { |
18571 | 509 |
Objects.requireNonNull(keyExtractor); |
510 |
return (Comparator<T> & Serializable) |
|
511 |
(c1, c2) -> Long.compare(keyExtractor.applyAsLong(c1), keyExtractor.applyAsLong(c2)); |
|
512 |
} |
|
513 |
||
514 |
/** |
|
515 |
* Accepts a function that extracts a {@code double} sort key from a type |
|
516 |
* {@code T}, and returns a {@code Comparator<T>} that compares by that |
|
517 |
* sort key. |
|
518 |
* |
|
519 |
* <p>The returned comparator is serializable if the specified function |
|
520 |
* is also serializable. |
|
521 |
* |
|
522 |
* @param <T> the type of element to be compared |
|
523 |
* @param keyExtractor the function used to extract the double sort key |
|
524 |
* @return a comparator that compares by an extracted key |
|
525 |
* @see #comparing(Function) |
|
526 |
* @throws NullPointerException if the argument is null |
|
527 |
* @since 1.8 |
|
528 |
*/ |
|
19605
0f98f8e3bdcb
8023528: Rename Comparator combinators to disambiguate overloading methods
henryjen
parents:
19208
diff
changeset
|
529 |
public static<T> Comparator<T> comparingDouble(ToDoubleFunction<? super T> keyExtractor) { |
18571 | 530 |
Objects.requireNonNull(keyExtractor); |
531 |
return (Comparator<T> & Serializable) |
|
532 |
(c1, c2) -> Double.compare(keyExtractor.applyAsDouble(c1), keyExtractor.applyAsDouble(c2)); |
|
16064
ef93558b0d63
8001667: Comparator combinators and extension methods
mduigou
parents:
15647
diff
changeset
|
533 |
} |
2 | 534 |
} |