1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This code is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License version 2 only, as
8  * published by the Free Software Foundation.  Oracle designates this
9  * particular file as subject to the "Classpath" exception as provided
10  * by Oracle in the LICENSE file that accompanied this code.
11  *
12  * This code is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15  * version 2 for more details (a copy is included in the LICENSE file that
16  * accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License version
19  * 2 along with this work; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21  *
22  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
23  * or visit www.oracle.com if you need additional information or have any
24  * questions.
25  */
26 
27 /*
28  * (C) Copyright Taligent, Inc. 1996-1998 -  All Rights Reserved
29  * (C) Copyright IBM Corp. 1996-1998 - All Rights Reserved
30  *
31  *   The original version of this source code and documentation is copyrighted
32  * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
33  * materials are provided under terms of a License Agreement between Taligent
34  * and Sun. This technology is protected by multiple US and International
35  * patents. This notice and attribution to Taligent may not be removed.
36  *   Taligent is a registered trademark of Taligent, Inc.
37  *
38  */
39 
40 package java.text;
41 
42 import java.util.Locale;
43 
44 import libcore.icu.ICU;
45 
46 /**
47  * The <code>Collator</code> class performs locale-sensitive
48  * <code>String</code> comparison. You use this class to build
49  * searching and sorting routines for natural language text.
50  *
51  * <p>
52  * <code>Collator</code> is an abstract base class. Subclasses
53  * implement specific collation strategies. One subclass,
54  * <code>RuleBasedCollator</code>, is currently provided with
55  * the Java Platform and is applicable to a wide set of languages. Other
56  * subclasses may be created to handle more specialized needs.
57  *
58  * <p>
59  * Like other locale-sensitive classes, you can use the static
60  * factory method, <code>getInstance</code>, to obtain the appropriate
61  * <code>Collator</code> object for a given locale. You will only need
62  * to look at the subclasses of <code>Collator</code> if you need
63  * to understand the details of a particular collation strategy or
64  * if you need to modify that strategy.
65  *
66  * <p>
67  * The following example shows how to compare two strings using
68  * the <code>Collator</code> for the default locale.
69  * <blockquote>
70  * <pre>{@code
71  * // Compare two strings in the default locale
72  * Collator myCollator = Collator.getInstance();
73  * if( myCollator.compare("abc", "ABC") < 0 )
74  *     System.out.println("abc is less than ABC");
75  * else
76  *     System.out.println("abc is greater than or equal to ABC");
77  * }</pre>
78  * </blockquote>
79  *
80  * <p>
81  * You can set a <code>Collator</code>'s <em>strength</em> property
82  * to determine the level of difference considered significant in
83  * comparisons. Four strengths are provided: <code>PRIMARY</code>,
84  * <code>SECONDARY</code>, <code>TERTIARY</code>, and <code>IDENTICAL</code>.
85  * The exact assignment of strengths to language features is
86  * locale dependant.  For example, in Czech, "e" and "f" are considered
87  * primary differences, while "e" and "&#283;" are secondary differences,
88  * "e" and "E" are tertiary differences and "e" and "e" are identical.
89  * The following shows how both case and accents could be ignored for
90  * US English.
91  * <blockquote>
92  * <pre>
93  * //Get the Collator for US English and set its strength to PRIMARY
94  * Collator usCollator = Collator.getInstance(Locale.US);
95  * usCollator.setStrength(Collator.PRIMARY);
96  * if( usCollator.compare("abc", "ABC") == 0 ) {
97  *     System.out.println("Strings are equivalent");
98  * }
99  * </pre>
100  * </blockquote>
101  * <p>
102  * For comparing <code>String</code>s exactly once, the <code>compare</code>
103  * method provides the best performance. When sorting a list of
104  * <code>String</code>s however, it is generally necessary to compare each
105  * <code>String</code> multiple times. In this case, <code>CollationKey</code>s
106  * provide better performance. The <code>CollationKey</code> class converts
107  * a <code>String</code> to a series of bits that can be compared bitwise
108  * against other <code>CollationKey</code>s. A <code>CollationKey</code> is
109  * created by a <code>Collator</code> object for a given <code>String</code>.
110  * <br>
111  * <strong>Note:</strong> <code>CollationKey</code>s from different
112  * <code>Collator</code>s can not be compared. See the class description
113  * for {@link CollationKey}
114  * for an example using <code>CollationKey</code>s.
115  *
116  * @see         RuleBasedCollator
117  * @see         CollationKey
118  * @see         CollationElementIterator
119  * @see         Locale
120  * @author      Helena Shih, Laura Werner, Richard Gillam
121  */
122 
123 public abstract class Collator
124     implements java.util.Comparator<Object>, Cloneable
125 {
126     /**
127      * Collator strength value.  When set, only PRIMARY differences are
128      * considered significant during comparison. The assignment of strengths
129      * to language features is locale dependant. A common example is for
130      * different base letters ("a" vs "b") to be considered a PRIMARY difference.
131      * @see java.text.Collator#setStrength
132      * @see java.text.Collator#getStrength
133      */
134     public final static int PRIMARY = 0;
135     /**
136      * Collator strength value.  When set, only SECONDARY and above differences are
137      * considered significant during comparison. The assignment of strengths
138      * to language features is locale dependant. A common example is for
139      * different accented forms of the same base letter ("a" vs "\u00E4") to be
140      * considered a SECONDARY difference.
141      * @see java.text.Collator#setStrength
142      * @see java.text.Collator#getStrength
143      */
144     public final static int SECONDARY = 1;
145     /**
146      * Collator strength value.  When set, only TERTIARY and above differences are
147      * considered significant during comparison. The assignment of strengths
148      * to language features is locale dependant. A common example is for
149      * case differences ("a" vs "A") to be considered a TERTIARY difference.
150      * @see java.text.Collator#setStrength
151      * @see java.text.Collator#getStrength
152      */
153     public final static int TERTIARY = 2;
154 
155     /**
156      * Collator strength value.  When set, all differences are
157      * considered significant during comparison. The assignment of strengths
158      * to language features is locale dependant. A common example is for control
159      * characters ("&#092;u0001" vs "&#092;u0002") to be considered equal at the
160      * PRIMARY, SECONDARY, and TERTIARY levels but different at the IDENTICAL
161      * level.  Additionally, differences between pre-composed accents such as
162      * "&#092;u00C0" (A-grave) and combining accents such as "A&#092;u0300"
163      * (A, combining-grave) will be considered significant at the IDENTICAL
164      * level if decomposition is set to NO_DECOMPOSITION.
165      */
166     public final static int IDENTICAL = 3;
167 
168     /**
169      * Decomposition mode value. With NO_DECOMPOSITION
170      * set, accented characters will not be decomposed for collation. This
171      * is the default setting and provides the fastest collation but
172      * will only produce correct results for languages that do not use accents.
173      * @see java.text.Collator#getDecomposition
174      * @see java.text.Collator#setDecomposition
175      */
176     public final static int NO_DECOMPOSITION = 0;
177 
178     /**
179      * Decomposition mode value. With CANONICAL_DECOMPOSITION
180      * set, characters that are canonical variants according to Unicode
181      * standard will be decomposed for collation. This should be used to get
182      * correct collation of accented characters.
183      * <p>
184      * CANONICAL_DECOMPOSITION corresponds to Normalization Form D as
185      * described in
186      * <a href="http://www.unicode.org/unicode/reports/tr15/tr15-23.html">Unicode
187      * Technical Report #15</a>.
188      * @see java.text.Collator#getDecomposition
189      * @see java.text.Collator#setDecomposition
190      */
191     public final static int CANONICAL_DECOMPOSITION = 1;
192 
193     /**
194      * Decomposition mode value. With FULL_DECOMPOSITION
195      * set, both Unicode canonical variants and Unicode compatibility variants
196      * will be decomposed for collation.  This causes not only accented
197      * characters to be collated, but also characters that have special formats
198      * to be collated with their norminal form. For example, the half-width and
199      * full-width ASCII and Katakana characters are then collated together.
200      * FULL_DECOMPOSITION is the most complete and therefore the slowest
201      * decomposition mode.
202      * <p>
203      * FULL_DECOMPOSITION corresponds to Normalization Form KD as
204      * described in
205      * <a href="http://www.unicode.org/unicode/reports/tr15/tr15-23.html">Unicode
206      * Technical Report #15</a>.
207      * @see java.text.Collator#getDecomposition
208      * @see java.text.Collator#setDecomposition
209      */
210     public final static int FULL_DECOMPOSITION = 2;
211 
212     /**
213      * Gets the Collator for the current default locale.
214      * The default locale is determined by java.util.Locale.getDefault.
215      * @return the Collator for the default locale.(for example, en_US)
216      * @see java.util.Locale#getDefault
217      */
getInstance()218     public static synchronized Collator getInstance() {
219         return getInstance(Locale.getDefault());
220     }
221 
222     /**
223      * Gets the Collator for the desired locale.
224      * @param desiredLocale the desired locale.
225      * @return the Collator for the desired locale.
226      * @see java.util.Locale
227      * @see java.util.ResourceBundle
228      */
getInstance(Locale desiredLocale)229     public static Collator getInstance(Locale desiredLocale) {
230         // BEGIN Android-changed: Switched to ICU.
231         synchronized(Collator.class) {
232             if (desiredLocale == null) {
233                 throw new NullPointerException("locale == null");
234             }
235             return new RuleBasedCollator((android.icu.text.RuleBasedCollator)
236                     android.icu.text.Collator.getInstance(desiredLocale));
237         }
238         // END Android-changed: Switched to ICU.
239     }
240 
241     /**
242      * Compares the source string to the target string according to the
243      * collation rules for this Collator.  Returns an integer less than,
244      * equal to or greater than zero depending on whether the source String is
245      * less than, equal to or greater than the target string.  See the Collator
246      * class description for an example of use.
247      * <p>
248      * For a one time comparison, this method has the best performance. If a
249      * given String will be involved in multiple comparisons, CollationKey.compareTo
250      * has the best performance. See the Collator class description for an example
251      * using CollationKeys.
252      * @param source the source string.
253      * @param target the target string.
254      * @return Returns an integer value. Value is less than zero if source is less than
255      * target, value is zero if source and target are equal, value is greater than zero
256      * if source is greater than target.
257      * @see java.text.CollationKey
258      * @see java.text.Collator#getCollationKey
259      */
compare(String source, String target)260     public abstract int compare(String source, String target);
261 
262     /**
263      * Compares its two arguments for order.  Returns a negative integer,
264      * zero, or a positive integer as the first argument is less than, equal
265      * to, or greater than the second.
266      * <p>
267      * This implementation merely returns
268      *  <code> compare((String)o1, (String)o2) </code>.
269      *
270      * @return a negative integer, zero, or a positive integer as the
271      *         first argument is less than, equal to, or greater than the
272      *         second.
273      * @exception ClassCastException the arguments cannot be cast to Strings.
274      * @see java.util.Comparator
275      * @since   1.2
276      */
277     @Override
compare(Object o1, Object o2)278     public int compare(Object o1, Object o2) {
279     return compare((String)o1, (String)o2);
280     }
281 
282     /**
283      * Transforms the String into a series of bits that can be compared bitwise
284      * to other CollationKeys. CollationKeys provide better performance than
285      * Collator.compare when Strings are involved in multiple comparisons.
286      * See the Collator class description for an example using CollationKeys.
287      * @param source the string to be transformed into a collation key.
288      * @return the CollationKey for the given String based on this Collator's collation
289      * rules. If the source String is null, a null CollationKey is returned.
290      * @see java.text.CollationKey
291      * @see java.text.Collator#compare
292      */
getCollationKey(String source)293     public abstract CollationKey getCollationKey(String source);
294 
295     /**
296      * Convenience method for comparing the equality of two strings based on
297      * this Collator's collation rules.
298      * @param source the source string to be compared with.
299      * @param target the target string to be compared with.
300      * @return true if the strings are equal according to the collation
301      * rules.  false, otherwise.
302      * @see java.text.Collator#compare
303      */
equals(String source, String target)304     public boolean equals(String source, String target)
305     {
306         // Android-changed: remove use of unnecessary EQUAL constant.
307         return (compare(source, target) == 0);
308     }
309 
310     /**
311      * Returns this Collator's strength property.  The strength property determines
312      * the minimum level of difference considered significant during comparison.
313      * See the Collator class description for an example of use.
314      * @return this Collator's current strength property.
315      * @see java.text.Collator#setStrength
316      * @see java.text.Collator#PRIMARY
317      * @see java.text.Collator#SECONDARY
318      * @see java.text.Collator#TERTIARY
319      * @see java.text.Collator#IDENTICAL
320      */
getStrength()321     public synchronized int getStrength()
322     {
323         // Android-changed: Switched to ICU.
324         // The value for IDENTICAL in ICU differs from that used in this class.
325         int value = icuColl.getStrength();
326         return (value == android.icu.text.Collator.IDENTICAL) ? IDENTICAL : value;
327     }
328 
329     /**
330      * Sets this Collator's strength property.  The strength property determines
331      * the minimum level of difference considered significant during comparison.
332      * See the Collator class description for an example of use.
333      * @param newStrength  the new strength value.
334      * @see java.text.Collator#getStrength
335      * @see java.text.Collator#PRIMARY
336      * @see java.text.Collator#SECONDARY
337      * @see java.text.Collator#TERTIARY
338      * @see java.text.Collator#IDENTICAL
339      * @exception  IllegalArgumentException If the new strength value is not one of
340      * PRIMARY, SECONDARY, TERTIARY or IDENTICAL.
341      */
setStrength(int newStrength)342     public synchronized void setStrength(int newStrength) {
343         // Android-changed: Switched to ICU.
344         // The ICU value for IDENTICAL differs from that defined in this class.
345         if (newStrength == IDENTICAL) {
346             newStrength = android.icu.text.Collator.IDENTICAL;
347         }
348         icuColl.setStrength(newStrength);
349     }
350 
351     /**
352      * Get the decomposition mode of this Collator. Decomposition mode
353      * determines how Unicode composed characters are handled. Adjusting
354      * decomposition mode allows the user to select between faster and more
355      * complete collation behavior.
356      * <p>The three values for decomposition mode are:
357      * <UL>
358      * <LI>NO_DECOMPOSITION,
359      * <LI>CANONICAL_DECOMPOSITION
360      * <LI>FULL_DECOMPOSITION.
361      * </UL>
362      * See the documentation for these three constants for a description
363      * of their meaning.
364      * @return the decomposition mode
365      * @see java.text.Collator#setDecomposition
366      * @see java.text.Collator#NO_DECOMPOSITION
367      * @see java.text.Collator#CANONICAL_DECOMPOSITION
368      * @see java.text.Collator#FULL_DECOMPOSITION
369      */
getDecomposition()370     public synchronized int getDecomposition()
371     {
372         // Android-changed: Switched to ICU.
373         return decompositionMode_ICU_Java(icuColl.getDecomposition());
374     }
375     /**
376      * Set the decomposition mode of this Collator. See getDecomposition
377      * for a description of decomposition mode.
378      * @param decompositionMode  the new decomposition mode.
379      * @see java.text.Collator#getDecomposition
380      * @see java.text.Collator#NO_DECOMPOSITION
381      * @see java.text.Collator#CANONICAL_DECOMPOSITION
382      * @see java.text.Collator#FULL_DECOMPOSITION
383      * @exception IllegalArgumentException If the given value is not a valid decomposition
384      * mode.
385      */
setDecomposition(int decompositionMode)386     public synchronized void setDecomposition(int decompositionMode) {
387         // Android-changed: Switched to ICU.
388         icuColl.setDecomposition(decompositionMode_Java_ICU(decompositionMode));
389     }
390 
391     // Android-changed: Removed javadoc references to CollatorProvider.
392     /**
393      * Returns an array of all locales for which the
394      * <code>getInstance</code> methods of this class can return
395      * localized instances.
396      *
397      * @return An array of locales for which localized
398      *         <code>Collator</code> instances are available.
399      */
getAvailableLocales()400     public static synchronized Locale[] getAvailableLocales() {
401         // Android-changed: Removed reference to CollatorProvider. Switched to ICU.
402         return android.icu.text.Collator.getAvailableLocales();
403     }
404 
405     // BEGIN Android-added: conversion method for decompositionMode constants.
decompositionMode_Java_ICU(int mode)406     private int decompositionMode_Java_ICU(int mode) {
407         switch (mode) {
408             case Collator.CANONICAL_DECOMPOSITION:
409                 return android.icu.text.Collator.CANONICAL_DECOMPOSITION;
410             case Collator.NO_DECOMPOSITION:
411                 return android.icu.text.Collator.NO_DECOMPOSITION;
412         }
413         throw new IllegalArgumentException("Bad mode: " + mode);
414     }
415 
decompositionMode_ICU_Java(int mode)416     private int decompositionMode_ICU_Java(int mode) {
417         int javaMode = mode;
418         switch (mode) {
419             case android.icu.text.Collator.NO_DECOMPOSITION:
420                 javaMode = Collator.NO_DECOMPOSITION;
421                 break;
422             case android.icu.text.Collator.CANONICAL_DECOMPOSITION:
423                 javaMode = Collator.CANONICAL_DECOMPOSITION;
424                 break;
425         }
426         return javaMode;
427     }
428     // END Android-added: conversion method for decompositionMode constants.
429 
430     // Android-changed: improve clone() documentation.
431     /**
432      * Returns a new collator with the same decomposition mode and
433      * strength value as this collator.
434      *
435      * @return a shallow copy of this collator.
436      * @see java.lang.Cloneable
437      */
438     @Override
clone()439     public Object clone()
440     {
441         try {
442             // Android-changed: Switched to ICU.
443             Collator clone = (Collator) super.clone();
444             clone.icuColl = (android.icu.text.Collator) icuColl.clone();
445             return clone;
446         } catch (CloneNotSupportedException e) {
447             throw new AssertionError(e);
448         }
449     }
450 
451     /**
452      * Compares the equality of two Collators.
453      * @param that the Collator to be compared with this.
454      * @return true if this Collator is the same as that Collator;
455      * false otherwise.
456      */
457     @Override
equals(Object that)458     public boolean equals(Object that)
459     {
460         if (this == that) {
461             return true;
462         }
463         if (that == null) {
464             return false;
465         }
466         if (getClass() != that.getClass()) {
467             return false;
468         }
469         Collator other = (Collator) that;
470         // Android-changed: Switched to ICU.
471         return icuColl == null ? other.icuColl == null : icuColl.equals(other.icuColl);
472     }
473 
474     /**
475      * Generates the hash code for this Collator.
476      */
477     @Override
hashCode()478     abstract public int hashCode();
479 
480     /**
481      * Default constructor.  This constructor is
482      * protected so subclasses can get access to it. Users typically create
483      * a Collator sub-class by calling the factory method getInstance.
484      * @see java.text.Collator#getInstance
485      */
Collator()486     protected Collator()
487     {
488         // Android-changed: Switched to ICU.
489         icuColl = android.icu.text.RuleBasedCollator.getInstance(Locale.getDefault());
490     }
491 
492     // Android-added: ICU Collator this delegates to.
493     android.icu.text.Collator icuColl;
494 
495     // Android-added: protected constructor taking a Collator.
Collator(android.icu.text.Collator icuColl)496     Collator(android.icu.text.Collator icuColl) {
497         this.icuColl = icuColl;
498     }
499 
500     // Android-removed: Fields and constants.
501 }
502