public class ComparatorUtils extends Object
Comparator objects.
Most of the functionality in this class can also be found in the comparators package. This class merely provides a convenient central place if you have use for more than one class in the comparators subpackage.
| Modifier and Type | Field and Description |
|---|---|
static Comparator |
NATURAL_COMPARATOR
Comparator for natural sort order.
|
| Modifier and Type | Method and Description |
|---|---|
static Comparator |
booleanComparator(boolean trueFirst)
Gets a Comparator that can sort Boolean objects.
|
static <E> Comparator |
chainedComparator(Collection
Gets a comparator that compares using a collection of
Comparators, applied in (default iterator) sequence until one returns not equal or the collection is exhausted.
|
static <E> Comparator |
chainedComparator(Comparator
Gets a comparator that compares using an array of
Comparators, applied in sequence until one returns not equal or the array is exhausted.
|
static <E> E |
max(E o1, E o2, Comparator
Returns the larger of the given objects according to the given comparator, returning the second object if the comparator returns equal.
|
static <E> E |
min(E o1, E o2, Comparator
Returns the smaller of the given objects according to the given comparator, returning the second object if the comparator returns equal.
|
static <E extends Comparable |
naturalComparator()
Gets a comparator that uses the natural order of the objects.
|
static <E> Comparator |
nullHighComparator(Comparator
Gets a Comparator that controls the comparison of
null values.
|
static <E> Comparator |
nullLowComparator(Comparator
Gets a Comparator that controls the comparison of
null values.
|
static <E> Comparator |
reversedComparator(Comparator
Gets a comparator that reverses the order of the given comparator.
|
static <I |
transformedComparator(Comparator
Gets a Comparator that passes transformed objects to the given comparator.
|
public static final ComparatorNATURAL_COMPARATOR
public static <E extends Comparable<? super E>> Comparator <E> naturalComparator()
E - the object type to compare
public static <E> Comparator<E> chainedComparator(Comparator <E>... comparators)
Comparators, applied in sequence until one returns not equal or the array is exhausted.
E - the object type to compare
comparators - the comparators to use, not null or empty or containing nulls
ComparatorChain formed from the input comparators
NullPointerException - if comparators array is null or contains a null
ComparatorChain
public static <E> Comparator<E> chainedComparator(Collection <Comparator <E>> comparators)
Comparators, applied in (default iterator) sequence until one returns not equal or the collection is exhausted.
E - the object type to compare
comparators - the comparators to use, not null or empty or containing nulls
ComparatorChain formed from the input comparators
NullPointerException - if comparators collection is null or contains a null
ClassCastException - if the comparators collection contains the wrong object type
ComparatorChain
public static <E> Comparator<E> reversedComparator(Comparator <E> comparator)
E - the object type to compare
comparator - the comparator to reverse
ReverseComparator
public static Comparator<Boolean > booleanComparator(boolean trueFirst)
The parameter specifies whether true or false is sorted first.
The comparator throws NullPointerException if a null value is compared.
public static <E> Comparator<E> nullLowComparator(Comparator <E> comparator)
null values.
The returned comparator will consider a null value to be less than any nonnull value, and equal to any other null value. Two nonnull values will be evaluated with the given comparator.
E - the object type to compare
comparator - the comparator that wants to allow nulls
NullComparator
public static <E> Comparator<E> nullHighComparator(Comparator <E> comparator)
null values.
The returned comparator will consider a null value to be greater than any nonnull value, and equal to any other null value. Two nonnull values will be evaluated with the given comparator.
E - the object type to compare
comparator - the comparator that wants to allow nulls
NullComparator
public static <I,O> Comparator <I> transformedComparator(Comparator <O> comparator, Transformer <? super I ,? extends O> transformer)
Objects passed to the returned comparator will first be transformed by the given transformer before they are compared by the given comparator.
I - the input object type of the transformed comparator
O - the object type of the decorated comparator
comparator - the sort order to use
transformer - the transformer to use
TransformingComparator
public static <E> E min(E o1,
E o2,
Comparator<E> comparator)
E - the object type to compare
o1 - the first object to compare
o2 - the second object to compare
comparator - the sort order to use
public static <E> E max(E o1,
E o2,
Comparator<E> comparator)
E - the object type to compare
o1 - the first object to compare
o2 - the second object to compare
comparator - the sort order to use