simtools.util
Class NumberStringComparator

java.lang.Object
  extended by simtools.util.NumberStringComparator
All Implemented Interfaces:
java.io.Serializable, java.util.Comparator

public class NumberStringComparator
extends java.lang.Object
implements java.util.Comparator, java.io.Serializable

Compare objects of type Number and String. Can handle any combination of both. Will throw an error if the Object type does not match. A String not containing a number is considered NaN and greater than any number String comparison (lexicographic order) is OK if both strings do not contain numbers.

See Also:
Serialized Form

Constructor Summary
NumberStringComparator()
           
 
Method Summary
 int compare(java.lang.Object o1, java.lang.Object o2)
          Delegates to numStringCompare
static int numStringCompare(java.lang.Object o1, java.lang.Object o2)
          Compare objects of type Number and String.
static java.lang.Number stringToNumber(java.lang.String string)
          Internal method that tries to parse a String to a Number First tries to parse to a Long, so it handles hex values and has a better precision for integer values.
 
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
 

Constructor Detail

NumberStringComparator

public NumberStringComparator()
Method Detail

stringToNumber

public static java.lang.Number stringToNumber(java.lang.String string)
Internal method that tries to parse a String to a Number First tries to parse to a Long, so it handles hex values and has a better precision for integer values. Then try with doubles.

Parameters:
string -
Returns:
Number : A converted number, or null

numStringCompare

public static int numStringCompare(java.lang.Object o1,
                                   java.lang.Object o2)
                            throws java.lang.ClassCastException
Compare objects of type Number and String. Can handle any combination of both. Will throw an error if the Object type does not match. A String not containing a number is considered NaN and greater than any number String comparison (lexicographic order) is OK if both strings do not contain numbers.

Parameters:
o1 - first number or sring
o2 - second number or sring
Returns:
int -1, 0 or 1 if o1 < o2, o1 = o2, o1 > o2
Throws:
java.lang.ClassCastException

compare

public int compare(java.lang.Object o1,
                   java.lang.Object o2)
Delegates to numStringCompare

Specified by:
compare in interface java.util.Comparator
See Also:
numStringCompare(java.lang.Object, java.lang.Object)