Class UnitTempUnitComparator

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

    public class UnitTempUnitComparator
    extends java.lang.Object
    implements java.util.Comparator<Unit>
    A comparator imposing an ordering on Unit and TempUnit objects by sorting them according to a parent-child relation.

    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 do not have a parent- child relation, instead of 0 the result of either the unit sub-comparator's or the same-parent sub-comparator's comparison is returned.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected java.util.Comparator<? super Unit> sameParentSubCmp  
      protected java.util.Comparator<? super Unit> unitSubCmp  
    • Constructor Summary

      Constructors 
      Constructor Description
      UnitTempUnitComparator​(java.util.Comparator<? super Unit> sameParentSubComparator, java.util.Comparator<? super Unit> unitSubComparator)
      Creates a new UnitTempUnitComparator object.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int compare​(Unit u1, Unit u2)
      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

      • sameParentSubCmp

        protected java.util.Comparator<? super Unit> sameParentSubCmp
      • unitSubCmp

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

      • UnitTempUnitComparator

        public UnitTempUnitComparator​(java.util.Comparator<? super Unit> sameParentSubComparator,
                                      java.util.Comparator<? super Unit> unitSubComparator)
        Creates a new UnitTempUnitComparator object.
        Parameters:
        sameParentSubComparator - if two units with the same parent unit are compared, this sub-comparator is applied if it is not null.
        unitSubComparator - if two units do not have the same parent unit and do not have a parent-child relation, this sub- comparator is applied if it is not null.
    • Method Detail

      • compare

        public int compare​(Unit u1,
                           Unit u2)
        Compares its two arguments for order according to their names.
        Specified by:
        compare in interface java.util.Comparator<Unit>
        Returns:
        a value less than zero if o1 is.an instance of class Unit and o2 an instance of class TempUnit and o1 is o2's parent. Conversely, if o1 is an instance of class TempUnit and o2 an instance of class Unit and o2 is the parent of o1, a value greater than zero is returned. When o1 and o2 have the same parent the result of the sameparentsubcomparator is applied if it is not null, else 0 is returned. If not both, o1 and o2, are instances of class TempUnit or they are but have different parents they or their parents respectively are compared with the unitsubcomparator if it is not null, else 0 is returned.