DESY ACOP Beans Home

com.cosylab.util
Class NumericStringComparator

java.lang.Object
  extended by com.cosylab.util.NumericStringComparator
All Implemented Interfaces:
java.util.Comparator

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

NumericStringComparator ... NumericStringComparator represents class which compares two Strings. Roules are followed:

  1. when there is no number or the strings are different before the first number, result should be the same as str1.compareTo(str2)
  2. when two strings are the same up to the first number, the whole number is taken into account and ordered by the result
  3. Whitespaces,zeros, and "_" characters preceding numbers should be omitted from the comparison, unless the omission would cause equality:
  4. All of the above should also apply to double numbers.
  5. When the compared parts of numbers are the same numerically (and written differently), they should be ordered alphabetically:
  6. The softEquals property controls how the strictness of the comparator with regards to spaces and separators. When softEquals is set to true, the comparator will return 0 even when two strings are not identical, but are equal numerically. e.g. compare("100","0100") will return 0;

Since:
Oct 15, 2003.
Version:
$Id: NumericStringComparator.java,v 1.16 2008-04-22 12:26:29 jbobnar Exp $
Author:
Andrej Kosmrlj (andrej.kosmrlj&x40;kgb.ijs.si)
See Also:
com.cosylab.util.test

Field Summary
static int DEC_SEPARATOR_BOTH
          Constant indicating that both dot and comma are treated as decimal separators
static int DEC_SEPARATOR_COMMA
          Constant indicating that comma is treated as a decimal separator
static int DEC_SEPARATOR_DOT
          Constant indicating that dot is treated as a decimal separator
static int DEC_SEPARATOR_NONE
          Constant indicating that decimal separators should not be treated differently than other characters
 
Constructor Summary
NumericStringComparator()
          Contructor creates NumericStringComparator.
NumericStringComparator(char[] separators)
          Contructor creates NumericStringComparator with specified array of separators.
NumericStringComparator(char[] separators, int decSeparator)
          Contructor creates NumericStringComparator with specified array of separators in decimal separator mode.
NumericStringComparator(int decSeparator)
          Contructor creates NumericStringComparator with specified decimal mode.
 
Method Summary
 void addSeparator(char sep)
          Method adds new separator.
 int compare(java.lang.Object o1, java.lang.Object o2)
           
 int getDecSeparatorMode()
          Method returns decimal separator mode, represented by int
static NumericStringComparator getDefault()
          Convenience method for obtaining a default NumericStringComparator.
 char[] getSeparators()
          Method returns separators
 boolean isSoftEquals()
          When softEquals property is set to true, the comparator will return 0 even when two strings are not identical, but are equal numerically.
 void setSeparators(char[] sep)
          Method sets separators
 void setSoftEquals(boolean softEquals)
          Sets the softEquals property.
 
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

DEC_SEPARATOR_DOT

public static final int DEC_SEPARATOR_DOT
Constant indicating that dot is treated as a decimal separator

See Also:
Constant Field Values

DEC_SEPARATOR_COMMA

public static final int DEC_SEPARATOR_COMMA
Constant indicating that comma is treated as a decimal separator

See Also:
Constant Field Values

DEC_SEPARATOR_BOTH

public static final int DEC_SEPARATOR_BOTH
Constant indicating that both dot and comma are treated as decimal separators

See Also:
Constant Field Values

DEC_SEPARATOR_NONE

public static final int DEC_SEPARATOR_NONE
Constant indicating that decimal separators should not be treated differently than other characters

See Also:
Constant Field Values
Constructor Detail

NumericStringComparator

public NumericStringComparator(char[] separators,
                               int decSeparator)
Contructor creates NumericStringComparator with specified array of separators in decimal separator mode. Separator is character which is not used in compare() method unless there are equal numbers in each String and if Strings are different only with separators

Parameters:
separators - array of separators
decSeparator - decimal separator mode

NumericStringComparator

public NumericStringComparator(char[] separators)
Contructor creates NumericStringComparator with specified array of separators. Decimal mode has default value: DEC_SEPARATOR_DOT.

Parameters:
separators - array of separators

NumericStringComparator

public NumericStringComparator(int decSeparator)
Contructor creates NumericStringComparator with specified decimal mode. Array of separators has default value: [' ', '_'].

Parameters:
decSeparator - decimal mode

NumericStringComparator

public NumericStringComparator()
Contructor creates NumericStringComparator. Array of separators has default value: [' ', '_']. Decimal mode has default value: DEC_SEPARATOR_DOT.

Method Detail

getDefault

public static NumericStringComparator getDefault()
Convenience method for obtaining a default NumericStringComparator. Array of separators has default value: [' ', '_']. Decimal mode has default value: DEC_SEPARATOR_DOT.

Returns:
The default instance of comparator.

addSeparator

public void addSeparator(char sep)
Method adds new separator.

Parameters:
sep - new separator
Throws:
java.lang.IllegalArgumentException - when the separator is an illegal character such as a digit, a decimal separator or the minus sign

setSeparators

public void setSeparators(char[] sep)
Method sets separators

Parameters:
sep - array of separators
Throws:
java.lang.IllegalArgumentException - when the separators array contains illegal character such as digits, decimal separators or minus sign

getSeparators

public char[] getSeparators()
Method returns separators

Returns:
char[] separators

getDecSeparatorMode

public int getDecSeparatorMode()
Method returns decimal separator mode, represented by int

Returns:
int

compare

public int compare(java.lang.Object o1,
                   java.lang.Object o2)
Specified by:
compare in interface java.util.Comparator

isSoftEquals

public boolean isSoftEquals()
When softEquals property is set to true, the comparator will return 0 even when two strings are not identical, but are equal numerically. e.g. compare("100","0100") will return 0;

Returns:
Returns the softEquals.

setSoftEquals

public void setSoftEquals(boolean softEquals)
Sets the softEquals property.

Parameters:
softEquals - The softEquals to set.
See Also:
isSoftEquals()


Copyright © 2010. All Rights Reserved.