Android APIs
public class

RuleBasedCollator

extends Collator
java.lang.Object
   ↳ java.text.Collator
     ↳ java.text.RuleBasedCollator

Class Overview

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.

Summary

[Expand]
Inherited Constants
From class java.text.Collator
Public Constructors
RuleBasedCollator(String rules)
Constructs a new instance of RuleBasedCollator using the specified rules.
Public Methods
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(String source)
Obtains a CollationElementIterator for the given string.
CollationElementIterator getCollationElementIterator(CharacterIterator source)
Obtains a CollationElementIterator for the given CharacterIterator.
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.
[Expand]
Inherited Methods
From class java.text.Collator
From class java.lang.Object
From interface java.util.Comparator

Public Constructors

public RuleBasedCollator (String rules)

Added in API level 1

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.

Parameters
rules String: the collation rules.
Throws
NullPointerException if rules == null.
ParseException if rules contains rules with invalid collation rule syntax.

Public Methods

public Object clone ()

Added in API level 1

Returns a new collator with the same collation rules, decomposition mode and strength value as this collator.

Returns
Object a shallow copy of this collator.

See also:

public int compare (String source, String target)

Added in API level 1

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.

Parameters
source String: the source text.
target String: the target text.
Returns
int an integer which may be a negative value, zero, or else a positive value depending on whether source is less than, equivalent to, or greater than target.

public boolean equals (Object obj)

Added in API level 1

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.

Parameters
obj Object: the object to compare with this object.
Returns
boolean true if the specified object is equal to this RuleBasedCollator; false otherwise.

See also:

public CollationElementIterator getCollationElementIterator (String source)

Added in API level 1

Obtains a CollationElementIterator for the given string.

Parameters
source String: the source string.
Returns
CollationElementIterator the CollationElementIterator for source.

public CollationElementIterator getCollationElementIterator (CharacterIterator source)

Added in API level 1

Obtains a CollationElementIterator for the given CharacterIterator. The source iterator's integrity will be preserved since a new copy will be created for use.

Parameters
source CharacterIterator: the source character iterator.
Returns
CollationElementIterator a CollationElementIterator for source.

public CollationKey getCollationKey (String source)

Added in API level 1

Returns the CollationKey for the given source text.

Parameters
source String: the specified source text.
Returns
CollationKey the CollationKey for the given source text.

public String getRules ()

Added in API level 1

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
String

public int hashCode ()

Added in API level 1

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.

Returns
int this object's hash code.