RuleBasedCollator is a concrete subclass of Collator. It allows customization of the Collator via user-specified rule sets. RuleBasedCollator is designed to be fully compliant to the Unicode Collation Algorithm (UCA) and conforms to ISO 14651.
A Collator is thread-safe only when frozen. See isFrozen() and com..
Users are strongly encouraged to read the User Guide for more information about the collation service before using this class.
Create a RuleBasedCollator from a locale by calling the getInstance(Locale) factory method in the base class Collator. Collator.getInstance(Locale) creates a RuleBasedCollator object based on the collation rules defined by the argument locale. If a customized collation ordering or attributes is required, use the RuleBasedCollator(String) constructor with the appropriate rules. The customized RuleBasedCollator will base its ordering on the CLDR root collation, while re-adjusting the attributes and orders of the characters in the specified rule accordingly.
RuleBasedCollator provides correct collation orders for most locales supported in ICU. If specific data for a locale is not available, the orders eventually falls back to the CLDR root sort order.
For information about the collation rule syntax and details about customization, please refer to the Collation customization section of the User Guide.
Note that there are some differences between the Collation rule syntax used in Java and ICU4J:
Examples
Creating Customized RuleBasedCollators:
String simple = "& a < b < c < d";
RuleBasedCollator simpleCollator = new RuleBasedCollator(simple);
String norwegian = "& a , A < b , B < c , C < d , D < e , E "
+ "< f , F < g , G < h , H < i , I < j , "
+ "J < k , K < l , L < m , M < n , N < "
+ "o , O < p , P < q , Q <r , R <s , S < "
+ "t , T < u , U < v , V < w , W < x , X "
+ "< y , Y < z , Z < \u00E5 = a\u030A "
+ ", \u00C5 = A\u030A ; aa , AA < \u00E6 "
+ ", \u00C6 < \u00F8 , \u00D8";
RuleBasedCollator norwegianCollator = new RuleBasedCollator(norwegian);
Concatenating rules to combine Collators:
// Create an en_US Collator object
RuleBasedCollator en_USCollator = (RuleBasedCollator)
Collator.getInstance(new Locale("en", "US", ""));
// Create a da_DK Collator object
RuleBasedCollator da_DKCollator = (RuleBasedCollator)
Collator.getInstance(new Locale("da", "DK", ""));
// Combine the two
// First, get the collation rules from en_USCollator
String en_USRules = en_USCollator.getRules();
// Second, get the collation rules from da_DKCollator
String da_DKRules = da_DKCollator.getRules();
RuleBasedCollator newCollator =
new RuleBasedCollator(en_USRules + da_DKRules);
// newCollator has the combined rules
Making changes to an existing RuleBasedCollator to create a new Collator object, by appending changes to
the existing rule:
// Create a new Collator object with additional rules
String addRules = "& C < ch, cH, Ch, CH";
RuleBasedCollator myCollator =
new RuleBasedCollator(en_USCollator.getRules() + addRules);
// myCollator contains the new rules
How to change the order of non-spacing accents:
// old rule with main accents
String oldRules = "= \u0301 ; \u0300 ; \u0302 ; \u0308 "
+ "; \u0327 ; \u0303 ; \u0304 ; \u0305 "
+ "; \u0306 ; \u0307 ; \u0309 ; \u030A "
+ "; \u030B ; \u030C ; \u030D ; \u030E "
+ "; \u030F ; \u0310 ; \u0311 ; \u0312 "
+ "< a , A ; ae, AE ; \u00e6 , \u00c6 "
+ "< b , B < c, C < e, E & C < d , D";
// change the order of accent characters
String addOn = "& \u0300 ; \u0308 ; \u0302";
RuleBasedCollator myCollator = new RuleBasedCollator(oldRules + addOn);
Putting in a new primary ordering before the default setting, e.g. sort English characters before or after Japanese
characters in the Japanese Collator:
// get en_US Collator rules
RuleBasedCollator en_USCollator
= (RuleBasedCollator)Collator.getInstance(Locale.US);
// add a few Japanese characters to sort before English characters
// suppose the last character before the first base letter 'a' in
// the English collation rule is \u2212
String jaString = "& \u2212 <\u3041, \u3042 <\u3043, "
+ "\u3044";
RuleBasedCollator myJapaneseCollator
= new RuleBasedCollator(en_USCollator.getRules() + jaString);
This class is not subclassable
| Modifier and Type | Class and Description |
|---|---|
| private static class | |
| private static class | |
| private static class | |
| private abstract static class | RuleBasedCollator.NFDIterator
Abstract iterator for identical-level string comparisons. |
| private static class |
| Modifier and Type | Field and Description |
|---|---|
| private boolean | |
| private RuleBasedCollator. | |
| pack-priv CollationData | |
| private Lock | frozenLock
Frozen state of the collator. |
| pack-priv SharedObject. | |
| pack-priv CollationTailoring | |
| private ULocale |
| Access | Constructor and Description |
|---|---|
| public | RuleBasedCollator(String
the collation rules to build the collation table from. rules)Constructor that takes the argument rules for customization. |
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| private void | |
| private void | |
| public Object | Returns: a new instance of this RuleBasedCollator objectOverrides com. |
| public RuleBasedCollator | cloneAsThawed()
Overrides com. Implements com. |
| public int | Returns: Returns an integer value. Value is less than zero if source is less than target, value is zero if source and target are equal, value is greater than zero if source is greater than target.the source text String. source, String the target text String. target)Implements abstract com. Implements java. |
| private static final int | compareNFDIter(Normalizer2Impl nfcImpl, RuleBasedCollator.
|
| protected int | doCompare(CharSequence left, CharSequence right)
Overrides com.
Deprecated
This API is ICU internal only.
Compares two CharSequences.
|
| public boolean | equals(Object
the Collator to compare to. obj)Overrides com. Implements java. |
| public Collator | Returns: the collator itself.Overrides com. Implements com. |
| private final RuleBasedCollator. | |
| public CollationElementIterator | |
| public CollationElementIterator | getCollationElementIterator(CharacterIterator source)
Return a CollationElementIterator for the given CharacterIterator. |
| public CollationElementIterator | getCollationElementIterator(UCharacterIterator source)
Return a CollationElementIterator for the given UCharacterIterator. |
| public CollationKey | Returns: the CollationKey for the given String based on this RuleBasedCollator's collation rules. If the source String is null, a null CollationKey is returned.the text String to be transformed into a collation key. source)Implements abstract com. Get a Collation key for the argument String source from this RuleBasedCollator. |
| private CollationKey | |
| public void | getContractionsAndExpansions(UnicodeSet
if not null, set to contain contractions contractions, UnicodeSet if not null, set to contain expansions expansions, boolean add the prefix contextual elements to contractions addPrefixes)Gets unicode sets containing contractions and/or expansions of a collator |
| public int | Returns: the decomposition modeOverrides com. |
| private final CollationSettings | |
| public ULocale | getLocale(ULocale.
type of information requested, either typecom. or com..Overrides com. ICU |
| public int | Returns: the maximum variable reordering group.Overrides com. ICU |
| public boolean | Returns: true if numeric collation is turned on, false otherwiseMethod to retrieve the numeric collation value. |
| private final CollationSettings | |
| public RawCollationKey | Returns: If key is null, a new instance of RawCollationKey will be created and returned, otherwise the user provided key will be returned.the text String to be transformed into a RawCollationKey source, RawCollationKey output RawCollationKey to store results key)Implements abstract com. |
| private RawCollationKey | getRawCollationKey(CharSequence source, RawCollationKey key, RuleBasedCollator.
|
| public int[] | Returns: a copy of the reordering codes for this collator; if none are set then returns an empty arrayOverrides com. |
| public String | Returns: the collation tailoring rulesGets the collation tailoring rules for this RuleBasedCollator. |
| public String | Returns: the current rules that defines this Collator.true if the rules that defines the full set of collation order is required, otherwise false for
returning only the tailored rules fullrules)Returns current rules. |
| public int | Returns: this Collator's current strength attribute.Overrides com. |
| public UnicodeSet | Returns: a pointer to a UnicodeSet object containing all the code points and sequences that may sort differently than in the root collator.Overrides com. |
| public VersionInfo | Returns: the version object associated with this collatorImplements abstract com. |
| public int | Returns: the variable top primary weightImplements abstract com. ICU |
| public VersionInfo | Returns: the version object associated with this collatorImplements abstract com. |
| public int | Returns: the unique hash code for this CollatorOverrides com. |
| private final void | |
| pack-priv void | internalAddContractions(int c, UnicodeSet set)
Deprecated
This API is ICU internal only.
Adds the contractions that start with character c to the set.
|
| private final void | |
| public long[] | internalGetCEs(CharSequence
the string str)
Deprecated
This API is ICU internal only.
Returns the CEs for the string.
|
| private void | |
| public boolean | Returns: true or falseChecks if the alternate handling behavior is the UCA defined SHIFTED or NON_IGNORABLE. |
| public boolean | |
| public boolean | Returns: true if French Collation is set to true, false otherwiseChecks if French Collation is set to true. |
| public boolean | isFrozen()
Overrides com. Implements com. |
| public boolean | Returns: false
Deprecated
ICU 50 Implementation detail, cannot be set via API, was removed from implementation.
Checks if the Hiragana Quaternary mode is set on.
|
| public boolean | Returns: true lower cased characters are sorted before upper cased characters, false otherwiseReturn true if a lowercase character is sorted before the corresponding uppercase character. |
| pack-priv final boolean | Returns: true if c is unsafecode point or code unit c)Tests whether a character is "unsafe" for use as a collation starting point. |
| public boolean | Returns: true if upper cased characters are sorted before lower cased characters, false otherwiseReturn true if an uppercase character is sorted before the corresponding lowercase character. |
| private final void | |
| public void | setAlternateHandlingDefault()
Sets the alternate handling mode to the initial mode set during construction of the RuleBasedCollator. |
| public void | setAlternateHandlingShifted(boolean
true if SHIFTED behavior for alternate handling is desired, false for the NON_IGNORABLE behavior. shifted)Sets the alternate handling for QUATERNARY strength to be either shifted or non-ignorable. |
| public final void | setCaseFirstDefault()
Sets the case first mode to the initial mode set during construction of the RuleBasedCollator. |
| public void | setCaseLevel(boolean
true if case level sorting is required, false otherwise flag)When case level is set to true, an additional weight is formed between the SECONDARY and TERTIARY weight, known as the case level. |
| public void | setCaseLevelDefault()
Sets the case level mode to the initial mode set during construction of the RuleBasedCollator. |
| public void | setDecomposition(int
the new decomposition mode decomposition)Overrides com. |
| public void | setDecompositionDefault()
Sets the decomposition mode to the initial mode set during construction of the RuleBasedCollator. |
| private void | |
| public void | setFrenchCollation(boolean
true to set the French collation on, false to set it off flag)Sets the mode for the direction of SECONDARY weights to be used in French collation. |
| public void | setFrenchCollationDefault()
Sets the French collation mode to the initial mode set during construction of the RuleBasedCollator. |
| public void | setHiraganaQuaternary(boolean
true if Hiragana Quaternary mode is to be on, false otherwise flag)
Deprecated
ICU 50 Implementation detail, cannot be set via API, was removed from implementation.
Sets the Hiragana Quaternary mode to be on or off.
|
| public void | setHiraganaQuaternaryDefault()
Deprecated
ICU 50 Implementation detail, cannot be set via API, was removed from implementation.
Sets the Hiragana Quaternary mode to the initial mode set during construction of the RuleBasedCollator.
|
| pack-priv void | setLocale(ULocale
the most specific locale containing any resource
data, or null valid, ULocale the locale containing data used to construct this
object, or null actual)Overrides com. |
| public void | setLowerCaseFirst(boolean
true for sorting lower cased characters before upper cased characters, false to ignore case
preferences. lowerfirst)Sets the orders of lower cased characters to sort before upper cased characters, in strength TERTIARY. |
| public RuleBasedCollator | Returns: thisone of Collator.ReorderCodes.SPACE, Collator.ReorderCodes.PUNCTUATION,
Collator.ReorderCodes.SYMBOL, Collator.ReorderCodes.CURRENCY;
or Collator.ReorderCodes.DEFAULT to restore the default max variable group group)Overrides com. ICU |
| public void | |
| public void | |
| public void | setReorderCodes(int...
the reordering codes to apply to this collator; if this is null or an empty array
then this clears any existing reordering order)Overrides com. |
| public void | setStrength(int
the new strength value. newStrength)Overrides com. |
| public void | setStrengthDefault()
Sets the collation strength to the initial mode set during the construction of the RuleBasedCollator. |
| public void | setUpperCaseFirst(boolean
true to sort uppercase characters before lowercase characters, false to sort lowercase characters
before uppercase characters upperfirst)Sets whether uppercase characters sort before lowercase characters or vice versa, in strength TERTIARY. |
| public int | Returns: variable top primary weightone or more (if contraction) characters to which the variable top should be set varTop)Implements abstract com.
Deprecated
ICU 53 Call
setMaxVariable(int) instead.
ICU |
| public void | setVariableTop(int
primary weight, as returned by setVariableTop or getVariableTop varTop)Implements abstract com.
Deprecated
ICU 53 Call setMaxVariable() instead.
ICU |
| private int | |
| private void | |
| private void |