net.sf.collections15.comparators
Class ComparableComparator<E extends java.lang.Comparable>

java.lang.Object
  extended by net.sf.collections15.comparators.ComparableComparator<E>
All Implemented Interfaces:
java.io.Serializable, java.util.Comparator<E>

public class ComparableComparator<E extends java.lang.Comparable>
extends java.lang.Object
implements java.util.Comparator<E>, java.io.Serializable

A Comparator that compares Comparable objects.

This Comparator is useful, for example, for enforcing the natural order in custom implementations of SortedSet and SortedMap.

Since:
Collections15 1.0
Author:
Henri Yandell, Chris Lambrou (port to Java 5.0)
See Also:
Collections.reverseOrder(), Serialized Form

Constructor Summary
protected ComparableComparator()
          Constructs a new instance.
 
Method Summary
 int compare(E obj1, E obj2)
          Compare the two Comparable arguments.
 boolean equals(java.lang.Object object)
          Determines whether or not a specified object is equal to this ComparableComparator instance.
static
<T extends java.lang.Comparable>
ComparableComparator<T>
getInstance()
          Gets a ComparableComparator instance.
 int hashCode()
          Implement a hash code for this comparator that is consistent with equals.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ComparableComparator

protected ComparableComparator()
Constructs a new instance. The protected access modifier forces use of the getInstance() method.

Method Detail

getInstance

public static <T extends java.lang.Comparable> ComparableComparator<T> getInstance()
Gets a ComparableComparator instance.

Developers are encouraged to reuse the comparator returned from this method instead of constructing a new instance to reduce allocation and GC overhead when multiple comparable comparators may be used in the same VM.

Returns:
A ComparableComparator apropriate to the generic type.

compare

public int compare(E obj1,
                   E obj2)
Compare the two Comparable arguments. This method is equivalent to: ((Comparable)obj1).compareTo(obj2)

Specified by:
compare in interface java.util.Comparator<E extends java.lang.Comparable>
Parameters:
obj1 - The first object to compare.
obj2 - The second object to compare.
Returns:
A negative value if obj1 is less than obj2, a positive value if obj1 is greater than obj2 or zero if obj1 and obj2 are equal.
Throws:
java.lang.NullPointerException - Thrown if obj1 is null, or when ((Comparable)obj1).compareTo(obj2) throws a NullPointerException.

hashCode

public int hashCode()
Implement a hash code for this comparator that is consistent with equals.

Overrides:
hashCode in class java.lang.Object
Returns:
A hash code for this comparator.
Since:
Collections15 1.0

equals

public boolean equals(java.lang.Object object)
Determines whether or not a specified object is equal to this ComparableComparator instance.

This implementation returns true only if object.getClass() equals this.getClass(). Subclasses may want to override this behavior to remain consistent with the Comparator.equals(Object) contract.

Specified by:
equals in interface java.util.Comparator<E extends java.lang.Comparable>
Overrides:
equals in class java.lang.Object
Parameters:
object - The object to compare to.
Returns:
true If the specified object is equal to this instance.
Since:
Collections15 1.0


Copyright © 2001-2005 SourceForge.net. All Rights Reserved.