|
|||||||||
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<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(Comparator<? super T> first,
Comparator<? super T> second)
Returns a comparator which tries two comparators in order until a non-zero result is found, returning that result, and returning zero only if both comparators return zero. |
|
static
|
compound(Comparator<? super T> first,
Comparator<? super T> second,
Comparator<? super T> third)
Returns a comparator which tries three comparators in order until a non-zero result is found, returning that result, and returning zero only if all comparators return zero. |
|
static
|
compound(Comparator<? super T> first,
Comparator<? super T> second,
Comparator<? super T> third,
Comparator<? super T> forth)
Returns a comparator which tries four comparators in order until a non-zero result is found, returning that result, and returning zero only if all comparators return zero. |
|
static
|
compound(Comparator<? super T> first,
Comparator<? super T> second,
Comparator<? super T> third,
Comparator<? super T> forth,
Comparator<? super T>... rest)
Returns a comparator 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
|
compound(Iterable<? extends Comparator<? super T>> comparators)
Returns a comparator 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
|
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,
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(List<? extends 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(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(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(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(Comparator<T> comparator)
Returns a comparator that treats null as less than all other
values and uses comparator to compare non-null values. |
|
static
|
toStringOrder()
Returns a comparator that compares objects by the natural ordering of their string representations as returned by toString() . |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final Ordering<Object> STRING_FORM_ORDER
toString()
. It does not support null
values.
TODO: Deprecate this in favor of toStringOrder()
.
Method Detail |
---|
public static <T extends Comparable> Ordering<T> naturalOrder()
NullPointerException
when passed a null
parameter.
public static <T> Ordering<T> nullLeastOrder(Comparator<T> comparator)
null
as less than all other
values and uses comparator
to compare non-null values.
public static <T extends Comparable> Ordering<T> nullLeastOrder()
public static <T> Ordering<T> nullGreatestOrder(Comparator<T> comparator)
null
as greater than all other
values and uses the given comparator to compare non-null values.
public static <T extends Comparable> Ordering<T> nullGreatestOrder()
public static <T> Ordering<T> compound(Comparator<? super T> first, Comparator<? super T> second)
first
- the first comparator to invokesecond
- the second comparator to invokecompound(Iterable)
public static <T> Ordering<T> compound(Comparator<? super T> first, Comparator<? super T> second, Comparator<? super T> third)
first
- the first comparator to invokesecond
- the second comparator to invokethird
- the third comparator to invokecompound(Iterable)
public static <T> Ordering<T> compound(Comparator<? super T> first, Comparator<? super T> second, Comparator<? super T> third, Comparator<? super T> forth)
first
- the first comparator to invokesecond
- the second comparator to invokethird
- the third comparator to invokeforth
- the fourth comparator to invokecompound(Iterable)
public static <T> Ordering<T> compound(Comparator<? super T> first, Comparator<? super T> second, Comparator<? super T> third, Comparator<? super T> forth, Comparator<? super T>... rest)
Subsequent changes to the rest
array do not affect the behavior
of the returned comparator.
first
- the first comparator to invokesecond
- the second comparator to invokethird
- the third comparator to invokeforth
- the fourth comparator to invokerest
- additional comparators to invoke as necessarycompound(Iterable)
public static <T> Ordering<T> compound(Iterable<? extends Comparator<? super T>> comparators)
The returned comparator is a "view" of the specified Iterable
instance; changes to the iterable will be reflected in the behavior of the
returned comparator.
Warning: Supplying an argument with undefined iteration order,
such as a HashSet
, will produce non-deterministic
results.
comparators
- the comparators to try in orderpublic static <F,T extends 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, Comparator<? super T> comparator)
function
- the function returning the value to comparecomparator
- the comparator that receives the function output
public static final <T> Ordering<T> toStringOrder()
toString()
. It does not
support null values.
public static <T extends Comparable> T min(T a, T b)
To handle more than two values, call
Ordering.min(Object, Object, Object, Object...)
or
Ordering.min(Iterable)
on the Ordering
returned by
Ordering.natural()
.
a
- non-null value to compare, returned if less than or equal to b.b
- non-null value to compare.
ClassCastException
- if the parameters are not mutually comparable
(for example, a string and an integer).public static <T extends Comparable> T max(T a, T b)
To handle more than two values, call
Ordering.max(Object, Object, Object, Object...)
or
Ordering.max(Iterable)
on the Ordering
returned by
Ordering.natural()
.
a
- non-null value to compare, returned if greater than or equal to b.b
- non-null value to compare.
ClassCastException
- if the parameters are not mutually comparable
(for example, a string and an integer).public static <T> T min(Comparator<? super T> comparator, @Nullable T a, @Nullable T b)
To handle more than two values, call
Ordering.min(Object, Object, Object, Object...)
or
Ordering.min(Iterable)
on the Ordering
returned by
Ordering.forComparator(Comparator)
.
comparator
- comparator that compares the two valuesa
- value to compare, returned if less than or equal to bb
- value to comparepublic static <T> T max(Comparator<? super T> comparator, @Nullable T a, @Nullable T b)
To handle more than two values, call
Ordering.max(Object, Object, Object, Object...)
or
Ordering.max(Iterable)
on the Ordering
returned by
Ordering.forComparator(Comparator)
.
comparator
- comparator that compares the two valuesa
- value to compare, returned if greater than or equal to bb
- value to comparepublic static <T> Ordering<T> givenOrder(List<? extends 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.
The returned comparator throws an ClassCastException
when it
receives an input parameter that isn't in valuesInOrder
.
valuesInOrder
- the values that the returned comparator will be able
to compare, in the order the comparator should follow
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.
The returned comparator throws a ClassCastException
when it
receives an input parameter that isn't equal to leastValue
or in remainingValuesInOrder
.
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
IllegalArgumentException
- if any non-consecutive duplicate values
(according to Object.equals(java.lang.Object)
) are present among the method
argumentspublic 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 |