net.sf.molae.pipe.sorted
Class ComparatorWithInfinity<T>

java.lang.Object
  extended by net.sf.molae.pipe.sorted.ComparatorWithInfinity<T>
All Implemented Interfaces:
Comparator<T>

public final class ComparatorWithInfinity<T>
extends Object
implements Comparator<T>

Allows to define two constants for negative and positive infinity that are always less or greater than any other object, respecitively.


Constructor Summary
ComparatorWithInfinity(Comparator<T> base, Object negativeInfinity, Object positiveInfinity)
          Constructs a ComparatorWithInfinity object.
 
Method Summary
 int compare(T o1, T o2)
          If either argument is positive or negative infinity as specified in the constructor, the corresponding result is returned, otherwise the base comparator is used.
 boolean equals(Object obj)
          Compares the specified object with this comparator for equality.
static
<T> Comparator<T>
newInstance(Comparator<T> base, Object negativeInfinity, Object positiveInfinity)
          Constructs a ComparatorWithInfinity object.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ComparatorWithInfinity

public ComparatorWithInfinity(Comparator<T> base,
                              Object negativeInfinity,
                              Object positiveInfinity)
Constructs a ComparatorWithInfinity object.

Parameters:
base - the underlying comparator.
negativeInfinity - an Object representing negative infinity.
positiveInfinity - an Object representing positive infinity.
Method Detail

newInstance

public static <T> Comparator<T> newInstance(Comparator<T> base,
                                            Object negativeInfinity,
                                            Object positiveInfinity)
Constructs a ComparatorWithInfinity object.

Parameters:
base - the underlying comparator.
negativeInfinity - an Object representing negative infinity.
positiveInfinity - an Object representing positive infinity.

compare

public int compare(T o1,
                   T o2)
If either argument is positive or negative infinity as specified in the constructor, the corresponding result is returned, otherwise the base comparator is used.

Specified by:
compare in interface Comparator<T>
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.

equals

public boolean equals(Object obj)
Compares the specified object with this comparator for equality. Returns true, iff the parameter is a ComparatorWithInfinity and all properties are equal.

Specified by:
equals in interface Comparator<T>
Overrides:
equals in class Object
Parameters:
obj - the reference object with which to compare.
Returns:
true if the specified Object is equal to this comparator