net.sf.molae.pipe.sorted
Class ComparatorUtil

java.lang.Object
  extended by net.sf.molae.pipe.sorted.ComparatorUtil

public final class ComparatorUtil
extends Object

Utility methods for Comparators.


Method Summary
static boolean areEqual(Object o1, Object o2)
          Checks the specified objects for equality.
static
<T> int
compare(T o1, T o2, Comparator<T> c)
          Compares two objects using the specified comparator.
static
<T> boolean
isSorted(Iterator<T> it, Comparator<? super T> comparator)
          Checks if the specified iterator is in sorted order of the specified comparator.
static
<T> T
max(T o1, T o2)
          Compares the two specified objects and returns the greater one.
static
<T> T
max(T o1, T o2, Comparator<? super T> comparator)
          Compares the two specified objects and returns the greater one.
static
<T> T
min(T o1, T o2)
          Compares the two specified objects and returns the smaller one.
static
<T> T
min(T o1, T o2, Comparator<? super T> comparator)
          Compares the two specified objects and returns the smaller one.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

areEqual

public static boolean areEqual(Object o1,
                               Object o2)
Checks the specified objects for equality. The objects are also considered equal if they are both null.

Parameters:
o1 - first object to compare
o2 - second object to compare
Returns:
true iff both objects are null or both objects are equal.

compare

public static <T> int compare(T o1,
                              T o2,
                              Comparator<T> c)
Compares two objects using the specified comparator.

Parameters:
o1 - first object to compare
o2 - second object to compare
c - comparator to use for comparison or null if natural ordering is used
Returns:
a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
Throws:
ClassCastException - if o1 and o2 cannot be compared to one another using the comparator (or, if it is null, using natural ordering).
NullPointerException - if o1 or o2 is null and natural order is used, or if the comparator does not tolerate null elements.

max

public static <T> T max(T o1,
                        T o2,
                        Comparator<? super T> comparator)
Compares the two specified objects and returns the greater one.

Parameters:
o1 - first object to compare
o2 - second object to compare
comparator - comparator to use for comparison or null if natural ordering is used
Returns:
The maximum of o1 and o2. If both are equal, o1 is returned.
Throws:
ClassCastException - if o1 and o2 cannot be compared to one another using the comparator (or, if it is null, using natural ordering).
NullPointerException - if o1 or o2 is null and natural order is used, or if the comparator does not tolerate null elements.

min

public static <T> T min(T o1,
                        T o2,
                        Comparator<? super T> comparator)
Compares the two specified objects and returns the smaller one.

Parameters:
o1 - first object to compare
o2 - second object to compare
comparator - comparator to use for comparison or null if natural ordering is used
Returns:
The minimum of o1 and o2. If both are equal, o1 is returned.
Throws:
ClassCastException - if o1 and o2 cannot be compared to one another using the comparator (or, if it is null, using natural ordering).
NullPointerException - if o1 or o2 is null and natural order is used, or if the comparator does not tolerate null elements.

max

public static <T> T max(T o1,
                        T o2)
Compares the two specified objects and returns the greater one.

Parameters:
o1 - first object to compare
o2 - second object to compare
Returns:
The maximum of o1 and o2. If both are equal, o1 is returned.
Throws:
ClassCastException - if o1 and o2 cannot be compared to one another.
NullPointerException - if o1 or o2 is null.

min

public static <T> T min(T o1,
                        T o2)
Compares the two specified objects and returns the smaller one.

Parameters:
o1 - first object to compare
o2 - second object to compare
Returns:
The minimum of o1 and o2. If both are equal, o1 is returned.
Throws:
ClassCastException - if o1 and o2 cannot be compared to one another.
NullPointerException - if o1 or o2 is null.

isSorted

public static <T> boolean isSorted(Iterator<T> it,
                                   Comparator<? super T> comparator)
Checks if the specified iterator is in sorted order of the specified comparator.

Parameters:
it - iterator to be examined
comparator - comparator to use for comparison or null if natural ordering is used
Returns:
true if the specified iterator is in sorted order of the specified comparator.
Throws:
ClassCastException - if the elements of the iterator cannont be compared to one another using. the comparator (or, if it is null, using natural ordering).
NullPointerException - the iterator contains a null element and natural order is used, or if the comparator does not tolerate null elements.