|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--com.ibm.icu.text.Collator
Collator 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 5 different levels of strength used in comparisons:
For more information about the collation service see the users guide.
Examples of use
// 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(NO_DECOMPOSITION); if (myCollator.compare("à\u0325", "a\u0325̀") != 0) { System.out.println("à\u0325 is not equals to a\u0325̀ without decomposition"); myCollator.setDecomposition(CANONICAL_DECOMPOSITION); if (myCollator.compare("à\u0325", "a\u0325̀") != 0) { System.out.println("Error: à\u0325 should be equals to a\u0325̀ with decomposition"); } else { System.out.println("à\u0325 is equals to a\u0325̀ with decomposition"); } } else { System.out.println("Error: à\u0325 should be not equals to a\u0325̀ without decomposition"); }
RuleBasedCollator
,
CollationKey
Field Summary | |
static int |
CANONICAL_DECOMPOSITION
Decomposition mode value. |
static int |
IDENTICAL
Smallest Collator strength value. |
static int |
NO_DECOMPOSITION
Decomposition mode value. |
static int |
PRIMARY
Strongest collator strength value. |
static int |
QUATERNARY
Fourth level collator strength value. |
static int |
SECONDARY
Second level collator strength value. |
static int |
TERTIARY
Third level collator strength value. |
Constructor Summary | |
Collator()
|
Method Summary | |
int |
compare(java.lang.Object source,
java.lang.Object target)
Compares the source text String to the target text String according to this Collator's rules, strength and decomposition mode. |
abstract int |
compare(java.lang.String source,
java.lang.String target)
Compares the source text String to the target text String according to this Collator's rules, strength and decomposition mode. |
abstract boolean |
equals(java.lang.Object that)
Compares the equality of two Collators. |
boolean |
equals(java.lang.String source,
java.lang.String target)
Convenience method for comparing the equality of two text Strings using this Collator's rules, strength and decomposition mode. |
abstract CollationKey |
getCollationKey(java.lang.String source)
Transforms the String into a CollationKey suitable for efficient repeated comparison. |
int |
getDecomposition()
Get the decomposition mode of this Collator. |
static Collator |
getInstance()
Gets the Collator for the current default locale. |
static Collator |
getInstance(java.util.Locale locale)
Gets the Collator for the desired locale. |
int |
getStrength()
Returns this Collator's strength property. |
abstract int |
hashCode()
Generates a unique hash code for this Collator. |
void |
setDecomposition(int decomposition)
Set the decomposition mode of this Collator. |
void |
setStrength(int newStrength)
Sets this Collator's strength property. |
Methods inherited from class java.lang.Object |
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
public static final int PRIMARY
setStrength(int)
,
getStrength()
public static final int SECONDARY
setStrength(int)
,
getStrength()
public static final int TERTIARY
setStrength(int)
,
getStrength()
public static final int QUATERNARY
setStrength(int)
,
getStrength()
public static final int IDENTICAL
Smallest Collator strength value. 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. See class documentation for more explanation.
Note this value is different from JDK's
public static final int NO_DECOMPOSITION
Decomposition mode value. With NO_DECOMPOSITION set, Strings will not be decomposed for collation. This is the default decomposition setting unless otherwise specified by the locale used to create the Collator.
Note this value is different from the JDK's.
CANONICAL_DECOMPOSITION
,
getDecomposition()
,
setDecomposition(int)
public static final int CANONICAL_DECOMPOSITION
Decomposition mode value. With CANONICAL_DECOMPOSITION set, characters that are canonical variants according to the Unicode standard will be decomposed for collation.
CANONICAL_DECOMPOSITION corresponds to Normalization Form D as described in Unicode Technical Report #15.
NO_DECOMPOSITION
,
getDecomposition()
,
setDecomposition(int)
Constructor Detail |
public Collator()
Method Detail |
public void setStrength(int newStrength)
Sets this Collator's strength property. The strength property determines the minimum level of difference considered significant during comparison.
The default strength for the Collator is TERTIARY, unless specified otherwise by the locale used to create the Collator.
See the Collator class description for an example of use.
new
- Strength the new strength value.java.lang.IllegalArgumentException
- if the new strength value is not one
of PRIMARY, SECONDARY, TERTIARY, QUATERNARY or IDENTICAL.getStrength()
,
PRIMARY
,
SECONDARY
,
TERTIARY
,
QUATERNARY
,
IDENTICAL
public void setDecomposition(int decomposition)
Set the decomposition mode of this Collator. Setting this decomposition property with CANONICAL_DECOMPOSITION allows the Collator to handle un-normalized text properly, producing the same results as if the text were normalized. If NO_DECOMPOSITION is set, it is the user's responsibility to insure that all text is already in the appropriate form before a comparison or before getting a CollationKey. Adjusting decomposition mode allows the user to select between faster and more complete collation behavior.
Since a great many of the world's languages do not require text normalization, most locales set NO_DECOMPOSITION as the default decomposition mode.
The default decompositon mode for the Collator is NO_DECOMPOSITON, unless specified otherwise by the locale used to create the Collator.See getDecomposition for a description of decomposition mode.
decomposition
- the new decomposition modejava.lang.IllegalArgumentException
- If the given value is not a valid
decomposition mode.getDecomposition()
,
NO_DECOMPOSITION
,
CANONICAL_DECOMPOSITION
public static final Collator getInstance()
Locale.getDefault()
,
getInstance(Locale)
public static final Collator getInstance(java.util.Locale locale)
locale
- the desired locale.Locale
,
ResourceBundle
,
getInstance()
public int getStrength()
Returns this Collator's strength property. The strength property determines the minimum level of difference considered significant.
See the Collator class description for more details.
setStrength(int)
,
PRIMARY
,
SECONDARY
,
TERTIARY
,
QUATERNARY
,
IDENTICAL
public int getDecomposition()
Get the decomposition mode of this Collator. Decomposition mode determines how Unicode composed characters are handled.
See the Collator class description for more details.
setDecomposition(int)
,
NO_DECOMPOSITION
,
CANONICAL_DECOMPOSITION
public int compare(java.lang.Object source, java.lang.Object target)
Compares the source text String to the target text String according to this Collator's rules, strength and decomposition mode. Returns an integer less than, equal to or greater than zero depending on whether the source String is less than, equal to or greater than the target String. See the Collator class description for an example of use.
compare
in interface java.util.Comparator
source
- the source String.target
- the target String.NullPointerException
- thrown if either arguments is null.
IllegalArgumentException thrown if either source or target is
not of the class String.CollationKey
,
getCollationKey(java.lang.String)
public boolean equals(java.lang.String source, java.lang.String target)
source
- the source string to be compared.target
- the target string to be compared.NullPointerException
- thrown if either arguments is null.compare(java.lang.Object, java.lang.Object)
public abstract boolean equals(java.lang.Object that)
equals
in interface java.util.Comparator
equals
in class java.lang.Object
that
- the Collator to be compared with this.public abstract int hashCode()
hashCode
in class java.lang.Object
public abstract int compare(java.lang.String source, java.lang.String target)
Compares the source text String to the target text String according to this Collator's rules, strength and decomposition mode. Returns an integer less than, equal to or greater than zero depending on whether the source String is less than, equal to or greater than the target String. See the Collator class description for an example of use.
source
- the source String.target
- the target String.NullPointerException
- thrown if either arguments is null.CollationKey
,
getCollationKey(java.lang.String)
public abstract CollationKey getCollationKey(java.lang.String source)
Transforms the String into a CollationKey suitable for efficient repeated comparison. The resulting key depends on the collator's rules, strength and decomposition mode.
See the CollationKey class documentation for more information.
source
- the string to be transformed into a CollationKey.CollationKey
,
compare(String, String)
|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |