All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class java.text.Collation

java.lang.Object
   |
   +----java.text.Collation

public class Collation
extends Object
implements Cloneable, Serializable
The Collation class is an abstract class which provides Unicode text comparison services. Text collation supports language-sensitive comparison of strings, allowing for text searching and alphabetical sorting. The collation classes provide a choice of ordering strength (for example, to ignore or not ignore case differences) and handle ignored, expanding, and contracting characters.

Developers don't need to know anything about the collation rules for various languages. Any features requiring collation can use the collation object associated with the current default locale, or with a specific locale (like France or Japan) if appropriate.

Because compare()'s algorithm is complex, it is faster to sort long lists of words by retrieving sort keys or collation keys with getSortKey() or getCollationKey() respectively. You can then cache the sort keys or collation keys and compare them using either SortKey.compareTo() or CollationKey.compareTo(). The following is a list of differences between sort key and collation key:

Collation subclasses implement different collation rules for different languages and different applications. (phone book, dictionary, etc.)

Use collation strength parameters, PRIMARY, SECONDARY, TERTIARY, and IDENTICAL to specify the comparison level. Each unicode character is assigned ordering priority: primary, secondary, tertiary and no difference.

Decomposition mode determines how composed characters are handled for Unicode.

LESS, EQUAL, GREATER identifies the result of unicode text strings comparison.

Use the static method Collation.getDefault() to instantiate the class by passing the desired locale as the argument.

Example of use:

     // Compare two strings in the default locale
     Collation myCollation = Collation.getDefault();
     byte result = myCollation.compare("abc", "ABC");
 

Another example:

     // compare two strings in French
     Collation myCollation = Collation.getDefault(Locale.FRANCE);
     byte result = myCollation.compare("abc", "ABC");
 

The following example demonstrates different ways of comparing two strings:

    String a = "abcdefgh", b = "ijklmnop";
    // This comparision is not as fast as sort key
    if (myCollation.compare(a, b) == Collation.LESS) { // ... }
    // Limited accents, but faster than compare
    SortKey aKey = myCollation.getSortKey(a);
    SortKey bKey = myCollation.getSortKey(b);
    if (aKey.compareTo(bKey) == Collation.LESS)
    { // ... }
    // Unlimited accents, faster than compare
    CollationKey aaKey = myCollation.getCollationKey(a);
    CollationKey bbKey = myCollation.getCollationKey(b);
    if (aaKey.compareTo(bbKey) == Collation.LESS)
    { // ... }
 
NOTE: Two sort keys from different collations cannot be compared.

To combine collations from two locales,

     // Create an en_US collation object
     Collation en_USCollation =
         Collation.getDefault(new Locale("EN", "US", ""));
     // Create a da_DK collation object
     Collation da_DKCollation =
         Collation.getDefault(new Locale("DA", "DK", ""));
     // Combine the two
     // First, get the collation rules from en_USCollation
     String en_USRules = en_USCollation.getRules();
     // Second, get the collation rules from da_DKCollation
     String da_DKRules = da_DKCollation.getRules();
     TableCollation newCollation =
         new TableCollation(en_USRules + da_DKRules);
     // newCollation has the combined rules
 

Another more interesting example would be to make changes on an existing table to create a new collation object. For example, add "& C < ch, cH, Ch, CH" to the en_USCollation object to create your own English collation object,

     // Create a new collation object with additional rules
     String addRules = "& C < ch, cH, Ch, CH";
     TableCollation myCollation =
         new TableCollation(en_USCollation + addRules);
     // myCollation contains the new rules
 

See Also:
TableCollation, SortKey, CollationKey, Locale

Variable Index

 o CANONICAL_DECOMPOSITION
Characters that are canonical variants according to Unicode 2.0 will be decomposed for sorting.
 o EQUAL
EQUAL is returned if source string is compared to be equal to target string in the compare() method.
 o FULL_DECOMPOSITION
Both canonical variants and compatibility variants be decomposed for sorting.
 o GREATER
GREATER is returned if source string is compared to be greater than target string in the compare() method.
 o IDENTICAL
Two characters are considered "identical" when they are equivalent unicode spellings.
 o LESS
LESS is returned if source string is compared to be less than target string in the compare() method.
 o NO_DECOMPOSITION
Accented characters will not be decomposed for sorting.
 o PRIMARY
Base letter represents a primary difference.
 o SECONDARY
Diacritical differences on the same base letter represent a secondary difference.
 o TERTIARY
Uppercase and lowercase versions of the same character represents a tertiary difference.

Constructor Index

 o Collation()
Default constructor of the collation object.

Method Index

 o clone()
Overrides Cloneable
 o compare(String, int, int, String, int, int)
This comparison function compares character data stored in the specified regions of two different strings.
 o compare(String, String)
The comparison function compares the character data stored in two different strings.
 o equals(Object)
Compares the equality of two collation objects.
 o equals(String, String)
Convenience method for comparing the equality of two strings based on the collation rules.
 o getAvailableLocales()
Get the set of Locales for which Collations are installed
 o getCollationKey(String)
Transforms the string into a series of characters that can be compared with java.text.CollationKey.compareTo().
 o getCollationKey(String, int, int)
Transforms the string into a series of characters that can be compared with java.text.CollationKey.compareTo().
 o getDecomposition()
Get the decomposition mode of the collation object.
 o getDefault()
Gets the table-based collation object for the current default locale.
 o getDefault(Locale)
Gets the table-based collation object for the desired locale.
 o getDisplayName(Locale)
Get name of the object for the desired Locale, in the langauge of the default locale.
 o getDisplayName(Locale, Locale)
Get name of the object for the desired Locale, in the desired langauge
 o getSortKey(String)
Transforms the string into a series of characters that can be compared with SortKey.compareTo().
 o getSortKey(String, int, int)
Transforms a specified region of the string into a series of chars that can be compared with SortKey.compareTo.
 o getStrength()
Determines the minimum strength that will be use in comparison or transformation.
 o greater(String, String)
Convenience method for comparing two strings based on the collation rules.
 o greaterOrEqual(String, String)
Convenience method for comparing two strings based on the collation rules.
 o hashCode()
Generates the hash code for the collation object
 o setDecomposition(byte)
Set the decomposition mode of the collation object.
 o setStrength(byte)
Sets the minimum strength to be used in comparison or transformation.

Variables

 o PRIMARY
  public final static byte PRIMARY
Base letter represents a primary difference. Set comparison level to PRIMARY to ignore secondary and tertiary differences. Use this to set the strength of a collation object. Example of primary difference, "abc" < "abd"

See Also:
setStrength, getStrength
 o SECONDARY
  public final static byte SECONDARY
Diacritical differences on the same base letter represent a secondary difference. Set comparison level to SECONDARY to ignore tertiary differences. Use this to set the strength of a collation object. Example of secondary difference, "ä" >> "a".

See Also:
setStrength, getStrength
 o TERTIARY
  public final static byte TERTIARY
Uppercase and lowercase versions of the same character represents a tertiary difference. Set comparison level to TERTIARY to include all comparison differences. Use this to set the strength of a collation object. Example of tertiary difference, "abc" <<< "ABC".

See Also:
setStrength, getStrength
 o IDENTICAL
  public final static byte IDENTICAL
Two characters are considered "identical" when they are equivalent unicode spellings. For example, "ä" == "a?".

 o LESS
  public final static byte LESS
LESS is returned if source string is compared to be less than target string in the compare() method.

See Also:
compare
 o EQUAL
  public final static byte EQUAL
EQUAL is returned if source string is compared to be equal to target string in the compare() method.

See Also:
compare
 o GREATER
  public final static byte GREATER
GREATER is returned if source string is compared to be greater than target string in the compare() method.

See Also:
compare
 o NO_DECOMPOSITION
  public final static byte NO_DECOMPOSITION
Accented characters will not be decomposed for sorting. Please see class description for more details.

 o CANONICAL_DECOMPOSITION
  public final static byte CANONICAL_DECOMPOSITION
Characters that are canonical variants according to Unicode 2.0 will be decomposed for sorting. Use this to set the decomposition mode in a collation object. Please see class description for more details.

 o FULL_DECOMPOSITION
  public final static byte FULL_DECOMPOSITION
Both canonical variants and compatibility variants be decomposed for sorting. Use this to set the decomposition mode in a collation object. Please see class description for more details.

Constructors

 o Collation
  protected Collation()
Default constructor of the collation object. This constructor is made protected so subclasses can get access to it.

Methods

 o getDefault
  public static synchronized Collation getDefault()
Gets the table-based collation object for the current default locale. The default locale is determined by java.util.Locale.getDefault.

Returns:
the collation object of the default locale.(for example, EN_US)
See Also:
getDefault
 o getDefault
  public static synchronized Collation getDefault(Locale desiredLocale)
Gets the table-based collation object for the desired locale. The resource of the desired locale will be loaded by java.util.ResourceBundle. Locale.ENGLISH is the base collation table and all other languages are built on top of it with additional language-specific modifications.

Parameters:
desiredLocale - the desired locale to create the collation table with.
Returns:
returns the created table-based collation object based on the desired locale.
See Also:
Locale, ResourceBundle
 o compare
  public abstract byte compare(String source,
                               String target)
The comparison function compares the character data stored in two different strings. Returns information about whether a string is less than, greater than or equal to another string.

Example of use:

     Collation myCollation = Collation.getDefault(Locale.US);
     myCollation.setStrength(Collation.PRIMARY);
     // result would be Collation.EQUAL ("abc" == "ABC")
     // (no primary difference between "abc" and "ABC")
     byte result = myCollation.compare("abc", "ABC");
     myCollation.setStrength(Collation.TERTIARY);
     // result would be Collation.LESS (abc" <<< "ABC")
     // (with tertiary difference between "abc" and "ABC")
     byte result = myCollation.compare("abc", "ABC");
 

Parameters:
source - the source string to be compared with.
target - the string that is to be compared with the source string.
Returns:
Returns a byte value. GREATER if source is greater than target; EQUAL if source is equal to target; LESS if source is less than target.
 o compare
  public abstract byte compare(String source,
                               int start,
                               int end,
                               String target,
                               int targetStart,
                               int targetEnd)
This comparison function compares character data stored in the specified regions of two different strings. Returns information about whether a string is less than, greater than or equal to another string. If the given starting offset is greater than the ending offset, the specified region of a string is the substring between the two offsets. This is consistent with the way java.lang.String performs when encounters the same situation. (Starting and ending offsets must valid offsets of the string.)

Parameters:
source - the source string to be compared with.
target - the string that is to be compared with the source string.
start - the starting offset (inclusive) of the range of the source string to be compared with.
end - the ending offset (exclusive) of the range of the source string to be compared with.
targetStart - the starting offset (inclusive) of the range of the target string to be compared with.
targetEnd - the ending offset (exclusive) of the range of the target string to be compared with.
Returns:
Returns a byte value. GREATER if source is greater than target; EQUAL if source is equal to target; LESS if source is less than target.
Throws: StringIndexOutOfBoundsException
If the starting offset or the ending offset is out of the range of the string.
See Also:
String
 o getSortKey
  public abstract SortKey getSortKey(String source)
Transforms the string into a series of characters that can be compared with SortKey.compareTo(). It is not possible to restore the original string from the chars in the sort key, and they should not be used as text. The generated sort key handles only a limited number of ignorable characters.

Use SortKey.equals() or SortKey.compareTo to compare the generated sort key string.

Example of use:

     Collation myCollation = Collation.getDefault(Locale.US);
     myCollation.setStrength(Collation.PRIMARY);
     SortKey sortKey1 = myCollation.getSortKey("abc");
     SortKey sortKey2 = myCollation.getSortKey("ABC");
     // Use SortKey.compareTo() to compare the sort keys
     // result would be Collation.EQUAL (sortKey1 == sortKey2)
     byte result = sortKey1.compareTo(sortKey2);
     myCollation.setStrength(Collation.TERTIARY);
     sortKey1 = myCollation.getSortKey("abc");
     sortKey2 = myCollation.getSortKey("ABC");
     // Use SortKey.compareTo() to compare the sort keys
     // result would be Collation.LESS (sortKey1 < sortKey2)
     result = sortKey1.compareTo(sortKey2);
 

If the source string is null, a null sort key will be returned.

Parameters:
source - the source string to be transformed into a sort key.
Returns:
the sort key of the string based on the collation rules.
See Also:
compareTo
 o getSortKey
  public abstract SortKey getSortKey(String source,
                                     int start,
                                     int end)
Transforms a specified region of the string into a series of chars that can be compared with SortKey.compareTo. It is not possible to restore the original string from the characters in the sort key, and they should not be used as text. If an indefinite number of ignorable character can occur, use getCollationKey. If the given starting offset is greater than the ending offset, the specified region of a string is the substring between the two offsets. This is consistent with the way java.lang.String performs when encounters the same situation. (Starting and ending offsets must valid offsets of the string.)

Parameters:
source - the source string to be transformed into a sort key.
start - the starting offset (inclusive) of the range of the source string to be transformed with.
end - the ending offset (exclusive) of the range of the source string to be transformed with.
Returns:
the transformed string.
Throws: StringIndexOutOfBoundsException
If the starting offset or the ending offset is out of the range of the string.
See Also:
SortKey, compareTo
 o getCollationKey
  public abstract CollationKey getCollationKey(String source)
Transforms the string into a series of characters that can be compared with java.text.CollationKey.compareTo(). Handles indefinite number of ignorable characters. Collation keys must be compared with CollationKey.compareTo().

Example of use:

     Collation myCollation = Collation.getDefault(Locale.US);
     CollationKey key1 = myCollation.getCollationKey("abc");
     CollationKey key2 = myCollation.getCollationKey("ABC");
     // Use java.text.CollationKey.compareTo() to compare the collation
     // keys. Result will be Collation.LESS (key1 < key2)
     byte result = key1.compareTo(key2);
 

If the source string is null, a null collation key will be returned.

Parameters:
source - the source string to be used for creating the full sort key.
Returns:
the collation key.
See Also:
CollationKey, getSortKey, compareTo, compareTo
 o getCollationKey
  public abstract CollationKey getCollationKey(String source,
                                               int start,
                                               int end)
Transforms the string into a series of characters that can be compared with java.text.CollationKey.compareTo(). Handles indefinite number of ignorable characters of the specified range of the source string. If the given starting offset is greater than the ending offset, the specified region of a string is the substring between the two offsets. This is consistent with the way java.lang.String performs when encounters the same situation. (Starting and ending offsets must valid offsets of the string.)

Parameters:
source - the source string to be used for creating the collation key
start - the starting offset (inclusive) of the range of the source string to be used to generate the collation key
start - the ending offset (exclusive) of the range of the source string to be used to generate the collation key
Returns:
the collation key
Throws: StringIndexOutOfBoundsException
If the starting offset or the ending offset is out of the range of the string.
See Also:
CollationKey, getSortKey, compareTo, compareTo
 o equals
  public boolean equals(String source,
                        String target)
Convenience method for comparing the equality of two strings based on the collation rules.

Parameters:
source - the source string to be compared with.
target - the target string to be compared with.
Returns:
true if the strings are equal according to the collation rules. false, otherwise.
See Also:
compare
 o greater
  public boolean greater(String source,
                         String target)
Convenience method for comparing two strings based on the collation rules.

Parameters:
source - the source string to be compared with.
target - the target string to be compared with.
Returns:
true if the first string is greater than the parameter, str, according to the collation rules. false, otherwise.
See Also:
compare
 o greaterOrEqual
  public boolean greaterOrEqual(String source,
                                String target)
Convenience method for comparing two strings based on the collation rules.

Parameters:
source - the source string to be compared with.
target - the target string to be compared with.
Returns:
true if the first string is greater than or equal to the parameter, str, according to the collation rules. false, otherwise.
See Also:
compare
 o getStrength
  public synchronized byte getStrength()
Determines the minimum strength that will be use in comparison or transformation.

E.g. with strength == SECONDARY, the tertiary difference is ignored

E.g. with strength == PRIMARY, the secondary and tertiary difference are ignored.

Returns:
the current comparison level.
See Also:
setStrength
 o setStrength
  public synchronized void setStrength(byte newStrength) throws IllegalArgumentException
Sets the minimum strength to be used in comparison or transformation.

Example of use:

     Collation myCollation = Collation.getDefault(Locale.US);
     myCollation.setStrength(Collation.PRIMARY)
     // result will be "abc" == "ABC"
     // tertiary differences will be ignored
     byte result = myCollation.compare("abc", "ABC");
 

Parameters:
newStrength - the new comparison level.
Throws: IllegalArgumentException
If the new strength is incorrect.
See Also:
getStrength
 o getDecomposition
  public synchronized byte getDecomposition()
Get the decomposition mode of the collation object.

Returns:
the decomposition mode
See Also:
setDecomposition
 o setDecomposition
  public synchronized void setDecomposition(byte decompositionMode) throws IllegalArgumentException
Set the decomposition mode of the collation object.

Parameters:
the - new decomposition mode
Throws: IllegalArgumentException
If the new decomposition mode is incorrect.
See Also:
getDecomposition
 o getAvailableLocales
  public static synchronized Locale[] getAvailableLocales()
Get the set of Locales for which Collations are installed

Returns:
the list of available locales which collations are installed
 o getDisplayName
  public static synchronized String getDisplayName(Locale objectLocale,
                                                   Locale displayLocale)
Get name of the object for the desired Locale, in the desired langauge

Parameters:
objectLocale - must be from getAvailableLocales
displayLocale - specifies the desired locale for output
Returns:
display-able name of the object for the object locale in the desired language
 o getDisplayName
  public static synchronized String getDisplayName(Locale objectLocale)
Get name of the object for the desired Locale, in the langauge of the default locale.

Parameters:
objectLocale - must be from getAvailableLocales
Returns:
name of the object for the desired locale in the default language
 o clone
  public Object clone()
Overrides Cloneable

Overrides:
clone in class Object
 o equals
  public abstract boolean equals(Object what)
Compares the equality of two collation objects.

Parameters:
what - the collation object to be compared with this.
Returns:
true if the current collation object is the same as the collation object what; false otherwise.
Overrides:
equals in class Object
 o hashCode
  public synchronized abstract int hashCode()
Generates the hash code for the collation object

Overrides:
hashCode in class Object

All Packages  Class Hierarchy  This Package  Previous  Next  Index