java.text
abstract public class: Collator [javadoc |
source]
java.lang.Object
java.text.Collator
All Implemented Interfaces:
Cloneable, Comparator
Direct Known Subclasses:
RuleBasedCollator
Performs locale-sensitive string comparison. A concrete subclass,
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 {@code Collator} deals only with two decomposition modes, the canonical
decomposition mode and one that does not use any decomposition. The
compatibility decomposition mode
{@code java.text.Collator.FULL_DECOMPOSITION} is not supported here. If the
canonical decomposition mode is set, {@code 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("\u00e0\u0325", "a\u0325\u0300") != 0) {
System.out.println("\u00e0\u0325 is not equal to a\u0325\u0300 without decomposition");
myCollator.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
if (myCollator.compare("\u00e0\u0325", "a\u0325\u0300") != 0) {
System.out.println("Error: \u00e0\u0325 should be equal to a\u0325\u0300 with decomposition");
} else {
System.out.println("\u00e0\u0325 is equal to a\u0325\u0300 with decomposition");
}
} else {
System.out.println("Error: \u00e0\u0325 should be not equal to a\u0325\u0300 without decomposition");
}
| Field Summary |
|---|
| static final int | EQUAL | |
| static final int | GREATER | |
| static final int | LESS | |
| public static final int | NO_DECOMPOSITION | Constant used to specify the decomposition rule. |
| public static final int | CANONICAL_DECOMPOSITION | Constant used to specify the decomposition rule. |
| public static final int | FULL_DECOMPOSITION | Constant used to specify the decomposition rule. This value for
decomposition is not supported. |
| public static final int | PRIMARY | Constant used to specify the collation strength. |
| public static final int | SECONDARY | Constant used to specify the collation strength. |
| public static final int | TERTIARY | Constant used to specify the collation strength. |
| public static final int | IDENTICAL | Constant used to specify the collation strength. |
| Collator | icuColl | |
| Method from java.text.Collator Summary: |
|---|
|
clone, compare, compare, equals, equals, getAvailableLocales, getCollationKey, getDecomposition, getInstance, getInstance, getStrength, hashCode, setDecomposition, setStrength |
| Methods from java.lang.Object: |
|---|
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Method from java.text.Collator Detail: |
public Object clone() {
try {
Collator clone = (Collator) super.clone();
clone.icuColl = (com.ibm.icu.text.Collator) this.icuColl.clone();
return clone;
} catch (CloneNotSupportedException e) {
return null;
}
}
Returns a new collator with the same decomposition mode and
strength value as this collator. |
public int compare(Object object1,
Object object2) {
return compare((String) object1, (String) object2);
}
Compares two objects to determine their relative order. The objects must
be strings. |
abstract public int compare(String string1,
String string2)
Compares two strings to determine their relative order. |
public boolean equals(Object object) {
if (!(object instanceof Collator)) {
return false;
}
Collator collator = (Collator) object;
return this.icuColl == null ? collator.icuColl == null : this.icuColl
.equals(collator.icuColl);
}
Compares this collator with the specified object and indicates if they
are equal. |
public boolean equals(String string1,
String string2) {
return compare(string1, string2) == 0;
}
Compares two strings using the collation rules to determine if they are
equal. |
public static Locale[] getAvailableLocales() {
return com.ibm.icu.text.Collator.getAvailableLocales();
}
Gets the list of installed java.util.Locale objects which support
{@code Collator}. |
abstract public CollationKey getCollationKey(String string)
Returns a CollationKey for the specified string for this collator
with the current decomposition rule and strength value. |
public int getDecomposition() {
return decompositionMode_ICU_Java(this.icuColl.getDecomposition());
}
Returns the decomposition rule for this collator. |
public static Collator getInstance() {
return getInstance(Locale.getDefault());
}
Returns a {@code Collator} instance which is appropriate for the default
{@code Locale}. |
public static Collator getInstance(Locale locale) {
String key = locale.toString();
for (int i = cache.size() - 1; i >= 0; i -= 2) {
if (cache.elementAt(i).equals(key)) {
return (Collator) (cache.elementAt(i - 1)).clone();
}
}
return new RuleBasedCollator(com.ibm.icu.text.Collator
.getInstance(locale));
}
Returns a {@code Collator} instance which is appropriate for the
specified {@code Locale}. |
public int getStrength() {
return strength_ICU_Java(this.icuColl.getStrength());
}
Returns the strength value for this collator. |
abstract public int hashCode()
Returns an integer hash code for this collator. |
public void setDecomposition(int value) {
this.icuColl.setDecomposition(decompositionMode_Java_ICU(value));
}
Sets the decomposition rule for this collator. |
public void setStrength(int value) {
this.icuColl.setStrength(strength_Java_ICU(value));
}
Sets the strength value for this collator. |