|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.google.common.collect.Ordering<T>
@GwtCompatible public abstract class Ordering<T>
A comparator with added methods to support common functions. For example:
if (Ordering.from(comparator).reverse().isOrdered(list)) { ... }
The from(Comparator) method returns the equivalent Ordering instance for a pre-existing comparator. You can also skip the
comparator step and extend Ordering directly:
Ordering<String> byLengthOrdering = new Ordering<String>() {
public int compare(String left, String right) {
return Ints.compare(left.length(), right.length());
}
};
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
ordering and function can themselves be serialized, then
ordering.onResultOf(function) can as well.
| Constructor Summary | |
|---|---|
Ordering()
|
|
| Method Summary | ||
|---|---|---|
int |
binarySearch(java.util.List<? extends T> sortedList,
T key)
Searches
sortedList for key using the binary search algorithm. |
|
|
compound(java.util.Comparator<? super U> secondaryComparator)
Returns an ordering which first uses the ordering this, but which
in the event of a "tie", then delegates to secondaryComparator. |
|
static
|
compound(java.lang.Iterable<? extends java.util.Comparator<? super T>> comparators)
Returns an ordering which tries each given comparator in order until a non-zero result is found, returning that result, and returning zero only if all comparators return zero. |
|
static
|
from(java.util.Comparator<T> comparator)
Returns an ordering for a pre-existing comparator. |
|
static
|
givenOrder(java.util.List<T> valuesInOrder)
Returns an ordering that compares objects according to the order in which they appear in the given list. |
|
static
|
givenOrder(T leastValue,
T... remainingValuesInOrder)
Returns an ordering that compares objects according to the order in which they are given to this method. |
|
boolean |
isOrdered(java.lang.Iterable<? extends T> iterable)
Returns true if each element in iterable after the first is
greater than or equal to the element that preceded it, according to this
ordering. |
|
boolean |
isStrictlyOrdered(java.lang.Iterable<? extends T> iterable)
Returns true if each element in iterable after the first is
strictly greater than the element that preceded it, according to
this ordering. |
|
|
max(E a,
E b)
Returns the larger of the two values according to this ordering. |
|
|
max(E a,
E b,
E c,
E... rest)
Returns the largest of the specified values according to this ordering. |
|
|
max(java.lang.Iterable<E> iterable)
Returns the largest of the specified values according to this ordering. |
|
|
min(E a,
E b)
Returns the smaller of the two values according to this ordering. |
|
|
min(E a,
E b,
E c,
E... rest)
Returns the smallest of the specified values according to this ordering. |
|
|
min(java.lang.Iterable<E> iterable)
Returns the smallest of the specified values according to this ordering. |
|
static
|
natural()
Returns a serializable ordering that uses the natural order of the values. |
|
Ordering<T> |
nullsFirst()
Returns an ordering that treats null as less than all other values
and uses this to compare non-null values. |
|
Ordering<T> |
nullsLast()
Returns an ordering that treats null as greater than all other
values and uses this ordering to compare non-null values. |
|
|
onResultOf(Function<F,? extends T> function)
Returns a new ordering on F which orders elements by first applying
a function to them, then comparing those results using this. |
|
Ordering<T> |
reverse()
Returns the reverse of this ordering; the Ordering equivalent to
Collections.reverseOrder(Comparator). |
|
|
sortedCopy(java.lang.Iterable<E> iterable)
Returns a copy of the given iterable sorted by this ordering. |
|
| 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 |
|---|
compare, equals |
| Constructor Detail |
|---|
public Ordering()
| Method Detail |
|---|
@GwtCompatible(serializable=true) public static <C extends java.lang.Comparable> Ordering<C> natural()
NullPointerException when 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.
public static <T> Ordering<T> from(java.util.Comparator<T> comparator)
comparator. Note
that if the comparator is not pre-existing, and you don't require
serialization, you can subclass Ordering and implement its
compare method instead.
comparator - the comparator that defines the orderpublic static <T> Ordering<T> givenOrder(java.util.List<T> valuesInOrder)
Object.equals(java.lang.Object)) may be compared. This comparator
imposes a "partial ordering" over the type T. Subsequent changes
to the valuesInOrder list will have no effect on the returned
comparator. Null values in the list are not supported.
The returned comparator throws an ClassCastException when it
receives an input parameter that isn't among the provided values.
The generated comparator is serializable if all the provided values are serializable.
valuesInOrder - the values that the returned comparator will be able
to compare, in the order the comparator should induce
java.lang.NullPointerException - if any of the provided values is null
java.lang.IllegalArgumentException - if valuesInOrder contains any
duplicate values (according to Object.equals(java.lang.Object))
public static <T> Ordering<T> givenOrder(@Nullable
T leastValue,
T... remainingValuesInOrder)
Object.equals(java.lang.Object)) may be compared. This comparator
imposes a "partial ordering" over the type T. Null values in the
argument list are not supported.
The returned comparator throws a ClassCastException when it
receives an input parameter that isn't among the provided values.
The generated comparator is serializable if all the provided values are serializable.
leastValue - the value which the returned comparator should consider
the "least" of all valuesremainingValuesInOrder - the rest of the values that the returned
comparator will be able to compare, in the order the comparator should
follow
java.lang.NullPointerException - if any of the provided values is null
java.lang.IllegalArgumentException - if any duplicate values (according to
Object.equals(Object)) are present among the method argumentspublic static <T> Ordering<T> compound(java.lang.Iterable<? extends java.util.Comparator<? super T>> comparators)
comparators iterable at the time it was provided to this
method.
The returned ordering is equivalent to that produced using Ordering.from(comp1).compound(comp2).compound(comp3) . . ..
Warning: Supplying an argument with undefined iteration order,
such as a HashSet, will produce non-deterministic results.
comparators - the comparators to try in orderpublic final <U extends T> Ordering<U> compound(java.util.Comparator<? super U> secondaryComparator)
this, but which
in the event of a "tie", then delegates to secondaryComparator.
For example, to sort a bug list first by status and second by priority, you
might use byStatus.compound(byPriority). For a compound ordering
with three or more components, simply chain multiple calls to this method.
An ordering produced by this method, or a chain of calls to this method,
is equivalent to one created using compound(Iterable) on
the same component comparators.
public final Ordering<T> reverse()
Ordering equivalent to
Collections.reverseOrder(Comparator).
public final <F> Ordering<F> onResultOf(Function<F,? extends T> function)
F which orders elements by first applying
a function to them, then comparing those results using this. For
example, to compare objects by their string forms, in a case-insensitive
manner, use: Ordering.from(String.CASE_INSENSITIVE_ORDER)
.onResultOf(Functions.toStringFunction())
public final Ordering<T> nullsFirst()
null as less than all other values
and uses this to compare non-null values.
public final Ordering<T> nullsLast()
null as greater than all other
values and uses this ordering to compare non-null values.
public final int binarySearch(java.util.List<? extends T> sortedList,
T key)
Searches
sortedList for key using the binary search algorithm. The
list must be sorted using this ordering.
sortedList - the list to be searchedkey - the key to be searched forpublic final <E extends T> java.util.List<E> sortedCopy(java.lang.Iterable<E> iterable)
Unlike Sets.newTreeSet(Iterable), this method does not collapse
elements that compare as zero, and the resulting collection does not
maintain its own sort order.
iterable - the elements to be copied and sorted
public final boolean isOrdered(java.lang.Iterable<? extends T> iterable)
true if each element in iterable after the first is
greater than or equal to the element that preceded it, according to this
ordering. Note that this is always true when the iterable has fewer than
two elements.
public final boolean isStrictlyOrdered(java.lang.Iterable<? extends T> iterable)
true if each element in iterable after the first is
strictly greater than the element that preceded it, according to
this ordering. Note that this is always true when the iterable has fewer
than two elements.
public final <E extends T> E max(java.lang.Iterable<E> iterable)
iterable - the iterable whose maximum element is to be determined
java.util.NoSuchElementException - if iterable is empty
java.lang.ClassCastException - if the parameters are not mutually
comparable under this ordering.
public final <E extends T> E max(E a,
E b,
E c,
E... rest)
a - value to compare, returned if greater than or equal to the rest.b - value to comparec - value to comparerest - values to compare
java.lang.ClassCastException - if the parameters are not mutually
comparable under this ordering.
public final <E extends T> E max(E a,
E b)
a - value to compare, returned if greater than or equal to b.b - value to compare.
java.lang.ClassCastException - if the parameters are not mutually
comparable under this ordering.public final <E extends T> E min(java.lang.Iterable<E> iterable)
iterable - the iterable whose minimum element is to be determined
java.util.NoSuchElementException - if iterable is empty
java.lang.ClassCastException - if the parameters are not mutually
comparable under this ordering.
public final <E extends T> E min(E a,
E b,
E c,
E... rest)
a - value to compare, returned if less than or equal to the rest.b - value to comparec - value to comparerest - values to compare
java.lang.ClassCastException - if the parameters are not mutually
comparable under this ordering.
public final <E extends T> E min(E a,
E b)
a - value to compare, returned if less than or equal to b.b - value to compare.
java.lang.ClassCastException - if the parameters are not mutually
comparable under this ordering.
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||