Class AbstractComparator<T>

java.lang.Object
org.deltava.comparators.AbstractComparator<T>
Type Parameters:
T - the object class
All Implemented Interfaces:
Serializable, Comparator<T>
Direct Known Subclasses:
AirlineComparator, AirportComparator, CountryComparator, EquipmentComparator, FlightReportComparator, GateComparator, PerformanceComparator, PersonComparator, RequestCounterComparator, ScheduleEntryComparator, StaffComparator, TestComparator

public abstract class AbstractComparator<T> extends Object implements Comparator<T>, Serializable
Implements common comparator functions. NOTE: Most comparators in this package impose orderings that are inconsistent with equals.
Since:
1.0
Version:
7.2
Author:
Luke
See Also:
  • Field Details

    • _comparisonType

      protected int _comparisonType
      The criteria by which to compare objects.
    • _typeNames

      protected String[] _typeNames
      Text descriptions of the different comparison types.
  • Constructor Details

    • AbstractComparator

      protected AbstractComparator(String[] typeNames)
      Create a new comparator with a given set of type names.
      Parameters:
      typeNames - An array of type names
      Throws:
      NullPointerException - if typeNames is null
  • Method Details

    • compareImpl

      protected abstract int compareImpl(T o1, T o2)
      This method does the actual comparison. It is called and then we modify the result if we are performing a reverse sort.
      Parameters:
      o1 - The first object to compare
      o2 - The second object to compare
      Returns:
      -1, 0, 1 as defined by the compareTo(Object, Object) interface
      Throws:
      ClassCastException - if the object types are not supported by this implementation
      See Also:
    • setComparisonType

      public void setComparisonType(int type)
      Sets the comparison type.
      Parameters:
      type - The comparison type.
      Throws:
      IllegalArgumentException - if the type is negative or > typeNames.length
      See Also:
    • setComparisonType

      public void setComparisonType(String type)
      Sets the comparison type.
      Parameters:
      type - The comparison type, contained within _typeNames
      Throws:
      IllegalArgumentException - if the type is not found in _typeNames
      See Also:
    • getTypeNames

      public String[] getTypeNames()
      Return the list of comparison type names.
      Returns:
      an array of type names
    • getComparisonType

      public int getComparisonType()
      Return the comparison type.
      Returns:
      The comparison type in use
      See Also:
    • compare

      public final int compare(T o1, T o2)
      Return the result by interrogating the implementation and applying a reverse sort if requred.
      Specified by:
      compare in interface Comparator<T>
      Parameters:
      o1 - The first object to compare
      o2 - The second object to compare
      Returns:
      -1, 0, 1 as defined by the compareTo(Object, Object) interface
      Throws:
      ClassCastException - if the object types are not supported by this implementation
      See Also: