Class Collator
- All Implemented Interfaces:
Cloneable,Comparator<Object>
- Direct Known Subclasses:
RuleBasedCollator
public abstract class Collator extends Object implements Comparator<Object>, Cloneable
RuleBasedCollator, allows customization of the collation ordering by
the use of rule sets.
Following the Unicode Consortium's specifications for the Unicode Collation Algorithm (UCA), there are 4 different levels of strength used in comparisons:
- PRIMARY strength: Typically, this is used to denote differences between base characters (for example, "a" < "b"). It is the strongest difference. For example, dictionaries are divided into different sections by base character.
- SECONDARY strength: Accents in the characters are considered secondary differences (for example, "as" < "às" < "at"). Other differences between letters can also be considered secondary differences, depending on the language. A secondary difference is ignored when there is a primary difference anywhere in the strings.
- TERTIARY strength: Upper and lower case differences in characters are distinguished at tertiary strength (for example, "ao" < "Ao" < "aò"). In addition, a variant of a letter differs from the base form on the tertiary strength (such as "A" and "Ⓐ"). Another example is the difference between large and small Kana. A tertiary difference is ignored when there is a primary or secondary difference anywhere in the strings.
- IDENTICAL strength: When all other strengths are equal, the IDENTICAL strength is used as a tiebreaker. The Unicode code point values of the NFD form of each string are compared, just in case there is no difference. For example, Hebrew cantellation marks are only distinguished at this strength. This strength should be used sparingly, as only code point value differences between two strings are an extremely rare occurrence. Using this strength substantially decreases the performance for both comparison and collation key generation APIs. This strength also increases the size of the collation key.
This Collator deals only with two decomposition modes, the canonical
decomposition mode and one that does not use any decomposition. The
compatibility decomposition mode
java.text.Collator.FULL_DECOMPOSITION is not supported here. If the
canonical decomposition mode is set, Collator handles un-normalized
text properly, producing the same results as if the text were normalized in
NFD. If canonical decomposition is turned off, it is the user's
responsibility to ensure that all text is already in the appropriate form
before performing a comparison or before getting a CollationKey.
Examples:
// Get the Collator for US English and set its strength to PRIMARY
Collator usCollator = Collator.getInstance(Locale.US);
usCollator.setStrength(Collator.PRIMARY);
if (usCollator.compare("abc", "ABC") == 0) {
System.out.println("Strings are equivalent");
}
The following example shows how to compare two strings using the collator for the default locale.
// Compare two strings in the default locale
Collator myCollator = Collator.getInstance();
myCollator.setDecomposition(Collator.NO_DECOMPOSITION);
if (myCollator.compare("ḁ̀", "ḁ̀") != 0) {
System.out.println("ḁ̀ is not equal to ḁ̀ without decomposition");
myCollator.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
if (myCollator.compare("ḁ̀", "ḁ̀") != 0) {
System.out.println("Error: ḁ̀ should be equal to ḁ̀ with decomposition");
} else {
System.out.println("ḁ̀ is equal to ḁ̀ with decomposition");
}
} else {
System.out.println("Error: ḁ̀ should be not equal to ḁ̀ without decomposition");
}
- See Also:
RuleBasedCollator,CollationKey
-
Field Summary
Fields Modifier and Type Field Description static intCANONICAL_DECOMPOSITIONConstant used to specify the decomposition rule.static intFULL_DECOMPOSITIONConstant used to specify the decomposition rule.static intIDENTICALConstant used to specify the collation strength.static intNO_DECOMPOSITIONConstant used to specify the decomposition rule.static intPRIMARYConstant used to specify the collation strength.static intSECONDARYConstant used to specify the collation strength.static intTERTIARYConstant used to specify the collation strength. -
Constructor Summary
Constructors Modifier Constructor Description protectedCollator()Constructs a newCollatorinstance. -
Method Summary
Modifier and Type Method Description Objectclone()Returns a new collator with the same decomposition mode and strength value as this collator.intcompare(Object object1, Object object2)Compares two objects to determine their relative order.abstract intcompare(String string1, String string2)Compares two strings to determine their relative order.booleanequals(Object object)Compares this collator with the specified object and indicates if they are equal.booleanequals(String string1, String string2)Compares two strings using the collation rules to determine if they are equal.static Locale[]getAvailableLocales()Returns an array of locales for which customCollatorinstances are available.abstract CollationKeygetCollationKey(String string)Returns aCollationKeyfor the specified string for this collator with the current decomposition rule and strength value.intgetDecomposition()Returns the decomposition rule for this collator.static CollatorgetInstance()Returns aCollatorinstance which is appropriate for the user's defaultLocale.static CollatorgetInstance(Locale locale)Returns aCollatorinstance which is appropriate forlocale.intgetStrength()Returns the strength value for this collator.abstract inthashCode()Returns an integer hash code for this object.voidsetDecomposition(int value)Sets the decomposition rule for this collator.voidsetStrength(int value)Sets the strength value for this collator.
-
Field Details
-
NO_DECOMPOSITION
public static final int NO_DECOMPOSITIONConstant used to specify the decomposition rule.- See Also:
- Constant Field Values
-
CANONICAL_DECOMPOSITION
public static final int CANONICAL_DECOMPOSITIONConstant used to specify the decomposition rule.- See Also:
- Constant Field Values
-
FULL_DECOMPOSITION
public static final int FULL_DECOMPOSITIONConstant used to specify the decomposition rule. This value for decomposition is not supported.- See Also:
- Constant Field Values
-
PRIMARY
public static final int PRIMARYConstant used to specify the collation strength.- See Also:
- Constant Field Values
-
SECONDARY
public static final int SECONDARYConstant used to specify the collation strength.- See Also:
- Constant Field Values
-
TERTIARY
public static final int TERTIARYConstant used to specify the collation strength.- See Also:
- Constant Field Values
-
IDENTICAL
public static final int IDENTICALConstant used to specify the collation strength.- See Also:
- Constant Field Values
-
-
Constructor Details
-
Collator
protected Collator()Constructs a newCollatorinstance.
-
-
Method Details
-
clone
Returns a new collator with the same decomposition mode and strength value as this collator. -
compare
Compares two objects to determine their relative order. The objects must be strings.- Specified by:
comparein interfaceComparator<Object>- Parameters:
object1- the first string to compare.object2- the second string to compare.- Returns:
- a negative value if
object1is less thanobject2, 0 if they are equal, and a positive value ifobject1is greater thanobject2. - Throws:
ClassCastException- ifobject1orobject2is not aString.
-
compare
Compares two strings to determine their relative order.- Parameters:
string1- the first string to compare.string2- the second string to compare.- Returns:
- a negative value if
string1is less thanstring2, 0 if they are equal and a positive value ifstring1is greater thanstring2.
-
equals
Compares this collator with the specified object and indicates if they are equal.- Specified by:
equalsin interfaceComparator<Object>- Overrides:
equalsin classObject- Parameters:
object- the object to compare with this object.- Returns:
trueifobjectis aCollatorobject and it has the same strength and decomposition values as this collator;falseotherwise.- See Also:
hashCode()
-
equals
Compares two strings using the collation rules to determine if they are equal.- Parameters:
string1- the first string to compare.string2- the second string to compare.- Returns:
trueifstring1andstring2are equal using the collation rules, false otherwise.
-
getAvailableLocales
Returns an array of locales for which customCollatorinstances are available.Note that Android does not support user-supplied locale service providers.
-
getCollationKey
Returns aCollationKeyfor the specified string for this collator with the current decomposition rule and strength value.- Parameters:
string- the source string that is converted into a collation key.- Returns:
- the collation key for
string.
-
getDecomposition
public int getDecomposition()Returns the decomposition rule for this collator.- Returns:
- the decomposition rule, either
NO_DECOMPOSITIONorCANONICAL_DECOMPOSITION.FULL_DECOMPOSITIONis not supported.
-
getInstance
Returns aCollatorinstance which is appropriate for the user's defaultLocale. See "Be wary of the default locale". -
getInstance
Returns aCollatorinstance which is appropriate forlocale. -
getStrength
public int getStrength()Returns the strength value for this collator.- Returns:
- the strength value, either PRIMARY, SECONDARY, TERTIARY or IDENTICAL.
-
hashCode
public abstract int hashCode()Description copied from class:ObjectReturns an integer hash code for this object. By contract, any two objects for whichObject.equals(java.lang.Object)returnstruemust return the same hash code value. This means that subclasses ofObjectusually override both methods or neither method.Note that hash values must not change over time unless information used in equals comparisons also changes.
See Writing a correct
hashCodemethod if you intend implementing your ownhashCodemethod.- Overrides:
hashCodein classObject- Returns:
- this object's hash code.
- See Also:
Object.equals(java.lang.Object)
-
setDecomposition
public void setDecomposition(int value)Sets the decomposition rule for this collator.- Parameters:
value- the decomposition rule, eitherNO_DECOMPOSITIONorCANONICAL_DECOMPOSITION.FULL_DECOMPOSITIONis not supported.- Throws:
IllegalArgumentException- if the provided decomposition rule is not valid. This includesFULL_DECOMPOSITION.
-
setStrength
public void setStrength(int value)Sets the strength value for this collator.- Parameters:
value- the strength value, either PRIMARY, SECONDARY, TERTIARY, or IDENTICAL.- Throws:
IllegalArgumentException- if the provided strength value is not valid.
-