Class ShipTypeComparator

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

    public class ShipTypeComparator
    extends java.lang.Object
    implements java.util.Comparator<Ship>
    A comparator imposing an ordering on Ship objects by comparing their types.

    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 ships have the same type 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 Ship> subCmp  
    • Constructor Summary

      Constructors 
      Constructor Description
      ShipTypeComparator​(java.util.Comparator<? super Ship> aSubCmp)
      Creates a new ShipTypeComparator object.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int compare​(Ship o1, Ship o2)
      Compares its two arguments for order according to their types. ' *
      • 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

      • subCmp

        protected java.util.Comparator<? super Ship> subCmp
    • Constructor Detail

      • ShipTypeComparator

        public ShipTypeComparator​(java.util.Comparator<? super Ship> aSubCmp)
        Creates a new ShipTypeComparator object.
        Parameters:
        aSubCmp - if two ships having the same type are compared, this sub-comparator is applied if it is notnull.
    • Method Detail

      • compare

        public int compare​(Ship o1,
                           Ship o2)
        Compares its two arguments for order according to their types. ' *
        Specified by:
        compare in interface java.util.Comparator<Ship>
        Returns:
        the natural ordering of o1's and o2's types as returned by BuildingType.compareTo(). If the types are equal and a sub-comparator was specified, the result of that sub-comparator's comparison is returned.