jdk/src/share/classes/java/util/Locale.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 1996-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, 1997 - 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * is copyrighted and owned by Taligent, Inc., a wholly-owned
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * subsidiary of IBM. These materials are provided under terms
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * of a License Agreement between Taligent and Sun. This technology
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * is protected by multiple US and International patents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * This notice and attribution to Taligent may not be removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Taligent is a registered trademark of Taligent, Inc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
package java.util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.text.MessageFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.util.concurrent.ConcurrentHashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.util.spi.LocaleNameProvider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.util.spi.LocaleServiceProvider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import sun.security.action.GetPropertyAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import sun.util.LocaleServiceProviderPool;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import sun.util.resources.LocaleData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import sun.util.resources.OpenListResourceBundle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * A <code>Locale</code> object represents a specific geographical, political,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * or cultural region. An operation that requires a <code>Locale</code> to perform
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * its task is called <em>locale-sensitive</em> and uses the <code>Locale</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * to tailor information for the user. For example, displaying a number
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * is a locale-sensitive operation--the number should be formatted
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * according to the customs/conventions of the user's native country,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * region, or culture.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * Create a <code>Locale</code> object using the constructors in this class:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * Locale(String language)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * Locale(String language, String country)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * Locale(String language, String country, String variant)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * The language argument is a valid <STRONG>ISO Language Code.</STRONG>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * These codes are the lower-case, two-letter codes as defined by ISO-639.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * You can find a full list of these codes at a number of sites, such as:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * <BR><a href ="http://www.loc.gov/standards/iso639-2/php/English_list.php">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * <code>http://www.loc.gov/standards/iso639-2/php/English_list.php</code></a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * The country argument is a valid <STRONG>ISO Country Code.</STRONG> These
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * codes are the upper-case, two-letter codes as defined by ISO-3166.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * You can find a full list of these codes at a number of sites, such as:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * <BR><a href="http://www.iso.ch/iso/en/prods-services/iso3166ma/02iso-3166-code-lists/list-en1.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * <code>http://www.iso.ch/iso/en/prods-services/iso3166ma/02iso-3166-code-lists/list-en1.html</code></a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * The variant argument is a vendor or browser-specific code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * For example, use WIN for Windows, MAC for Macintosh, and POSIX for POSIX.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * Where there are two variants, separate them with an underscore, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * put the most important one first. For example, a Traditional Spanish collation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * might construct a locale with parameters for language, country and variant as:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * "es", "ES", "Traditional_WIN".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * Because a <code>Locale</code> object is just an identifier for a region,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * no validity check is performed when you construct a <code>Locale</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * If you want to see whether particular resources are available for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * <code>Locale</code> you construct, you must query those resources. For
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * example, ask the <code>NumberFormat</code> for the locales it supports
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * using its <code>getAvailableLocales</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * <BR><STRONG>Note:</STRONG> When you ask for a resource for a particular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * locale, you get back the best available match, not necessarily
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * precisely what you asked for. For more information, look at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * {@link ResourceBundle}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * The <code>Locale</code> class provides a number of convenient constants
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * that you can use to create <code>Locale</code> objects for commonly used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * locales. For example, the following creates a <code>Locale</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * for the United States:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * Locale.US
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * Once you've created a <code>Locale</code> you can query it for information about
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * itself. Use <code>getCountry</code> to get the ISO Country Code and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * <code>getLanguage</code> to get the ISO Language Code. You can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * use <code>getDisplayCountry</code> to get the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * name of the country suitable for displaying to the user. Similarly,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * you can use <code>getDisplayLanguage</code> to get the name of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * the language suitable for displaying to the user. Interestingly,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * the <code>getDisplayXXX</code> methods are themselves locale-sensitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * and have two versions: one that uses the default locale and one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * that uses the locale specified as an argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * The Java Platform provides a number of classes that perform locale-sensitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * operations. For example, the <code>NumberFormat</code> class formats
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * numbers, currency, or percentages in a locale-sensitive manner. Classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * such as <code>NumberFormat</code> have a number of convenience methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * for creating a default object of that type. For example, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * <code>NumberFormat</code> class provides these three convenience methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 * for creating a default <code>NumberFormat</code> object:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 * NumberFormat.getInstance()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 * NumberFormat.getCurrencyInstance()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 * NumberFormat.getPercentInstance()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 * These methods have two variants; one with an explicit locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 * and one without; the latter using the default locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 * NumberFormat.getInstance(myLocale)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 * NumberFormat.getCurrencyInstance(myLocale)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 * NumberFormat.getPercentInstance(myLocale)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 * A <code>Locale</code> is the mechanism for identifying the kind of object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 * (<code>NumberFormat</code>) that you would like to get. The locale is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 * <STRONG>just</STRONG> a mechanism for identifying objects,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 * <STRONG>not</STRONG> a container for the objects themselves.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 * @see         ResourceBundle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 * @see         java.text.Format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 * @see         java.text.NumberFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 * @see         java.text.Collator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 * @author      Mark Davis
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 * @since       1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
public final class Locale implements Cloneable, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    // cache to store singleton Locales
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    private final static ConcurrentHashMap<String, Locale> cache =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        new ConcurrentHashMap<String, Locale>(32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /** Useful constant for language.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    static public final Locale ENGLISH = createSingleton("en__", "en", "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    /** Useful constant for language.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    static public final Locale FRENCH = createSingleton("fr__", "fr", "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    /** Useful constant for language.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    static public final Locale GERMAN = createSingleton("de__", "de", "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    /** Useful constant for language.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    static public final Locale ITALIAN = createSingleton("it__", "it", "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    /** Useful constant for language.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    static public final Locale JAPANESE = createSingleton("ja__", "ja", "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    /** Useful constant for language.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    static public final Locale KOREAN = createSingleton("ko__", "ko", "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    /** Useful constant for language.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    static public final Locale CHINESE = createSingleton("zh__", "zh", "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    /** Useful constant for language.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    static public final Locale SIMPLIFIED_CHINESE = createSingleton("zh_CN_", "zh", "CN");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    /** Useful constant for language.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    static public final Locale TRADITIONAL_CHINESE = createSingleton("zh_TW_", "zh", "TW");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    /** Useful constant for country.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    static public final Locale FRANCE = createSingleton("fr_FR_", "fr", "FR");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    /** Useful constant for country.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    static public final Locale GERMANY = createSingleton("de_DE_", "de", "DE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    /** Useful constant for country.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    static public final Locale ITALY = createSingleton("it_IT_", "it", "IT");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    /** Useful constant for country.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    static public final Locale JAPAN = createSingleton("ja_JP_", "ja", "JP");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    /** Useful constant for country.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    static public final Locale KOREA = createSingleton("ko_KR_", "ko", "KR");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    /** Useful constant for country.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    static public final Locale CHINA = SIMPLIFIED_CHINESE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    /** Useful constant for country.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    static public final Locale PRC = SIMPLIFIED_CHINESE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    /** Useful constant for country.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    static public final Locale TAIWAN = TRADITIONAL_CHINESE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    /** Useful constant for country.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    static public final Locale UK = createSingleton("en_GB_", "en", "GB");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    /** Useful constant for country.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    static public final Locale US = createSingleton("en_US_", "en", "US");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    /** Useful constant for country.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    static public final Locale CANADA = createSingleton("en_CA_", "en", "CA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    /** Useful constant for country.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    static public final Locale CANADA_FRENCH = createSingleton("fr_CA_", "fr", "CA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * Useful constant for the root locale.  The root locale is the locale whose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * language, country, and variant are empty ("") strings.  This is regarded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * as the base locale of all locales, and is used as the language/country
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * neutral locale for the locale sensitive operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    static public final Locale ROOT = createSingleton("__", "", "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    /** serialization ID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    static final long serialVersionUID = 9149081749638150636L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * Display types for retrieving localized names from the name providers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    private static final int DISPLAY_LANGUAGE = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    private static final int DISPLAY_COUNTRY  = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    private static final int DISPLAY_VARIANT  = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * Construct a locale from language, country, variant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * NOTE:  ISO 639 is not a stable standard; some of the language codes it defines
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * (specifically iw, ji, and in) have changed.  This constructor accepts both the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * old codes (iw, ji, and in) and the new codes (he, yi, and id), but all other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * API on Locale will return only the OLD codes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * @param language lowercase two-letter ISO-639 code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * @param country uppercase two-letter ISO-3166 code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * @param variant vendor and browser specific code. See class description.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * @exception NullPointerException thrown if any argument is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    public Locale(String language, String country, String variant) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        this.language = convertOldISOCodes(language);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        this.country = toUpperCase(country).intern();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        this.variant = variant.intern();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * Construct a locale from language, country.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * NOTE:  ISO 639 is not a stable standard; some of the language codes it defines
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * (specifically iw, ji, and in) have changed.  This constructor accepts both the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * old codes (iw, ji, and in) and the new codes (he, yi, and id), but all other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * API on Locale will return only the OLD codes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * @param language lowercase two-letter ISO-639 code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * @param country uppercase two-letter ISO-3166 code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * @exception NullPointerException thrown if either argument is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    public Locale(String language, String country) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        this(language, country, "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * Construct a locale from a language code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * NOTE:  ISO 639 is not a stable standard; some of the language codes it defines
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * (specifically iw, ji, and in) have changed.  This constructor accepts both the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * old codes (iw, ji, and in) and the new codes (he, yi, and id), but all other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * API on Locale will return only the OLD codes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * @param language lowercase two-letter ISO-639 code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * @exception NullPointerException thrown if argument is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    public Locale(String language) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        this(language, "", "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * Constructs a <code>Locale</code> using <code>language</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * and <code>country</code>.  This constructor assumes that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * <code>language</code> and <code>contry</code> are interned and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * it is invoked by createSingleton only. (flag is just for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * avoiding the conflict with the public constructors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    private Locale(String language, String country, boolean flag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        this.language = language;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        this.country = country;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        this.variant = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * Creates a <code>Locale</code> instance with the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * <code>language</code> and <code>counry</code> and puts the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * instance under the given <code>key</code> in the cache. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * method must be called only when initializing the Locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * constants.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    private static Locale createSingleton(String key, String language, String country) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        Locale locale = new Locale(language, country, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        cache.put(key, locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        return locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * Returns a <code>Locale</code> constructed from the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * <code>language</code>, <code>country</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * <code>variant</code>. If the same <code>Locale</code> instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * is available in the cache, then that instance is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * returned. Otherwise, a new <code>Locale</code> instance is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * created and cached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * @param language lowercase two-letter ISO-639 code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * @param country uppercase two-letter ISO-3166 code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * @param variant vendor and browser specific code. See class description.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * @return the <code>Locale</code> instance requested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * @exception NullPointerException if any argument is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    static Locale getInstance(String language, String country, String variant) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        if (language== null || country == null || variant == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        sb.append(language).append('_').append(country).append('_').append(variant);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        String key = sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        Locale locale = cache.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        if (locale == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            locale = new Locale(language, country, variant);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            Locale l = cache.putIfAbsent(key, locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            if (l != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                locale = l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        return locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * Gets the current value of the default locale for this instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * of the Java Virtual Machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * The Java Virtual Machine sets the default locale during startup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * based on the host environment. It is used by many locale-sensitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * methods if no locale is explicitly specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * It can be changed using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * {@link #setDefault(java.util.Locale) setDefault} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * @return the default locale for this instance of the Java Virtual Machine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    public static Locale getDefault() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        // do not synchronize this method - see 4071298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        // it's OK if more than one default locale happens to be created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        if (defaultLocale == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            String language, region, country, variant;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            language = AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                new GetPropertyAction("user.language", "en"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            // for compatibility, check for old user.region property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            region = AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                new GetPropertyAction("user.region"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            if (region != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                // region can be of form country, country_variant, or _variant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                int i = region.indexOf('_');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                if (i >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                    country = region.substring(0, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                    variant = region.substring(i + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                    country = region;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                    variant = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                country = AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                    new GetPropertyAction("user.country", ""));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                variant = AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                    new GetPropertyAction("user.variant", ""));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            defaultLocale = getInstance(language, country, variant);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        return defaultLocale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * Sets the default locale for this instance of the Java Virtual Machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * This does not affect the host locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * If there is a security manager, its <code>checkPermission</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * method is called with a <code>PropertyPermission("user.language", "write")</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * permission before the default locale is changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * The Java Virtual Machine sets the default locale during startup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * based on the host environment. It is used by many locale-sensitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * methods if no locale is explicitly specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * Since changing the default locale may affect many different areas
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * of functionality, this method should only be used if the caller
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * is prepared to reinitialize locale-sensitive code running
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * within the same Java Virtual Machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * @throws SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     *        if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     *        <code>checkPermission</code> method doesn't allow the operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * @throws NullPointerException if <code>newLocale</code> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * @param newLocale the new default locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * @see SecurityManager#checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * @see java.util.PropertyPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    public static synchronized void setDefault(Locale newLocale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        if (newLocale == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            throw new NullPointerException("Can't set default locale to NULL");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        if (sm != null) sm.checkPermission(new PropertyPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                        ("user.language", "write"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            defaultLocale = newLocale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * Returns an array of all installed locales.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * The returned array represents the union of locales supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * by the Java runtime environment and by installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * {@link java.util.spi.LocaleServiceProvider LocaleServiceProvider}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * implementations.  It must contain at least a <code>Locale</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * instance equal to {@link java.util.Locale#US Locale.US}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * @return An array of installed locales.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    public static Locale[] getAvailableLocales() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        return LocaleServiceProviderPool.getAllAvailableLocales();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * Returns a list of all 2-letter country codes defined in ISO 3166.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * Can be used to create Locales.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    public static String[] getISOCountries() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        if (isoCountries == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            isoCountries = getISO2Table(LocaleISOData.isoCountryTable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        String[] result = new String[isoCountries.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        System.arraycopy(isoCountries, 0, result, 0, isoCountries.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * Returns a list of all 2-letter language codes defined in ISO 639.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * Can be used to create Locales.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * [NOTE:  ISO 639 is not a stable standard-- some languages' codes have changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * The list this function returns includes both the new and the old codes for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * languages whose codes have changed.]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    public static String[] getISOLanguages() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        if (isoLanguages == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            isoLanguages = getISO2Table(LocaleISOData.isoLanguageTable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        String[] result = new String[isoLanguages.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        System.arraycopy(isoLanguages, 0, result, 0, isoLanguages.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    private static final String[] getISO2Table(String table) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        int len = table.length() / 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        String[] isoTable = new String[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        for (int i = 0, j = 0; i < len; i++, j += 5) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            isoTable[i] = table.substring(j, j + 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        return isoTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * Returns the language code for this locale, which will either be the empty string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * or a lowercase ISO 639 code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * <p>NOTE:  ISO 639 is not a stable standard-- some languages' codes have changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * Locale's constructor recognizes both the new and the old codes for the languages
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * whose codes have changed, but this function always returns the old code.  If you
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * want to check for a specific language whose code has changed, don't do <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * if (locale.getLanguage().equals("he"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     *    ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * </pre>Instead, do<pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * if (locale.getLanguage().equals(new Locale("he", "", "").getLanguage()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     *    ...</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * @see #getDisplayLanguage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    public String getLanguage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        return language;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * Returns the country/region code for this locale, which will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * either be the empty string or an uppercase ISO 3166 2-letter code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * @see #getDisplayCountry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    public String getCountry() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        return country;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * Returns the variant code for this locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * @see #getDisplayVariant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    public String getVariant() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        return variant;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * Getter for the programmatic name of the entire locale,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * with the language, country and variant separated by underbars.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * Language is always lower case, and country is always upper case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * If the language is missing, the string will begin with an underbar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * If both the language and country fields are missing, this function
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * will return the empty string, even if the variant field is filled in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * (you can't have a locale with just a variant-- the variant must accompany
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * a valid language or country code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * Examples: "en", "de_DE", "_GB", "en_US_WIN", "de__POSIX", "fr__MAC"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * @see #getDisplayName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    public final String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        boolean l = language.length() != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        boolean c = country.length() != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        boolean v = variant.length() != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        StringBuilder result = new StringBuilder(language);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        if (c||(l&&v)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            result.append('_').append(country); // This may just append '_'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        if (v&&(l||c)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            result.append('_').append(variant);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        return result.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * Returns a three-letter abbreviation for this locale's language.  If the locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * doesn't specify a language, this will be the empty string.  Otherwise, this will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * be a lowercase ISO 639-2/T language code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * The ISO 639-2 language codes can be found on-line at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * <a href="http://www.loc.gov/standards/iso639-2/englangn.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * <code>http://www.loc.gov/standards/iso639-2/englangn.html</code>.</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * @exception MissingResourceException Throws MissingResourceException if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * three-letter language abbreviation is not available for this locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    public String getISO3Language() throws MissingResourceException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        String language3 = getISO3Code(language, LocaleISOData.isoLanguageTable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        if (language3 == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            throw new MissingResourceException("Couldn't find 3-letter language code for "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                    + language, "FormatData_" + toString(), "ShortLanguage");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        return language3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * Returns a three-letter abbreviation for this locale's country.  If the locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * doesn't specify a country, this will be the empty string.  Otherwise, this will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * be an uppercase ISO 3166 3-letter country code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * The ISO 3166-2 country codes can be found on-line at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * <a href="http://www.davros.org/misc/iso3166.txt">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * <code>http://www.davros.org/misc/iso3166.txt</code>.</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * @exception MissingResourceException Throws MissingResourceException if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * three-letter country abbreviation is not available for this locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    public String getISO3Country() throws MissingResourceException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        String country3 = getISO3Code(country, LocaleISOData.isoCountryTable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        if (country3 == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            throw new MissingResourceException("Couldn't find 3-letter country code for "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                    + country, "FormatData_" + toString(), "ShortCountry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        return country3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    private static final String getISO3Code(String iso2Code, String table) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        int codeLength = iso2Code.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        if (codeLength == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        int tableLength = table.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        int index = tableLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        if (codeLength == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            char c1 = iso2Code.charAt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            char c2 = iso2Code.charAt(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            for (index = 0; index < tableLength; index += 5) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                if (table.charAt(index) == c1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                    && table.charAt(index + 1) == c2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        return index < tableLength ? table.substring(index + 2, index + 5) : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * Returns a name for the locale's language that is appropriate for display to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * user.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * If possible, the name returned will be localized for the default locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * For example, if the locale is fr_FR and the default locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * is en_US, getDisplayLanguage() will return "French"; if the locale is en_US and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * the default locale is fr_FR, getDisplayLanguage() will return "anglais".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * If the name returned cannot be localized for the default locale,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * (say, we don't have a Japanese name for Croatian),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * this function falls back on the English name, and uses the ISO code as a last-resort
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * value.  If the locale doesn't specify a language, this function returns the empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    public final String getDisplayLanguage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        return getDisplayLanguage(getDefault());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * Returns a name for the locale's language that is appropriate for display to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * user.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * If possible, the name returned will be localized according to inLocale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * For example, if the locale is fr_FR and inLocale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * is en_US, getDisplayLanguage() will return "French"; if the locale is en_US and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * inLocale is fr_FR, getDisplayLanguage() will return "anglais".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * If the name returned cannot be localized according to inLocale,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * (say, we don't have a Japanese name for Croatian),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * this function falls back on the English name, and finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * on the ISO code as a last-resort value.  If the locale doesn't specify a language,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * this function returns the empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * @exception NullPointerException if <code>inLocale</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
    public String getDisplayLanguage(Locale inLocale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        return getDisplayString(language, inLocale, DISPLAY_LANGUAGE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * Returns a name for the locale's country that is appropriate for display to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * user.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * If possible, the name returned will be localized for the default locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * For example, if the locale is fr_FR and the default locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * is en_US, getDisplayCountry() will return "France"; if the locale is en_US and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * the default locale is fr_FR, getDisplayCountry() will return "Etats-Unis".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * If the name returned cannot be localized for the default locale,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * (say, we don't have a Japanese name for Croatia),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * this function falls back on the English name, and uses the ISO code as a last-resort
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * value.  If the locale doesn't specify a country, this function returns the empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    public final String getDisplayCountry() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        return getDisplayCountry(getDefault());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * Returns a name for the locale's country that is appropriate for display to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * user.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * If possible, the name returned will be localized according to inLocale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * For example, if the locale is fr_FR and inLocale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * is en_US, getDisplayCountry() will return "France"; if the locale is en_US and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * inLocale is fr_FR, getDisplayCountry() will return "Etats-Unis".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * If the name returned cannot be localized according to inLocale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * (say, we don't have a Japanese name for Croatia),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * this function falls back on the English name, and finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * on the ISO code as a last-resort value.  If the locale doesn't specify a country,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * this function returns the empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     * @exception NullPointerException if <code>inLocale</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
    public String getDisplayCountry(Locale inLocale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        return getDisplayString(country, inLocale, DISPLAY_COUNTRY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
    private String getDisplayString(String code, Locale inLocale, int type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        if (code.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        if (inLocale == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            OpenListResourceBundle bundle = LocaleData.getLocaleNames(inLocale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            String key = (type == DISPLAY_VARIANT ? "%%"+code : code);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            String result = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            // Check whether a provider can provide an implementation that's closer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            // to the requested locale than what the Java runtime itself can provide.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            LocaleServiceProviderPool pool =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                LocaleServiceProviderPool.getPool(LocaleNameProvider.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            if (pool.hasProviders()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                result = pool.getLocalizedObject(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                                    LocaleNameGetter.INSTANCE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                                    inLocale, bundle, key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                                    type, code);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            if (result == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                result = bundle.getString(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            if (result != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            // just fall through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        return code;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     * Returns a name for the locale's variant code that is appropriate for display to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * user.  If possible, the name will be localized for the default locale.  If the locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     * doesn't specify a variant code, this function returns the empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
    public final String getDisplayVariant() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        return getDisplayVariant(getDefault());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     * Returns a name for the locale's variant code that is appropriate for display to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * user.  If possible, the name will be localized for inLocale.  If the locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     * doesn't specify a variant code, this function returns the empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * @exception NullPointerException if <code>inLocale</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
    public String getDisplayVariant(Locale inLocale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        if (variant.length() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
            return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        OpenListResourceBundle bundle = LocaleData.getLocaleNames(inLocale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        String names[] = getDisplayVariantArray(bundle, inLocale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        // Get the localized patterns for formatting a list, and use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        // them to format the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        String listPattern = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        String listCompositionPattern = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            listPattern = bundle.getString("ListPattern");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            listCompositionPattern = bundle.getString("ListCompositionPattern");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        } catch (MissingResourceException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        return formatList(names, listPattern, listCompositionPattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * Returns a name for the locale that is appropriate for display to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     * user.  This will be the values returned by getDisplayLanguage(), getDisplayCountry(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * and getDisplayVariant() assembled into a single string.  The display name will have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     * one of the following forms:<p><blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * language (country, variant)<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * language (country)<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * language (variant)<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     * country (variant)<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     * language<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     * country<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     * variant<p></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * depending on which fields are specified in the locale.  If the language, country,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     * and variant fields are all empty, this function returns the empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
    public final String getDisplayName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        return getDisplayName(getDefault());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     * Returns a name for the locale that is appropriate for display to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     * user.  This will be the values returned by getDisplayLanguage(), getDisplayCountry(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     * and getDisplayVariant() assembled into a single string.  The display name will have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     * one of the following forms:<p><blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     * language (country, variant)<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     * language (country)<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     * language (variant)<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * country (variant)<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * language<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * country<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * variant<p></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     * depending on which fields are specified in the locale.  If the language, country,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * and variant fields are all empty, this function returns the empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     * @exception NullPointerException if <code>inLocale</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
    public String getDisplayName(Locale inLocale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        OpenListResourceBundle bundle = LocaleData.getLocaleNames(inLocale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        String languageName = getDisplayLanguage(inLocale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        String countryName = getDisplayCountry(inLocale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        String[] variantNames = getDisplayVariantArray(bundle, inLocale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        // Get the localized patterns for formatting a display name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        String displayNamePattern = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        String listPattern = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        String listCompositionPattern = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
            displayNamePattern = bundle.getString("DisplayNamePattern");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
            listPattern = bundle.getString("ListPattern");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            listCompositionPattern = bundle.getString("ListCompositionPattern");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        } catch (MissingResourceException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        // The display name consists of a main name, followed by qualifiers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        // Typically, the format is "MainName (Qualifier, Qualifier)" but this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        // depends on what pattern is stored in the display locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        String   mainName       = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        String[] qualifierNames = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        // The main name is the language, or if there is no language, the country.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        // If there is neither language nor country (an anomalous situation) then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        // the display name is simply the variant's display name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        if (languageName.length() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
            mainName = languageName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
            if (countryName.length() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                qualifierNames = new String[variantNames.length + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                System.arraycopy(variantNames, 0, qualifierNames, 1, variantNames.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                qualifierNames[0] = countryName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
            else qualifierNames = variantNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        else if (countryName.length() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
            mainName = countryName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
            qualifierNames = variantNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
            return formatList(variantNames, listPattern, listCompositionPattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        // Create an array whose first element is the number of remaining
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        // elements.  This serves as a selector into a ChoiceFormat pattern from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        // the resource.  The second and third elements are the main name and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        // the qualifier; if there are no qualifiers, the third element is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
        // unused by the format pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        Object[] displayNames = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
            new Integer(qualifierNames.length != 0 ? 2 : 1),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
            mainName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
            // We could also just call formatList() and have it handle the empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
            // list case, but this is more efficient, and we want it to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
            // efficient since all the language-only locales will not have any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
            // qualifiers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
            qualifierNames.length != 0 ? formatList(qualifierNames, listPattern, listCompositionPattern) : null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
        if (displayNamePattern != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
            return new MessageFormat(displayNamePattern).format(displayNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
            // If we cannot get the message format pattern, then we use a simple
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
            // hard-coded pattern.  This should not occur in practice unless the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
            // installation is missing some core files (FormatData etc.).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
            StringBuilder result = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
            result.append((String)displayNames[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
            if (displayNames.length > 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
                result.append(" (");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
                result.append((String)displayNames[2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                result.append(')');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
            return result.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     * Overrides Cloneable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
    public Object clone()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
            Locale that = (Locale)super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
            return that;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        } catch (CloneNotSupportedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
            throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     * Override hashCode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     * Since Locales are often used in hashtables, caches the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     * for speed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        int hc = hashCodeValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        if (hc == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
            hc = (language.hashCode() << 8) ^ country.hashCode() ^ (variant.hashCode() << 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
            hashCodeValue = hc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        return hc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
    // Overrides
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     * Returns true if this Locale is equal to another object.  A Locale is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     * deemed equal to another Locale with identical language, country,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     * and variant, and unequal to all other objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     * @return true if this Locale is equal to the specified object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
        if (this == obj)                      // quick check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
        if (!(obj instanceof Locale))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
        Locale other = (Locale) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
        return language == other.language
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
            && country == other.country
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
            && variant == other.variant;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
    // ================= privates =====================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
    // XXX instance and class variables. For now keep these separate, since it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
    // faster to match. Later, make into single string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     * @see #getLanguage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
    private final String language;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     * @see #getCountry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
    private final String country;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     * @see #getVariant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
    private final String variant;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     * Placeholder for the object's hash code.  Always -1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
    private volatile int hashcode = -1;        // lazy evaluate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     * Calculated hashcode to fix 4518797.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
    private transient volatile int hashCodeValue = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
    private static Locale defaultLocale = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
     * Return an array of the display names of the variant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     * @param bundle the ResourceBundle to use to get the display names
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     * @return an array of display names, possible of zero length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
    private String[] getDisplayVariantArray(OpenListResourceBundle bundle, Locale inLocale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        // Split the variant name into tokens separated by '_'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
        StringTokenizer tokenizer = new StringTokenizer(variant, "_");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        String[] names = new String[tokenizer.countTokens()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        // For each variant token, lookup the display name.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
        // not found, use the variant name itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
        for (int i=0; i<names.length; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
            names[i] = getDisplayString(tokenizer.nextToken(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
                                inLocale, DISPLAY_VARIANT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        return names;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
     * Format a list using given pattern strings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
     * If either of the patterns is null, then a the list is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
     * formatted by concatenation with the delimiter ','.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
     * @param stringList the list of strings to be formatted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
     * @param listPattern should create a MessageFormat taking 0-3 arguments
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
     * and formatting them into a list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
     * @param listCompositionPattern should take 2 arguments
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     * and is used by composeList.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     * @return a string representing the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
    private static String formatList(String[] stringList, String listPattern, String listCompositionPattern) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
        // If we have no list patterns, compose the list in a simple,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
        // non-localized way.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        if (listPattern == null || listCompositionPattern == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
            StringBuffer result = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
            for (int i=0; i<stringList.length; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
                if (i>0) result.append(',');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
                result.append(stringList[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
            return result.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        // Compose the list down to three elements if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
        if (stringList.length > 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
            MessageFormat format = new MessageFormat(listCompositionPattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
            stringList = composeList(format, stringList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        // Rebuild the argument list with the list length as the first element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
        Object[] args = new Object[stringList.length + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
        System.arraycopy(stringList, 0, args, 1, stringList.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
        args[0] = new Integer(stringList.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
        // Format it using the pattern in the resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
        MessageFormat format = new MessageFormat(listPattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
        return format.format(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
     * Given a list of strings, return a list shortened to three elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
     * Shorten it by applying the given format to the first two elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
     * recursively.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
     * @param format a format which takes two arguments
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
     * @param list a list of strings
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
     * @return if the list is three elements or shorter, the same list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
     * otherwise, a new list of three elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
    private static String[] composeList(MessageFormat format, String[] list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
        if (list.length <= 3) return list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
        // Use the given format to compose the first two elements into one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
        String[] listItems = { list[0], list[1] };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
        String newItem = format.format(listItems);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
        // Form a new list one element shorter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        String[] newList = new String[list.length-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
        System.arraycopy(list, 2, newList, 1, newList.length-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
        newList[0] = newItem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
        // Recurse
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
        return composeList(format, newList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     * Replace the deserialized Locale object with a newly
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     * created object. Newer language codes are replaced with older ISO
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     * codes. The country and variant codes are replaced with internalized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     * String copies.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
    private Object readResolve() throws java.io.ObjectStreamException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        return getInstance(language, country, variant);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
    private static volatile String[] isoLanguages = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
    private static volatile String[] isoCountries = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
     * Locale needs its own, locale insensitive version of toLowerCase to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
     * avoid circularity problems between Locale and String.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
     * The most straightforward algorithm is used. Look at optimizations later.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
    private String toLowerCase(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
        char[] buf = new char[str.length()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
        for (int i = 0; i < buf.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
            buf[i] = Character.toLowerCase(str.charAt(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
        return new String( buf );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
     * Locale needs its own, locale insensitive version of toUpperCase to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
     * avoid circularity problems between Locale and String.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
     * The most straightforward algorithm is used. Look at optimizations later.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
    private String toUpperCase(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
        char[] buf = new char[str.length()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
        for (int i = 0; i < buf.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
            buf[i] = Character.toUpperCase(str.charAt(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
        return new String( buf );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
    private String convertOldISOCodes(String language) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
        // we accept both the old and the new ISO codes for the languages whose ISO
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
        // codes have changed, but we always store the OLD code, for backward compatibility
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
        language = toLowerCase(language).intern();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
        if (language == "he") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
            return "iw";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
        } else if (language == "yi") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
            return "ji";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        } else if (language == "id") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
            return "in";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
            return language;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
     * Obtains a localized locale names from a LocaleNameProvider
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
     * implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
    private static class LocaleNameGetter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
        implements LocaleServiceProviderPool.LocalizedObjectGetter<LocaleNameProvider, String> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
        private static final LocaleNameGetter INSTANCE = new LocaleNameGetter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
        public String getObject(LocaleNameProvider localeNameProvider,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
                                Locale locale,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
                                String key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
                                Object... params) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
            assert params.length == 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
            int type = (Integer)params[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
            String code = (String)params[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
            switch(type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
            case DISPLAY_LANGUAGE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
                return localeNameProvider.getDisplayLanguage(code, locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
            case DISPLAY_COUNTRY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
                return localeNameProvider.getDisplayCountry(code, locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
            case DISPLAY_VARIANT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
                return localeNameProvider.getDisplayVariant(code, locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
                assert false; // shouldn't happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
}