• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  * Copyright (c) 1997, 2006, 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>
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      * Collator strength value.  When set, only PRIMARY differences are
127      * considered significant during comparison. The assignment of strengths
128      * to language features is locale dependant. A common example is for
129      * different base letters ("a" vs "b") to be considered a PRIMARY difference.
130      *
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      *
142      * @see java.text.Collator#setStrength
143      * @see java.text.Collator#getStrength
144      */
145     public final static int SECONDARY = 1;
146     /**
147      * Collator strength value.  When set, only TERTIARY and above differences are
148      * considered significant during comparison. The assignment of strengths
149      * to language features is locale dependant. A common example is for
150      * case differences ("a" vs "A") to be considered a TERTIARY difference.
151      *
152      * @see java.text.Collator#setStrength
153      * @see java.text.Collator#getStrength
154      */
155     public final static int TERTIARY = 2;
156 
157     /**
158      * Collator strength value.  When set, all differences are
159      * considered significant during comparison. The assignment of strengths
160      * to language features is locale dependant. A common example is for control
161      * characters ("&#092;u0001" vs "&#092;u0002") to be considered equal at the
162      * PRIMARY, SECONDARY, and TERTIARY levels but different at the IDENTICAL
163      * level.  Additionally, differences between pre-composed accents such as
164      * "&#092;u00C0" (A-grave) and combining accents such as "A&#092;u0300"
165      * (A, combining-grave) will be considered significant at the IDENTICAL
166      * level if decomposition is set to NO_DECOMPOSITION.
167      */
168     public final static int IDENTICAL = 3;
169 
170     /**
171      * Decomposition mode value. With NO_DECOMPOSITION
172      * set, accented characters will not be decomposed for collation. This
173      * is the default setting and provides the fastest collation but
174      * will only produce correct results for languages that do not use accents.
175      *
176      * @see java.text.Collator#getDecomposition
177      * @see java.text.Collator#setDecomposition
178      */
179     public final static int NO_DECOMPOSITION = 0;
180 
181     /**
182      * Decomposition mode value. With CANONICAL_DECOMPOSITION
183      * set, characters that are canonical variants according to Unicode
184      * standard will be decomposed for collation. This should be used to get
185      * correct collation of accented characters.
186      * <p>
187      * CANONICAL_DECOMPOSITION corresponds to Normalization Form D as
188      * described in
189      * <a href="http://www.unicode.org/unicode/reports/tr15/tr15-23.html">Unicode
190      * Technical Report #15</a>.
191      *
192      * @see java.text.Collator#getDecomposition
193      * @see java.text.Collator#setDecomposition
194      */
195     public final static int CANONICAL_DECOMPOSITION = 1;
196 
197     /**
198      * Decomposition mode value. With FULL_DECOMPOSITION
199      * set, both Unicode canonical variants and Unicode compatibility variants
200      * will be decomposed for collation.  This causes not only accented
201      * characters to be collated, but also characters that have special formats
202      * to be collated with their norminal form. For example, the half-width and
203      * full-width ASCII and Katakana characters are then collated together.
204      * FULL_DECOMPOSITION is the most complete and therefore the slowest
205      * decomposition mode.
206      * <p>
207      * FULL_DECOMPOSITION corresponds to Normalization Form KD as
208      * described in
209      * <a href="http://www.unicode.org/unicode/reports/tr15/tr15-23.html">Unicode
210      * Technical Report #15</a>.
211      *
212      * @see java.text.Collator#getDecomposition
213      * @see java.text.Collator#setDecomposition
214      */
215     public final static int FULL_DECOMPOSITION = 2;
216 
217     android.icu.text.Collator icuColl;
218 
Collator(android.icu.text.Collator icuColl)219     Collator(android.icu.text.Collator icuColl) {
220         this.icuColl = icuColl;
221     }
222 
223     /**
224      * Default constructor.  This constructor is
225      * protected so subclasses can get access to it. Users typically create
226      * a Collator sub-class by calling the factory method getInstance.
227      *
228      * @see java.text.Collator#getInstance
229      */
Collator()230     protected Collator() {
231         icuColl = android.icu.text.RuleBasedCollator.getInstance(Locale.getDefault());
232     }
233 
234     /**
235      * Gets the Collator for the current default locale.
236      * The default locale is determined by java.util.Locale.getDefault.
237      *
238      * @return the Collator for the default locale.(for example, en_US)
239      * @see java.util.Locale#getDefault
240      */
getInstance()241     public static synchronized Collator getInstance() {
242         return getInstance(Locale.getDefault());
243     }
244 
245     /**
246      * Gets the Collator for the desired locale.
247      *
248      * @param desiredLocale the desired locale.
249      * @return the Collator for the desired locale.
250      * @see java.util.Locale
251      * @see java.util.ResourceBundle
252      */
getInstance(Locale desiredLocale)253     public static synchronized Collator getInstance(Locale desiredLocale) {
254         if (desiredLocale == null) {
255             throw new NullPointerException("locale == null");
256         }
257         return new RuleBasedCollator((android.icu.text.RuleBasedCollator)
258                 android.icu.text.Collator.getInstance(desiredLocale));
259     }
260 
261     /**
262      * Returns an array of all locales for which the
263      * <code>getInstance</code> methods of this class can return
264      * localized instances.
265      * The returned array represents the union of locales supported
266      * by the Java runtime and by installed
267      * {@link java.text.spi.CollatorProvider CollatorProvider} implementations.
268      * It must contain at least a Locale instance equal to
269      * {@link java.util.Locale#US Locale.US}.
270      *
271      * @return An array of locales for which localized
272      * <code>Collator</code> instances are available.
273      */
getAvailableLocales()274     public static synchronized Locale[] getAvailableLocales() {
275         return ICU.getAvailableCollatorLocales();
276     }
277 
278     /**
279      * Returns a new collator with the same decomposition mode and
280      * strength value as this collator.
281      *
282      * @return a shallow copy of this collator.
283      * @see java.lang.Cloneable
284      */
285     @Override
clone()286     public Object clone() {
287         try {
288             Collator clone = (Collator) super.clone();
289             clone.icuColl = (android.icu.text.Collator) icuColl.clone();
290             return clone;
291         } catch (CloneNotSupportedException e) {
292             throw new AssertionError(e);
293         }
294     }
295 
296     /**
297      * Compares the source string to the target string according to the
298      * collation rules for this Collator.  Returns an integer less than,
299      * equal to or greater than zero depending on whether the source String is
300      * less than, equal to or greater than the target string.  See the Collator
301      * class description for an example of use.
302      * <p>
303      * For a one time comparison, this method has the best performance. If a
304      * given String will be involved in multiple comparisons, CollationKey.compareTo
305      * has the best performance. See the Collator class description for an example
306      * using CollationKeys.
307      *
308      * @param source the source string.
309      * @param target the target string.
310      * @return Returns an integer value. Value is less than zero if source is less than
311      * target, value is zero if source and target are equal, value is greater than zero
312      * if source is greater than target.
313      * @see java.text.CollationKey
314      * @see java.text.Collator#getCollationKey
315      */
compare(String source, String target)316     public abstract int compare(String source, String target);
317 
318     /**
319      * Compares its two arguments for order.  Returns a negative integer,
320      * zero, or a positive integer as the first argument is less than, equal
321      * to, or greater than the second.
322      * <p>
323      * This implementation merely returns
324      * <code> compare((String)o1, (String)o2) </code>.
325      *
326      * @return a negative integer, zero, or a positive integer as the
327      * first argument is less than, equal to, or greater than the
328      * second.
329      * @throws ClassCastException the arguments cannot be cast to Strings.
330      * @see java.util.Comparator
331      * @since 1.2
332      */
compare(Object o1, Object o2)333     public int compare(Object o1, Object o2) {
334         return compare((String) o1, (String) o2);
335     }
336 
337     /**
338      * Transforms the String into a series of bits that can be compared bitwise
339      * to other CollationKeys. CollationKeys provide better performance than
340      * Collator.compare when Strings are involved in multiple comparisons.
341      * See the Collator class description for an example using CollationKeys.
342      *
343      * @param source the string to be transformed into a collation key.
344      * @return the CollationKey for the given String based on this Collator's collation
345      * rules. If the source String is null, a null CollationKey is returned.
346      * @see java.text.CollationKey
347      * @see java.text.Collator#compare
348      */
getCollationKey(String source)349     public abstract CollationKey getCollationKey(String source);
350 
351     /**
352      * Convenience method for comparing the equality of two strings based on
353      * this Collator's collation rules.
354      *
355      * @param source the source string to be compared with.
356      * @param target the target string to be compared with.
357      * @return true if the strings are equal according to the collation
358      * rules.  false, otherwise.
359      * @see java.text.Collator#compare
360      */
equals(String source, String target)361     public boolean equals(String source, String target) {
362         return (compare(source, target) == 0);
363     }
364 
365     /**
366      * Returns this Collator's strength property.  The strength property determines
367      * the minimum level of difference considered significant during comparison.
368      * See the Collator class description for an example of use.
369      *
370      * @return this Collator's current strength property.
371      * @see java.text.Collator#setStrength
372      * @see java.text.Collator#PRIMARY
373      * @see java.text.Collator#SECONDARY
374      * @see java.text.Collator#TERTIARY
375      * @see java.text.Collator#IDENTICAL
376      */
getStrength()377     public synchronized int getStrength() {
378         // The value for IDENTICAL in ICU differs from that used in this class.
379         int value = icuColl.getStrength();
380         return (value == android.icu.text.Collator.IDENTICAL) ? IDENTICAL : value;
381     }
382 
383     /**
384      * Sets this Collator's strength property.  The strength property determines
385      * the minimum level of difference considered significant during comparison.
386      * See the Collator class description for an example of use.
387      *
388      * @param newStrength the new strength value.
389      * @throws IllegalArgumentException If the new strength value is not one of
390      *                                  PRIMARY, SECONDARY, TERTIARY or IDENTICAL.
391      * @see Collator#getStrength
392      * @see Collator#PRIMARY
393      * @see Collator#SECONDARY
394      * @see Collator#TERTIARY
395      * @see Collator#IDENTICAL
396      */
setStrength(int newStrength)397     public synchronized void setStrength(int newStrength) {
398         // The ICU value for IDENTICAL differs from that defined in this class.
399         if (newStrength == IDENTICAL) {
400             newStrength = android.icu.text.Collator.IDENTICAL;
401         }
402         icuColl.setStrength(newStrength);
403     }
404 
405     /**
406      * Get the decomposition mode of this Collator. Decomposition mode
407      * determines how Unicode composed characters are handled. Adjusting
408      * decomposition mode allows the user to select between faster and more
409      * complete collation behavior.
410      * <p>The three values for decomposition mode are:
411      * <UL>
412      * <LI>NO_DECOMPOSITION,
413      * <LI>CANONICAL_DECOMPOSITION
414      * <LI>FULL_DECOMPOSITION.
415      * </UL>
416      * See the documentation for these three constants for a description
417      * of their meaning.
418      *
419      * @return the decomposition mode
420      * @see java.text.Collator#setDecomposition
421      * @see java.text.Collator#NO_DECOMPOSITION
422      * @see java.text.Collator#CANONICAL_DECOMPOSITION
423      * @see java.text.Collator#FULL_DECOMPOSITION
424      */
getDecomposition()425     public synchronized int getDecomposition() {
426         return decompositionMode_ICU_Java(icuColl.getDecomposition());
427     }
428 
429     /**
430      * Set the decomposition mode of this Collator. See getDecomposition
431      * for a description of decomposition mode.
432      *
433      * @param decompositionMode the new decomposition mode.
434      * @throws IllegalArgumentException If the given value is not a valid decomposition
435      *                                  mode.
436      * @see java.text.Collator#getDecomposition
437      * @see java.text.Collator#NO_DECOMPOSITION
438      * @see java.text.Collator#CANONICAL_DECOMPOSITION
439      * @see java.text.Collator#FULL_DECOMPOSITION
440      */
setDecomposition(int decompositionMode)441     public synchronized void setDecomposition(int decompositionMode) {
442         icuColl.setDecomposition(decompositionMode_Java_ICU(decompositionMode));
443     }
444 
decompositionMode_Java_ICU(int mode)445     private int decompositionMode_Java_ICU(int mode) {
446         switch (mode) {
447             case Collator.CANONICAL_DECOMPOSITION:
448                 return android.icu.text.Collator.CANONICAL_DECOMPOSITION;
449             case Collator.NO_DECOMPOSITION:
450                 return android.icu.text.Collator.NO_DECOMPOSITION;
451         }
452         throw new IllegalArgumentException("Bad mode: " + mode);
453     }
454 
decompositionMode_ICU_Java(int mode)455     private int decompositionMode_ICU_Java(int mode) {
456         int javaMode = mode;
457         switch (mode) {
458             case android.icu.text.Collator.NO_DECOMPOSITION:
459                 javaMode = Collator.NO_DECOMPOSITION;
460                 break;
461             case android.icu.text.Collator.CANONICAL_DECOMPOSITION:
462                 javaMode = Collator.CANONICAL_DECOMPOSITION;
463                 break;
464         }
465         return javaMode;
466     }
467 
468     /**
469      * Compares the equality of two Collators.
470      *
471      * @param that the Collator to be compared with this.
472      * @return true if this Collator is the same as that Collator;
473      * false otherwise.
474      */
equals(Object that)475     public boolean equals(Object that) {
476         if (this == that) return true;
477         if (that == null) return false;
478         if (getClass() != that.getClass()) return false;
479         Collator other = (Collator) that;
480         return icuColl == null ? other.icuColl == null : icuColl.equals(other.icuColl);
481     }
482 
483     /**
484      * Generates the hash code for this Collator.
485      */
hashCode()486     abstract public int hashCode();
487 }
488