org.openmrs.util
Class NaturalStrings

java.lang.Object
  extended by org.openmrs.util.NaturalStrings

public final class NaturalStrings
extends java.lang.Object

Utility class for natural String comparison operations

Since:
1.5

Method Summary
static int compareNatural(java.text.Collator collator, java.lang.String s, java.lang.String t)
           Compares two strings using the given collator and comparing contained numbers based on their numeric values.
static int compareNatural(java.lang.String s, java.lang.String t)
           Compares two strings using the current locale's rules and comparing contained numbers based on their numeric values.
static int compareNaturalAscii(java.lang.String s, java.lang.String t)
           Compares two strings using each character's Unicode value for non-digit characters and the numeric values off any contained numbers.
static int compareNaturalIgnoreCaseAscii(java.lang.String s, java.lang.String t)
           Compares two strings using each character's Unicode value - ignoring upper/lower case - for non-digit characters and the numeric values of any contained numbers.
static java.util.Comparator<java.lang.String> getNaturalComparator()
          Returns a comparator that compares contained numbers based on their numeric values and compares other parts using the current locale's order rules.
static java.util.Comparator<java.lang.String> getNaturalComparator(java.text.Collator collator)
          Returns a comparator that compares contained numbers based on their numeric values and compares other parts using the given collator.
static java.util.Comparator<java.lang.String> getNaturalComparatorAscii()
          Returns a comparator that compares contained numbers based on their numeric values and compares other parts based on each character's Unicode value.
static java.util.Comparator<java.lang.String> getNaturalComparatorIgnoreCaseAscii()
          Returns a comparator that compares contained numbers based on their numeric values and compares other parts based on each character's Unicode value while ignore upper/lower case differences.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getNaturalComparator

public static java.util.Comparator<java.lang.String> getNaturalComparator()
Returns a comparator that compares contained numbers based on their numeric values and compares other parts using the current locale's order rules.

For example in German locale this will be a comparator that handles umlauts correctly and ignores upper/lower case differences.

Returns:

A string comparator that uses the current locale's order rules and handles embedded numbers correctly.

See Also:
getNaturalComparator(java.text.Collator)

getNaturalComparator

public static java.util.Comparator<java.lang.String> getNaturalComparator(java.text.Collator collator)
Returns a comparator that compares contained numbers based on their numeric values and compares other parts using the given collator.

Parameters:
collator - used for locale specific comparison of text (non-number) subwords - must not be null
Returns:

A string comparator that uses the given Collator to compare subwords and handles embedded numbers correctly.

See Also:
getNaturalComparator()

getNaturalComparatorAscii

public static java.util.Comparator<java.lang.String> getNaturalComparatorAscii()
Returns a comparator that compares contained numbers based on their numeric values and compares other parts based on each character's Unicode value.

Returns:

a string comparator that does case sensitive comparisons on pure ascii strings and handles embedded numbers correctly.

Do not use if your app might ever run on any locale that uses more than 7-bit ascii characters.
See Also:
getNaturalComparator(), getNaturalComparator(java.text.Collator)

getNaturalComparatorIgnoreCaseAscii

public static java.util.Comparator<java.lang.String> getNaturalComparatorIgnoreCaseAscii()
Returns a comparator that compares contained numbers based on their numeric values and compares other parts based on each character's Unicode value while ignore upper/lower case differences. Do not use if your app might ever run on any locale that uses more than 7-bit ascii characters.

Returns:

a string comparator that does case insensitive comparisons on pure ascii strings and handles embedded numbers correctly.

See Also:
getNaturalComparator(), getNaturalComparator(java.text.Collator)

compareNatural

public static int compareNatural(java.lang.String s,
                                 java.lang.String t)

Compares two strings using the current locale's rules and comparing contained numbers based on their numeric values.

This is probably the best default comparison to use.

If you know that the texts to be compared are in a certain language that differs from the default locale's langage, then get a collator for the desired locale ( Collator.getInstance(java.util.Locale)) and pass it to compareNatural(java.text.Collator, String, String)

Parameters:
s - first string
t - second string
Returns:
zero iff s and t are equal, a value less than zero iff s lexicographically precedes t and a value larger than zero iff s lexicographically follows t

compareNatural

public static int compareNatural(java.text.Collator collator,
                                 java.lang.String s,
                                 java.lang.String t)

Compares two strings using the given collator and comparing contained numbers based on their numeric values.

Parameters:
s - first string
t - second string
Returns:
zero iff s and t are equal, a value less than zero iff s lexicographically precedes t and a value larger than zero iff s lexicographically follows t

compareNaturalAscii

public static int compareNaturalAscii(java.lang.String s,
                                      java.lang.String t)

Compares two strings using each character's Unicode value for non-digit characters and the numeric values off any contained numbers.

(This will probably make sense only for strings containing 7-bit ascii characters only.)

Returns:
zero iff s and t are equal, a value less than zero iff s lexicographically precedes t and a value larger than zero iff s lexicographically follows t

compareNaturalIgnoreCaseAscii

public static int compareNaturalIgnoreCaseAscii(java.lang.String s,
                                                java.lang.String t)

Compares two strings using each character's Unicode value - ignoring upper/lower case - for non-digit characters and the numeric values of any contained numbers.

(This will probably make sense only for strings containing 7-bit ascii characters only.)

Returns:
zero iff s and t are equal, a value less than zero iff s lexicographically precedes t and a value larger than zero iff s lexicographically follows t

OpenMRS-1.7.x

Generated Apr 27 2012 10:06 PM. NOTE - these libraries are in active development and subject to change