|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.google.common.collect.Comparators
public final class Comparators
Standard comparators and utilities for creating and working with comparators.
Some of these methods return an Ordering, a serializable class
that implements Comparator and includes many additional methods.
Several method signatures include <C extends Comparable> with a
raw Comparable, instead of
<C extends Comparable<? super C>. That's necessary to support classes
defined without generics.
| Field Summary | |
|---|---|
static Ordering<java.lang.Object> |
STRING_FORM_ORDER
A comparator that compares objects by the natural ordering of their string representations as returned by toString. |
| Method Summary | ||
|---|---|---|
static int |
compare(boolean a,
boolean b)
Compares the two specified boolean values. |
|
static int |
compare(byte a,
byte b)
Compares the two specified byte values. |
|
static int |
compare(char a,
char b)
Compares the two specified char values. |
|
static int |
compare(double a,
double b)
Compares the two specified double values. |
|
static int |
compare(float a,
float b)
Compares the two specified float values. |
|
static int |
compare(int a,
int b)
Compares the two specified int values. |
|
static int |
compare(long a,
long b)
Compares the two specified long values. |
|
static int |
compare(short a,
short b)
Compares the two specified short values. |
|
static
|
compound(java.util.Comparator<? super T> primary,
java.util.Comparator<? super T> secondary,
java.util.Comparator<? super T>... rest)
Returns a comparator which tries each given comparator in order until a non-zero result is found, returning this result, and returning zero only if all comparators return zero. |
|
static
|
compound(java.util.List<? extends java.util.Comparator<? super T>> comparators)
Returns a comparator which tries each given comparator in order until a non-zero result is found, returning this result, and returning zero only if all comparators return zero. |
|
static
|
fromFunction(Function<F,T> function)
Creates a comparator that compares any two items by applying a function to each of them and using the natural ordering of the results. |
|
static
|
fromFunction(Function<F,T> function,
java.util.Comparator<? super T> comparator)
Creates a comparator that compares any two items by applying a function to each of them and using the supplied comparator to compare the results. |
|
static
|
givenOrder(java.util.List<T> valuesInOrder)
Returns a comparator that compares objects according to the order in which they appear in the given list. |
|
static
|
givenOrder(T leastValue,
T... remainingValuesInOrder)
Returns the comparator that compares objects according to the order in which they are given to this method. |
|
static
|
max(java.util.Comparator<? super T> comparator,
T a,
T b)
Returns the larger of the two values according to the specified comparator. |
|
static
|
max(T a,
T b)
Returns the larger of the two values, according to their natural ordering. |
|
static
|
min(java.util.Comparator<? super T> comparator,
T a,
T b)
Returns the smaller of the two values according to the specified comparator. |
|
static
|
min(T a,
T b)
Returns the smaller of the two values, according to their natural ordering. |
|
static
|
naturalOrder()
Returns a comparator that uses the natural ordering of the values. |
|
static
|
nullGreatestOrder()
Returns a comparator that uses the natural ordering of the values, but also handles null values, treating them as greater than all other values. |
|
static
|
nullGreatestOrder(java.util.Comparator<T> comparator)
Returns a comparator that treats null as greater than all other
values and uses the given comparator to compare non-null values. |
|
static
|
nullLeastOrder()
Returns a comparator that uses the natural ordering of the values, but also handles null values, treating them as less than all other values. |
|
static
|
nullLeastOrder(java.util.Comparator<T> comparator)
Returns a comparator that treats null as less than all other
values and uses comparator to compare non-null values. |
|
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public static final Ordering<java.lang.Object> STRING_FORM_ORDER
toString. Does not support null
values.
| Method Detail |
|---|
public static <C extends java.lang.Comparable> Ordering<C> naturalOrder()
Collections.reverseOrder()public static <T> Ordering<T> nullLeastOrder(java.util.Comparator<T> comparator)
null as less than all other
values and uses comparator to compare non-null values.
public static <C extends java.lang.Comparable> Ordering<C> nullLeastOrder()
public static <T> Ordering<T> nullGreatestOrder(java.util.Comparator<T> comparator)
null as greater than all other
values and uses the given comparator to compare non-null values.
public static <C extends java.lang.Comparable> Ordering<C> nullGreatestOrder()
public static <T> Ordering<T> compound(java.util.Comparator<? super T> primary,
java.util.Comparator<? super T> secondary,
java.util.Comparator<? super T>... rest)
The returned comparator is a "view" of the specified rest array;
changes to the array will be reflected in the behavior of the returned
comparator.
TODO: Add compound methods that take 2, 3, or 4 arguments, so
the calling code doesn't need to say
@SuppressWarnings("unchecked").
primary - the primary comparatorsecondary - the secondary comparatorrest - additional comparators to invoke as necessarycompound(List)public static <T> Ordering<T> compound(java.util.List<? extends java.util.Comparator<? super T>> comparators)
The returned comparator is a "view" of the specified List
instance; changes to the collection of comparators will be reflected in the
behavior of the returned comparator.
comparators - a collection of comparators to try in orderpublic static <F,T extends java.lang.Comparable> Ordering<F> fromFunction(Function<F,T> function)
function - the function returning the value to compare. The function
should never return null.
public static <F,T> Ordering<F> fromFunction(Function<F,T> function,
java.util.Comparator<? super T> comparator)
function - the function returning the value to comparecomparator - the comparator that receives the function output
public static <T extends java.lang.Comparable> T min(T a,
T b)
a - non-null value to compare, returned if less than or equal to b.b - non-null value to compare.
java.lang.ClassCastException - if the parameters are not mutually comparable
(for example, a string and an integer).
public static <T extends java.lang.Comparable> T max(T a,
T b)
a - non-null value to compare, returned if greater than or equal to b.b - non-null value to compare.
java.lang.ClassCastException - if the parameters are not mutually comparable
(for example, a string and an integer).
public static <T> T min(java.util.Comparator<? super T> comparator,
@Nullable
T a,
@Nullable
T b)
a - value to compare, returned if less than or equal to bb - value to compare
public static <T> T max(java.util.Comparator<? super T> comparator,
@Nullable
T a,
@Nullable
T b)
a - value to compare, returned if greater than or equal to bb - value to comparepublic 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 supported.
valuesInOrder - the values that the returned comparator will be able
to compare, in the order the comparator should follow
java.lang.IllegalArgumentException - if valuesInOrder contains any
non-consecutive 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 supported.
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.IllegalArgumentException - if any non-consecutive duplicate values
(according to Object.equals(java.lang.Object)) are present among the method
arguments
public static int compare(byte a,
byte b)
byte values. The sign of the value
returned is the same as that of the value that would be returned by the
call:
Byte.valueOf(a).compareTo(Byte.valueOf(b))
a - the first byte to compareb - the second byte to compare
a is less than b; a positive
value if a is greater than b; otherwise zero.
public static int compare(char a,
char b)
char values. The sign of the value
returned is the same as that of the value that would be returned by the
call:
Character.valueOf(a).compareTo(Character.valueOf(b))
a - the first char to compareb - the second char to compare
a is less than b; a positive
value if a is greater than b; otherwise zero.
public static int compare(short a,
short b)
short values. The sign of the value
returned is the same as that of the value that would be returned by the
call:
Short.valueOf(a).compareTo(Short.valueOf(b))
a - the first short to compareb - the second short to compare
a is less than b; a positive
value if a is greater than b; otherwise zero.
public static int compare(int a,
int b)
int values. The sign of the value
returned is the same as that of the value that would be returned by the
call:
Integer.valueOf(a).compareTo(Integer.valueOf(b))
a - the first int to compareb - the second int to compare
a is less than b; a positive
value if a is greater than b; otherwise zero.
public static int compare(long a,
long b)
long values. The sign of the value
returned is the same as that of the value that would be returned by the
call:
Long.valueOf(a).compareTo(Long.valueOf(b))
a - the first long to compareb - the second long to compare
a is less than b; a positive
value if a is greater than b; otherwise zero.
public static int compare(double a,
double b)
double values. The sign of the value
returned is the same as that of the value that would be returned by the
call:
Double.valueOf(a).compareTo(Double.valueOf(b))
a - the first double to compareb - the second double to compare
a is less than b; a positive
value if a is greater than b; otherwise zero.Double.compare(double, double)
public static int compare(float a,
float b)
float values. The sign of the value
returned is the same as that of the value that would be returned by the
call:
Float.valueOf(a).compareTo(Float.valueOf(b))
a - the first float to compareb - the second float to compare
a is less than b; a positive
value if a is greater than b; otherwise zero.Float.compare(float, float)
public static int compare(boolean a,
boolean b)
boolean values. The sign of the value
returned is the same as that of the value that would be returned by the
call:
Boolean.valueOf(a).compareTo(Boolean.valueOf(b))
a - the first boolean to compareb - the second boolean to compare
a is false and b is true; a
positive value if a is true and b is false; otherwise
zero.
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||