Class UnitPreferredFactionComparator

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

    public class UnitPreferredFactionComparator
    extends java.lang.Object
    implements java.util.Comparator<Unit>
    A comparator sorting units according to whether they belong to a specified faction or not.

    Note: this comparator imposes orderings that are inconsistent with equals

    This class allows to introduce sub-comparators in order to create a sorting hierarchy.

    This comparator does not impose a strict ordering on the compared units, since either two units may be of the same faction or the may be of different factions, where neither of them belongs to the specified faction. In these cases the units remain "unsorted".

    In order to have these units sorted, one sub-comparator for each of the two cases above my be introduced, that may or, again, may not impose a strict order on the compared elements.

    • Constructor Summary

      Constructors 
      Constructor Description
      UnitPreferredFactionComparator​(java.util.Comparator<? super Unit> preferredFactionSubComparator, java.util.Comparator<? super Unit> otherFactionSubComparator, int fID)
      Creates a new UnitPreferredFactionComparator object with the specified sub-comparators and the specified faction id.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int compare​(Unit o1, Unit o2)
      Compares two units, where unit one is regarded less than unit two if unit one belongs to the preferred faction, whereas unit two does not.
      • 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

      • preferredFactionSubCmp

        protected java.util.Comparator<? super Unit> preferredFactionSubCmp
      • otherFactionSubCmp

        protected java.util.Comparator<? super Unit> otherFactionSubCmp
      • factionID

        protected int factionID
    • Constructor Detail

      • UnitPreferredFactionComparator

        public UnitPreferredFactionComparator​(java.util.Comparator<? super Unit> preferredFactionSubComparator,
                                              java.util.Comparator<? super Unit> otherFactionSubComparator,
                                              int fID)
        Creates a new UnitPreferredFactionComparator object with the specified sub-comparators and the specified faction id.
        Parameters:
        preferredFactionSubComparator - a Comparator applied if both compared units are in the preferred faction.
        otherFactionSubComparator - a Comparator applied if both compared units are not in the preferred faction.
    • Method Detail

      • compare

        public int compare​(Unit o1,
                           Unit o2)
        Compares two units, where unit one is regarded less than unit two if unit one belongs to the preferred faction, whereas unit two does not. Cases of equality are handled by sub-comparators, if specified, or returned as such.
        Specified by:
        compare in interface java.util.Comparator<Unit>
        Returns:
        -1 if o1 belongs to the preferred faction and o2 does not, 1 if o2 belongs to the preferred faction and o1 does not. The return value in the other cases (both or neither of them belong to the preferred faction) depends on the sub-comparators.