Class RegionIslandComparator

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

    public class RegionIslandComparator
    extends java.lang.Object
    implements java.util.Comparator<Region>
    A comparator imposing an ordering on Region objects by comparing the islands 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 regions belong to the same island and they would be regarded as equal by this comparator, instead of 0 the result of the sub-comparator's comparison is returned. Similarly, the no-island sub-comparator is applied when the island members of both regions are null.

    • Constructor Summary

      Constructors 
      Constructor Description
      RegionIslandComparator​(java.util.Comparator<? super Island> islandComparator, java.util.Comparator<? super Region> sameIslandSubComparator, java.util.Comparator<? super Region> noIslandSubComparator)
      Creates a new RegionIslandComparator object.
    • Method Summary

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

      • islandCmp

        protected java.util.Comparator<? super Island> islandCmp
      • sameIslandSubCmp

        protected java.util.Comparator<? super Region> sameIslandSubCmp
      • noIslandSubCmp

        protected java.util.Comparator<? super Region> noIslandSubCmp
    • Constructor Detail

      • RegionIslandComparator

        public RegionIslandComparator​(java.util.Comparator<? super Island> islandComparator,
                                      java.util.Comparator<? super Region> sameIslandSubComparator,
                                      java.util.Comparator<? super Region> noIslandSubComparator)
        Creates a new RegionIslandComparator object.
        Parameters:
        islandComparator - determines how the islands are sorted. If null is specified the islands are sorted according to their natural order.
        sameIslandSubComparator - if two regions belonging to the same island are compared, this sub-comparator is applied if it is not null.
        noIslandSubComparator - if the compared regions both have island members that are null, this sub-comparator is applied if it is not null.
    • Method Detail

      • compare

        public int compare​(Region o1,
                           Region o2)
        Compares its two arguments for order according to the islands they belong to.
        Specified by:
        compare in interface java.util.Comparator<Region>
        Returns:
        the result of comparing o1's and o2's islands with the specified island comparator. If both belong to the same island and a same-island sub-comparator was specified, the result of that sub-comparator's comparison is returned. If one of the islands is null, it is considered larger than the other one. If both islands are null the no-island sub-comparator is applied.