RuleBasedCollator.java revision 1c13b45d19d7c9ab363c5984c2a9f329a67ef6f4
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 *
271c13b45d19d7c9ab363c5984c2a9f329a67ef6f4Elliott Hughes * <p>Most of the time, you create a {@link Collator} instance for a {@link Locale}
281c13b45d19d7c9ab363c5984c2a9f329a67ef6f4Elliott 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        }
1041c13b45d19d7c9ab363c5984c2a9f329a67ef6f4Elliott Hughes        // icu4c is fine with empty rules, but the RI isn't.
105a7d4139bed693bf6037bf5f7f49a24b077102adcElliott Hughes        if (rules.isEmpty()) {
106a7d4139bed693bf6037bf5f7f49a24b077102adcElliott Hughes            throw new ParseException("empty rules", 0);
107a7d4139bed693bf6037bf5f7f49a24b077102adcElliott Hughes        }
108adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        try {
1094dfae7b3e2b56160646c17d93b32b3ff495c0053Elliott Hughes            icuColl = new RuleBasedCollatorICU(rules);
110adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        } catch (Exception e) {
111adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            if (e instanceof ParseException) {
112adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                throw (ParseException) e;
113adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            }
114adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            /*
115adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project             * -1 means it's not a ParseException. Maybe IOException thrown when
11603c0a8e681c776fdba0389ab8593282139afc6d6Elliott Hughes             * an error occurred while reading internal data.
117adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project             */
118adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            throw new ParseException(e.getMessage(), -1);
119adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
120adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
121adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
122adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
123adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Obtains a {@code CollationElementIterator} for the given
124adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * {@code CharacterIterator}. The source iterator's integrity will be
125adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * preserved since a new copy will be created for use.
126f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes     *
127adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param source
128adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            the source character iterator.
129adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @return a {@code CollationElementIterator} for {@code source}.
130adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
131a7d4139bed693bf6037bf5f7f49a24b077102adcElliott Hughes    public CollationElementIterator getCollationElementIterator(CharacterIterator source) {
132adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        if (source == null) {
13386acc043d3334651ee26c65467d78d6cefedd397Kenny Root            throw new NullPointerException("source == null");
134adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
135866e7ae17a3da81a02b0b144e0c9c2b3196d293aElliott Hughes        return new CollationElementIterator(icuColl.getCollationElementIterator(source));
136adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
137adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
138adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
139adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Obtains a {@code CollationElementIterator} for the given string.
140f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes     *
141adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param source
142adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            the source string.
143adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @return the {@code CollationElementIterator} for {@code source}.
144adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
145adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public CollationElementIterator getCollationElementIterator(String source) {
146adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        if (source == null) {
14786acc043d3334651ee26c65467d78d6cefedd397Kenny Root            throw new NullPointerException("source == null");
148adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
149866e7ae17a3da81a02b0b144e0c9c2b3196d293aElliott Hughes        return new CollationElementIterator(icuColl.getCollationElementIterator(source));
150adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
151adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
152adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
153adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Returns the collation rules of this collator. These {@code rules} can be
1545d287a9d9768195f53e244414b465ec4a6f72625The Android Open Source Project     * fed into the {@code RuleBasedCollator(String)} constructor.
155adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *
1561c13b45d19d7c9ab363c5984c2a9f329a67ef6f4Elliott Hughes     * <p>The returned string will be empty unless you constructed the instance yourself.
1571c13b45d19d7c9ab363c5984c2a9f329a67ef6f4Elliott Hughes     * The string forms of the collation rules are omitted to save space on the device.
158adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
159adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public String getRules() {
160866e7ae17a3da81a02b0b144e0c9c2b3196d293aElliott Hughes        return icuColl.getRules();
161adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
162adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
163adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
164adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Returns a new collator with the same collation rules, decomposition mode and
165adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * strength value as this collator.
166f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes     *
167adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @return a shallow copy of this collator.
168adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @see java.lang.Cloneable
169adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
170adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    @Override
171adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public Object clone() {
172adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        RuleBasedCollator clone = (RuleBasedCollator) super.clone();
173adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        return clone;
174adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
175adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
176adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
177adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Compares the {@code source} text to the {@code target} text according to
178adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * the collation rules, strength and decomposition mode for this
179adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * {@code RuleBasedCollator}. See the {@code Collator} class description
180adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * for an example of use.
181f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes     *
182adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param source
183adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            the source text.
184adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param target
185adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            the target text.
186adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @return an integer which may be a negative value, zero, or else a
187adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *         positive value depending on whether {@code source} is less than,
188adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *         equivalent to, or greater than {@code target}.
189adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
190adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    @Override
191adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public int compare(String source, String target) {
19286acc043d3334651ee26c65467d78d6cefedd397Kenny Root        if (source == null) {
19386acc043d3334651ee26c65467d78d6cefedd397Kenny Root            throw new NullPointerException("source == null");
19486acc043d3334651ee26c65467d78d6cefedd397Kenny Root        } else if (target == null) {
19586acc043d3334651ee26c65467d78d6cefedd397Kenny Root            throw new NullPointerException("target == null");
196adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
1974dfae7b3e2b56160646c17d93b32b3ff495c0053Elliott Hughes        return icuColl.compare(source, target);
198adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
199adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
200adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
201adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Returns the {@code CollationKey} for the given source text.
202f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes     *
203adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param source
204adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            the specified source text.
205adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @return the {@code CollationKey} for the given source text.
206adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
207adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    @Override
208adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public CollationKey getCollationKey(String source) {
209ebe438a0734f24ded1772778e5e712c820981234Elliott Hughes        return icuColl.getCollationKey(source);
210adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
211adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
212adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    @Override
213adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public int hashCode() {
214866e7ae17a3da81a02b0b144e0c9c2b3196d293aElliott Hughes        return icuColl.getRules().hashCode();
215adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
216adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
217adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    /**
218adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Compares the specified object with this {@code RuleBasedCollator} and
219adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * indicates if they are equal. In order to be equal, {@code object} must be
220adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * an instance of {@code Collator} with the same collation rules and the
221adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * same attributes.
222f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes     *
223adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @param obj
224adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *            the object to compare with this object.
225adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @return {@code true} if the specified object is equal to this
226adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     *         {@code RuleBasedCollator}; {@code false} otherwise.
227adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * @see #hashCode
228adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
229adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    @Override
230adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public boolean equals(Object obj) {
231adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        if (!(obj instanceof Collator)) {
232adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            return false;
233adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
234adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        return super.equals(obj);
235adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
236adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project}
237