Class RuleBasedCollator
- java.lang.Object
-
- java.text.Collator
-
- java.text.RuleBasedCollator
-
- All Implemented Interfaces:
Cloneable,Comparator<Object>
public class RuleBasedCollator extends Collator
A concrete subclass ofCollator. It is based on the ICU RuleBasedCollator which implements the CLDR and Unicode collation algorithms.Most of the time, you create a
Collatorinstance for aLocaleby calling theCollator.getInstance()factory method. You can construct aRuleBasedCollatorif you need a custom sort order.The root collator's sort order is the CLDR root collation order which in turn is the Unicode default sort order with a few modifications. A
RuleBasedCollatoris built from a ruleStringwhich changes the sort order of some characters and strings relative to the default order.A rule string usually contains one or more rule chains. A rule chain consists of a reset followed by one or more rules. The reset anchors the following rules in the default sort order. The rules change the order of the their characters and strings relative to the reset point.
A reset is an ampersand
&followed by one or more characters for the reset position. A rule is a relation operator, which specifies the level of difference, also followed by one or more characters. A multi-character rule creates a "contraction". A multi-character reset position usually creates "expansions".For example, the following rules make "ä" sort with a diacritic-like (secondary) difference from "ae" (like in German phonebook sorting), and make "å" and "aa" sort as a base letter (primary) after "z" (like in Danish). Uppercase forms sort with a case-like (tertiary) difference after their lowercase forms.
&AE<<ä <<<Ä &z<å<<<Å<<<aa<<<Aa<<<AA
For details see
- CLDR Collation Rule Syntax
- ICU User Guide Collation Customization
Note: earlier versions of
RuleBasedCollatorup to and including Android 4.4 (KitKat) allowed the omission of the reset from the first rule chain. This was interpreted as an implied reset after the last non-Han script in the default order. However, this is not a useful reset position, except for large tailorings of Han characters themselves. Starting with the CLDR 24 collation specification and the ICU 53 implementation, the initial reset is required.If the rule string does not follow the syntax, then
RuleBasedCollatorthrows aParseException.
-
-
Field Summary
-
Fields inherited from class java.text.Collator
CANONICAL_DECOMPOSITION, FULL_DECOMPOSITION, IDENTICAL, NO_DECOMPOSITION, PRIMARY, SECONDARY, TERTIARY
-
-
Constructor Summary
Constructors Constructor Description RuleBasedCollator(String rules)Constructs a new instance ofRuleBasedCollatorusing the specifiedrules.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Objectclone()Returns a new collator with the same collation rules, decomposition mode and strength value as this collator.intcompare(String source, String target)Compares thesourcetext to thetargettext according to the collation rules, strength and decomposition mode for thisRuleBasedCollator.booleanequals(Object obj)Compares the specified object with thisRuleBasedCollatorand indicates if they are equal.CollationElementIteratorgetCollationElementIterator(String source)Obtains aCollationElementIteratorfor the given string.CollationElementIteratorgetCollationElementIterator(CharacterIterator source)Obtains aCollationElementIteratorfor the givenCharacterIterator.CollationKeygetCollationKey(String source)Returns theCollationKeyfor the given source text.StringgetRules()Returns the collation rules of this collator.inthashCode()Returns an integer hash code for this object.-
Methods inherited from class java.text.Collator
compare, equals, getAvailableLocales, getDecomposition, getInstance, getInstance, getStrength, setDecomposition, setStrength
-
-
-
-
Constructor Detail
-
RuleBasedCollator
public RuleBasedCollator(String rules) throws ParseException
Constructs a new instance ofRuleBasedCollatorusing the specifiedrules. (See theclass description.)Note that the
rulesare interpreted as a delta to the default sort order. This differs from other implementations which work with fullrulesspecifications and may result in different behavior.- Parameters:
rules- the collation rules.- Throws:
NullPointerException- ifrules == null.ParseException- ifrulescontains rules with invalid collation rule syntax.
-
-
Method Detail
-
getCollationElementIterator
public CollationElementIterator getCollationElementIterator(CharacterIterator source)
Obtains aCollationElementIteratorfor the givenCharacterIterator. The source iterator's integrity will be preserved since a new copy will be created for use.- Parameters:
source- the source character iterator.- Returns:
- a
CollationElementIteratorforsource.
-
getCollationElementIterator
public CollationElementIterator getCollationElementIterator(String source)
Obtains aCollationElementIteratorfor the given string.- Parameters:
source- the source string.- Returns:
- the
CollationElementIteratorforsource.
-
getRules
public String getRules()
Returns the collation rules of this collator. Theserulescan be fed into theRuleBasedCollator(String)constructor.The returned string will be empty unless you constructed the instance yourself. The string forms of the collation rules are omitted to save space on the device.
-
clone
public Object clone()
Returns a new collator with the same collation rules, decomposition mode and strength value as this collator.
-
compare
public int compare(String source, String target)
Compares thesourcetext to thetargettext according to the collation rules, strength and decomposition mode for thisRuleBasedCollator. See theCollatorclass description for an example of use.
-
getCollationKey
public CollationKey getCollationKey(String source)
Returns theCollationKeyfor the given source text.- Specified by:
getCollationKeyin classCollator- Parameters:
source- the specified source text.- Returns:
- the
CollationKeyfor the given source text.
-
hashCode
public 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.- Specified by:
hashCodein classCollator- Returns:
- this object's hash code.
- See Also:
Object.equals(java.lang.Object)
-
equals
public boolean equals(Object obj)
Compares the specified object with thisRuleBasedCollatorand indicates if they are equal. In order to be equal,objectmust be an instance ofCollatorwith the same collation rules and the same attributes.- Specified by:
equalsin interfaceComparator<Object>- Overrides:
equalsin classCollator- Parameters:
obj- the object to compare with this object.- Returns:
trueif the specified object is equal to thisRuleBasedCollator;falseotherwise.- See Also:
hashCode()
-
-