Class UnitGroupComparator

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

    public class UnitGroupComparator
    extends java.lang.Object
    implements java.util.Comparator<Unit>
    A comparator imposing an ordering on Unit objects by comparing the groups they belong to.

    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 units belong to the same group or they have no group set and they would be regarded as equal by this comparator, instead of 0 the result of the sub-comparator's comparison is returned.

    • Constructor Summary

      Constructors 
      Constructor Description
      UnitGroupComparator​(java.util.Comparator<? super Group> groupComparator, java.util.Comparator<? super Unit> sameGroupSubComparator, java.util.Comparator<? super Unit> noGroupSubComparator)
      Creates a new UnitGroupComparator object.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int compare​(Unit o1, Unit o2)
      Compares its two arguments for order according to the groups they belong to.
      • 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

      • groupCmp

        protected java.util.Comparator<? super Group> groupCmp
      • sameGroupSubCmp

        protected java.util.Comparator<? super Unit> sameGroupSubCmp
      • noGroupSubCmp

        protected java.util.Comparator<? super Unit> noGroupSubCmp
    • Constructor Detail

      • UnitGroupComparator

        public UnitGroupComparator​(java.util.Comparator<? super Group> groupComparator,
                                   java.util.Comparator<? super Unit> sameGroupSubComparator,
                                   java.util.Comparator<? super Unit> noGroupSubComparator)
        Creates a new UnitGroupComparator object.
        Parameters:
        groupComparator - the comparator used to compare the units' groups.
        sameGroupSubComparator - if two units belonging to the same group are compared, this sub-comparator is applied if it is not null.
        noGroupSubComparator - if two units belonging to no group are compared, this sub-comparator is applied if it is not null.
    • Method Detail

      • compare

        public int compare​(Unit o1,
                           Unit o2)
        Compares its two arguments for order according to the groups they belong to.
        Specified by:
        compare in interface java.util.Comparator<Unit>
        Parameters:
        o1 - the 1st object to compare
        o2 - the 2nd object to compare
        Returns:
        the difference of o1's and o2's group ids. If both belong to the same group and a sub-comparator was specified, the result that sub-comparator's comparison is returned. If both units do not belong to any group and a no-group sub-comparator was specified, the result of that sub-comparator's comparison is returned. Undefined values are evaluated as > 0.