gov.sns.tools.data
Class SortOrdering

java.lang.Object
  |
  +--gov.sns.tools.data.SortOrdering
All Implemented Interfaces:
java.util.Comparator

public class SortOrdering
extends java.lang.Object
implements java.util.Comparator

SortOrdering can be used in DataTable queries to order records based on their values associated with the sort ordering keys. These associated values must be comparable (i.e. their class must implement the Comparable interface). The comparison is made starting with the first key and considers successive keys as necessary until the records can be ordered or all keys are exhausted resulting in equality.


Field Summary
protected  java.lang.String[] keys
           
 
Constructor Summary
SortOrdering()
          Empty constructor with now sort keys specified.
SortOrdering(java.lang.String key)
          Constructor with a single sort key.
SortOrdering(java.lang.String[] newKeys)
          Constructor with an array of sort keys.
 
Method Summary
 void addKey(java.lang.String newKey)
          Add a new sort key to the end of the sort keys.
 int compare(GenericRecord record1, GenericRecord record2)
          Compare two records.
 int compare(java.lang.Object record1, java.lang.Object record2)
          Compare two records to satisfy the Comparator interface.
 java.lang.String[] getKeys()
          Get the sort ordering keys.
 
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

keys

protected java.lang.String[] keys
Constructor Detail

SortOrdering

public SortOrdering()
Empty constructor with now sort keys specified.


SortOrdering

public SortOrdering(java.lang.String key)
Constructor with a single sort key.

Parameters:
key - The lone key used for sorting.

SortOrdering

public SortOrdering(java.lang.String[] newKeys)
Constructor with an array of sort keys.

Parameters:
newKeys - The array of keys used for the sort ordering.
Method Detail

addKey

public void addKey(java.lang.String newKey)
Add a new sort key to the end of the sort keys.

Parameters:
newKey - The new sort key.

getKeys

public java.lang.String[] getKeys()
Get the sort ordering keys.

Returns:
The sort ordering keys.

compare

public int compare(java.lang.Object record1,
                   java.lang.Object record2)
Compare two records to satisfy the Comparator interface. The sort ordering compares records based on their values associated with the sort ordering keys. These associated values must be comparable (i.e. their class must implement the Comparable interface). The comparison is made starting with the first key and considers successive keys as necessary until the records can be ordered or all keys are exhausted resulting in equality.

Specified by:
compare in interface java.util.Comparator
Parameters:
record1 - The first record of the comparison. Its class should be a subclass of GenericRecord.
record2 - The second record of the comparison. Its class should be a subclass of GenericRecord.
Returns:
0 if the records are equal, negative if record2 > record1 and positive if record1 > record2
Throws:
java.lang.ClassCastException - if the records cannot be cast to GenericRecord.

compare

public int compare(GenericRecord record1,
                   GenericRecord record2)
Compare two records. The sort ordering compares records based on their values associated with the sort ordering keys. These associated values must be comparable (i.e. their class must implement the Comparable interface). The comparison is made starting with the first key and considers successive keys as necessary until the records can be ordered or all keys are exhausted resulting in equality.

Parameters:
record1 - The first record of the comparison.
record2 - The second record of the comparison.
Returns:
0 if the records are equal, negative if record2 > record1 and positive if record1 > record2