sdsu.compare
Class NumericStringComparer

java.lang.Object
  |
  +--sdsu.compare.Comparer
        |
        +--sdsu.compare.NumericStringComparer

public class NumericStringComparer
extends Comparer

A concrete class for comparing strings and strings that are in number format. Assumes that the string representation does not contain leading zeros. Longer strings are larger than shorter strings. If a string contains a decimal point then the number of characters (or digits) to the left of the decimal point is used as the length of the string. If two strings are of equal length than normal strign compare is used. The following comparisons are all true using this comparer

		lessThan( "2", "3" )
lessThan( "22", "3" )
lessThan( "234", "236" )
lessThan( "123a", "123b" )
lessThan( "12.3", "9.8" )
lessThan( "a", "b" )
lessThan( "XX12", "aaaab" )
lessThan( "22.457", "22.6" )
lessThan( "22.4", "22.6333" )

 

Version:
1.0 5 December 1997
Author:
Roger Whitney (whitney@cs.sdsu.edu)
See Also:
Serialized Form

Method Summary
 boolean equals(java.lang.Object leftOperand, java.lang.Object rightOperand)
          Returns true if the leftOperand is equal to the rightOperand.
static Comparer getInstance()
          Returns a NumericStringComparer object.
 boolean greaterThan(java.lang.Object leftOperand, java.lang.Object rightOperand)
          Returns true if the leftOperand is greater than the rightOperand.
 boolean lessThan(java.lang.Object leftOperand, java.lang.Object rightOperand)
          Returns true if the leftOperand is less than the rightOperand.
 
Methods inherited from class sdsu.compare.Comparer
greaterThanOrEqual, lessThanOrEqual
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getInstance

public static Comparer getInstance()
Returns a NumericStringComparer object.

lessThan

public final boolean lessThan(java.lang.Object leftOperand,
                              java.lang.Object rightOperand)
                       throws java.lang.ClassCastException
Returns true if the leftOperand is less than the rightOperand.
Throws:
java.lang.ClassCastException - If operand objects are not strings. ClassCastException is a RuntimeException, so compiler does not force you to catch this exception.
Overrides:
lessThan in class Comparer

greaterThan

public final boolean greaterThan(java.lang.Object leftOperand,
                                 java.lang.Object rightOperand)
                          throws java.lang.ClassCastException
Returns true if the leftOperand is greater than the rightOperand.
Throws:
java.lang.ClassCastException - If operand objects are not strings objects. ClassCastException is a RuntimeException, so compiler does not force you to catch this exception.
Overrides:
greaterThan in class Comparer

equals

public final boolean equals(java.lang.Object leftOperand,
                            java.lang.Object rightOperand)
                     throws java.lang.ClassCastException
Returns true if the leftOperand is equal to the rightOperand.
Throws:
java.lang.ClassCastException - If operand objects are not Number objects. ClassCastException is a RuntimeException, so compiler does not force you to catch this exception.
Overrides:
equals in class Comparer