org.apache.commons.collections
Class ComparatorUtils

java.lang.Object
  extended by org.apache.commons.collections.ComparatorUtils

public class ComparatorUtils
extends Object

Provides convenient static utility methods for 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.

Since:
Commons Collections 2.1
Version:
$Revision: 1.1.1.1 $ $Date: 2005/05/23 04:32:48 $
Author:
Paul Jack, Matt Hall, John Watkinson, Stephen Colebourne

Field Summary
static Comparator NATURAL_COMPARATOR
          Comparator for natural sort order.
 
Constructor Summary
ComparatorUtils()
          ComparatorUtils should not normally be instantiated.
 
Method Summary
static Comparator<Boolean> booleanComparator(boolean trueFirst)
          Gets a Comparator that can sort Boolean objects.
static
<T> Comparator<T>
chainedComparator(Collection<T> comparators)
          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
<T> Comparator<T>
chainedComparator(Comparator<T>[] comparators)
          Gets a comparator that compares using an array of Comparators, applied in sequence until one returns not equal or the array is exhausted.
static
<T> Comparator<T>
chainedComparator(Comparator<T> comparator1, Comparator<T> comparator2)
          Gets a comparator that compares using two Comparators.
static
<T> T
max(T o1, T o2, Comparator<T> comparator)
          Returns the larger of the given objects according to the given comparator, returning the second object if the comparator returns equal.
static
<T> T
min(T o1, T o2, Comparator<T> comparator)
          Returns the smaller of the given objects according to the given comparator, returning the second object if the comparator returns equal.
static Comparator naturalComparator()
          Gets a comparator that uses the natural order of the objects.
static
<T> Comparator<T>
nullHighComparator(Comparator<T> comparator)
          Gets a Comparator that controls the comparison of null values.
static
<T> Comparator<T>
nullLowComparator(Comparator<T> comparator)
          Gets a Comparator that controls the comparison of null values.
static
<T> Comparator<T>
reversedComparator(Comparator<T> comparator)
          Gets a comparator that reverses the order of the given comparator.
static
<I,O> Comparator<O>
transformedComparator(Comparator<I> comparator, Transformer<I,O> transformer)
          Gets a Comparator that passes transformed objects to the given comparator.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NATURAL_COMPARATOR

public static final Comparator NATURAL_COMPARATOR
Comparator for natural sort order.

See Also:
ComparableComparator.getInstance()
Constructor Detail

ComparatorUtils

public ComparatorUtils()
ComparatorUtils should not normally be instantiated.

Method Detail

naturalComparator

public static Comparator naturalComparator()
Gets a comparator that uses the natural order of the objects.

Returns:
a comparator which uses natural order

chainedComparator

public static <T> Comparator<T> chainedComparator(Comparator<T> comparator1,
                                                  Comparator<T> comparator2)
Gets a comparator that compares using two Comparators.

The second comparator is used if the first comparator returns equal.

Parameters:
comparator1 - the first comparator to use, not null
comparator2 - the first comparator to use, not null
Returns:
a ComparatorChain formed from the two comparators
Throws:
NullPointerException - if either comparator is null
See Also:
ComparatorChain

chainedComparator

public static <T> Comparator<T> chainedComparator(Comparator<T>[] comparators)
Gets a comparator that compares using an array of Comparators, applied in sequence until one returns not equal or the array is exhausted.

Parameters:
comparators - the comparators to use, not null or empty or containing nulls
Returns:
a ComparatorChain formed from the input comparators
Throws:
NullPointerException - if comparators array is null or contains a null
See Also:
ComparatorChain

chainedComparator

public static <T> Comparator<T> chainedComparator(Collection<T> comparators)
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.

Parameters:
comparators - the comparators to use, not null or empty or containing nulls
Returns:
a ComparatorChain formed from the input comparators
Throws:
NullPointerException - if comparators collection is null or contains a null
ClassCastException - if the comparators collection contains the wrong object type
See Also:
ComparatorChain

reversedComparator

public static <T> Comparator<T> reversedComparator(Comparator<T> comparator)
Gets a comparator that reverses the order of the given comparator.

Parameters:
comparator - the comparator to reverse
Returns:
a comparator that reverses the order of the input comparator
See Also:
ReverseComparator

booleanComparator

public static Comparator<Boolean> booleanComparator(boolean trueFirst)
Gets a Comparator that can sort Boolean objects.

The parameter specifies whether true or false is sorted first.

The comparator throws NullPointerException if a null value is compared.

Parameters:
trueFirst - when true, sort true Booleans before false Booleans.
Returns:
a comparator that sorts booleans

nullLowComparator

public static <T> Comparator<T> nullLowComparator(Comparator<T> comparator)
Gets a Comparator that controls the comparison of 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.

Parameters:
comparator - the comparator that wants to allow nulls
Returns:
a version of that comparator that allows nulls
See Also:
NullComparator

nullHighComparator

public static <T> Comparator<T> nullHighComparator(Comparator<T> comparator)
Gets a Comparator that controls the comparison of 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.

Parameters:
comparator - the comparator that wants to allow nulls
Returns:
a version of that comparator that allows nulls
See Also:
NullComparator

transformedComparator

public static <I,O> Comparator<O> transformedComparator(Comparator<I> comparator,
                                                        Transformer<I,O> transformer)
Gets a Comparator that passes transformed objects to the given comparator.

Objects passed to the returned comparator will first be transformed by the given transformer before they are compared by the given comparator.

Parameters:
comparator - the sort order to use
transformer - the transformer to use
Returns:
a comparator that transforms its input objects before comparing them
See Also:
TransformingComparator

min

public static <T> T min(T o1,
                        T o2,
                        Comparator<T> comparator)
Returns the smaller of the given objects according to the given comparator, returning the second object if the comparator returns equal.

Parameters:
o1 - the first object to compare
o2 - the second object to compare
comparator - the sort order to use
Returns:
the smaller of the two objects

max

public static <T> T max(T o1,
                        T o2,
                        Comparator<T> comparator)
Returns the larger of the given objects according to the given comparator, returning the second object if the comparator returns equal.

Parameters:
o1 - the first object to compare
o2 - the second object to compare
comparator - the sort order to use
Returns:
the larger of the two objects


Copyright © 2005-2005 Apache Software Foundation, Matt Hall, John Watkinson. All Rights Reserved.