Top Description Inners Fields Constructors Methods
com.ibm.icu.text

public final Class RuleBasedCollator

extends Collator
Class Inheritance
Imports
java.lang.reflect.InvocationTargetException, .Method, java.text.CharacterIterator, .ParseException, java.util.Arrays, .Objects, java.util.concurrent.locks.Lock, .ReentrantLock, com.ibm.icu.impl.ClassLoaderUtil, .Normalizer2Impl, .Normalizer2Impl.ReorderingBuffer, com.ibm.icu.impl.coll.BOCSU, .Collation, .CollationCompare, .CollationData, .CollationFastLatin, .CollationIterator, .CollationKeys, .CollationKeys.SortKeyByteSink, .CollationLoader, .CollationRoot, .CollationSettings, .CollationTailoring, .ContractionsAndExpansions, .FCDUTF16CollationIterator, .SharedObject, .TailoredSet, .UTF16CollationIterator, com.ibm.icu.lang.UScript, com.ibm.icu.util.ULocale, .VersionInfo

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.ibm.icu.util.Freezable.

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

Author
Syn Wee Quek
ICU Status
Stable since ICU 2.8.

Nested and Inner Type Summary

Modifier and TypeClass 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

Field Summary

Modifier and TypeField and Description
private boolean
private RuleBasedCollator.CollationBuffer
pack-priv CollationData
private Lock
frozenLock

Frozen state of the collator.

pack-priv SharedObject.Reference<CollationSettings>
pack-priv CollationTailoring
private ULocale
Inherited from com.ibm.icu.text.Collator:
CANONICAL_DECOMPOSITIONFULL_DECOMPOSITIONIDENTICALNO_DECOMPOSITIONPRIMARYQUATERNARYSECONDARYTERTIARY

Constructor Summary

AccessConstructor 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

Method Summary

Modifier and TypeMethod and Description
private void
private void
public Object

Returns:

a new instance of this RuleBasedCollator object
clone
()

Overrides com.ibm.icu.text.Collator.clone.

Clones the RuleBasedCollator
public RuleBasedCollator
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.
compare
(String
the source text String.
source
,
String
the target text String.
target
)

Implements abstract com.ibm.icu.text.Collator.compare.

Implements java.util.Comparator.compare.

Compares the source text String to the target text String according to the collation rules, strength and decomposition mode for this RuleBasedCollator.
private static final int
protected int
doCompare(CharSequence left, CharSequence right)

Overrides com.ibm.icu.text.Collator.doCompare.

Deprecated This API is ICU internal only.
Compares two CharSequences.
public boolean
equals(Object
the Collator to compare to.
obj
)

Overrides com.ibm.icu.text.Collator.equals.

Implements java.util.Comparator.equals.

Compares the equality of two Collator objects.
public Collator

Returns:

the collator itself.
freeze
()

Overrides com.ibm.icu.text.Collator.freeze.

Implements com.ibm.icu.util.Freezable.freeze.

Freezes the collator.
private final RuleBasedCollator.CollationBuffer
public CollationElementIterator
getCollationElementIterator(String source)

Return a CollationElementIterator for the given String.

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.
getCollationKey
(String
the text String to be transformed into a collation key.
source
)

Implements abstract com.ibm.icu.text.Collator.getCollationKey.

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 mode
getDecomposition
()

Overrides com.ibm.icu.text.Collator.getDecomposition.

Returns the decomposition mode of this Collator.
private final CollationSettings
public ULocale
public int

Returns:

the maximum variable reordering group.
getMaxVariable
()

Overrides com.ibm.icu.text.Collator.getMaxVariable.

ICU

public boolean

Returns:

true if numeric collation is turned on, false otherwise
getNumericCollation
()

Method 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.
getRawCollationKey
(String
the text String to be transformed into a RawCollationKey
source
,
RawCollationKey
output RawCollationKey to store results
key
)

Implements abstract com.ibm.icu.text.Collator.getRawCollationKey.

Gets the simpler form of a CollationKey for the String source following the rules of this Collator and stores the result into the user provided argument key.
private RawCollationKey
public int[]

Returns:

a copy of the reordering codes for this collator; if none are set then returns an empty array
getReorderCodes
()

Overrides com.ibm.icu.text.Collator.getReorderCodes.

Retrieves the reordering codes for this collator.
public String

Returns:

the collation tailoring rules
getRules
()

Gets the collation tailoring rules for this RuleBasedCollator.

public String

Returns:

the current rules that defines this Collator.
getRules
(boolean
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.
getStrength
()

Overrides com.ibm.icu.text.Collator.getStrength.

Returns this Collator's strength attribute.
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.
getTailoredSet
()

Overrides com.ibm.icu.text.Collator.getTailoredSet.

Get a UnicodeSet that contains all the characters and sequences tailored in this collator.
public VersionInfo

Returns:

the version object associated with this collator
getUCAVersion
()

Implements abstract com.ibm.icu.text.Collator.getUCAVersion.

Get the UCA version of this collator object.
public int

Returns:

the variable top primary weight
getVariableTop
()

Implements abstract com.ibm.icu.text.Collator.getVariableTop.

ICU

public VersionInfo

Returns:

the version object associated with this collator
getVersion
()

Implements abstract com.ibm.icu.text.Collator.getVersion.

Get the version of this collator object.
public int

Returns:

the unique hash code for this Collator
hashCode
()

Overrides com.ibm.icu.text.Collator.hashCode.

Generates a unique hash code for this RuleBasedCollator.
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
internalBuildTailoring(String
rule string
rules
)

Implements from-rule constructors.

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 false
isAlternateHandlingShifted
()

Checks if the alternate handling behavior is the UCA defined SHIFTED or NON_IGNORABLE.

public boolean

Returns:

the case level mode
isCaseLevel
()

Checks if case level is set to true.

public boolean

Returns:

true if French Collation is set to true, false otherwise
isFrenchCollation
()

Checks if French Collation is set to true.

public boolean
isFrozen()

Overrides com.ibm.icu.text.Collator.isFrozen.

Implements com.ibm.icu.util.Freezable.isFrozen.

Determines whether the object has been frozen or not.
public boolean

Returns:

false
isHiraganaQuaternary
()

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 otherwise
isLowerCaseFirst
()

Return true if a lowercase character is sorted before the corresponding uppercase character.

pack-priv final boolean

Returns:

true if c is unsafe
isUnsafe
(int
code 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 otherwise
isUpperCaseFirst
()

Return 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.ibm.icu.text.Collator.setDecomposition.

Sets the decomposition mode of this Collator.
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.ibm.icu.text.Collator.setLocale.

Set information about the locales that were used to create this object.
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:

this
setMaxVariable
(int
one 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.ibm.icu.text.Collator.setMaxVariable.

ICU

public void
setNumericCollation(boolean
true to turn numeric collation on and false to turn it off
flag
)

ICU

public void
setNumericCollationDefault()

Method to set numeric collation to its default value.

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.ibm.icu.text.Collator.setReorderCodes.

Sets the reordering codes for this collator.
public void
setStrength(int
the new strength value.
newStrength
)

Overrides com.ibm.icu.text.Collator.setStrength.

Sets this Collator's strength attribute.
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 weight
setVariableTop
(String
one or more (if contraction) characters to which the variable top should be set
varTop
)

Implements abstract com.ibm.icu.text.Collator.setVariableTop.

Deprecated ICU 53 Call setMaxVariable(int) instead.

ICU

public void
setVariableTop(int
primary weight, as returned by setVariableTop or getVariableTop
varTop
)

Implements abstract com.ibm.icu.text.Collator.setVariableTop.

Deprecated ICU 53 Call setMaxVariable() instead.

ICU

private int
private void
private void
Inherited from com.ibm.icu.text.Collator:
compareequalsgetAvailableLocalesgetAvailableULocalesgetDisplayNamegetDisplayNamegetDisplayNamegetDisplayNamegetEquivalentReorderCodesgetFunctionalEquivalentgetFunctionalEquivalentgetInstancegetInstancegetInstancegetKeywordsgetKeywordValuesgetKeywordValuesForLocaleregisterFactoryregisterInstancesetStrength2unregister