public class RuleBasedCollator extends Collator
Collator.
It is based on the ICU RuleBasedCollator which implements the
CLDR and Unicode collation algorithms.
Most of the time, you create a Collator instance for a Locale
by calling the Collator.getInstance() factory method.
You can construct a RuleBasedCollator if 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 RuleBasedCollator is built from a rule String which 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
Note: earlier versions of RuleBasedCollator up 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 RuleBasedCollator throws a
ParseException.
CANONICAL_DECOMPOSITION, FULL_DECOMPOSITION, IDENTICAL, NO_DECOMPOSITION, PRIMARY, SECONDARY, TERTIARY| Constructor and Description |
|---|
RuleBasedCollator(String rules)
Constructs a new instance of
RuleBasedCollator using the
specified rules. |
| Modifier and Type | Method and Description |
|---|---|
Object |
clone()
Returns a new collator with the same collation rules, decomposition mode and
strength value as this collator.
|
int |
compare(String source,
String target)
Compares the
source text to the target text according to
the collation rules, strength and decomposition mode for this
RuleBasedCollator. |
boolean |
equals(Object obj)
Compares the specified object with this
RuleBasedCollator and
indicates if they are equal. |
CollationElementIterator |
getCollationElementIterator(CharacterIterator source)
Obtains a
CollationElementIterator for the given
CharacterIterator. |
CollationElementIterator |
getCollationElementIterator(String source)
Obtains a
CollationElementIterator for the given string. |
CollationKey |
getCollationKey(String source)
Returns the
CollationKey for the given source text. |
String |
getRules()
Returns the collation rules of this collator.
|
int |
hashCode()
Returns an integer hash code for this object.
|
compare, equals, getAvailableLocales, getDecomposition, getInstance, getInstance, getStrength, setDecomposition, setStrengthfinalize, getClass, notify, notifyAll, toString, wait, wait, waitcomparing, comparing, comparingDouble, comparingInt, comparingLong, naturalOrder, nullsFirst, nullsLast, reversed, reverseOrder, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLongpublic RuleBasedCollator(String rules) throws ParseException
RuleBasedCollator using the
specified rules. (See the class description.)
Note that the rules are interpreted as a delta to the
default sort order. This differs
from other implementations which work with full rules
specifications and may result in different behavior.
rules - the collation rules.NullPointerException - if rules == null.ParseException - if rules contains rules with invalid collation rule
syntax.public CollationElementIterator getCollationElementIterator(CharacterIterator source)
CollationElementIterator for the given
CharacterIterator. The source iterator's integrity will be
preserved since a new copy will be created for use.source - the source character iterator.CollationElementIterator for source.public CollationElementIterator getCollationElementIterator(String source)
CollationElementIterator for the given string.source - the source string.CollationElementIterator for source.public String getRules()
rules can be
fed into the RuleBasedCollator(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.
public Object clone()
public int compare(String source, String target)
source text to the target text according to
the collation rules, strength and decomposition mode for this
RuleBasedCollator. See the Collator class description
for an example of use.public CollationKey getCollationKey(String source)
CollationKey for the given source text.getCollationKey in class Collatorsource - the specified source text.CollationKey for the given source text.public int hashCode()
ObjectObject.equals(java.lang.Object) returns true must return
the same hash code value. This means that subclasses of Object
usually 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
hashCode method
if you intend implementing your own hashCode method.
hashCode in class CollatorObject.equals(java.lang.Object)public boolean equals(Object obj)
RuleBasedCollator and
indicates if they are equal. In order to be equal, object must be
an instance of Collator with the same collation rules and the
same attributes.equals in interface Comparator<Object>equals in class Collatorobj - the object to compare with this object.true if the specified object is equal to this
RuleBasedCollator; false otherwise.hashCode()