Class SortIndexComparator<T extends Sorted>

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

    public class SortIndexComparator<T extends Sorted>
    extends java.lang.Object
    implements java.util.Comparator<T>
    A comparator imposing an ordering on objects implementing the Sorted interface by comparing their sorting indices.

    Note: this comparator can impose 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 sorting index and they would be regarded as equal by this comparator, instead of 0 the result of the sub-comparator's comparison is returned.

    Note 2: If the subcomparator is not consistent with this comparator, this can lead to wrong results! For example, if this comparator returns compare(o2, o3) < 0 and is undefined on other both (o1, o3) and (o1, o2), and the subcomparator returns compare(o1, o2) < 0 and compare(o3, o1) < 0, then it is impossible to predict if a sorting algorithm might return the ordering (o1, o2, o3) or (o3, o1, o2), or (o2, o3, o1).

    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected java.util.Comparator<? super T> sameIndexSubCmp  
    • Constructor Summary

      Constructors 
      Constructor Description
      SortIndexComparator​(java.util.Comparator<? super T> sameIndexSubComparator)
      Creates a new SortIndexComparator 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 sort indices.
      • 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

      • sameIndexSubCmp

        protected java.util.Comparator<? super T extends Sorted> sameIndexSubCmp
    • Constructor Detail

      • SortIndexComparator

        public SortIndexComparator​(java.util.Comparator<? super T> sameIndexSubComparator)
        Creates a new SortIndexComparator object.
        Parameters:
        sameIndexSubComparator - if two objects with the same sort index are compared, the given sub-comparator is applied (if not null).
    • Method Detail

      • compare

        public int compare​(T o1,
                           T o2)
        Compares its two arguments for order according to their sort indices.
        Specified by:
        compare in interface java.util.Comparator<T extends Sorted>
        Returns:
        the numerical difference of o1's and o2's sort indices. If the sort indices are equal and a sub-comparator was specified, the result of that sub-comparator's comparison is returned.