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