org.apache.commons.collections.comparators
Class ComparableComparator

java.lang.Object
  |
  +--org.apache.commons.collections.comparators.ComparableComparator
All Implemented Interfaces:
java.util.Comparator, java.io.Serializable

public class ComparableComparator
extends java.lang.Object
implements java.util.Comparator, java.io.Serializable

A Comparator that compares Comparable objects. Throws ClassCastExceptions if the objects are not Comparable, or if they are null. Throws ClassCastException if the compareTo of both objects do not provide an inverse result of each other as per the Comparable javadoc. This Comparator is useful, for example, for enforcing the natural order in custom implementations of SortedSet and SortedMap.

Since:
2.0
Version:
$Id: ComparableComparator.java,v 1.5.2.1 2004/05/22 12:14:04 scolebourne Exp $
Author:
bayard@generationjava.com
See Also:
Serialized Form

Constructor Summary
ComparableComparator()
           
 
Method Summary
 int compare(java.lang.Object o1, java.lang.Object o2)
           
static ComparableComparator getInstance()
          Return a shared instance of a ComparableComparator.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.Comparator
equals
 

Constructor Detail

ComparableComparator

public ComparableComparator()
Method Detail

getInstance

public static ComparableComparator getInstance()
Return a shared instance of a ComparableComparator. Developers are encouraged to use 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.


compare

public int compare(java.lang.Object o1,
                   java.lang.Object o2)
Specified by:
compare in interface java.util.Comparator


Copyright © 2001-2004 The Apache Software Foundation. All Rights Reserved.