Class GroupingComparator<T>

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

    public class GroupingComparator<T>
    extends java.lang.Object
    implements java.util.Comparator<T>
    This comparator glues two comparators together.

    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 units belong to the same faction 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
      protected java.util.Comparator<? super T> main  
      protected GroupingComparator<? super T> sub  
    • Constructor Summary

      Constructors 
      Constructor Description
      GroupingComparator​(java.util.Comparator<? super T> mainComparator, java.util.Comparator<? super T> subComparator)  
      GroupingComparator​(java.util.Comparator<? super T> mainComparator, GroupingComparator<? super T> subComparator)
      Creates a new GroupingComparator object.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static <S> GroupingComparator<S> buildFromList​(java.util.Comparator<S>[] comparators)  
      int compare​(T o1, T o2)
      Compares its two arguments.
      • 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

      • main

        protected java.util.Comparator<? super T> main
    • Constructor Detail

      • GroupingComparator

        public GroupingComparator​(java.util.Comparator<? super T> mainComparator,
                                  GroupingComparator<? super T> subComparator)
        Creates a new GroupingComparator object.
        Parameters:
        mainComparator - the comparator used to compare the given objects
        subComparator - the comparator used to compare the given objects if mainComparator delivers 0.
      • GroupingComparator

        public GroupingComparator​(java.util.Comparator<? super T> mainComparator,
                                  java.util.Comparator<? super T> subComparator)
    • Method Detail

      • compare

        public int compare​(T o1,
                           T o2)
        Compares its two arguments. Also it returns powers of 2 to return the depth of the underlying comparators
        Specified by:
        compare in interface java.util.Comparator<T>
      • buildFromList

        public static <S> GroupingComparator<S> buildFromList​(java.util.Comparator<S>[] comparators)