Class ToStringComparator<T>

  • All Implemented Interfaces:
    java.util.Comparator<T>

    public class ToStringComparator<T>
    extends java.lang.Object
    implements java.util.Comparator<T>
    A comparator imposing an ordering on named objects by comparing their names alphabetically.

    Note: this comparator imposes orderings that are inconsistent with equals.

    In order to overcome the inconsistency with equals this comparator allows the introduction of a sub-comparator which is applied in cases of equality. I.e. if the two compared objects have the same name and they would be regarded as equal by this comparator, instead of 0 the result of the sub-comparator's comparison is returned.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.util.Comparator<java.lang.Object> DEFAULT
      The default NameComparator without a subComparator
      protected java.util.Comparator<? super java.lang.Object> sameNameSubCmp  
    • Constructor Summary

      Constructors 
      Constructor Description
      ToStringComparator​(java.util.Comparator<? super java.lang.Object> sameStringSubComparator)
      Creates a new NameComparator object.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int compare​(T o1, T o2)
      Compares its two arguments for order according to their names.
      • 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
    • Field Detail

      • sameNameSubCmp

        protected java.util.Comparator<? super java.lang.Object> sameNameSubCmp
      • DEFAULT

        public static final java.util.Comparator<java.lang.Object> DEFAULT
        The default NameComparator without a subComparator
    • Constructor Detail

      • ToStringComparator

        public ToStringComparator​(java.util.Comparator<? super java.lang.Object> sameStringSubComparator)
        Creates a new NameComparator object.
        Parameters:
        sameStringSubComparator - if two objects with the same name are compared, this sub-comparator is applied if it is not null.
    • Method Detail

      • compare

        public int compare​(T o1,
                           T o2)
        Compares its two arguments for order according to their names.
        Specified by:
        compare in interface java.util.Comparator<T>
        Parameters:
        o1 - an instance of interface Named.
        o2 - an instance of interface Named.
        Returns:
        the lexical difference of o1's and o2's names as returned by String.compareTo(). If the names are equal and a sub-comparator was specified, the result of that sub-comparator's comparison is returned. Undefined values are evaluated as > 0.