Class Ordering<T>
- java.lang.Object
-
- org.glassfish.jersey.internal.guava.Ordering<T>
-
- All Implemented Interfaces:
Comparator<T>
public abstract class Ordering<T> extends Object implements Comparator<T>
A comparator, with additional methods to support common operations. This is an "enriched" version ofComparator, in the same sense thatFluentIterableis an enrichedIterable.The common ways to get an instance of
Orderingare:- Subclass it and implement
compare(T, T)instead of implementingComparatordirectly - Pass a pre-existing
Comparatorinstance tofrom(Comparator) - Use the natural ordering,
natural()
Then you can use the chaining methods to get an altered version of that
Ordering, including:reverse()#compound(Comparator)#onResultOf(Function)nullsFirst()/nullsLast()
Finally, use the resulting
Orderinganywhere aComparatoris required, or use any of its special operations, such as:#immutableSortedCopy#isOrdered/#isStrictlyOrderedmin(java.util.Iterator<E>)/max(java.util.Iterator<E>)
Except as noted, the orderings returned by the factory methods of this class are serializable if and only if the provided instances that back them are. For example, if
orderingandfunctioncan themselves be serialized, thenordering.onResultOf(function)can as well.See the Guava User Guide article on
Ordering.- Since:
- 2.0 (imported from Google Collections Library)
- Author:
- Jesse Wilson, Kevin Bourrillion
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract intcompare(T left, T right)static <T> Ordering<T>from(Comparator<T> comparator)Returns an ordering based on an existing comparator instance.static <C extends Comparable>
Ordering<C>natural()Returns a serializable ordering that uses the natural order of the values.<S extends T>
Ordering<S>reverse()Returns the reverse of this ordering; theOrderingequivalent toCollections.reverseOrder(Comparator).-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface java.util.Comparator
equals, reversed, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLong
-
-
-
-
Method Detail
-
natural
public static <C extends Comparable> Ordering<C> natural()
Returns a serializable ordering that uses the natural order of the values. The ordering throws aNullPointerExceptionwhen passed a null parameter.The type specification is
<C extends Comparable>, instead of the technically correct<C extends Comparable<? super C>>, to support legacy types from before Java 5.
-
from
public static <T> Ordering<T> from(Comparator<T> comparator)
Returns an ordering based on an existing comparator instance. Note that it is unnecessary to create a new anonymous inner class implementingComparatorjust to pass it in here. Instead, simply subclassOrderingand implement itscomparemethod directly.- Parameters:
comparator- the comparator that defines the order- Returns:
- comparator itself if it is already an
Ordering; otherwise an ordering that wraps that comparator
-
reverse
public <S extends T> Ordering<S> reverse()
Returns the reverse of this ordering; theOrderingequivalent toCollections.reverseOrder(Comparator).
-
compare
public abstract int compare(T left, T right)
- Specified by:
comparein interfaceComparator<T>
-
-