Class NameComparator

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

    public class NameComparator
    extends java.lang.Object
    implements java.util.Comparator<Named>
    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<Named> DEFAULT
      The default NameComparator without a subComparator
      protected java.util.Comparator<? super Named> sameNameSubCmp  
    • Constructor Summary

      Constructors 
      Constructor Description
      NameComparator​(java.util.Comparator<? super Named> sameNameSubComparator)
      Creates a new NameComparator object.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int compare​(Named o1, Named 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 Named> sameNameSubCmp
      • DEFAULT

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

      • NameComparator

        public NameComparator​(java.util.Comparator<? super Named> sameNameSubComparator)
        Creates a new NameComparator object.
        Parameters:
        sameNameSubComparator - 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​(Named o1,
                           Named o2)
        Compares its two arguments for order according to their names.
        Specified by:
        compare in interface java.util.Comparator<Named>
        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.