• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.common.collect;
18 
19 import com.google.common.annotations.GwtCompatible;
20 import com.google.common.annotations.VisibleForTesting;
21 import com.google.common.base.Function;
22 import static com.google.common.base.Preconditions.checkNotNull;
23 
24 import java.util.Collections;
25 import java.util.Comparator;
26 import java.util.HashSet;
27 import java.util.Iterator;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.NoSuchElementException;
31 import java.util.SortedMap;
32 import java.util.SortedSet;
33 import java.util.concurrent.atomic.AtomicInteger;
34 
35 /**
36  * A comparator with added methods to support common functions. For example:
37  * <pre>   {@code
38  *
39  *   if (Ordering.from(comparator).reverse().isOrdered(list)) { ... }}</pre>
40  *
41  * <p>The {@link #from(Comparator)} method returns the equivalent {@code
42  * Ordering} instance for a pre-existing comparator. You can also skip the
43  * comparator step and extend {@code Ordering} directly: <pre>   {@code
44  *
45  *   Ordering<String> byLengthOrdering = new Ordering<String>() {
46  *     public int compare(String left, String right) {
47  *       return Ints.compare(left.length(), right.length());
48  *     }
49  *   };}</pre>
50  *
51  * Except as noted, the orderings returned by the factory methods of this
52  * class are serializable if and only if the provided instances that back them
53  * are. For example, if {@code ordering} and {@code function} can themselves be
54  * serialized, then {@code ordering.onResultOf(function)} can as well.
55  *
56  * @author Jesse Wilson
57  * @author Kevin Bourrillion
58  * @since 2010.01.04 <b>stable</b> (imported from Google Collections Library)
59  */
60 @GwtCompatible
61 public abstract class Ordering<T> implements Comparator<T> {
62   // Static factories
63 
64   /**
65    * Returns a serializable ordering that uses the natural order of the values.
66    * The ordering throws a {@link NullPointerException} when passed a null
67    * parameter.
68    *
69    * <p>The type specification is {@code <C extends Comparable>}, instead of
70    * the technically correct {@code <C extends Comparable<? super C>>}, to
71    * support legacy types from before Java 5.
72    */
73   @GwtCompatible(serializable = true)
74   @SuppressWarnings("unchecked") // TODO: the right way to explain this??
natural()75   public static <C extends Comparable> Ordering<C> natural() {
76     return (Ordering) NaturalOrdering.INSTANCE;
77   }
78 
79   /**
80    * Returns an ordering for a pre-existing {@code comparator}. Note
81    * that if the comparator is not pre-existing, and you don't require
82    * serialization, you can subclass {@code Ordering} and implement its
83    * {@link #compare(Object, Object) compare} method instead.
84    *
85    * @param comparator the comparator that defines the order
86    */
87   @GwtCompatible(serializable = true)
from(Comparator<T> comparator)88   public static <T> Ordering<T> from(Comparator<T> comparator) {
89     return (comparator instanceof Ordering)
90         ? (Ordering<T>) comparator
91         : new ComparatorOrdering<T>(comparator);
92   }
93 
94   /**
95    * Simply returns its argument.
96    *
97    * @deprecated no need to use this
98    */
99   @GwtCompatible(serializable = true)
from(Ordering<T> ordering)100   @Deprecated public static <T> Ordering<T> from(Ordering<T> ordering) {
101     return checkNotNull(ordering);
102   }
103 
104   /**
105    * Returns an ordering that compares objects according to the order in
106    * which they appear in the given list. Only objects present in the list
107    * (according to {@link Object#equals}) may be compared. This comparator
108    * imposes a "partial ordering" over the type {@code T}. Subsequent changes
109    * to the {@code valuesInOrder} list will have no effect on the returned
110    * comparator. Null values in the list are not supported.
111    *
112    * <p>The returned comparator throws an {@link ClassCastException} when it
113    * receives an input parameter that isn't among the provided values.
114    *
115    * <p>The generated comparator is serializable if all the provided values are
116    * serializable.
117    *
118    * @param valuesInOrder the values that the returned comparator will be able
119    *     to compare, in the order the comparator should induce
120    * @return the comparator described above
121    * @throws NullPointerException if any of the provided values is null
122    * @throws IllegalArgumentException if {@code valuesInOrder} contains any
123    *     duplicate values (according to {@link Object#equals})
124    */
125   @GwtCompatible(serializable = true)
explicit(List<T> valuesInOrder)126   public static <T> Ordering<T> explicit(List<T> valuesInOrder) {
127     return new ExplicitOrdering<T>(valuesInOrder);
128   }
129 
130   /**
131    * Returns an ordering that compares objects according to the order in
132    * which they are given to this method. Only objects present in the argument
133    * list (according to {@link Object#equals}) may be compared. This comparator
134    * imposes a "partial ordering" over the type {@code T}. Null values in the
135    * argument list are not supported.
136    *
137    * <p>The returned comparator throws a {@link ClassCastException} when it
138    * receives an input parameter that isn't among the provided values.
139    *
140    * <p>The generated comparator is serializable if all the provided values are
141    * serializable.
142    *
143    * @param leastValue the value which the returned comparator should consider
144    *     the "least" of all values
145    * @param remainingValuesInOrder the rest of the values that the returned
146    *     comparator will be able to compare, in the order the comparator should
147    *     follow
148    * @return the comparator described above
149    * @throws NullPointerException if any of the provided values is null
150    * @throws IllegalArgumentException if any duplicate values (according to
151    *     {@link Object#equals(Object)}) are present among the method arguments
152    */
153   @GwtCompatible(serializable = true)
explicit( T leastValue, T... remainingValuesInOrder)154   public static <T> Ordering<T> explicit(
155       T leastValue, T... remainingValuesInOrder) {
156     return explicit(Lists.asList(leastValue, remainingValuesInOrder));
157   }
158 
159   /**
160    * Exception thrown by a {@link Ordering#explicit(List)} or {@link
161    * Ordering#explicit(Object, Object[])} comparator when comparing a value
162    * outside the set of values it can compare. Extending {@link
163    * ClassCastException} may seem odd, but it is required.
164    */
165   // TODO: consider making this exception type public. or consider getting rid
166   // of it.
167   @VisibleForTesting
168   static class IncomparableValueException extends ClassCastException {
169     final Object value;
170 
IncomparableValueException(Object value)171     IncomparableValueException(Object value) {
172       super("Cannot compare value: " + value);
173       this.value = value;
174     }
175 
176     private static final long serialVersionUID = 0;
177   }
178 
179   /**
180    * Returns an arbitrary ordering over all objects, for which {@code compare(a,
181    * b) == 0} implies {@code a == b} (identity equality). There is no meaning
182    * whatsoever to the order imposed, but it is constant for the life of the VM.
183    *
184    * <p>Because the ordering is identity-based, it is not "consistent with
185    * {@link Object#equals(Object)}" as defined by {@link Comparator}. Use
186    * caution when building a {@link SortedSet} or {@link SortedMap} from it, as
187    * the resulting collection will not behave exactly according to spec.
188    *
189    * <p>This ordering is not serializable, as its implementation relies on
190    * {@link System#identityHashCode(Object)}, so its behavior cannot be
191    * preserved across serialization.
192    *
193    * @since 2010.01.04 <b>tentative</b>
194    */
arbitrary()195   public static Ordering<Object> arbitrary() {
196     return ArbitraryOrderingHolder.ARBITRARY_ORDERING;
197   }
198 
199   private static class ArbitraryOrderingHolder {
200     static final Ordering<Object> ARBITRARY_ORDERING = new ArbitraryOrdering();
201   }
202 
203   @VisibleForTesting static class ArbitraryOrdering extends Ordering<Object> {
204     private Map<Object, Integer> uids =
205         Platform.tryWeakKeys(new MapMaker()).makeComputingMap(
206             new Function<Object, Integer>() {
207               final AtomicInteger counter = new AtomicInteger(0);
208               public Integer apply(Object from) {
209                 return counter.getAndIncrement();
210               }
211             });
212 
compare(Object left, Object right)213     /*@Override*/ public int compare(Object left, Object right) {
214       if (left == right) {
215         return 0;
216       }
217       int leftCode = identityHashCode(left);
218       int rightCode = identityHashCode(right);
219       if (leftCode != rightCode) {
220         return leftCode < rightCode ? -1 : 1;
221       }
222 
223       // identityHashCode collision (rare, but not as rare as you'd think)
224       int result = uids.get(left).compareTo(uids.get(right));
225       if (result == 0) {
226         throw new AssertionError(); // extremely, extremely unlikely.
227       }
228       return result;
229     }
230 
toString()231     @Override public String toString() {
232       return "Ordering.arbitrary()";
233     }
234 
235     /*
236      * We need to be able to mock identityHashCode() calls for tests, because it
237      * can take 1-10 seconds to find colliding objects. Mocking frameworks that
238      * can do magic to mock static method calls still can't do so for a system
239      * class, so we need the indirection. In production, Hotspot should still
240      * recognize that the call is 1-morphic and should still be willing to
241      * inline it if necessary.
242      */
identityHashCode(Object object)243     int identityHashCode(Object object) {
244       return System.identityHashCode(object);
245     }
246   }
247 
248   /**
249    * Returns an ordering that compares objects by the natural ordering of their
250    * string representations as returned by {@code toString()}. It does not
251    * support null values.
252    *
253    * <p>The comparator is serializable.
254    */
255   @GwtCompatible(serializable = true)
usingToString()256   public static Ordering<Object> usingToString() {
257     return UsingToStringOrdering.INSTANCE;
258   }
259 
260   /**
261    * Returns an ordering which tries each given comparator in order until a
262    * non-zero result is found, returning that result, and returning zero only if
263    * all comparators return zero. The returned ordering is based on the state of
264    * the {@code comparators} iterable at the time it was provided to this
265    * method.
266    *
267    * <p>The returned ordering is equivalent to that produced using {@code
268    * Ordering.from(comp1).compound(comp2).compound(comp3) . . .}.
269    *
270    * <p><b>Warning:</b> Supplying an argument with undefined iteration order,
271    * such as a {@link HashSet}, will produce non-deterministic results.
272    *
273    * @param comparators the comparators to try in order
274    */
275   @GwtCompatible(serializable = true)
compound( Iterable<? extends Comparator<? super T>> comparators)276   public static <T> Ordering<T> compound(
277       Iterable<? extends Comparator<? super T>> comparators) {
278     return new CompoundOrdering<T>(comparators);
279   }
280 
281   /**
282    * Constructs a new instance of this class (only invokable by the subclass
283    * constructor, typically implicit).
284    */
Ordering()285   protected Ordering() {}
286 
287   // Non-static factories
288 
289   /**
290    * Returns an ordering which first uses the ordering {@code this}, but which
291    * in the event of a "tie", then delegates to {@code secondaryComparator}.
292    * For example, to sort a bug list first by status and second by priority, you
293    * might use {@code byStatus.compound(byPriority)}. For a compound ordering
294    * with three or more components, simply chain multiple calls to this method.
295    *
296    * <p>An ordering produced by this method, or a chain of calls to this method,
297    * is equivalent to one created using {@link Ordering#compound(Iterable)} on
298    * the same component comparators.
299    */
300   @GwtCompatible(serializable = true)
compound( Comparator<? super U> secondaryComparator)301   public <U extends T> Ordering<U> compound(
302       Comparator<? super U> secondaryComparator) {
303     return new CompoundOrdering<U>(this, checkNotNull(secondaryComparator));
304   }
305 
306   /**
307    * Returns the reverse of this ordering; the {@code Ordering} equivalent to
308    * {@link Collections#reverseOrder(Comparator)}.
309    */
310   // type parameter <S> lets us avoid the extra <String> in statements like:
311   // Ordering<String> o = Ordering.<String>natural().reverse();
312   @GwtCompatible(serializable = true)
reverse()313   public <S extends T> Ordering<S> reverse() {
314     return new ReverseOrdering<S>(this);
315   }
316 
317   /**
318    * Returns a new ordering on {@code F} which orders elements by first applying
319    * a function to them, then comparing those results using {@code this}. For
320    * example, to compare objects by their string forms, in a case-insensitive
321    * manner, use: <pre>   {@code
322    *
323    *   Ordering.from(String.CASE_INSENSITIVE_ORDER)
324    *       .onResultOf(Functions.toStringFunction())}</pre>
325    */
326   @GwtCompatible(serializable = true)
onResultOf(Function<F, ? extends T> function)327   public <F> Ordering<F> onResultOf(Function<F, ? extends T> function) {
328     return new ByFunctionOrdering<F, T>(function, this);
329   }
330 
331   /**
332    * Returns a new ordering which sorts iterables by comparing corresponding
333    * elements pairwise until a nonzero result is found; imposes "dictionary
334    * order". If the end of one iterable is reached, but not the other, the
335    * shorter iterable is considered to be less than the longer one. For example,
336    * a lexicographical natural ordering over integers considers {@code
337    * [] < [1] < [1, 1] < [1, 2] < [2]}.
338    *
339    * <p>Note that {@code ordering.lexicographical().reverse()} is not
340    * equivalent to {@code ordering.reverse().lexicographical()} (consider how
341    * each would order {@code [1]} and {@code [1, 1]}).
342    *
343    * @since 2010.01.04 <b>tentative</b>
344    */
345   @GwtCompatible(serializable = true)
346   // type parameter <S> lets us avoid the extra <String> in statements like:
347   // Ordering<Iterable<String>> o =
348   //     Ordering.<String>natural().lexicographical();
lexicographical()349   public <S extends T> Ordering<Iterable<S>> lexicographical() {
350     /*
351      * Note that technically the returned ordering should be capable of
352      * handling not just {@code Iterable<S>} instances, but also any {@code
353      * Iterable<? extends S>}. However, the need for this comes up so rarely
354      * that it doesn't justify making everyone else deal with the very ugly
355      * wildcard.
356      */
357     return new LexicographicalOrdering<S>(this);
358   }
359 
360   /**
361    * Returns an ordering that treats {@code null} as less than all other values
362    * and uses {@code this} to compare non-null values.
363    */
364   // type parameter <S> lets us avoid the extra <String> in statements like:
365   // Ordering<String> o = Ordering.<String>natural().nullsFirst();
366   @GwtCompatible(serializable = true)
nullsFirst()367   public <S extends T> Ordering<S> nullsFirst() {
368     return new NullsFirstOrdering<S>(this);
369   }
370 
371   /**
372    * Returns an ordering that treats {@code null} as greater than all other
373    * values and uses this ordering to compare non-null values.
374    */
375   // type parameter <S> lets us avoid the extra <String> in statements like:
376   // Ordering<String> o = Ordering.<String>natural().nullsLast();
377   @GwtCompatible(serializable = true)
nullsLast()378   public <S extends T> Ordering<S> nullsLast() {
379     return new NullsLastOrdering<S>(this);
380   }
381 
382   /**
383    * {@link Collections#binarySearch(List, Object, Comparator) Searches}
384    * {@code sortedList} for {@code key} using the binary search algorithm. The
385    * list must be sorted using this ordering.
386    *
387    * @param sortedList the list to be searched
388    * @param key the key to be searched for
389    */
binarySearch(List<? extends T> sortedList, T key)390   public int binarySearch(List<? extends T> sortedList, T key) {
391     return Collections.binarySearch(sortedList, key, this);
392   }
393 
394   /**
395    * Returns a copy of the given iterable sorted by this ordering. The input is
396    * not modified. The returned list is modifiable, serializable, and has random
397    * access.
398    *
399    * <p>Unlike {@link Sets#newTreeSet(Iterable)}, this method does not collapse
400    * elements that compare as zero, and the resulting collection does not
401    * maintain its own sort order.
402    *
403    * @param iterable the elements to be copied and sorted
404    * @return a new list containing the given elements in sorted order
405    */
sortedCopy(Iterable<E> iterable)406   public <E extends T> List<E> sortedCopy(Iterable<E> iterable) {
407     List<E> list = Lists.newArrayList(iterable);
408     Collections.sort(list, this);
409     return list;
410   }
411 
412   /**
413    * Returns {@code true} if each element in {@code iterable} after the first is
414    * greater than or equal to the element that preceded it, according to this
415    * ordering. Note that this is always true when the iterable has fewer than
416    * two elements.
417    */
isOrdered(Iterable<? extends T> iterable)418   public boolean isOrdered(Iterable<? extends T> iterable) {
419     Iterator<? extends T> it = iterable.iterator();
420     if (it.hasNext()) {
421       T prev = it.next();
422       while (it.hasNext()) {
423         T next = it.next();
424         if (compare(prev, next) > 0) {
425           return false;
426         }
427         prev = next;
428       }
429     }
430     return true;
431   }
432 
433   /**
434    * Returns {@code true} if each element in {@code iterable} after the first is
435    * <i>strictly</i> greater than the element that preceded it, according to
436    * this ordering. Note that this is always true when the iterable has fewer
437    * than two elements.
438    */
isStrictlyOrdered(Iterable<? extends T> iterable)439   public boolean isStrictlyOrdered(Iterable<? extends T> iterable) {
440     Iterator<? extends T> it = iterable.iterator();
441     if (it.hasNext()) {
442       T prev = it.next();
443       while (it.hasNext()) {
444         T next = it.next();
445         if (compare(prev, next) >= 0) {
446           return false;
447         }
448         prev = next;
449       }
450     }
451     return true;
452   }
453 
454   /**
455    * Returns the largest of the specified values according to this ordering. If
456    * there are multiple largest values, the first of those is returned.
457    *
458    * @param iterable the iterable whose maximum element is to be determined
459    * @throws NoSuchElementException if {@code iterable} is empty
460    * @throws ClassCastException if the parameters are not <i>mutually
461    *     comparable</i> under this ordering.
462    */
max(Iterable<E> iterable)463   public <E extends T> E max(Iterable<E> iterable) {
464     Iterator<E> iterator = iterable.iterator();
465 
466     // let this throw NoSuchElementException as necessary
467     E maxSoFar = iterator.next();
468 
469     while (iterator.hasNext()) {
470       maxSoFar = max(maxSoFar, iterator.next());
471     }
472 
473     return maxSoFar;
474   }
475 
476   /**
477    * Returns the largest of the specified values according to this ordering. If
478    * there are multiple largest values, the first of those is returned.
479    *
480    * @param a value to compare, returned if greater than or equal to the rest.
481    * @param b value to compare
482    * @param c value to compare
483    * @param rest values to compare
484    * @throws ClassCastException if the parameters are not <i>mutually
485    *     comparable</i> under this ordering.
486    */
max(E a, E b, E c, E... rest)487   public <E extends T> E max(E a, E b, E c, E... rest) {
488     E maxSoFar = max(max(a, b), c);
489 
490     for (E r : rest) {
491       maxSoFar = max(maxSoFar, r);
492     }
493 
494     return maxSoFar;
495   }
496 
497   /**
498    * Returns the larger of the two values according to this ordering. If the
499    * values compare as 0, the first is returned.
500    *
501    * <p><b>Implementation note:</b> this method is invoked by the default
502    * implementations of the other {@code max} overloads, so overriding it will
503    * affect their behavior.
504    *
505    * @param a value to compare, returned if greater than or equal to b.
506    * @param b value to compare.
507    * @throws ClassCastException if the parameters are not <i>mutually
508    *     comparable</i> under this ordering.
509    */
max(E a, E b)510   public <E extends T> E max(E a, E b) {
511     return compare(a, b) >= 0 ? a : b;
512   }
513 
514   /**
515    * Returns the smallest of the specified values according to this ordering. If
516    * there are multiple smallest values, the first of those is returned.
517    *
518    * @param iterable the iterable whose minimum element is to be determined
519    * @throws NoSuchElementException if {@code iterable} is empty
520    * @throws ClassCastException if the parameters are not <i>mutually
521    *     comparable</i> under this ordering.
522    */
min(Iterable<E> iterable)523   public <E extends T> E min(Iterable<E> iterable) {
524     Iterator<E> iterator = iterable.iterator();
525 
526     // let this throw NoSuchElementException as necessary
527     E minSoFar = iterator.next();
528 
529     while (iterator.hasNext()) {
530       minSoFar = min(minSoFar, iterator.next());
531     }
532 
533     return minSoFar;
534   }
535 
536   /**
537    * Returns the smallest of the specified values according to this ordering. If
538    * there are multiple smallest values, the first of those is returned.
539    *
540    * @param a value to compare, returned if less than or equal to the rest.
541    * @param b value to compare
542    * @param c value to compare
543    * @param rest values to compare
544    * @throws ClassCastException if the parameters are not <i>mutually
545    *     comparable</i> under this ordering.
546    */
min(E a, E b, E c, E... rest)547   public <E extends T> E min(E a, E b, E c, E... rest) {
548     E minSoFar = min(min(a, b), c);
549 
550     for (E r : rest) {
551       minSoFar = min(minSoFar, r);
552     }
553 
554     return minSoFar;
555   }
556 
557   /**
558    * Returns the smaller of the two values according to this ordering. If the
559    * values compare as 0, the first is returned.
560    *
561    * <p><b>Implementation note:</b> this method is invoked by the default
562    * implementations of the other {@code min} overloads, so overriding it will
563    * affect their behavior.
564    *
565    * @param a value to compare, returned if less than or equal to b.
566    * @param b value to compare.
567    * @throws ClassCastException if the parameters are not <i>mutually
568    *     comparable</i> under this ordering.
569    */
min(E a, E b)570   public <E extends T> E min(E a, E b) {
571     return compare(a, b) <= 0 ? a : b;
572   }
573 
574   // Never make these public
575   static final int LEFT_IS_GREATER = 1;
576   static final int RIGHT_IS_GREATER = -1;
577 }
578