org.deltava.comparators
Class AbstractComparator<T>

java.lang.Object
  extended by org.deltava.comparators.AbstractComparator<T>
All Implemented Interfaces:
Serializable, Comparator<T>
Direct Known Subclasses:
AirportComparator, EquipmentComparator, FlightReportComparator, PerformanceComparator, PersonComparator, 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:
3.0
Author:
Luke
See Also:
Serialized Form

Field Summary
protected  int _comparisonType
          The criteria by which to compare objects.
protected  boolean _reverseSort
          Are we doing a reverse sort?
protected  String[] _typeNames
          Text descriptions of the different comparison types.
 
Constructor Summary
protected AbstractComparator(String[] typeNames)
          Create a new comparator with a given set of type names.
 
Method Summary
 int compare(T o1, T o2)
          Return the result by interrogating the implementation and applying a reverse sort if requred.
protected abstract  int compareImpl(T o1, T o2)
          This method does the actual comparison.
 int getComparisonType()
          Return the comparison type.
 String[] getTypeNames()
          Return the list of comparison type names.
 boolean isReverseSort()
          Return if we are performing a reverse sort.
 void setComparisonType(int type)
          Sets the comparison type.
 void setComparisonType(String type)
          Sets the comparison type.
 void setReverseSort(boolean rSort)
          Updates the reverse sort parameter to sort in descending order.
 
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
 

Field Detail

_reverseSort

protected boolean _reverseSort
Are we doing a reverse sort?


_comparisonType

protected int _comparisonType
The criteria by which to compare objects.


_typeNames

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

Constructor Detail

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 Detail

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:
compare(Object, Object), Comparator.compare(java.lang.Object, java.lang.Object)

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:
getComparisonType()

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()

getTypeNames

public String[] getTypeNames()
Return the list of comparison type names.

Returns:
an array of type names

isReverseSort

public boolean isReverseSort()
Return if we are performing a reverse sort.

Returns:
TRUE if a reverse sort
See Also:
setReverseSort(boolean)

getComparisonType

public int getComparisonType()
Return the comparison type.

Returns:
The comparison type in use
See Also:
setComparisonType(int), setComparisonType(String)

setReverseSort

public final void setReverseSort(boolean rSort)
Updates the reverse sort parameter to sort in descending order.

Parameters:
rSort - TRUE if sorting in descending order, otherwise FALSE
See Also:
isReverseSort()

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:
compareImpl(Object, Object), Comparator.compare(java.lang.Object, java.lang.Object)


Copyright © 2004-2009 Global Virtual Airlines Group. All Rights Reserved.