Class FastComparableComparator

  • All Implemented Interfaces:
    java.io.Serializable, java.util.Comparator<java.lang.Object>

    public class FastComparableComparator
    extends java.lang.Object
    implements java.util.Comparator<java.lang.Object>, java.io.Serializable
    A Comparator that compares Comparable objects. Throws ClassCastExceptions if the objects are not Comparable, or if they are null. Different from ComparableComparator, it will not throw exception when the two compareTo methods don't provide an inverse result of each other as per the Comparable javadoc. We do so mainly to reduce one call to compareTo.
    If both objects are null, they will be treated as equal. If one is null and the other is not, the null value will be treated as smaller then non-null value.
    Author:
    bayard@generationjava.com, JIDE Software
    See Also:
    Serialized Form
    • Constructor Summary

      Constructors 
      Constructor Description
      FastComparableComparator()
      Constructs a FastComparableComparator.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int compare​(java.lang.Object o1, java.lang.Object o2)  
      static FastComparableComparator getInstance()
      Return a shared instance of a ComparableComparator.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.util.Comparator

        equals, reversed, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLong
    • Constructor Detail

      • FastComparableComparator

        public FastComparableComparator()
        Constructs a FastComparableComparator.
    • Method Detail

      • getInstance

        public static FastComparableComparator 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.
        Returns:
        an instance of ComparableComparator.
      • compare

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