PIRL

PIRL.Conductor
Class String_Vector_Comparator

java.lang.Object
  extended by PIRL.Conductor.String_Vector_Comparator
All Implemented Interfaces:
Comparator

public class String_Vector_Comparator
extends Object
implements Comparator

A String_Vector_Comparator implements the Comparator interface to sort Lists of Objects.

It is particularly useful, for example, in sorting a set of results from a Database query. Use an instance of this class as the Comparator in the Collections.sort(List, Comparator) static method, which actually does the sorting. The Objects to be compared are assumed to be Lists. The Objects at the specified index of each List are converted to Strings. If these Strings can be parsed as Doubles then these are compared numerically. Otherwise the Strings are are compared lexicographically.

Author:
Christian Schaller, Bradford Castalia - UA/PIRL

Field Summary
static String ID
          Class identification name with source code version and date.
 
Constructor Summary
String_Vector_Comparator()
          Construct a String_Vector_Comparator that will compare index zero of the Lists.
String_Vector_Comparator(int index)
          Construct a String_Vector_Comparator using a sepecified index for the Lists to be compared.
 
Method Summary
 int compare(Object list_0, Object list_1)
          Compare two Lists.
 int Index()
          Get the List index to be compared.
 String_Vector_Comparator Index(int index)
          Set the List index to be compared.
 
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

ID

public static final String ID
Class identification name with source code version and date.

See Also:
Constant Field Values
Constructor Detail

String_Vector_Comparator

public String_Vector_Comparator(int index)
Construct a String_Vector_Comparator using a sepecified index for the Lists to be compared.

Parameters:
index - The List index to be compared.

String_Vector_Comparator

public String_Vector_Comparator()
Construct a String_Vector_Comparator that will compare index zero of the Lists.

Method Detail

Index

public String_Vector_Comparator Index(int index)
Set the List index to be compared.

Parameters:
index - The List index to be compared.
Returns:
This String_Vector_Comparator.
Throws:
IllegalArgumentException - If the index is negative.

Index

public int Index()
Get the List index to be compared.

Returns:
The List index to be compared.
See Also:
Index(int)

compare

public int compare(Object list_0,
                   Object list_1)
Compare two Lists.

The objects at each List's Index entry are first checked for being null. If both are null, 0 is returned; if only the first is null, -1 is returned; if only the second is null, 1 is returned.

Non-null list entries are converted to a Strings. If these Strings can be converted to Doubles, then they are compared numerically using Double.compareTo. Otherwise they are compared lexicographically using String compareTo.

Specified by:
compare in interface Comparator
Parameters:
list_0 - The first List for comparison.
list_1 - The second List for comparison.
Returns:
An integer less than, equal to, or greater than zero as the first List Index entry is less than, equal to, or greater than the second List Index entry.
Throws:
IllegalArgumentException - If the arguments are not Lists.
ArrayIndexOutOfBoundsException - If the arguments do not have at least Index + 1 entries.

PIRL

Copyright (C) \ 2003-2009 Bradford Castalia, University of Arizona