java.lang.Object | ||
↳ | java.text.Collator | |
↳ | java.text.RuleBasedCollator |
A concrete subclass of 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 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
.
[Expand]
Inherited Constants | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.text.Collator
|
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Constructs a new instance of
RuleBasedCollator using the
specified rules .
|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Returns a new collator with the same collation rules, decomposition mode and
strength value as this collator.
| |||||||||||
Compares the
source text to the target text according to
the collation rules, strength and decomposition mode for this
RuleBasedCollator .
| |||||||||||
Compares the specified object with this
RuleBasedCollator and
indicates if they are equal.
| |||||||||||
Obtains a
CollationElementIterator for the given string.
| |||||||||||
Obtains a
CollationElementIterator for the given
CharacterIterator .
| |||||||||||
Returns the
CollationKey for the given source text.
| |||||||||||
Returns the collation rules of this collator.
| |||||||||||
Returns an integer hash code for this object.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.text.Collator
| |||||||||||
From class
java.lang.Object
| |||||||||||
From interface
java.util.Comparator
|
Constructs a new instance of 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.
|
Returns a new collator with the same collation rules, decomposition mode and strength value as this collator.
Compares the 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.
source | the source text. |
---|---|
target | the target text. |
source
is less than,
equivalent to, or greater than target
.
Compares the specified object with this 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.
obj | the object to compare with this object. |
---|
true
if the specified object is equal to this
RuleBasedCollator
; false
otherwise.Obtains a CollationElementIterator
for the given string.
source | the source string. |
---|
CollationElementIterator
for source
.
Obtains a 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
.
Returns the CollationKey
for the given source text.
source | the specified source text. |
---|
CollationKey
for the given source text.
Returns the collation rules of this collator. These 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.
Returns an integer hash code for this object. By contract, any two
objects for which equals(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.