Class UnitGuiseFactionComparator

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

    public class UnitGuiseFactionComparator
    extends java.lang.Object
    implements java.util.Comparator<Unit>
    A comparator imposing an ordering on Unit objects by comparing the factions they pretend to 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 faction 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
      UnitGuiseFactionComparator​(java.util.Comparator<? super Faction> factionComparator, java.util.Comparator<? super Unit> sameFactionSubComparator)
      Creates a new UnitFactionComparator 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 factions they are disguised as.
      • 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

      • factionCmp

        protected java.util.Comparator<? super Faction> factionCmp
      • sameFactionSubCmp

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

      • UnitGuiseFactionComparator

        public UnitGuiseFactionComparator​(java.util.Comparator<? super Faction> factionComparator,
                                          java.util.Comparator<? super Unit> sameFactionSubComparator)
        Creates a new UnitFactionComparator object.
        Parameters:
        factionComparator - the comparator used to compare the units' factions.
        sameFactionSubComparator - if two units belonging to the same faction 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 factions they are disguised as.
        Specified by:
        compare in interface java.util.Comparator<Unit>
        Returns:
        the result of the faction comparator's comparison of o1's and o2. If are disguised as the same faction and a sub-comparator was specified, the result that sub-comparator's comparison is returned. Unknown values are evaluated as > 0.