Class RuleBasedCollator in Java

digitization, transformation, binary

Class RuleBasedCollator in Java

RuleBasedCollator class is a concrete subclass of the abstract Collator class. It performs collations using a table of rules that are specified in textual form. Most applications do not use this class directly; instead, they call Collator.getInstance() to obtain a Collator object (typically a RuleBasedCollator object) that implements the default collation order for a specified or default locale.

We should only need to use this class if you are collating strings for a locale that is not supported by default, or if you need to implement a highly customized collation order.

Ads code goes here

The RuleBasedCollator class is a concrete subclass of Collator that can compare strings using a table of rules. The rules for many locales already exist. To get a useful Collator for a given locale, use the getInstance(Locale) factory method of Collator.

If we need a special-purpose Collator or a Collator for a new locale, we can create your own
RuleBasedCollator from a string that represents the rules. The rules are expressed in three primary forms:

  • [relation] [text]
  • [reset] [text]
  • [modifier]

The rules can be chained together. The only modifier is the @ character, which specifies that all diacriticals are sorted backward, as in French.

The valid relations are:
> Greater than as a primary difference
; Greater than as a secondary difference or difference in accent
, Greater than as a tertiary difference or difference in case
= Equal

For example “<a<b” is two chained rules that state that ‘a’ is greater than all ignorable characters, and ‘b’ is greater than ‘a’. To expand this rule to include capitals, use “<a,A<b,B”.
A reset, specified by the & character, is used to insert rules in an existing list of chained rules. For example, we can add a rule to sort ‘e’ with an umlaut (Unicode 0308) after plain ‘e’. The existing rules might look like “<a<b<c<d<e<f”. We can add the following reset, “&e;e\u0308”, so that the complete rule table looks like “<a<b<c<d<e<f&e;e\u0308”.

READ  Class FileOutputStream in Java

The structure of the class RuleBasedCollator is given by

public class java.text.RuleBasedCollator extends java.text.Collator{
// Public Constructor
public RuleBasedCollator(String rules) throws ParseException;
// Public Instance Methods
public Object clone(); // Overrides Collator
public int compare(String source, String target); // Defines Collator
public boolean equals(Object obj); // Overrides Collator
public CollationElementIterator getCollationElementIterator(String source);
public CollationKey getCollationKey(String source); // Defines Collator
public String getRules();
public int hashCode(); // Defines Collator
}

The details of the class structure are given as follows:

public RuleBasedCollator(String rules);

public RuleBasedCollator(String rules) constructor creates a RuleBasedCollator with the given rules.

Parameter
rules – A string that contains the rules.

public Object clone();

public Object clone() method creates a copy of this RuledBasedCollator and returns it.

This method returns a copy of this RuledBasedCollator.

public int compare(String source, String target); 

public int compare(String source, String target); method compares the given strings according to the rules for this RuleBasedCollator and returns a value that indicates their relationship. If either of the strings are compared more than once, a CollationKey should be used instead.

This method returns -1 if the source is less than the target, 0 if the strings are equal, or 1 if the source is greater than the target.

Parameter
source – The source string.
target – The target string.

public boolean equals(Object obj); 

public boolean equals(Object obj); method returns true if obj is an instance of RuleBasedCollator and is equivalent to this RuleBasedCollator.

This method returns true if the objects are equal; false if they are not.

Parameter
obj – The object to be compared with this object.

public CollationElementIterator getCollationElementIterator(String source);

public CollationElementIterator getCollationElementIterator(String source) method generates a CollationElementIterator for the given string.

READ  Class Exception in Java

This method returns a CollationElementIterator for the given string.
Parameter
source – The source string.

public CollationKey getCollationKey(String source); 

public CollationKey getCollationKey(String source); method generates a CollationKey for the given string. The returned object can be compared with other CollationKey objects using CollationKey.compareTo(). This comparison is faster than using RuleBasedCollator.compare(), so if the same string is used for many comparisons, we should use CollationKey objects.

This method returns a CollationKey for the given string.

Parameter
source – The source string.

public String getRules();

public String getRules() method returns a string that contains the rules that this RuleBasedCollator is using.

This method returns the rules string for this RuleBasedCollator.

public int hashCode(); 

public int hashCode(); method returns a hashcode for this RuleBasedCollator.

This method returns a hashcode for this object.

Apart from these RuleBasedCollator class also has inherited methods from class- Object. They are as follows:

  • finalize()
  • notifyAll()
  • wait()
  • wait(long, int)
  • getClass()
  • notify()
  • toString()
  • wait(long)

RuleBasedCollator class also has inherited methods from Collator. They are as follows:

  • equals(String, String)
  • getStrength()
  • setStrength(int)
  • getDecomposition()
  • setDecomposition(int)

Share and Enjoy !

Leave a Comment

Your email address will not be published. Required fields are marked *