1f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes/*
2adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Licensed to the Apache Software Foundation (ASF) under one or more
3adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * contributor license agreements.  See the NOTICE file distributed with
4adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * this work for additional information regarding copyright ownership.
5adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * The ASF licenses this file to You under the Apache License, Version 2.0
6adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * (the "License"); you may not use this file except in compliance with
7adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * the License.  You may obtain a copy of the License at
8f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes *
9adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *     http://www.apache.org/licenses/LICENSE-2.0
10f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes *
11adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * Unless required by applicable law or agreed to in writing, software
12adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
13adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * See the License for the specific language governing permissions and
15adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * limitations under the License.
16adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */
17adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
18adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectpackage java.text;
19adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
204dfae7b3e2b56160646c17d93b32b3ff495c0053Elliott Hughesimport libcore.icu.RuleBasedCollatorICU;
214dfae7b3e2b56160646c17d93b32b3ff495c0053Elliott Hughes
22adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project/**
231c13b45d19d7c9ab363c5984c2a9f329a67ef6f4Elliott Hughes * A concrete subclass of {@link Collator}.
241c13b45d19d7c9ab363c5984c2a9f329a67ef6f4Elliott Hughes * It is based on the ICU RuleBasedCollator which implements the
251c13b45d19d7c9ab363c5984c2a9f329a67ef6f4Elliott Hughes * CLDR and Unicode collation algorithms.
261c13b45d19d7c9ab363c5984c2a9f329a67ef6f4Elliott Hughes *
27b9c3557fb8d2d9a1f9fe9f8c7ec0b058758b6db6Elliott Hughes * <p>Most of the time, you create a {@link Collator} instance for a {@link java.util.Locale}
28b9c3557fb8d2d9a1f9fe9f8c7ec0b058758b6db6Elliott Hughes * by calling the {@link Collator#getInstance} factory method.
291c13b45d19d7c9ab363c5984c2a9f329a67ef6f4Elliott Hughes * You can construct a {@code RuleBasedCollator} if you need a custom sort order.
301c13b45d19d7c9ab363c5984c2a9f329a67ef6f4Elliott Hughes *
311c13b45d19d7c9ab363c5984c2a9f329a67ef6f4Elliott Hughes * <p>The root collator's sort order is the CLDR root collation order
321c13b45d19d7c9ab363c5984c2a9f329a67ef6f4Elliott Hughes * which in turn is the Unicode default sort order with a few modifications.
331c13b45d19d7c9ab363c5984c2a9f329a67ef6f4Elliott Hughes * A {@code RuleBasedCollator} is built from a rule {@code String} which changes the
341c13b45d19d7c9ab363c5984c2a9f329a67ef6f4Elliott Hughes * sort order of some characters and strings relative to the default order.
351c13b45d19d7c9ab363c5984c2a9f329a67ef6f4Elliott Hughes *
361c13b45d19d7c9ab363c5984c2a9f329a67ef6f4Elliott Hughes * <p>A rule string usually contains one or more rule chains.
371c13b45d19d7c9ab363c5984c2a9f329a67ef6f4Elliott Hughes * A rule chain consists of a reset followed by one or more rules.
381c13b45d19d7c9ab363c5984c2a9f329a67ef6f4Elliott Hughes * The reset anchors the following rules in the default sort order.
391c13b45d19d7c9ab363c5984c2a9f329a67ef6f4Elliott Hughes * The rules change the order of the their characters and strings
401c13b45d19d7c9ab363c5984c2a9f329a67ef6f4Elliott Hughes * relative to the reset point.
411c13b45d19d7c9ab363c5984c2a9f329a67ef6f4Elliott Hughes *
421c13b45d19d7c9ab363c5984c2a9f329a67ef6f4Elliott Hughes * <p>A reset is an ampersand {@code &amp;} followed by one or more characters for the reset position.
431c13b45d19d7c9ab363c5984c2a9f329a67ef6f4Elliott Hughes * A rule is a relation operator, which specifies the level of difference,
441c13b45d19d7c9ab363c5984c2a9f329a67ef6f4Elliott Hughes * also followed by one or more characters.
451c13b45d19d7c9ab363c5984c2a9f329a67ef6f4Elliott Hughes * A multi-character rule creates a "contraction".
461c13b45d19d7c9ab363c5984c2a9f329a67ef6f4Elliott Hughes * A multi-character reset position usually creates "expansions".
471c13b45d19d7c9ab363c5984c2a9f329a67ef6f4Elliott Hughes *
481c13b45d19d7c9ab363c5984c2a9f329a67ef6f4Elliott Hughes * <p>For example, the following rules
491c13b45d19d7c9ab363c5984c2a9f329a67ef6f4Elliott Hughes * make "ä" sort with a diacritic-like (secondary) difference from "ae"
501c13b45d19d7c9ab363c5984c2a9f329a67ef6f4Elliott Hughes * (like in German phonebook sorting),
511c13b45d19d7c9ab363c5984c2a9f329a67ef6f4Elliott Hughes * and make "å" and "aa" sort as a base letter (primary) after "z" (like in Danish).
521c13b45d19d7c9ab363c5984c2a9f329a67ef6f4Elliott Hughes * Uppercase forms sort with a case-like (tertiary) difference after their lowercase forms.
539b354e75f2418e54638e02d153216660b67d01b8Jesse Wilson *
54adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <blockquote>
55adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * <pre>
561c13b45d19d7c9ab363c5984c2a9f329a67ef6f4Elliott Hughes * &amp;AE&lt;&lt;ä &lt;&lt;&lt;Ä
571c13b45d19d7c9ab363c5984c2a9f329a67ef6f4Elliott Hughes * &amp;z&lt;å&lt;&lt;&lt;Å&lt;&lt;&lt;aa&lt;&lt;&lt;Aa&lt;&lt;&lt;AA
58adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * </pre>
59adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project * </blockquote>
609b354e75f2418e54638e02d153216660b67d01b8Jesse Wilson *
611c13b45d19d7c9ab363c5984c2a9f329a67ef6f4Elliott Hughes * <p>For details see
621c13b45d19d7c9ab363c5984c2a9f329a67ef6f4Elliott Hughes * <ul>
631c13b45d19d7c9ab363c5984c2a9f329a67ef6f4Elliott Hughes *   <li>CLDR <a href="http://www.unicode.org/reports/tr35/tr35-collation.html#Rules">Collation Rule Syntax</a>
641c13b45d19d7c9ab363c5984c2a9f329a67ef6f4Elliott Hughes *   <li>ICU User Guide <a href="http://userguide.icu-project.org/collation/customization">Collation Customization</a>
651c13b45d19d7c9ab363c5984c2a9f329a67ef6f4Elliott Hughes * </ul>
669b354e75f2418e54638e02d153216660b67d01b8Jesse Wilson *
671c13b45d19d7c9ab363c5984c2a9f329a67ef6f4Elliott Hughes * <p>Note: earlier versions of {@code RuleBasedCollator} up to and including Android 4.4 (KitKat)
681c13b45d19d7c9ab363c5984c2a9f329a67ef6f4Elliott Hughes * allowed the omission of the reset from the first rule chain.
691c13b45d19d7c9ab363c5984c2a9f329a67ef6f4Elliott Hughes * This was interpreted as an implied reset after the last non-Han script in the default order.
701c13b45d19d7c9ab363c5984c2a9f329a67ef6f4Elliott Hughes * However, this is not a useful reset position, except for large tailorings of
711c13b45d19d7c9ab363c5984c2a9f329a67ef6f4Elliott Hughes * Han characters themselves.
721c13b45d19d7c9ab363c5984c2a9f329a67ef6f4Elliott Hughes * Starting with the CLDR 24 collation specification and the ICU 53 implementation,
731c13b45d19d7c9ab363c5984c2a9f329a67ef6f4Elliott Hughes * the initial reset is required.
749b354e75f2418e54638e02d153216660b67d01b8Jesse Wilson *
751c13b45d19d7c9ab363c5984c2a9f329a67ef6f4Elliott Hughes * <p>If the rule string does not follow the syntax, then {@code RuleBasedCollator} throws a
761c13b45d19d7c9ab363c5984c2a9f329a67ef6f4Elliott Hughes * {@code ParseException}.
77adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */
78adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectpublic class RuleBasedCollator extends Collator {
794dfae7b3e2b56160646c17d93b32b3ff495c0053Elliott Hughes    RuleBasedCollator(RuleBasedCollatorICU wrapper) {
80adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        super(wrapper);
81adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
82adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
83adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
84adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Constructs a new instance of {@code RuleBasedCollator} using the
851c13b45d19d7c9ab363c5984c2a9f329a67ef6f4Elliott Hughes     * specified {@code rules}. (See the {@link RuleBasedCollator class description}.)
86adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * <p>
871c13b45d19d7c9ab363c5984c2a9f329a67ef6f4Elliott Hughes     * Note that the {@code rules} are interpreted as a delta to the
881c13b45d19d7c9ab363c5984c2a9f329a67ef6f4Elliott Hughes     * default sort order. This differs
891c13b45d19d7c9ab363c5984c2a9f329a67ef6f4Elliott Hughes     * from other implementations which work with full {@code rules}
90adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * specifications and may result in different behavior.
91adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *
92adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param rules
93adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            the collation rules.
94adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @throws NullPointerException
95a7d4139bed693bf6037bf5f7f49a24b077102adcElliott Hughes     *             if {@code rules == null}.
96adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @throws ParseException
97adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *             if {@code rules} contains rules with invalid collation rule
98adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *             syntax.
99adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
100adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public RuleBasedCollator(String rules) throws ParseException {
101adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        if (rules == null) {
10286acc043d3334651ee26c65467d78d6cefedd397Kenny Root            throw new NullPointerException("rules == null");
103adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
104adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        try {
1054dfae7b3e2b56160646c17d93b32b3ff495c0053Elliott Hughes            icuColl = new RuleBasedCollatorICU(rules);
106adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        } catch (Exception e) {
107adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            if (e instanceof ParseException) {
108adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                throw (ParseException) e;
109adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            }
110adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            /*
111adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project             * -1 means it's not a ParseException. Maybe IOException thrown when
11203c0a8e681c776fdba0389ab8593282139afc6d6Elliott Hughes             * an error occurred while reading internal data.
113adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project             */
114adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            throw new ParseException(e.getMessage(), -1);
115adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
116adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
117adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
118adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
119adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Obtains a {@code CollationElementIterator} for the given
120adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * {@code CharacterIterator}. The source iterator's integrity will be
121adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * preserved since a new copy will be created for use.
122f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes     *
123adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param source
124adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            the source character iterator.
125adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @return a {@code CollationElementIterator} for {@code source}.
126adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
127a7d4139bed693bf6037bf5f7f49a24b077102adcElliott Hughes    public CollationElementIterator getCollationElementIterator(CharacterIterator source) {
128adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        if (source == null) {
12986acc043d3334651ee26c65467d78d6cefedd397Kenny Root            throw new NullPointerException("source == null");
130adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
131866e7ae17a3da81a02b0b144e0c9c2b3196d293aElliott Hughes        return new CollationElementIterator(icuColl.getCollationElementIterator(source));
132adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
133adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
134adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
135adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Obtains a {@code CollationElementIterator} for the given string.
136f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes     *
137adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param source
138adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            the source string.
139adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @return the {@code CollationElementIterator} for {@code source}.
140adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
141adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public CollationElementIterator getCollationElementIterator(String source) {
142adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        if (source == null) {
14386acc043d3334651ee26c65467d78d6cefedd397Kenny Root            throw new NullPointerException("source == null");
144adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
145866e7ae17a3da81a02b0b144e0c9c2b3196d293aElliott Hughes        return new CollationElementIterator(icuColl.getCollationElementIterator(source));
146adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
147adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
148adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
149adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Returns the collation rules of this collator. These {@code rules} can be
1505d287a9d9768195f53e244414b465ec4a6f72625The Android Open Source Project     * fed into the {@code RuleBasedCollator(String)} constructor.
151adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *
1521c13b45d19d7c9ab363c5984c2a9f329a67ef6f4Elliott Hughes     * <p>The returned string will be empty unless you constructed the instance yourself.
1531c13b45d19d7c9ab363c5984c2a9f329a67ef6f4Elliott Hughes     * The string forms of the collation rules are omitted to save space on the device.
154adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
155adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public String getRules() {
156866e7ae17a3da81a02b0b144e0c9c2b3196d293aElliott Hughes        return icuColl.getRules();
157adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
158adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
159adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
160adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Returns a new collator with the same collation rules, decomposition mode and
161adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * strength value as this collator.
162f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes     *
163adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @return a shallow copy of this collator.
164adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @see java.lang.Cloneable
165adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
166adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    @Override
167adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public Object clone() {
168adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        RuleBasedCollator clone = (RuleBasedCollator) super.clone();
169adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        return clone;
170adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
171adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
172adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
173adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Compares the {@code source} text to the {@code target} text according to
174adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * the collation rules, strength and decomposition mode for this
175adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * {@code RuleBasedCollator}. See the {@code Collator} class description
176adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * for an example of use.
177f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes     *
178adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param source
179adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            the source text.
180adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param target
181adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            the target text.
182adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @return an integer which may be a negative value, zero, or else a
183adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *         positive value depending on whether {@code source} is less than,
184adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *         equivalent to, or greater than {@code target}.
185adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
186adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    @Override
187adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public int compare(String source, String target) {
18886acc043d3334651ee26c65467d78d6cefedd397Kenny Root        if (source == null) {
18986acc043d3334651ee26c65467d78d6cefedd397Kenny Root            throw new NullPointerException("source == null");
19086acc043d3334651ee26c65467d78d6cefedd397Kenny Root        } else if (target == null) {
19186acc043d3334651ee26c65467d78d6cefedd397Kenny Root            throw new NullPointerException("target == null");
192adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
1934dfae7b3e2b56160646c17d93b32b3ff495c0053Elliott Hughes        return icuColl.compare(source, target);
194adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
195adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
196adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
197adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Returns the {@code CollationKey} for the given source text.
198f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes     *
199adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param source
200adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            the specified source text.
201adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @return the {@code CollationKey} for the given source text.
202adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
203adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    @Override
204adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public CollationKey getCollationKey(String source) {
205ebe438a0734f24ded1772778e5e712c820981234Elliott Hughes        return icuColl.getCollationKey(source);
206adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
207adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
208adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    @Override
209adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public int hashCode() {
210866e7ae17a3da81a02b0b144e0c9c2b3196d293aElliott Hughes        return icuColl.getRules().hashCode();
211adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
212adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
213adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
214adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Compares the specified object with this {@code RuleBasedCollator} and
215adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * indicates if they are equal. In order to be equal, {@code object} must be
216adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * an instance of {@code Collator} with the same collation rules and the
217adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * same attributes.
218f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes     *
219adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param obj
220adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            the object to compare with this object.
221adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @return {@code true} if the specified object is equal to this
222adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *         {@code RuleBasedCollator}; {@code false} otherwise.
223adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @see #hashCode
224adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
225adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    @Override
226adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public boolean equals(Object obj) {
227adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        if (!(obj instanceof Collator)) {
228adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            return false;
229adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
230adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        return super.equals(obj);
231adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
232adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project}
233