rt/emul/compact/src/main/java/java/util/Locale.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 28 Sep 2013 02:18:42 +0200
branchjdk7-b147
changeset 1318 1af7f8903b62
child 1320 e49c4c2c3737
permissions -rw-r--r--
String.format needs Locale
jtulach@1318
     1
/*
jtulach@1318
     2
 * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
jtulach@1318
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jtulach@1318
     4
 *
jtulach@1318
     5
 * This code is free software; you can redistribute it and/or modify it
jtulach@1318
     6
 * under the terms of the GNU General Public License version 2 only, as
jtulach@1318
     7
 * published by the Free Software Foundation.  Oracle designates this
jtulach@1318
     8
 * particular file as subject to the "Classpath" exception as provided
jtulach@1318
     9
 * by Oracle in the LICENSE file that accompanied this code.
jtulach@1318
    10
 *
jtulach@1318
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
jtulach@1318
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jtulach@1318
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jtulach@1318
    14
 * version 2 for more details (a copy is included in the LICENSE file that
jtulach@1318
    15
 * accompanied this code).
jtulach@1318
    16
 *
jtulach@1318
    17
 * You should have received a copy of the GNU General Public License version
jtulach@1318
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
jtulach@1318
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jtulach@1318
    20
 *
jtulach@1318
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jtulach@1318
    22
 * or visit www.oracle.com if you need additional information or have any
jtulach@1318
    23
 * questions.
jtulach@1318
    24
 */
jtulach@1318
    25
jtulach@1318
    26
/*
jtulach@1318
    27
 * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
jtulach@1318
    28
 * (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved
jtulach@1318
    29
 *
jtulach@1318
    30
 * The original version of this source code and documentation
jtulach@1318
    31
 * is copyrighted and owned by Taligent, Inc., a wholly-owned
jtulach@1318
    32
 * subsidiary of IBM. These materials are provided under terms
jtulach@1318
    33
 * of a License Agreement between Taligent and Sun. This technology
jtulach@1318
    34
 * is protected by multiple US and International patents.
jtulach@1318
    35
 *
jtulach@1318
    36
 * This notice and attribution to Taligent may not be removed.
jtulach@1318
    37
 * Taligent is a registered trademark of Taligent, Inc.
jtulach@1318
    38
 *
jtulach@1318
    39
 */
jtulach@1318
    40
jtulach@1318
    41
package java.util;
jtulach@1318
    42
jtulach@1318
    43
import java.io.IOException;
jtulach@1318
    44
import java.io.ObjectInputStream;
jtulach@1318
    45
import java.io.ObjectOutputStream;
jtulach@1318
    46
import java.io.ObjectStreamField;
jtulach@1318
    47
import java.io.Serializable;
jtulach@1318
    48
import java.security.AccessController;
jtulach@1318
    49
import java.text.MessageFormat;
jtulach@1318
    50
import java.util.spi.LocaleNameProvider;
jtulach@1318
    51
jtulach@1318
    52
import sun.security.action.GetPropertyAction;
jtulach@1318
    53
import sun.util.LocaleServiceProviderPool;
jtulach@1318
    54
import sun.util.locale.BaseLocale;
jtulach@1318
    55
import sun.util.locale.InternalLocaleBuilder;
jtulach@1318
    56
import sun.util.locale.LanguageTag;
jtulach@1318
    57
import sun.util.locale.LocaleExtensions;
jtulach@1318
    58
import sun.util.locale.LocaleObjectCache;
jtulach@1318
    59
import sun.util.locale.LocaleSyntaxException;
jtulach@1318
    60
import sun.util.locale.LocaleUtils;
jtulach@1318
    61
import sun.util.locale.ParseStatus;
jtulach@1318
    62
import sun.util.locale.UnicodeLocaleExtension;
jtulach@1318
    63
import sun.util.resources.LocaleData;
jtulach@1318
    64
import sun.util.resources.OpenListResourceBundle;
jtulach@1318
    65
jtulach@1318
    66
/**
jtulach@1318
    67
 * A <code>Locale</code> object represents a specific geographical, political,
jtulach@1318
    68
 * or cultural region. An operation that requires a <code>Locale</code> to perform
jtulach@1318
    69
 * its task is called <em>locale-sensitive</em> and uses the <code>Locale</code>
jtulach@1318
    70
 * to tailor information for the user. For example, displaying a number
jtulach@1318
    71
 * is a locale-sensitive operation&mdash; the number should be formatted
jtulach@1318
    72
 * according to the customs and conventions of the user's native country,
jtulach@1318
    73
 * region, or culture.
jtulach@1318
    74
 *
jtulach@1318
    75
 * <p> The <code>Locale</code> class implements identifiers
jtulach@1318
    76
 * interchangeable with BCP 47 (IETF BCP 47, "Tags for Identifying
jtulach@1318
    77
 * Languages"), with support for the LDML (UTS#35, "Unicode Locale
jtulach@1318
    78
 * Data Markup Language") BCP 47-compatible extensions for locale data
jtulach@1318
    79
 * exchange.
jtulach@1318
    80
 *
jtulach@1318
    81
 * <p> A <code>Locale</code> object logically consists of the fields
jtulach@1318
    82
 * described below.
jtulach@1318
    83
 *
jtulach@1318
    84
 * <dl>
jtulach@1318
    85
 *   <dt><a name="def_language"/><b>language</b></dt>
jtulach@1318
    86
 *
jtulach@1318
    87
 *   <dd>ISO 639 alpha-2 or alpha-3 language code, or registered
jtulach@1318
    88
 *   language subtags up to 8 alpha letters (for future enhancements).
jtulach@1318
    89
 *   When a language has both an alpha-2 code and an alpha-3 code, the
jtulach@1318
    90
 *   alpha-2 code must be used.  You can find a full list of valid
jtulach@1318
    91
 *   language codes in the IANA Language Subtag Registry (search for
jtulach@1318
    92
 *   "Type: language").  The language field is case insensitive, but
jtulach@1318
    93
 *   <code>Locale</code> always canonicalizes to lower case.</dd><br>
jtulach@1318
    94
 *
jtulach@1318
    95
 *   <dd>Well-formed language values have the form
jtulach@1318
    96
 *   <code>[a-zA-Z]{2,8}</code>.  Note that this is not the the full
jtulach@1318
    97
 *   BCP47 language production, since it excludes extlang.  They are
jtulach@1318
    98
 *   not needed since modern three-letter language codes replace
jtulach@1318
    99
 *   them.</dd><br>
jtulach@1318
   100
 *
jtulach@1318
   101
 *   <dd>Example: "en" (English), "ja" (Japanese), "kok" (Konkani)</dd><br>
jtulach@1318
   102
 *
jtulach@1318
   103
 *   <dt><a name="def_script"/><b>script</b></dt>
jtulach@1318
   104
 *
jtulach@1318
   105
 *   <dd>ISO 15924 alpha-4 script code.  You can find a full list of
jtulach@1318
   106
 *   valid script codes in the IANA Language Subtag Registry (search
jtulach@1318
   107
 *   for "Type: script").  The script field is case insensitive, but
jtulach@1318
   108
 *   <code>Locale</code> always canonicalizes to title case (the first
jtulach@1318
   109
 *   letter is upper case and the rest of the letters are lower
jtulach@1318
   110
 *   case).</dd><br>
jtulach@1318
   111
 *
jtulach@1318
   112
 *   <dd>Well-formed script values have the form
jtulach@1318
   113
 *   <code>[a-zA-Z]{4}</code></dd><br>
jtulach@1318
   114
 *
jtulach@1318
   115
 *   <dd>Example: "Latn" (Latin), "Cyrl" (Cyrillic)</dd><br>
jtulach@1318
   116
 *
jtulach@1318
   117
 *   <dt><a name="def_region"/><b>country (region)</b></dt>
jtulach@1318
   118
 *
jtulach@1318
   119
 *   <dd>ISO 3166 alpha-2 country code or UN M.49 numeric-3 area code.
jtulach@1318
   120
 *   You can find a full list of valid country and region codes in the
jtulach@1318
   121
 *   IANA Language Subtag Registry (search for "Type: region").  The
jtulach@1318
   122
 *   country (region) field is case insensitive, but
jtulach@1318
   123
 *   <code>Locale</code> always canonicalizes to upper case.</dd><br>
jtulach@1318
   124
 *
jtulach@1318
   125
 *   <dd>Well-formed country/region values have
jtulach@1318
   126
 *   the form <code>[a-zA-Z]{2} | [0-9]{3}</code></dd><br>
jtulach@1318
   127
 *
jtulach@1318
   128
 *   <dd>Example: "US" (United States), "FR" (France), "029"
jtulach@1318
   129
 *   (Caribbean)</dd><br>
jtulach@1318
   130
 *
jtulach@1318
   131
 *   <dt><a name="def_variant"/><b>variant</b></dt>
jtulach@1318
   132
 *
jtulach@1318
   133
 *   <dd>Any arbitrary value used to indicate a variation of a
jtulach@1318
   134
 *   <code>Locale</code>.  Where there are two or more variant values
jtulach@1318
   135
 *   each indicating its own semantics, these values should be ordered
jtulach@1318
   136
 *   by importance, with most important first, separated by
jtulach@1318
   137
 *   underscore('_').  The variant field is case sensitive.</dd><br>
jtulach@1318
   138
 *
jtulach@1318
   139
 *   <dd>Note: IETF BCP 47 places syntactic restrictions on variant
jtulach@1318
   140
 *   subtags.  Also BCP 47 subtags are strictly used to indicate
jtulach@1318
   141
 *   additional variations that define a language or its dialects that
jtulach@1318
   142
 *   are not covered by any combinations of language, script and
jtulach@1318
   143
 *   region subtags.  You can find a full list of valid variant codes
jtulach@1318
   144
 *   in the IANA Language Subtag Registry (search for "Type: variant").
jtulach@1318
   145
 *
jtulach@1318
   146
 *   <p>However, the variant field in <code>Locale</code> has
jtulach@1318
   147
 *   historically been used for any kind of variation, not just
jtulach@1318
   148
 *   language variations.  For example, some supported variants
jtulach@1318
   149
 *   available in Java SE Runtime Environments indicate alternative
jtulach@1318
   150
 *   cultural behaviors such as calendar type or number script.  In
jtulach@1318
   151
 *   BCP 47 this kind of information, which does not identify the
jtulach@1318
   152
 *   language, is supported by extension subtags or private use
jtulach@1318
   153
 *   subtags.</dd><br>
jtulach@1318
   154
 *
jtulach@1318
   155
 *   <dd>Well-formed variant values have the form <code>SUBTAG
jtulach@1318
   156
 *   (('_'|'-') SUBTAG)*</code> where <code>SUBTAG =
jtulach@1318
   157
 *   [0-9][0-9a-zA-Z]{3} | [0-9a-zA-Z]{5,8}</code>. (Note: BCP 47 only
jtulach@1318
   158
 *   uses hyphen ('-') as a delimiter, this is more lenient).</dd><br>
jtulach@1318
   159
 *
jtulach@1318
   160
 *   <dd>Example: "polyton" (Polytonic Greek), "POSIX"</dd><br>
jtulach@1318
   161
 *
jtulach@1318
   162
 *   <dt><a name="def_extensions"/><b>extensions</b></dt>
jtulach@1318
   163
 *
jtulach@1318
   164
 *   <dd>A map from single character keys to string values, indicating
jtulach@1318
   165
 *   extensions apart from language identification.  The extensions in
jtulach@1318
   166
 *   <code>Locale</code> implement the semantics and syntax of BCP 47
jtulach@1318
   167
 *   extension subtags and private use subtags. The extensions are
jtulach@1318
   168
 *   case insensitive, but <code>Locale</code> canonicalizes all
jtulach@1318
   169
 *   extension keys and values to lower case. Note that extensions
jtulach@1318
   170
 *   cannot have empty values.</dd><br>
jtulach@1318
   171
 *
jtulach@1318
   172
 *   <dd>Well-formed keys are single characters from the set
jtulach@1318
   173
 *   <code>[0-9a-zA-Z]</code>.  Well-formed values have the form
jtulach@1318
   174
 *   <code>SUBTAG ('-' SUBTAG)*</code> where for the key 'x'
jtulach@1318
   175
 *   <code>SUBTAG = [0-9a-zA-Z]{1,8}</code> and for other keys
jtulach@1318
   176
 *   <code>SUBTAG = [0-9a-zA-Z]{2,8}</code> (that is, 'x' allows
jtulach@1318
   177
 *   single-character subtags).</dd><br>
jtulach@1318
   178
 *
jtulach@1318
   179
 *   <dd>Example: key="u"/value="ca-japanese" (Japanese Calendar),
jtulach@1318
   180
 *   key="x"/value="java-1-7"</dd>
jtulach@1318
   181
 * </dl>
jtulach@1318
   182
 *
jtulach@1318
   183
 * <b>Note:</b> Although BCP 47 requires field values to be registered
jtulach@1318
   184
 * in the IANA Language Subtag Registry, the <code>Locale</code> class
jtulach@1318
   185
 * does not provide any validation features.  The <code>Builder</code>
jtulach@1318
   186
 * only checks if an individual field satisfies the syntactic
jtulach@1318
   187
 * requirement (is well-formed), but does not validate the value
jtulach@1318
   188
 * itself.  See {@link Builder} for details.
jtulach@1318
   189
 *
jtulach@1318
   190
 * <h4><a name="def_locale_extension">Unicode locale/language extension</h4>
jtulach@1318
   191
 *
jtulach@1318
   192
 * <p>UTS#35, "Unicode Locale Data Markup Language" defines optional
jtulach@1318
   193
 * attributes and keywords to override or refine the default behavior
jtulach@1318
   194
 * associated with a locale.  A keyword is represented by a pair of
jtulach@1318
   195
 * key and type.  For example, "nu-thai" indicates that Thai local
jtulach@1318
   196
 * digits (value:"thai") should be used for formatting numbers
jtulach@1318
   197
 * (key:"nu").
jtulach@1318
   198
 *
jtulach@1318
   199
 * <p>The keywords are mapped to a BCP 47 extension value using the
jtulach@1318
   200
 * extension key 'u' ({@link #UNICODE_LOCALE_EXTENSION}).  The above
jtulach@1318
   201
 * example, "nu-thai", becomes the extension "u-nu-thai".code
jtulach@1318
   202
 *
jtulach@1318
   203
 * <p>Thus, when a <code>Locale</code> object contains Unicode locale
jtulach@1318
   204
 * attributes and keywords,
jtulach@1318
   205
 * <code>getExtension(UNICODE_LOCALE_EXTENSION)</code> will return a
jtulach@1318
   206
 * String representing this information, for example, "nu-thai".  The
jtulach@1318
   207
 * <code>Locale</code> class also provides {@link
jtulach@1318
   208
 * #getUnicodeLocaleAttributes}, {@link #getUnicodeLocaleKeys}, and
jtulach@1318
   209
 * {@link #getUnicodeLocaleType} which allow you to access Unicode
jtulach@1318
   210
 * locale attributes and key/type pairs directly.  When represented as
jtulach@1318
   211
 * a string, the Unicode Locale Extension lists attributes
jtulach@1318
   212
 * alphabetically, followed by key/type sequences with keys listed
jtulach@1318
   213
 * alphabetically (the order of subtags comprising a key's type is
jtulach@1318
   214
 * fixed when the type is defined)
jtulach@1318
   215
 *
jtulach@1318
   216
 * <p>A well-formed locale key has the form
jtulach@1318
   217
 * <code>[0-9a-zA-Z]{2}</code>.  A well-formed locale type has the
jtulach@1318
   218
 * form <code>"" | [0-9a-zA-Z]{3,8} ('-' [0-9a-zA-Z]{3,8})*</code> (it
jtulach@1318
   219
 * can be empty, or a series of subtags 3-8 alphanums in length).  A
jtulach@1318
   220
 * well-formed locale attribute has the form
jtulach@1318
   221
 * <code>[0-9a-zA-Z]{3,8}</code> (it is a single subtag with the same
jtulach@1318
   222
 * form as a locale type subtag).
jtulach@1318
   223
 *
jtulach@1318
   224
 * <p>The Unicode locale extension specifies optional behavior in
jtulach@1318
   225
 * locale-sensitive services.  Although the LDML specification defines
jtulach@1318
   226
 * various keys and values, actual locale-sensitive service
jtulach@1318
   227
 * implementations in a Java Runtime Environment might not support any
jtulach@1318
   228
 * particular Unicode locale attributes or key/type pairs.
jtulach@1318
   229
 *
jtulach@1318
   230
 * <h4>Creating a Locale</h4>
jtulach@1318
   231
 *
jtulach@1318
   232
 * <p>There are several different ways to create a <code>Locale</code>
jtulach@1318
   233
 * object.
jtulach@1318
   234
 *
jtulach@1318
   235
 * <h5>Builder</h5>
jtulach@1318
   236
 *
jtulach@1318
   237
 * <p>Using {@link Builder} you can construct a <code>Locale</code> object
jtulach@1318
   238
 * that conforms to BCP 47 syntax.
jtulach@1318
   239
 *
jtulach@1318
   240
 * <h5>Constructors</h5>
jtulach@1318
   241
 *
jtulach@1318
   242
 * <p>The <code>Locale</code> class provides three constructors:
jtulach@1318
   243
 * <blockquote>
jtulach@1318
   244
 * <pre>
jtulach@1318
   245
 *     {@link #Locale(String language)}
jtulach@1318
   246
 *     {@link #Locale(String language, String country)}
jtulach@1318
   247
 *     {@link #Locale(String language, String country, String variant)}
jtulach@1318
   248
 * </pre>
jtulach@1318
   249
 * </blockquote>
jtulach@1318
   250
 * These constructors allow you to create a <code>Locale</code> object
jtulach@1318
   251
 * with language, country and variant, but you cannot specify
jtulach@1318
   252
 * script or extensions.
jtulach@1318
   253
 *
jtulach@1318
   254
 * <h5>Factory Methods</h5>
jtulach@1318
   255
 *
jtulach@1318
   256
 * <p>The method {@link #forLanguageTag} creates a <code>Locale</code>
jtulach@1318
   257
 * object for a well-formed BCP 47 language tag.
jtulach@1318
   258
 *
jtulach@1318
   259
 * <h5>Locale Constants</h5>
jtulach@1318
   260
 *
jtulach@1318
   261
 * <p>The <code>Locale</code> class provides a number of convenient constants
jtulach@1318
   262
 * that you can use to create <code>Locale</code> objects for commonly used
jtulach@1318
   263
 * locales. For example, the following creates a <code>Locale</code> object
jtulach@1318
   264
 * for the United States:
jtulach@1318
   265
 * <blockquote>
jtulach@1318
   266
 * <pre>
jtulach@1318
   267
 *     Locale.US
jtulach@1318
   268
 * </pre>
jtulach@1318
   269
 * </blockquote>
jtulach@1318
   270
 *
jtulach@1318
   271
 * <h4>Use of Locale</h4>
jtulach@1318
   272
 *
jtulach@1318
   273
 * <p>Once you've created a <code>Locale</code> you can query it for information
jtulach@1318
   274
 * about itself. Use <code>getCountry</code> to get the country (or region)
jtulach@1318
   275
 * code and <code>getLanguage</code> to get the language code.
jtulach@1318
   276
 * You can use <code>getDisplayCountry</code> to get the
jtulach@1318
   277
 * name of the country suitable for displaying to the user. Similarly,
jtulach@1318
   278
 * you can use <code>getDisplayLanguage</code> to get the name of
jtulach@1318
   279
 * the language suitable for displaying to the user. Interestingly,
jtulach@1318
   280
 * the <code>getDisplayXXX</code> methods are themselves locale-sensitive
jtulach@1318
   281
 * and have two versions: one that uses the default locale and one
jtulach@1318
   282
 * that uses the locale specified as an argument.
jtulach@1318
   283
 *
jtulach@1318
   284
 * <p>The Java Platform provides a number of classes that perform locale-sensitive
jtulach@1318
   285
 * operations. For example, the <code>NumberFormat</code> class formats
jtulach@1318
   286
 * numbers, currency, and percentages in a locale-sensitive manner. Classes
jtulach@1318
   287
 * such as <code>NumberFormat</code> have several convenience methods
jtulach@1318
   288
 * for creating a default object of that type. For example, the
jtulach@1318
   289
 * <code>NumberFormat</code> class provides these three convenience methods
jtulach@1318
   290
 * for creating a default <code>NumberFormat</code> object:
jtulach@1318
   291
 * <blockquote>
jtulach@1318
   292
 * <pre>
jtulach@1318
   293
 *     NumberFormat.getInstance()
jtulach@1318
   294
 *     NumberFormat.getCurrencyInstance()
jtulach@1318
   295
 *     NumberFormat.getPercentInstance()
jtulach@1318
   296
 * </pre>
jtulach@1318
   297
 * </blockquote>
jtulach@1318
   298
 * Each of these methods has two variants; one with an explicit locale
jtulach@1318
   299
 * and one without; the latter uses the default locale:
jtulach@1318
   300
 * <blockquote>
jtulach@1318
   301
 * <pre>
jtulach@1318
   302
 *     NumberFormat.getInstance(myLocale)
jtulach@1318
   303
 *     NumberFormat.getCurrencyInstance(myLocale)
jtulach@1318
   304
 *     NumberFormat.getPercentInstance(myLocale)
jtulach@1318
   305
 * </pre>
jtulach@1318
   306
 * </blockquote>
jtulach@1318
   307
 * A <code>Locale</code> is the mechanism for identifying the kind of object
jtulach@1318
   308
 * (<code>NumberFormat</code>) that you would like to get. The locale is
jtulach@1318
   309
 * <STRONG>just</STRONG> a mechanism for identifying objects,
jtulach@1318
   310
 * <STRONG>not</STRONG> a container for the objects themselves.
jtulach@1318
   311
 *
jtulach@1318
   312
 * <h4>Compatibility</h4>
jtulach@1318
   313
 *
jtulach@1318
   314
 * <p>In order to maintain compatibility with existing usage, Locale's
jtulach@1318
   315
 * constructors retain their behavior prior to the Java Runtime
jtulach@1318
   316
 * Environment version 1.7.  The same is largely true for the
jtulach@1318
   317
 * <code>toString</code> method. Thus Locale objects can continue to
jtulach@1318
   318
 * be used as they were. In particular, clients who parse the output
jtulach@1318
   319
 * of toString into language, country, and variant fields can continue
jtulach@1318
   320
 * to do so (although this is strongly discouraged), although the
jtulach@1318
   321
 * variant field will have additional information in it if script or
jtulach@1318
   322
 * extensions are present.
jtulach@1318
   323
 *
jtulach@1318
   324
 * <p>In addition, BCP 47 imposes syntax restrictions that are not
jtulach@1318
   325
 * imposed by Locale's constructors. This means that conversions
jtulach@1318
   326
 * between some Locales and BCP 47 language tags cannot be made without
jtulach@1318
   327
 * losing information. Thus <code>toLanguageTag</code> cannot
jtulach@1318
   328
 * represent the state of locales whose language, country, or variant
jtulach@1318
   329
 * do not conform to BCP 47.
jtulach@1318
   330
 *
jtulach@1318
   331
 * <p>Because of these issues, it is recommended that clients migrate
jtulach@1318
   332
 * away from constructing non-conforming locales and use the
jtulach@1318
   333
 * <code>forLanguageTag</code> and <code>Locale.Builder</code> APIs instead.
jtulach@1318
   334
 * Clients desiring a string representation of the complete locale can
jtulach@1318
   335
 * then always rely on <code>toLanguageTag</code> for this purpose.
jtulach@1318
   336
 *
jtulach@1318
   337
 * <h5><a name="special_cases_constructor"/>Special cases</h5>
jtulach@1318
   338
 *
jtulach@1318
   339
 * <p>For compatibility reasons, two
jtulach@1318
   340
 * non-conforming locales are treated as special cases.  These are
jtulach@1318
   341
 * <b><tt>ja_JP_JP</tt></b> and <b><tt>th_TH_TH</tt></b>. These are ill-formed
jtulach@1318
   342
 * in BCP 47 since the variants are too short. To ease migration to BCP 47,
jtulach@1318
   343
 * these are treated specially during construction.  These two cases (and only
jtulach@1318
   344
 * these) cause a constructor to generate an extension, all other values behave
jtulach@1318
   345
 * exactly as they did prior to Java 7.
jtulach@1318
   346
 *
jtulach@1318
   347
 * <p>Java has used <tt>ja_JP_JP</tt> to represent Japanese as used in
jtulach@1318
   348
 * Japan together with the Japanese Imperial calendar. This is now
jtulach@1318
   349
 * representable using a Unicode locale extension, by specifying the
jtulach@1318
   350
 * Unicode locale key <tt>ca</tt> (for "calendar") and type
jtulach@1318
   351
 * <tt>japanese</tt>. When the Locale constructor is called with the
jtulach@1318
   352
 * arguments "ja", "JP", "JP", the extension "u-ca-japanese" is
jtulach@1318
   353
 * automatically added.
jtulach@1318
   354
 *
jtulach@1318
   355
 * <p>Java has used <tt>th_TH_TH</tt> to represent Thai as used in
jtulach@1318
   356
 * Thailand together with Thai digits. This is also now representable using
jtulach@1318
   357
 * a Unicode locale extension, by specifying the Unicode locale key
jtulach@1318
   358
 * <tt>nu</tt> (for "number") and value <tt>thai</tt>. When the Locale
jtulach@1318
   359
 * constructor is called with the arguments "th", "TH", "TH", the
jtulach@1318
   360
 * extension "u-nu-thai" is automatically added.
jtulach@1318
   361
 *
jtulach@1318
   362
 * <h5>Serialization</h5>
jtulach@1318
   363
 *
jtulach@1318
   364
 * <p>During serialization, writeObject writes all fields to the output
jtulach@1318
   365
 * stream, including extensions.
jtulach@1318
   366
 *
jtulach@1318
   367
 * <p>During deserialization, readResolve adds extensions as described
jtulach@1318
   368
 * in <a href="#special_cases_constructor">Special Cases</a>, only
jtulach@1318
   369
 * for the two cases th_TH_TH and ja_JP_JP.
jtulach@1318
   370
 *
jtulach@1318
   371
 * <h5>Legacy language codes</h5>
jtulach@1318
   372
 *
jtulach@1318
   373
 * <p>Locale's constructor has always converted three language codes to
jtulach@1318
   374
 * their earlier, obsoleted forms: <tt>he</tt> maps to <tt>iw</tt>,
jtulach@1318
   375
 * <tt>yi</tt> maps to <tt>ji</tt>, and <tt>id</tt> maps to
jtulach@1318
   376
 * <tt>in</tt>.  This continues to be the case, in order to not break
jtulach@1318
   377
 * backwards compatibility.
jtulach@1318
   378
 *
jtulach@1318
   379
 * <p>The APIs added in 1.7 map between the old and new language codes,
jtulach@1318
   380
 * maintaining the old codes internal to Locale (so that
jtulach@1318
   381
 * <code>getLanguage</code> and <code>toString</code> reflect the old
jtulach@1318
   382
 * code), but using the new codes in the BCP 47 language tag APIs (so
jtulach@1318
   383
 * that <code>toLanguageTag</code> reflects the new one). This
jtulach@1318
   384
 * preserves the equivalence between Locales no matter which code or
jtulach@1318
   385
 * API is used to construct them. Java's default resource bundle
jtulach@1318
   386
 * lookup mechanism also implements this mapping, so that resources
jtulach@1318
   387
 * can be named using either convention, see {@link ResourceBundle.Control}.
jtulach@1318
   388
 *
jtulach@1318
   389
 * <h5>Three-letter language/country(region) codes</h5>
jtulach@1318
   390
 *
jtulach@1318
   391
 * <p>The Locale constructors have always specified that the language
jtulach@1318
   392
 * and the country param be two characters in length, although in
jtulach@1318
   393
 * practice they have accepted any length.  The specification has now
jtulach@1318
   394
 * been relaxed to allow language codes of two to eight characters and
jtulach@1318
   395
 * country (region) codes of two to three characters, and in
jtulach@1318
   396
 * particular, three-letter language codes and three-digit region
jtulach@1318
   397
 * codes as specified in the IANA Language Subtag Registry.  For
jtulach@1318
   398
 * compatibility, the implementation still does not impose a length
jtulach@1318
   399
 * constraint.
jtulach@1318
   400
 *
jtulach@1318
   401
 * @see Builder
jtulach@1318
   402
 * @see ResourceBundle
jtulach@1318
   403
 * @see java.text.Format
jtulach@1318
   404
 * @see java.text.NumberFormat
jtulach@1318
   405
 * @see java.text.Collator
jtulach@1318
   406
 * @author Mark Davis
jtulach@1318
   407
 * @since 1.1
jtulach@1318
   408
 */
jtulach@1318
   409
public final class Locale implements Cloneable, Serializable {
jtulach@1318
   410
jtulach@1318
   411
    static private final  Cache LOCALECACHE = new Cache();
jtulach@1318
   412
jtulach@1318
   413
    /** Useful constant for language.
jtulach@1318
   414
     */
jtulach@1318
   415
    static public final Locale ENGLISH = createConstant("en", "");
jtulach@1318
   416
jtulach@1318
   417
    /** Useful constant for language.
jtulach@1318
   418
     */
jtulach@1318
   419
    static public final Locale FRENCH = createConstant("fr", "");
jtulach@1318
   420
jtulach@1318
   421
    /** Useful constant for language.
jtulach@1318
   422
     */
jtulach@1318
   423
    static public final Locale GERMAN = createConstant("de", "");
jtulach@1318
   424
jtulach@1318
   425
    /** Useful constant for language.
jtulach@1318
   426
     */
jtulach@1318
   427
    static public final Locale ITALIAN = createConstant("it", "");
jtulach@1318
   428
jtulach@1318
   429
    /** Useful constant for language.
jtulach@1318
   430
     */
jtulach@1318
   431
    static public final Locale JAPANESE = createConstant("ja", "");
jtulach@1318
   432
jtulach@1318
   433
    /** Useful constant for language.
jtulach@1318
   434
     */
jtulach@1318
   435
    static public final Locale KOREAN = createConstant("ko", "");
jtulach@1318
   436
jtulach@1318
   437
    /** Useful constant for language.
jtulach@1318
   438
     */
jtulach@1318
   439
    static public final Locale CHINESE = createConstant("zh", "");
jtulach@1318
   440
jtulach@1318
   441
    /** Useful constant for language.
jtulach@1318
   442
     */
jtulach@1318
   443
    static public final Locale SIMPLIFIED_CHINESE = createConstant("zh", "CN");
jtulach@1318
   444
jtulach@1318
   445
    /** Useful constant for language.
jtulach@1318
   446
     */
jtulach@1318
   447
    static public final Locale TRADITIONAL_CHINESE = createConstant("zh", "TW");
jtulach@1318
   448
jtulach@1318
   449
    /** Useful constant for country.
jtulach@1318
   450
     */
jtulach@1318
   451
    static public final Locale FRANCE = createConstant("fr", "FR");
jtulach@1318
   452
jtulach@1318
   453
    /** Useful constant for country.
jtulach@1318
   454
     */
jtulach@1318
   455
    static public final Locale GERMANY = createConstant("de", "DE");
jtulach@1318
   456
jtulach@1318
   457
    /** Useful constant for country.
jtulach@1318
   458
     */
jtulach@1318
   459
    static public final Locale ITALY = createConstant("it", "IT");
jtulach@1318
   460
jtulach@1318
   461
    /** Useful constant for country.
jtulach@1318
   462
     */
jtulach@1318
   463
    static public final Locale JAPAN = createConstant("ja", "JP");
jtulach@1318
   464
jtulach@1318
   465
    /** Useful constant for country.
jtulach@1318
   466
     */
jtulach@1318
   467
    static public final Locale KOREA = createConstant("ko", "KR");
jtulach@1318
   468
jtulach@1318
   469
    /** Useful constant for country.
jtulach@1318
   470
     */
jtulach@1318
   471
    static public final Locale CHINA = SIMPLIFIED_CHINESE;
jtulach@1318
   472
jtulach@1318
   473
    /** Useful constant for country.
jtulach@1318
   474
     */
jtulach@1318
   475
    static public final Locale PRC = SIMPLIFIED_CHINESE;
jtulach@1318
   476
jtulach@1318
   477
    /** Useful constant for country.
jtulach@1318
   478
     */
jtulach@1318
   479
    static public final Locale TAIWAN = TRADITIONAL_CHINESE;
jtulach@1318
   480
jtulach@1318
   481
    /** Useful constant for country.
jtulach@1318
   482
     */
jtulach@1318
   483
    static public final Locale UK = createConstant("en", "GB");
jtulach@1318
   484
jtulach@1318
   485
    /** Useful constant for country.
jtulach@1318
   486
     */
jtulach@1318
   487
    static public final Locale US = createConstant("en", "US");
jtulach@1318
   488
jtulach@1318
   489
    /** Useful constant for country.
jtulach@1318
   490
     */
jtulach@1318
   491
    static public final Locale CANADA = createConstant("en", "CA");
jtulach@1318
   492
jtulach@1318
   493
    /** Useful constant for country.
jtulach@1318
   494
     */
jtulach@1318
   495
    static public final Locale CANADA_FRENCH = createConstant("fr", "CA");
jtulach@1318
   496
jtulach@1318
   497
    /**
jtulach@1318
   498
     * Useful constant for the root locale.  The root locale is the locale whose
jtulach@1318
   499
     * language, country, and variant are empty ("") strings.  This is regarded
jtulach@1318
   500
     * as the base locale of all locales, and is used as the language/country
jtulach@1318
   501
     * neutral locale for the locale sensitive operations.
jtulach@1318
   502
     *
jtulach@1318
   503
     * @since 1.6
jtulach@1318
   504
     */
jtulach@1318
   505
    static public final Locale ROOT = createConstant("", "");
jtulach@1318
   506
jtulach@1318
   507
    /**
jtulach@1318
   508
     * The key for the private use extension ('x').
jtulach@1318
   509
     *
jtulach@1318
   510
     * @see #getExtension(char)
jtulach@1318
   511
     * @see Builder#setExtension(char, String)
jtulach@1318
   512
     * @since 1.7
jtulach@1318
   513
     */
jtulach@1318
   514
    static public final char PRIVATE_USE_EXTENSION = 'x';
jtulach@1318
   515
jtulach@1318
   516
    /**
jtulach@1318
   517
     * The key for Unicode locale extension ('u').
jtulach@1318
   518
     *
jtulach@1318
   519
     * @see #getExtension(char)
jtulach@1318
   520
     * @see Builder#setExtension(char, String)
jtulach@1318
   521
     * @since 1.7
jtulach@1318
   522
     */
jtulach@1318
   523
    static public final char UNICODE_LOCALE_EXTENSION = 'u';
jtulach@1318
   524
jtulach@1318
   525
    /** serialization ID
jtulach@1318
   526
     */
jtulach@1318
   527
    static final long serialVersionUID = 9149081749638150636L;
jtulach@1318
   528
jtulach@1318
   529
    /**
jtulach@1318
   530
     * Display types for retrieving localized names from the name providers.
jtulach@1318
   531
     */
jtulach@1318
   532
    private static final int DISPLAY_LANGUAGE = 0;
jtulach@1318
   533
    private static final int DISPLAY_COUNTRY  = 1;
jtulach@1318
   534
    private static final int DISPLAY_VARIANT  = 2;
jtulach@1318
   535
    private static final int DISPLAY_SCRIPT   = 3;
jtulach@1318
   536
jtulach@1318
   537
    /**
jtulach@1318
   538
     * Private constructor used by getInstance method
jtulach@1318
   539
     */
jtulach@1318
   540
    private Locale(BaseLocale baseLocale, LocaleExtensions extensions) {
jtulach@1318
   541
        this.baseLocale = baseLocale;
jtulach@1318
   542
        this.localeExtensions = extensions;
jtulach@1318
   543
    }
jtulach@1318
   544
jtulach@1318
   545
    /**
jtulach@1318
   546
     * Construct a locale from language, country and variant.
jtulach@1318
   547
     * This constructor normalizes the language value to lowercase and
jtulach@1318
   548
     * the country value to uppercase.
jtulach@1318
   549
     * <p>
jtulach@1318
   550
     * <b>Note:</b>
jtulach@1318
   551
     * <ul>
jtulach@1318
   552
     * <li>ISO 639 is not a stable standard; some of the language codes it defines
jtulach@1318
   553
     * (specifically "iw", "ji", and "in") have changed.  This constructor accepts both the
jtulach@1318
   554
     * old codes ("iw", "ji", and "in") and the new codes ("he", "yi", and "id"), but all other
jtulach@1318
   555
     * API on Locale will return only the OLD codes.
jtulach@1318
   556
     * <li>For backward compatibility reasons, this constructor does not make
jtulach@1318
   557
     * any syntactic checks on the input.
jtulach@1318
   558
     * <li>The two cases ("ja", "JP", "JP") and ("th", "TH", "TH") are handled specially,
jtulach@1318
   559
     * see <a href="#special_cases_constructor">Special Cases</a> for more information.
jtulach@1318
   560
     * </ul>
jtulach@1318
   561
     *
jtulach@1318
   562
     * @param language An ISO 639 alpha-2 or alpha-3 language code, or a language subtag
jtulach@1318
   563
     * up to 8 characters in length.  See the <code>Locale</code> class description about
jtulach@1318
   564
     * valid language values.
jtulach@1318
   565
     * @param country An ISO 3166 alpha-2 country code or a UN M.49 numeric-3 area code.
jtulach@1318
   566
     * See the <code>Locale</code> class description about valid country values.
jtulach@1318
   567
     * @param variant Any arbitrary value used to indicate a variation of a <code>Locale</code>.
jtulach@1318
   568
     * See the <code>Locale</code> class description for the details.
jtulach@1318
   569
     * @exception NullPointerException thrown if any argument is null.
jtulach@1318
   570
     */
jtulach@1318
   571
    public Locale(String language, String country, String variant) {
jtulach@1318
   572
        if (language== null || country == null || variant == null) {
jtulach@1318
   573
            throw new NullPointerException();
jtulach@1318
   574
        }
jtulach@1318
   575
        baseLocale = BaseLocale.getInstance(convertOldISOCodes(language), "", country, variant);
jtulach@1318
   576
        localeExtensions = getCompatibilityExtensions(language, "", country, variant);
jtulach@1318
   577
    }
jtulach@1318
   578
jtulach@1318
   579
    /**
jtulach@1318
   580
     * Construct a locale from language and country.
jtulach@1318
   581
     * This constructor normalizes the language value to lowercase and
jtulach@1318
   582
     * the country value to uppercase.
jtulach@1318
   583
     * <p>
jtulach@1318
   584
     * <b>Note:</b>
jtulach@1318
   585
     * <ul>
jtulach@1318
   586
     * <li>ISO 639 is not a stable standard; some of the language codes it defines
jtulach@1318
   587
     * (specifically "iw", "ji", and "in") have changed.  This constructor accepts both the
jtulach@1318
   588
     * old codes ("iw", "ji", and "in") and the new codes ("he", "yi", and "id"), but all other
jtulach@1318
   589
     * API on Locale will return only the OLD codes.
jtulach@1318
   590
     * <li>For backward compatibility reasons, this constructor does not make
jtulach@1318
   591
     * any syntactic checks on the input.
jtulach@1318
   592
     * </ul>
jtulach@1318
   593
     *
jtulach@1318
   594
     * @param language An ISO 639 alpha-2 or alpha-3 language code, or a language subtag
jtulach@1318
   595
     * up to 8 characters in length.  See the <code>Locale</code> class description about
jtulach@1318
   596
     * valid language values.
jtulach@1318
   597
     * @param country An ISO 3166 alpha-2 country code or a UN M.49 numeric-3 area code.
jtulach@1318
   598
     * See the <code>Locale</code> class description about valid country values.
jtulach@1318
   599
     * @exception NullPointerException thrown if either argument is null.
jtulach@1318
   600
     */
jtulach@1318
   601
    public Locale(String language, String country) {
jtulach@1318
   602
        this(language, country, "");
jtulach@1318
   603
    }
jtulach@1318
   604
jtulach@1318
   605
    /**
jtulach@1318
   606
     * Construct a locale from a language code.
jtulach@1318
   607
     * This constructor normalizes the language value to lowercase.
jtulach@1318
   608
     * <p>
jtulach@1318
   609
     * <b>Note:</b>
jtulach@1318
   610
     * <ul>
jtulach@1318
   611
     * <li>ISO 639 is not a stable standard; some of the language codes it defines
jtulach@1318
   612
     * (specifically "iw", "ji", and "in") have changed.  This constructor accepts both the
jtulach@1318
   613
     * old codes ("iw", "ji", and "in") and the new codes ("he", "yi", and "id"), but all other
jtulach@1318
   614
     * API on Locale will return only the OLD codes.
jtulach@1318
   615
     * <li>For backward compatibility reasons, this constructor does not make
jtulach@1318
   616
     * any syntactic checks on the input.
jtulach@1318
   617
     * </ul>
jtulach@1318
   618
     *
jtulach@1318
   619
     * @param language An ISO 639 alpha-2 or alpha-3 language code, or a language subtag
jtulach@1318
   620
     * up to 8 characters in length.  See the <code>Locale</code> class description about
jtulach@1318
   621
     * valid language values.
jtulach@1318
   622
     * @exception NullPointerException thrown if argument is null.
jtulach@1318
   623
     * @since 1.4
jtulach@1318
   624
     */
jtulach@1318
   625
    public Locale(String language) {
jtulach@1318
   626
        this(language, "", "");
jtulach@1318
   627
    }
jtulach@1318
   628
jtulach@1318
   629
    /**
jtulach@1318
   630
     * This method must be called only for creating the Locale.*
jtulach@1318
   631
     * constants due to making shortcuts.
jtulach@1318
   632
     */
jtulach@1318
   633
    private static Locale createConstant(String lang, String country) {
jtulach@1318
   634
        BaseLocale base = BaseLocale.createInstance(lang, country);
jtulach@1318
   635
        return getInstance(base, null);
jtulach@1318
   636
    }
jtulach@1318
   637
jtulach@1318
   638
    /**
jtulach@1318
   639
     * Returns a <code>Locale</code> constructed from the given
jtulach@1318
   640
     * <code>language</code>, <code>country</code> and
jtulach@1318
   641
     * <code>variant</code>. If the same <code>Locale</code> instance
jtulach@1318
   642
     * is available in the cache, then that instance is
jtulach@1318
   643
     * returned. Otherwise, a new <code>Locale</code> instance is
jtulach@1318
   644
     * created and cached.
jtulach@1318
   645
     *
jtulach@1318
   646
     * @param language lowercase 2 to 8 language code.
jtulach@1318
   647
     * @param country uppercase two-letter ISO-3166 code and numric-3 UN M.49 area code.
jtulach@1318
   648
     * @param variant vendor and browser specific code. See class description.
jtulach@1318
   649
     * @return the <code>Locale</code> instance requested
jtulach@1318
   650
     * @exception NullPointerException if any argument is null.
jtulach@1318
   651
     */
jtulach@1318
   652
    static Locale getInstance(String language, String country, String variant) {
jtulach@1318
   653
        return getInstance(language, "", country, variant, null);
jtulach@1318
   654
    }
jtulach@1318
   655
jtulach@1318
   656
    static Locale getInstance(String language, String script, String country,
jtulach@1318
   657
                                      String variant, LocaleExtensions extensions) {
jtulach@1318
   658
        if (language== null || script == null || country == null || variant == null) {
jtulach@1318
   659
            throw new NullPointerException();
jtulach@1318
   660
        }
jtulach@1318
   661
jtulach@1318
   662
        if (extensions == null) {
jtulach@1318
   663
            extensions = getCompatibilityExtensions(language, script, country, variant);
jtulach@1318
   664
        }
jtulach@1318
   665
jtulach@1318
   666
        BaseLocale baseloc = BaseLocale.getInstance(language, script, country, variant);
jtulach@1318
   667
        return getInstance(baseloc, extensions);
jtulach@1318
   668
    }
jtulach@1318
   669
jtulach@1318
   670
    static Locale getInstance(BaseLocale baseloc, LocaleExtensions extensions) {
jtulach@1318
   671
        LocaleKey key = new LocaleKey(baseloc, extensions);
jtulach@1318
   672
        return LOCALECACHE.get(key);
jtulach@1318
   673
    }
jtulach@1318
   674
jtulach@1318
   675
    private static class Cache extends LocaleObjectCache<LocaleKey, Locale> {
jtulach@1318
   676
        private Cache() {
jtulach@1318
   677
        }
jtulach@1318
   678
jtulach@1318
   679
        @Override
jtulach@1318
   680
        protected Locale createObject(LocaleKey key) {
jtulach@1318
   681
            return new Locale(key.base, key.exts);
jtulach@1318
   682
        }
jtulach@1318
   683
    }
jtulach@1318
   684
jtulach@1318
   685
    private static final class LocaleKey {
jtulach@1318
   686
        private final BaseLocale base;
jtulach@1318
   687
        private final LocaleExtensions exts;
jtulach@1318
   688
        private final int hash;
jtulach@1318
   689
jtulach@1318
   690
        private LocaleKey(BaseLocale baseLocale, LocaleExtensions extensions) {
jtulach@1318
   691
            base = baseLocale;
jtulach@1318
   692
            exts = extensions;
jtulach@1318
   693
jtulach@1318
   694
            // Calculate the hash value here because it's always used.
jtulach@1318
   695
            int h = base.hashCode();
jtulach@1318
   696
            if (exts != null) {
jtulach@1318
   697
                h ^= exts.hashCode();
jtulach@1318
   698
            }
jtulach@1318
   699
            hash = h;
jtulach@1318
   700
        }
jtulach@1318
   701
jtulach@1318
   702
        @Override
jtulach@1318
   703
        public boolean equals(Object obj) {
jtulach@1318
   704
            if (this == obj) {
jtulach@1318
   705
                return true;
jtulach@1318
   706
            }
jtulach@1318
   707
            if (!(obj instanceof LocaleKey)) {
jtulach@1318
   708
                return false;
jtulach@1318
   709
            }
jtulach@1318
   710
            LocaleKey other = (LocaleKey)obj;
jtulach@1318
   711
            if (hash != other.hash || !base.equals(other.base)) {
jtulach@1318
   712
                return false;
jtulach@1318
   713
            }
jtulach@1318
   714
            if (exts == null) {
jtulach@1318
   715
                return other.exts == null;
jtulach@1318
   716
            }
jtulach@1318
   717
            return exts.equals(other.exts);
jtulach@1318
   718
        }
jtulach@1318
   719
jtulach@1318
   720
        @Override
jtulach@1318
   721
        public int hashCode() {
jtulach@1318
   722
            return hash;
jtulach@1318
   723
        }
jtulach@1318
   724
    }
jtulach@1318
   725
jtulach@1318
   726
    /**
jtulach@1318
   727
     * Gets the current value of the default locale for this instance
jtulach@1318
   728
     * of the Java Virtual Machine.
jtulach@1318
   729
     * <p>
jtulach@1318
   730
     * The Java Virtual Machine sets the default locale during startup
jtulach@1318
   731
     * based on the host environment. It is used by many locale-sensitive
jtulach@1318
   732
     * methods if no locale is explicitly specified.
jtulach@1318
   733
     * It can be changed using the
jtulach@1318
   734
     * {@link #setDefault(java.util.Locale) setDefault} method.
jtulach@1318
   735
     *
jtulach@1318
   736
     * @return the default locale for this instance of the Java Virtual Machine
jtulach@1318
   737
     */
jtulach@1318
   738
    public static Locale getDefault() {
jtulach@1318
   739
        // do not synchronize this method - see 4071298
jtulach@1318
   740
        // it's OK if more than one default locale happens to be created
jtulach@1318
   741
        if (defaultLocale == null) {
jtulach@1318
   742
            initDefault();
jtulach@1318
   743
        }
jtulach@1318
   744
        return defaultLocale;
jtulach@1318
   745
    }
jtulach@1318
   746
jtulach@1318
   747
    /**
jtulach@1318
   748
     * Gets the current value of the default locale for the specified Category
jtulach@1318
   749
     * for this instance of the Java Virtual Machine.
jtulach@1318
   750
     * <p>
jtulach@1318
   751
     * The Java Virtual Machine sets the default locale during startup based
jtulach@1318
   752
     * on the host environment. It is used by many locale-sensitive methods
jtulach@1318
   753
     * if no locale is explicitly specified. It can be changed using the
jtulach@1318
   754
     * setDefault(Locale.Category, Locale) method.
jtulach@1318
   755
     *
jtulach@1318
   756
     * @param category - the specified category to get the default locale
jtulach@1318
   757
     * @throws NullPointerException - if category is null
jtulach@1318
   758
     * @return the default locale for the specified Category for this instance
jtulach@1318
   759
     *     of the Java Virtual Machine
jtulach@1318
   760
     * @see #setDefault(Locale.Category, Locale)
jtulach@1318
   761
     * @since 1.7
jtulach@1318
   762
     */
jtulach@1318
   763
    public static Locale getDefault(Locale.Category category) {
jtulach@1318
   764
        // do not synchronize this method - see 4071298
jtulach@1318
   765
        // it's OK if more than one default locale happens to be created
jtulach@1318
   766
        switch (category) {
jtulach@1318
   767
        case DISPLAY:
jtulach@1318
   768
            if (defaultDisplayLocale == null) {
jtulach@1318
   769
                initDefault(category);
jtulach@1318
   770
            }
jtulach@1318
   771
            return defaultDisplayLocale;
jtulach@1318
   772
        case FORMAT:
jtulach@1318
   773
            if (defaultFormatLocale == null) {
jtulach@1318
   774
                initDefault(category);
jtulach@1318
   775
            }
jtulach@1318
   776
            return defaultFormatLocale;
jtulach@1318
   777
        default:
jtulach@1318
   778
            assert false: "Unknown Category";
jtulach@1318
   779
        }
jtulach@1318
   780
        return getDefault();
jtulach@1318
   781
    }
jtulach@1318
   782
jtulach@1318
   783
    private static void initDefault() {
jtulach@1318
   784
        String language, region, script, country, variant;
jtulach@1318
   785
        language = AccessController.doPrivileged(
jtulach@1318
   786
            new GetPropertyAction("user.language", "en"));
jtulach@1318
   787
        // for compatibility, check for old user.region property
jtulach@1318
   788
        region = AccessController.doPrivileged(
jtulach@1318
   789
            new GetPropertyAction("user.region"));
jtulach@1318
   790
        if (region != null) {
jtulach@1318
   791
            // region can be of form country, country_variant, or _variant
jtulach@1318
   792
            int i = region.indexOf('_');
jtulach@1318
   793
            if (i >= 0) {
jtulach@1318
   794
                country = region.substring(0, i);
jtulach@1318
   795
                variant = region.substring(i + 1);
jtulach@1318
   796
            } else {
jtulach@1318
   797
                country = region;
jtulach@1318
   798
                variant = "";
jtulach@1318
   799
            }
jtulach@1318
   800
            script = "";
jtulach@1318
   801
        } else {
jtulach@1318
   802
            script = AccessController.doPrivileged(
jtulach@1318
   803
                new GetPropertyAction("user.script", ""));
jtulach@1318
   804
            country = AccessController.doPrivileged(
jtulach@1318
   805
                new GetPropertyAction("user.country", ""));
jtulach@1318
   806
            variant = AccessController.doPrivileged(
jtulach@1318
   807
                new GetPropertyAction("user.variant", ""));
jtulach@1318
   808
        }
jtulach@1318
   809
        defaultLocale = getInstance(language, script, country, variant, null);
jtulach@1318
   810
    }
jtulach@1318
   811
jtulach@1318
   812
    private static void initDefault(Locale.Category category) {
jtulach@1318
   813
        // make sure defaultLocale is initialized
jtulach@1318
   814
        if (defaultLocale == null) {
jtulach@1318
   815
            initDefault();
jtulach@1318
   816
        }
jtulach@1318
   817
jtulach@1318
   818
        Locale defaultCategoryLocale = getInstance(
jtulach@1318
   819
            AccessController.doPrivileged(
jtulach@1318
   820
                new GetPropertyAction(category.languageKey, defaultLocale.getLanguage())),
jtulach@1318
   821
            AccessController.doPrivileged(
jtulach@1318
   822
                new GetPropertyAction(category.scriptKey, defaultLocale.getScript())),
jtulach@1318
   823
            AccessController.doPrivileged(
jtulach@1318
   824
                new GetPropertyAction(category.countryKey, defaultLocale.getCountry())),
jtulach@1318
   825
            AccessController.doPrivileged(
jtulach@1318
   826
                new GetPropertyAction(category.variantKey, defaultLocale.getVariant())),
jtulach@1318
   827
            null);
jtulach@1318
   828
jtulach@1318
   829
        switch (category) {
jtulach@1318
   830
        case DISPLAY:
jtulach@1318
   831
            defaultDisplayLocale = defaultCategoryLocale;
jtulach@1318
   832
            break;
jtulach@1318
   833
        case FORMAT:
jtulach@1318
   834
            defaultFormatLocale = defaultCategoryLocale;
jtulach@1318
   835
            break;
jtulach@1318
   836
        }
jtulach@1318
   837
    }
jtulach@1318
   838
jtulach@1318
   839
    /**
jtulach@1318
   840
     * Sets the default locale for this instance of the Java Virtual Machine.
jtulach@1318
   841
     * This does not affect the host locale.
jtulach@1318
   842
     * <p>
jtulach@1318
   843
     * If there is a security manager, its <code>checkPermission</code>
jtulach@1318
   844
     * method is called with a <code>PropertyPermission("user.language", "write")</code>
jtulach@1318
   845
     * permission before the default locale is changed.
jtulach@1318
   846
     * <p>
jtulach@1318
   847
     * The Java Virtual Machine sets the default locale during startup
jtulach@1318
   848
     * based on the host environment. It is used by many locale-sensitive
jtulach@1318
   849
     * methods if no locale is explicitly specified.
jtulach@1318
   850
     * <p>
jtulach@1318
   851
     * Since changing the default locale may affect many different areas
jtulach@1318
   852
     * of functionality, this method should only be used if the caller
jtulach@1318
   853
     * is prepared to reinitialize locale-sensitive code running
jtulach@1318
   854
     * within the same Java Virtual Machine.
jtulach@1318
   855
     * <p>
jtulach@1318
   856
     * By setting the default locale with this method, all of the default
jtulach@1318
   857
     * locales for each Category are also set to the specified default locale.
jtulach@1318
   858
     *
jtulach@1318
   859
     * @throws SecurityException
jtulach@1318
   860
     *        if a security manager exists and its
jtulach@1318
   861
     *        <code>checkPermission</code> method doesn't allow the operation.
jtulach@1318
   862
     * @throws NullPointerException if <code>newLocale</code> is null
jtulach@1318
   863
     * @param newLocale the new default locale
jtulach@1318
   864
     * @see SecurityManager#checkPermission
jtulach@1318
   865
     * @see java.util.PropertyPermission
jtulach@1318
   866
     */
jtulach@1318
   867
    public static synchronized void setDefault(Locale newLocale) {
jtulach@1318
   868
        setDefault(Category.DISPLAY, newLocale);
jtulach@1318
   869
        setDefault(Category.FORMAT, newLocale);
jtulach@1318
   870
        defaultLocale = newLocale;
jtulach@1318
   871
    }
jtulach@1318
   872
jtulach@1318
   873
    /**
jtulach@1318
   874
     * Sets the default locale for the specified Category for this instance
jtulach@1318
   875
     * of the Java Virtual Machine. This does not affect the host locale.
jtulach@1318
   876
     * <p>
jtulach@1318
   877
     * If there is a security manager, its checkPermission method is called
jtulach@1318
   878
     * with a PropertyPermission("user.language", "write") permission before
jtulach@1318
   879
     * the default locale is changed.
jtulach@1318
   880
     * <p>
jtulach@1318
   881
     * The Java Virtual Machine sets the default locale during startup based
jtulach@1318
   882
     * on the host environment. It is used by many locale-sensitive methods
jtulach@1318
   883
     * if no locale is explicitly specified.
jtulach@1318
   884
     * <p>
jtulach@1318
   885
     * Since changing the default locale may affect many different areas of
jtulach@1318
   886
     * functionality, this method should only be used if the caller is
jtulach@1318
   887
     * prepared to reinitialize locale-sensitive code running within the
jtulach@1318
   888
     * same Java Virtual Machine.
jtulach@1318
   889
     * <p>
jtulach@1318
   890
     *
jtulach@1318
   891
     * @param category - the specified category to set the default locale
jtulach@1318
   892
     * @param newLocale - the new default locale
jtulach@1318
   893
     * @throws SecurityException - if a security manager exists and its
jtulach@1318
   894
     *     checkPermission method doesn't allow the operation.
jtulach@1318
   895
     * @throws NullPointerException - if category and/or newLocale is null
jtulach@1318
   896
     * @see SecurityManager#checkPermission(java.security.Permission)
jtulach@1318
   897
     * @see PropertyPermission
jtulach@1318
   898
     * @see #getDefault(Locale.Category)
jtulach@1318
   899
     * @since 1.7
jtulach@1318
   900
     */
jtulach@1318
   901
    public static synchronized void setDefault(Locale.Category category,
jtulach@1318
   902
        Locale newLocale) {
jtulach@1318
   903
        if (category == null)
jtulach@1318
   904
            throw new NullPointerException("Category cannot be NULL");
jtulach@1318
   905
        if (newLocale == null)
jtulach@1318
   906
            throw new NullPointerException("Can't set default locale to NULL");
jtulach@1318
   907
jtulach@1318
   908
        SecurityManager sm = System.getSecurityManager();
jtulach@1318
   909
        if (sm != null) sm.checkPermission(new PropertyPermission
jtulach@1318
   910
                        ("user.language", "write"));
jtulach@1318
   911
        switch (category) {
jtulach@1318
   912
        case DISPLAY:
jtulach@1318
   913
            defaultDisplayLocale = newLocale;
jtulach@1318
   914
            break;
jtulach@1318
   915
        case FORMAT:
jtulach@1318
   916
            defaultFormatLocale = newLocale;
jtulach@1318
   917
            break;
jtulach@1318
   918
        default:
jtulach@1318
   919
            assert false: "Unknown Category";
jtulach@1318
   920
        }
jtulach@1318
   921
    }
jtulach@1318
   922
jtulach@1318
   923
    /**
jtulach@1318
   924
     * Returns an array of all installed locales.
jtulach@1318
   925
     * The returned array represents the union of locales supported
jtulach@1318
   926
     * by the Java runtime environment and by installed
jtulach@1318
   927
     * {@link java.util.spi.LocaleServiceProvider LocaleServiceProvider}
jtulach@1318
   928
     * implementations.  It must contain at least a <code>Locale</code>
jtulach@1318
   929
     * instance equal to {@link java.util.Locale#US Locale.US}.
jtulach@1318
   930
     *
jtulach@1318
   931
     * @return An array of installed locales.
jtulach@1318
   932
     */
jtulach@1318
   933
    public static Locale[] getAvailableLocales() {
jtulach@1318
   934
        return LocaleServiceProviderPool.getAllAvailableLocales();
jtulach@1318
   935
    }
jtulach@1318
   936
jtulach@1318
   937
    /**
jtulach@1318
   938
     * Returns a list of all 2-letter country codes defined in ISO 3166.
jtulach@1318
   939
     * Can be used to create Locales.
jtulach@1318
   940
     * <p>
jtulach@1318
   941
     * <b>Note:</b> The <code>Locale</code> class also supports other codes for
jtulach@1318
   942
     * country (region), such as 3-letter numeric UN M.49 area codes.
jtulach@1318
   943
     * Therefore, the list returned by this method does not contain ALL valid
jtulach@1318
   944
     * codes that can be used to create Locales.
jtulach@1318
   945
     */
jtulach@1318
   946
    public static String[] getISOCountries() {
jtulach@1318
   947
        if (isoCountries == null) {
jtulach@1318
   948
            isoCountries = getISO2Table(LocaleISOData.isoCountryTable);
jtulach@1318
   949
        }
jtulach@1318
   950
        String[] result = new String[isoCountries.length];
jtulach@1318
   951
        System.arraycopy(isoCountries, 0, result, 0, isoCountries.length);
jtulach@1318
   952
        return result;
jtulach@1318
   953
    }
jtulach@1318
   954
jtulach@1318
   955
    /**
jtulach@1318
   956
     * Returns a list of all 2-letter language codes defined in ISO 639.
jtulach@1318
   957
     * Can be used to create Locales.
jtulach@1318
   958
     * <p>
jtulach@1318
   959
     * <b>Note:</b>
jtulach@1318
   960
     * <ul>
jtulach@1318
   961
     * <li>ISO 639 is not a stable standard&mdash; some languages' codes have changed.
jtulach@1318
   962
     * The list this function returns includes both the new and the old codes for the
jtulach@1318
   963
     * languages whose codes have changed.
jtulach@1318
   964
     * <li>The <code>Locale</code> class also supports language codes up to
jtulach@1318
   965
     * 8 characters in length.  Therefore, the list returned by this method does
jtulach@1318
   966
     * not contain ALL valid codes that can be used to create Locales.
jtulach@1318
   967
     * </ul>
jtulach@1318
   968
     */
jtulach@1318
   969
    public static String[] getISOLanguages() {
jtulach@1318
   970
        if (isoLanguages == null) {
jtulach@1318
   971
            isoLanguages = getISO2Table(LocaleISOData.isoLanguageTable);
jtulach@1318
   972
        }
jtulach@1318
   973
        String[] result = new String[isoLanguages.length];
jtulach@1318
   974
        System.arraycopy(isoLanguages, 0, result, 0, isoLanguages.length);
jtulach@1318
   975
        return result;
jtulach@1318
   976
    }
jtulach@1318
   977
jtulach@1318
   978
    private static final String[] getISO2Table(String table) {
jtulach@1318
   979
        int len = table.length() / 5;
jtulach@1318
   980
        String[] isoTable = new String[len];
jtulach@1318
   981
        for (int i = 0, j = 0; i < len; i++, j += 5) {
jtulach@1318
   982
            isoTable[i] = table.substring(j, j + 2);
jtulach@1318
   983
        }
jtulach@1318
   984
        return isoTable;
jtulach@1318
   985
    }
jtulach@1318
   986
jtulach@1318
   987
    /**
jtulach@1318
   988
     * Returns the language code of this Locale.
jtulach@1318
   989
     *
jtulach@1318
   990
     * <p><b>Note:</b> ISO 639 is not a stable standard&mdash; some languages' codes have changed.
jtulach@1318
   991
     * Locale's constructor recognizes both the new and the old codes for the languages
jtulach@1318
   992
     * whose codes have changed, but this function always returns the old code.  If you
jtulach@1318
   993
     * want to check for a specific language whose code has changed, don't do
jtulach@1318
   994
     * <pre>
jtulach@1318
   995
     * if (locale.getLanguage().equals("he")) // BAD!
jtulach@1318
   996
     *    ...
jtulach@1318
   997
     * </pre>
jtulach@1318
   998
     * Instead, do
jtulach@1318
   999
     * <pre>
jtulach@1318
  1000
     * if (locale.getLanguage().equals(new Locale("he").getLanguage()))
jtulach@1318
  1001
     *    ...
jtulach@1318
  1002
     * </pre>
jtulach@1318
  1003
     * @return The language code, or the empty string if none is defined.
jtulach@1318
  1004
     * @see #getDisplayLanguage
jtulach@1318
  1005
     */
jtulach@1318
  1006
    public String getLanguage() {
jtulach@1318
  1007
        return baseLocale.getLanguage();
jtulach@1318
  1008
    }
jtulach@1318
  1009
jtulach@1318
  1010
    /**
jtulach@1318
  1011
     * Returns the script for this locale, which should
jtulach@1318
  1012
     * either be the empty string or an ISO 15924 4-letter script
jtulach@1318
  1013
     * code. The first letter is uppercase and the rest are
jtulach@1318
  1014
     * lowercase, for example, 'Latn', 'Cyrl'.
jtulach@1318
  1015
     *
jtulach@1318
  1016
     * @return The script code, or the empty string if none is defined.
jtulach@1318
  1017
     * @see #getDisplayScript
jtulach@1318
  1018
     * @since 1.7
jtulach@1318
  1019
     */
jtulach@1318
  1020
    public String getScript() {
jtulach@1318
  1021
        return baseLocale.getScript();
jtulach@1318
  1022
    }
jtulach@1318
  1023
jtulach@1318
  1024
    /**
jtulach@1318
  1025
     * Returns the country/region code for this locale, which should
jtulach@1318
  1026
     * either be the empty string, an uppercase ISO 3166 2-letter code,
jtulach@1318
  1027
     * or a UN M.49 3-digit code.
jtulach@1318
  1028
     *
jtulach@1318
  1029
     * @return The country/region code, or the empty string if none is defined.
jtulach@1318
  1030
     * @see #getDisplayCountry
jtulach@1318
  1031
     */
jtulach@1318
  1032
    public String getCountry() {
jtulach@1318
  1033
        return baseLocale.getRegion();
jtulach@1318
  1034
    }
jtulach@1318
  1035
jtulach@1318
  1036
    /**
jtulach@1318
  1037
     * Returns the variant code for this locale.
jtulach@1318
  1038
     *
jtulach@1318
  1039
     * @return The variant code, or the empty string if none is defined.
jtulach@1318
  1040
     * @see #getDisplayVariant
jtulach@1318
  1041
     */
jtulach@1318
  1042
    public String getVariant() {
jtulach@1318
  1043
        return baseLocale.getVariant();
jtulach@1318
  1044
    }
jtulach@1318
  1045
jtulach@1318
  1046
    /**
jtulach@1318
  1047
     * Returns the extension (or private use) value associated with
jtulach@1318
  1048
     * the specified key, or null if there is no extension
jtulach@1318
  1049
     * associated with the key. To be well-formed, the key must be one
jtulach@1318
  1050
     * of <code>[0-9A-Za-z]</code>. Keys are case-insensitive, so
jtulach@1318
  1051
     * for example 'z' and 'Z' represent the same extension.
jtulach@1318
  1052
     *
jtulach@1318
  1053
     * @param key the extension key
jtulach@1318
  1054
     * @return The extension, or null if this locale defines no
jtulach@1318
  1055
     * extension for the specified key.
jtulach@1318
  1056
     * @throws IllegalArgumentException if key is not well-formed
jtulach@1318
  1057
     * @see #PRIVATE_USE_EXTENSION
jtulach@1318
  1058
     * @see #UNICODE_LOCALE_EXTENSION
jtulach@1318
  1059
     * @since 1.7
jtulach@1318
  1060
     */
jtulach@1318
  1061
    public String getExtension(char key) {
jtulach@1318
  1062
        if (!LocaleExtensions.isValidKey(key)) {
jtulach@1318
  1063
            throw new IllegalArgumentException("Ill-formed extension key: " + key);
jtulach@1318
  1064
        }
jtulach@1318
  1065
        return (localeExtensions == null) ? null : localeExtensions.getExtensionValue(key);
jtulach@1318
  1066
    }
jtulach@1318
  1067
jtulach@1318
  1068
    /**
jtulach@1318
  1069
     * Returns the set of extension keys associated with this locale, or the
jtulach@1318
  1070
     * empty set if it has no extensions. The returned set is unmodifiable.
jtulach@1318
  1071
     * The keys will all be lower-case.
jtulach@1318
  1072
     *
jtulach@1318
  1073
     * @return The set of extension keys, or the empty set if this locale has
jtulach@1318
  1074
     * no extensions.
jtulach@1318
  1075
     * @since 1.7
jtulach@1318
  1076
     */
jtulach@1318
  1077
    public Set<Character> getExtensionKeys() {
jtulach@1318
  1078
        if (localeExtensions == null) {
jtulach@1318
  1079
            return Collections.emptySet();
jtulach@1318
  1080
        }
jtulach@1318
  1081
        return localeExtensions.getKeys();
jtulach@1318
  1082
    }
jtulach@1318
  1083
jtulach@1318
  1084
    /**
jtulach@1318
  1085
     * Returns the set of unicode locale attributes associated with
jtulach@1318
  1086
     * this locale, or the empty set if it has no attributes. The
jtulach@1318
  1087
     * returned set is unmodifiable.
jtulach@1318
  1088
     *
jtulach@1318
  1089
     * @return The set of attributes.
jtulach@1318
  1090
     * @since 1.7
jtulach@1318
  1091
     */
jtulach@1318
  1092
    public Set<String> getUnicodeLocaleAttributes() {
jtulach@1318
  1093
        if (localeExtensions == null) {
jtulach@1318
  1094
            return Collections.emptySet();
jtulach@1318
  1095
        }
jtulach@1318
  1096
        return localeExtensions.getUnicodeLocaleAttributes();
jtulach@1318
  1097
    }
jtulach@1318
  1098
jtulach@1318
  1099
    /**
jtulach@1318
  1100
     * Returns the Unicode locale type associated with the specified Unicode locale key
jtulach@1318
  1101
     * for this locale. Returns the empty string for keys that are defined with no type.
jtulach@1318
  1102
     * Returns null if the key is not defined. Keys are case-insensitive. The key must
jtulach@1318
  1103
     * be two alphanumeric characters ([0-9a-zA-Z]), or an IllegalArgumentException is
jtulach@1318
  1104
     * thrown.
jtulach@1318
  1105
     *
jtulach@1318
  1106
     * @param key the Unicode locale key
jtulach@1318
  1107
     * @return The Unicode locale type associated with the key, or null if the
jtulach@1318
  1108
     * locale does not define the key.
jtulach@1318
  1109
     * @throws IllegalArgumentException if the key is not well-formed
jtulach@1318
  1110
     * @throws NullPointerException if <code>key</code> is null
jtulach@1318
  1111
     * @since 1.7
jtulach@1318
  1112
     */
jtulach@1318
  1113
    public String getUnicodeLocaleType(String key) {
jtulach@1318
  1114
        if (!UnicodeLocaleExtension.isKey(key)) {
jtulach@1318
  1115
            throw new IllegalArgumentException("Ill-formed Unicode locale key: " + key);
jtulach@1318
  1116
        }
jtulach@1318
  1117
        return (localeExtensions == null) ? null : localeExtensions.getUnicodeLocaleType(key);
jtulach@1318
  1118
    }
jtulach@1318
  1119
jtulach@1318
  1120
    /**
jtulach@1318
  1121
     * Returns the set of Unicode locale keys defined by this locale, or the empty set if
jtulach@1318
  1122
     * this locale has none.  The returned set is immutable.  Keys are all lower case.
jtulach@1318
  1123
     *
jtulach@1318
  1124
     * @return The set of Unicode locale keys, or the empty set if this locale has
jtulach@1318
  1125
     * no Unicode locale keywords.
jtulach@1318
  1126
     * @since 1.7
jtulach@1318
  1127
     */
jtulach@1318
  1128
    public Set<String> getUnicodeLocaleKeys() {
jtulach@1318
  1129
        if (localeExtensions == null) {
jtulach@1318
  1130
            return Collections.emptySet();
jtulach@1318
  1131
        }
jtulach@1318
  1132
        return localeExtensions.getUnicodeLocaleKeys();
jtulach@1318
  1133
    }
jtulach@1318
  1134
jtulach@1318
  1135
    /**
jtulach@1318
  1136
     * Package locale method returning the Locale's BaseLocale,
jtulach@1318
  1137
     * used by ResourceBundle
jtulach@1318
  1138
     * @return base locale of this Locale
jtulach@1318
  1139
     */
jtulach@1318
  1140
    BaseLocale getBaseLocale() {
jtulach@1318
  1141
        return baseLocale;
jtulach@1318
  1142
    }
jtulach@1318
  1143
jtulach@1318
  1144
    /**
jtulach@1318
  1145
     * Package private method returning the Locale's LocaleExtensions,
jtulach@1318
  1146
     * used by ResourceBundle.
jtulach@1318
  1147
     * @return locale exnteions of this Locale,
jtulach@1318
  1148
     *         or {@code null} if no extensions are defined
jtulach@1318
  1149
     */
jtulach@1318
  1150
     LocaleExtensions getLocaleExtensions() {
jtulach@1318
  1151
         return localeExtensions;
jtulach@1318
  1152
     }
jtulach@1318
  1153
jtulach@1318
  1154
    /**
jtulach@1318
  1155
     * Returns a string representation of this <code>Locale</code>
jtulach@1318
  1156
     * object, consisting of language, country, variant, script,
jtulach@1318
  1157
     * and extensions as below:
jtulach@1318
  1158
     * <p><blockquote>
jtulach@1318
  1159
     * language + "_" + country + "_" + (variant + "_#" | "#") + script + "-" + extensions
jtulach@1318
  1160
     * </blockquote>
jtulach@1318
  1161
     *
jtulach@1318
  1162
     * Language is always lower case, country is always upper case, script is always title
jtulach@1318
  1163
     * case, and extensions are always lower case.  Extensions and private use subtags
jtulach@1318
  1164
     * will be in canonical order as explained in {@link #toLanguageTag}.
jtulach@1318
  1165
     *
jtulach@1318
  1166
     * <p>When the locale has neither script nor extensions, the result is the same as in
jtulach@1318
  1167
     * Java 6 and prior.
jtulach@1318
  1168
     *
jtulach@1318
  1169
     * <p>If both the language and country fields are missing, this function will return
jtulach@1318
  1170
     * the empty string, even if the variant, script, or extensions field is present (you
jtulach@1318
  1171
     * can't have a locale with just a variant, the variant must accompany a well-formed
jtulach@1318
  1172
     * language or country code).
jtulach@1318
  1173
     *
jtulach@1318
  1174
     * <p>If script or extensions are present and variant is missing, no underscore is
jtulach@1318
  1175
     * added before the "#".
jtulach@1318
  1176
     *
jtulach@1318
  1177
     * <p>This behavior is designed to support debugging and to be compatible with
jtulach@1318
  1178
     * previous uses of <code>toString</code> that expected language, country, and variant
jtulach@1318
  1179
     * fields only.  To represent a Locale as a String for interchange purposes, use
jtulach@1318
  1180
     * {@link #toLanguageTag}.
jtulach@1318
  1181
     *
jtulach@1318
  1182
     * <p>Examples: <ul><tt>
jtulach@1318
  1183
     * <li>en
jtulach@1318
  1184
     * <li>de_DE
jtulach@1318
  1185
     * <li>_GB
jtulach@1318
  1186
     * <li>en_US_WIN
jtulach@1318
  1187
     * <li>de__POSIX
jtulach@1318
  1188
     * <li>zh_CN_#Hans
jtulach@1318
  1189
     * <li>zh_TW_#Hant-x-java
jtulach@1318
  1190
     * <li>th_TH_TH_#u-nu-thai</tt></ul>
jtulach@1318
  1191
     *
jtulach@1318
  1192
     * @return A string representation of the Locale, for debugging.
jtulach@1318
  1193
     * @see #getDisplayName
jtulach@1318
  1194
     * @see #toLanguageTag
jtulach@1318
  1195
     */
jtulach@1318
  1196
    @Override
jtulach@1318
  1197
    public final String toString() {
jtulach@1318
  1198
        boolean l = (baseLocale.getLanguage().length() != 0);
jtulach@1318
  1199
        boolean s = (baseLocale.getScript().length() != 0);
jtulach@1318
  1200
        boolean r = (baseLocale.getRegion().length() != 0);
jtulach@1318
  1201
        boolean v = (baseLocale.getVariant().length() != 0);
jtulach@1318
  1202
        boolean e = (localeExtensions != null && localeExtensions.getID().length() != 0);
jtulach@1318
  1203
jtulach@1318
  1204
        StringBuilder result = new StringBuilder(baseLocale.getLanguage());
jtulach@1318
  1205
        if (r || (l && (v || s || e))) {
jtulach@1318
  1206
            result.append('_')
jtulach@1318
  1207
                .append(baseLocale.getRegion()); // This may just append '_'
jtulach@1318
  1208
        }
jtulach@1318
  1209
        if (v && (l || r)) {
jtulach@1318
  1210
            result.append('_')
jtulach@1318
  1211
                .append(baseLocale.getVariant());
jtulach@1318
  1212
        }
jtulach@1318
  1213
jtulach@1318
  1214
        if (s && (l || r)) {
jtulach@1318
  1215
            result.append("_#")
jtulach@1318
  1216
                .append(baseLocale.getScript());
jtulach@1318
  1217
        }
jtulach@1318
  1218
jtulach@1318
  1219
        if (e && (l || r)) {
jtulach@1318
  1220
            result.append('_');
jtulach@1318
  1221
            if (!s) {
jtulach@1318
  1222
                result.append('#');
jtulach@1318
  1223
            }
jtulach@1318
  1224
            result.append(localeExtensions.getID());
jtulach@1318
  1225
        }
jtulach@1318
  1226
jtulach@1318
  1227
        return result.toString();
jtulach@1318
  1228
    }
jtulach@1318
  1229
jtulach@1318
  1230
    /**
jtulach@1318
  1231
     * Returns a well-formed IETF BCP 47 language tag representing
jtulach@1318
  1232
     * this locale.
jtulach@1318
  1233
     *
jtulach@1318
  1234
     * <p>If this <code>Locale</code> has a language, country, or
jtulach@1318
  1235
     * variant that does not satisfy the IETF BCP 47 language tag
jtulach@1318
  1236
     * syntax requirements, this method handles these fields as
jtulach@1318
  1237
     * described below:
jtulach@1318
  1238
     *
jtulach@1318
  1239
     * <p><b>Language:</b> If language is empty, or not <a
jtulach@1318
  1240
     * href="#def_language" >well-formed</a> (for example "a" or
jtulach@1318
  1241
     * "e2"), it will be emitted as "und" (Undetermined).
jtulach@1318
  1242
     *
jtulach@1318
  1243
     * <p><b>Country:</b> If country is not <a
jtulach@1318
  1244
     * href="#def_region">well-formed</a> (for example "12" or "USA"),
jtulach@1318
  1245
     * it will be omitted.
jtulach@1318
  1246
     *
jtulach@1318
  1247
     * <p><b>Variant:</b> If variant <b>is</b> <a
jtulach@1318
  1248
     * href="#def_variant">well-formed</a>, each sub-segment
jtulach@1318
  1249
     * (delimited by '-' or '_') is emitted as a subtag.  Otherwise:
jtulach@1318
  1250
     * <ul>
jtulach@1318
  1251
     *
jtulach@1318
  1252
     * <li>if all sub-segments match <code>[0-9a-zA-Z]{1,8}</code>
jtulach@1318
  1253
     * (for example "WIN" or "Oracle_JDK_Standard_Edition"), the first
jtulach@1318
  1254
     * ill-formed sub-segment and all following will be appended to
jtulach@1318
  1255
     * the private use subtag.  The first appended subtag will be
jtulach@1318
  1256
     * "lvariant", followed by the sub-segments in order, separated by
jtulach@1318
  1257
     * hyphen. For example, "x-lvariant-WIN",
jtulach@1318
  1258
     * "Oracle-x-lvariant-JDK-Standard-Edition".
jtulach@1318
  1259
     *
jtulach@1318
  1260
     * <li>if any sub-segment does not match
jtulach@1318
  1261
     * <code>[0-9a-zA-Z]{1,8}</code>, the variant will be truncated
jtulach@1318
  1262
     * and the problematic sub-segment and all following sub-segments
jtulach@1318
  1263
     * will be omitted.  If the remainder is non-empty, it will be
jtulach@1318
  1264
     * emitted as a private use subtag as above (even if the remainder
jtulach@1318
  1265
     * turns out to be well-formed).  For example,
jtulach@1318
  1266
     * "Solaris_isjustthecoolestthing" is emitted as
jtulach@1318
  1267
     * "x-lvariant-Solaris", not as "solaris".</li></ul>
jtulach@1318
  1268
     *
jtulach@1318
  1269
     * <p><b>Special Conversions:</b> Java supports some old locale
jtulach@1318
  1270
     * representations, including deprecated ISO language codes,
jtulach@1318
  1271
     * for compatibility. This method performs the following
jtulach@1318
  1272
     * conversions:
jtulach@1318
  1273
     * <ul>
jtulach@1318
  1274
     *
jtulach@1318
  1275
     * <li>Deprecated ISO language codes "iw", "ji", and "in" are
jtulach@1318
  1276
     * converted to "he", "yi", and "id", respectively.
jtulach@1318
  1277
     *
jtulach@1318
  1278
     * <li>A locale with language "no", country "NO", and variant
jtulach@1318
  1279
     * "NY", representing Norwegian Nynorsk (Norway), is converted
jtulach@1318
  1280
     * to a language tag "nn-NO".</li></ul>
jtulach@1318
  1281
     *
jtulach@1318
  1282
     * <p><b>Note:</b> Although the language tag created by this
jtulach@1318
  1283
     * method is well-formed (satisfies the syntax requirements
jtulach@1318
  1284
     * defined by the IETF BCP 47 specification), it is not
jtulach@1318
  1285
     * necessarily a valid BCP 47 language tag.  For example,
jtulach@1318
  1286
     * <pre>
jtulach@1318
  1287
     *   new Locale("xx", "YY").toLanguageTag();</pre>
jtulach@1318
  1288
     *
jtulach@1318
  1289
     * will return "xx-YY", but the language subtag "xx" and the
jtulach@1318
  1290
     * region subtag "YY" are invalid because they are not registered
jtulach@1318
  1291
     * in the IANA Language Subtag Registry.
jtulach@1318
  1292
     *
jtulach@1318
  1293
     * @return a BCP47 language tag representing the locale
jtulach@1318
  1294
     * @see #forLanguageTag(String)
jtulach@1318
  1295
     * @since 1.7
jtulach@1318
  1296
     */
jtulach@1318
  1297
    public String toLanguageTag() {
jtulach@1318
  1298
        LanguageTag tag = LanguageTag.parseLocale(baseLocale, localeExtensions);
jtulach@1318
  1299
        StringBuilder buf = new StringBuilder();
jtulach@1318
  1300
jtulach@1318
  1301
        String subtag = tag.getLanguage();
jtulach@1318
  1302
        if (subtag.length() > 0) {
jtulach@1318
  1303
            buf.append(LanguageTag.canonicalizeLanguage(subtag));
jtulach@1318
  1304
        }
jtulach@1318
  1305
jtulach@1318
  1306
        subtag = tag.getScript();
jtulach@1318
  1307
        if (subtag.length() > 0) {
jtulach@1318
  1308
            buf.append(LanguageTag.SEP);
jtulach@1318
  1309
            buf.append(LanguageTag.canonicalizeScript(subtag));
jtulach@1318
  1310
        }
jtulach@1318
  1311
jtulach@1318
  1312
        subtag = tag.getRegion();
jtulach@1318
  1313
        if (subtag.length() > 0) {
jtulach@1318
  1314
            buf.append(LanguageTag.SEP);
jtulach@1318
  1315
            buf.append(LanguageTag.canonicalizeRegion(subtag));
jtulach@1318
  1316
        }
jtulach@1318
  1317
jtulach@1318
  1318
        List<String>subtags = tag.getVariants();
jtulach@1318
  1319
        for (String s : subtags) {
jtulach@1318
  1320
            buf.append(LanguageTag.SEP);
jtulach@1318
  1321
            // preserve casing
jtulach@1318
  1322
            buf.append(s);
jtulach@1318
  1323
        }
jtulach@1318
  1324
jtulach@1318
  1325
        subtags = tag.getExtensions();
jtulach@1318
  1326
        for (String s : subtags) {
jtulach@1318
  1327
            buf.append(LanguageTag.SEP);
jtulach@1318
  1328
            buf.append(LanguageTag.canonicalizeExtension(s));
jtulach@1318
  1329
        }
jtulach@1318
  1330
jtulach@1318
  1331
        subtag = tag.getPrivateuse();
jtulach@1318
  1332
        if (subtag.length() > 0) {
jtulach@1318
  1333
            if (buf.length() > 0) {
jtulach@1318
  1334
                buf.append(LanguageTag.SEP);
jtulach@1318
  1335
            }
jtulach@1318
  1336
            buf.append(LanguageTag.PRIVATEUSE).append(LanguageTag.SEP);
jtulach@1318
  1337
            // preserve casing
jtulach@1318
  1338
            buf.append(subtag);
jtulach@1318
  1339
        }
jtulach@1318
  1340
jtulach@1318
  1341
        return buf.toString();
jtulach@1318
  1342
    }
jtulach@1318
  1343
jtulach@1318
  1344
    /**
jtulach@1318
  1345
     * Returns a locale for the specified IETF BCP 47 language tag string.
jtulach@1318
  1346
     *
jtulach@1318
  1347
     * <p>If the specified language tag contains any ill-formed subtags,
jtulach@1318
  1348
     * the first such subtag and all following subtags are ignored.  Compare
jtulach@1318
  1349
     * to {@link Locale.Builder#setLanguageTag} which throws an exception
jtulach@1318
  1350
     * in this case.
jtulach@1318
  1351
     *
jtulach@1318
  1352
     * <p>The following <b>conversions</b> are performed:<ul>
jtulach@1318
  1353
     *
jtulach@1318
  1354
     * <li>The language code "und" is mapped to language "".
jtulach@1318
  1355
     *
jtulach@1318
  1356
     * <li>The language codes "he", "yi", and "id" are mapped to "iw",
jtulach@1318
  1357
     * "ji", and "in" respectively. (This is the same canonicalization
jtulach@1318
  1358
     * that's done in Locale's constructors.)
jtulach@1318
  1359
     *
jtulach@1318
  1360
     * <li>The portion of a private use subtag prefixed by "lvariant",
jtulach@1318
  1361
     * if any, is removed and appended to the variant field in the
jtulach@1318
  1362
     * result locale (without case normalization).  If it is then
jtulach@1318
  1363
     * empty, the private use subtag is discarded:
jtulach@1318
  1364
     *
jtulach@1318
  1365
     * <pre>
jtulach@1318
  1366
     *     Locale loc;
jtulach@1318
  1367
     *     loc = Locale.forLanguageTag("en-US-x-lvariant-POSIX");
jtulach@1318
  1368
     *     loc.getVariant(); // returns "POSIX"
jtulach@1318
  1369
     *     loc.getExtension('x'); // returns null
jtulach@1318
  1370
     *
jtulach@1318
  1371
     *     loc = Locale.forLanguageTag("de-POSIX-x-URP-lvariant-Abc-Def");
jtulach@1318
  1372
     *     loc.getVariant(); // returns "POSIX_Abc_Def"
jtulach@1318
  1373
     *     loc.getExtension('x'); // returns "urp"
jtulach@1318
  1374
     * </pre>
jtulach@1318
  1375
     *
jtulach@1318
  1376
     * <li>When the languageTag argument contains an extlang subtag,
jtulach@1318
  1377
     * the first such subtag is used as the language, and the primary
jtulach@1318
  1378
     * language subtag and other extlang subtags are ignored:
jtulach@1318
  1379
     *
jtulach@1318
  1380
     * <pre>
jtulach@1318
  1381
     *     Locale.forLanguageTag("ar-aao").getLanguage(); // returns "aao"
jtulach@1318
  1382
     *     Locale.forLanguageTag("en-abc-def-us").toString(); // returns "abc_US"
jtulach@1318
  1383
     * </pre>
jtulach@1318
  1384
     *
jtulach@1318
  1385
     * <li>Case is normalized except for variant tags, which are left
jtulach@1318
  1386
     * unchanged.  Language is normalized to lower case, script to
jtulach@1318
  1387
     * title case, country to upper case, and extensions to lower
jtulach@1318
  1388
     * case.
jtulach@1318
  1389
     *
jtulach@1318
  1390
     * <li>If, after processing, the locale would exactly match either
jtulach@1318
  1391
     * ja_JP_JP or th_TH_TH with no extensions, the appropriate
jtulach@1318
  1392
     * extensions are added as though the constructor had been called:
jtulach@1318
  1393
     *
jtulach@1318
  1394
     * <pre>
jtulach@1318
  1395
     *    Locale.forLanguageTag("ja-JP-x-lvariant-JP").toLanguageTag();
jtulach@1318
  1396
     *    // returns "ja-JP-u-ca-japanese-x-lvariant-JP"
jtulach@1318
  1397
     *    Locale.forLanguageTag("th-TH-x-lvariant-TH").toLanguageTag();
jtulach@1318
  1398
     *    // returns "th-TH-u-nu-thai-x-lvariant-TH"
jtulach@1318
  1399
     * <pre></ul>
jtulach@1318
  1400
     *
jtulach@1318
  1401
     * <p>This implements the 'Language-Tag' production of BCP47, and
jtulach@1318
  1402
     * so supports grandfathered (regular and irregular) as well as
jtulach@1318
  1403
     * private use language tags.  Stand alone private use tags are
jtulach@1318
  1404
     * represented as empty language and extension 'x-whatever',
jtulach@1318
  1405
     * and grandfathered tags are converted to their canonical replacements
jtulach@1318
  1406
     * where they exist.
jtulach@1318
  1407
     *
jtulach@1318
  1408
     * <p>Grandfathered tags with canonical replacements are as follows:
jtulach@1318
  1409
     *
jtulach@1318
  1410
     * <table>
jtulach@1318
  1411
     * <tbody align="center">
jtulach@1318
  1412
     * <tr><th>grandfathered tag</th><th>&nbsp;</th><th>modern replacement</th></tr>
jtulach@1318
  1413
     * <tr><td>art-lojban</td><td>&nbsp;</td><td>jbo</td></tr>
jtulach@1318
  1414
     * <tr><td>i-ami</td><td>&nbsp;</td><td>ami</td></tr>
jtulach@1318
  1415
     * <tr><td>i-bnn</td><td>&nbsp;</td><td>bnn</td></tr>
jtulach@1318
  1416
     * <tr><td>i-hak</td><td>&nbsp;</td><td>hak</td></tr>
jtulach@1318
  1417
     * <tr><td>i-klingon</td><td>&nbsp;</td><td>tlh</td></tr>
jtulach@1318
  1418
     * <tr><td>i-lux</td><td>&nbsp;</td><td>lb</td></tr>
jtulach@1318
  1419
     * <tr><td>i-navajo</td><td>&nbsp;</td><td>nv</td></tr>
jtulach@1318
  1420
     * <tr><td>i-pwn</td><td>&nbsp;</td><td>pwn</td></tr>
jtulach@1318
  1421
     * <tr><td>i-tao</td><td>&nbsp;</td><td>tao</td></tr>
jtulach@1318
  1422
     * <tr><td>i-tay</td><td>&nbsp;</td><td>tay</td></tr>
jtulach@1318
  1423
     * <tr><td>i-tsu</td><td>&nbsp;</td><td>tsu</td></tr>
jtulach@1318
  1424
     * <tr><td>no-bok</td><td>&nbsp;</td><td>nb</td></tr>
jtulach@1318
  1425
     * <tr><td>no-nyn</td><td>&nbsp;</td><td>nn</td></tr>
jtulach@1318
  1426
     * <tr><td>sgn-BE-FR</td><td>&nbsp;</td><td>sfb</td></tr>
jtulach@1318
  1427
     * <tr><td>sgn-BE-NL</td><td>&nbsp;</td><td>vgt</td></tr>
jtulach@1318
  1428
     * <tr><td>sgn-CH-DE</td><td>&nbsp;</td><td>sgg</td></tr>
jtulach@1318
  1429
     * <tr><td>zh-guoyu</td><td>&nbsp;</td><td>cmn</td></tr>
jtulach@1318
  1430
     * <tr><td>zh-hakka</td><td>&nbsp;</td><td>hak</td></tr>
jtulach@1318
  1431
     * <tr><td>zh-min-nan</td><td>&nbsp;</td><td>nan</td></tr>
jtulach@1318
  1432
     * <tr><td>zh-xiang</td><td>&nbsp;</td><td>hsn</td></tr>
jtulach@1318
  1433
     * </tbody>
jtulach@1318
  1434
     * </table>
jtulach@1318
  1435
     *
jtulach@1318
  1436
     * <p>Grandfathered tags with no modern replacement will be
jtulach@1318
  1437
     * converted as follows:
jtulach@1318
  1438
     *
jtulach@1318
  1439
     * <table>
jtulach@1318
  1440
     * <tbody align="center">
jtulach@1318
  1441
     * <tr><th>grandfathered tag</th><th>&nbsp;</th><th>converts to</th></tr>
jtulach@1318
  1442
     * <tr><td>cel-gaulish</td><td>&nbsp;</td><td>xtg-x-cel-gaulish</td></tr>
jtulach@1318
  1443
     * <tr><td>en-GB-oed</td><td>&nbsp;</td><td>en-GB-x-oed</td></tr>
jtulach@1318
  1444
     * <tr><td>i-default</td><td>&nbsp;</td><td>en-x-i-default</td></tr>
jtulach@1318
  1445
     * <tr><td>i-enochian</td><td>&nbsp;</td><td>und-x-i-enochian</td></tr>
jtulach@1318
  1446
     * <tr><td>i-mingo</td><td>&nbsp;</td><td>see-x-i-mingo</td></tr>
jtulach@1318
  1447
     * <tr><td>zh-min</td><td>&nbsp;</td><td>nan-x-zh-min</td></tr>
jtulach@1318
  1448
     * </tbody>
jtulach@1318
  1449
     * </table>
jtulach@1318
  1450
     *
jtulach@1318
  1451
     * <p>For a list of all grandfathered tags, see the
jtulach@1318
  1452
     * IANA Language Subtag Registry (search for "Type: grandfathered").
jtulach@1318
  1453
     *
jtulach@1318
  1454
     * <p><b>Note</b>: there is no guarantee that <code>toLanguageTag</code>
jtulach@1318
  1455
     * and <code>forLanguageTag</code> will round-trip.
jtulach@1318
  1456
     *
jtulach@1318
  1457
     * @param languageTag the language tag
jtulach@1318
  1458
     * @return The locale that best represents the language tag.
jtulach@1318
  1459
     * @throws NullPointerException if <code>languageTag</code> is <code>null</code>
jtulach@1318
  1460
     * @see #toLanguageTag()
jtulach@1318
  1461
     * @see java.util.Locale.Builder#setLanguageTag(String)
jtulach@1318
  1462
     * @since 1.7
jtulach@1318
  1463
     */
jtulach@1318
  1464
    public static Locale forLanguageTag(String languageTag) {
jtulach@1318
  1465
        LanguageTag tag = LanguageTag.parse(languageTag, null);
jtulach@1318
  1466
        InternalLocaleBuilder bldr = new InternalLocaleBuilder();
jtulach@1318
  1467
        bldr.setLanguageTag(tag);
jtulach@1318
  1468
        BaseLocale base = bldr.getBaseLocale();
jtulach@1318
  1469
        LocaleExtensions exts = bldr.getLocaleExtensions();
jtulach@1318
  1470
        if (exts == null && base.getVariant().length() > 0) {
jtulach@1318
  1471
            exts = getCompatibilityExtensions(base.getLanguage(), base.getScript(),
jtulach@1318
  1472
                                              base.getRegion(), base.getVariant());
jtulach@1318
  1473
        }
jtulach@1318
  1474
        return getInstance(base, exts);
jtulach@1318
  1475
    }
jtulach@1318
  1476
jtulach@1318
  1477
    /**
jtulach@1318
  1478
     * Returns a three-letter abbreviation of this locale's language.
jtulach@1318
  1479
     * If the language matches an ISO 639-1 two-letter code, the
jtulach@1318
  1480
     * corresponding ISO 639-2/T three-letter lowercase code is
jtulach@1318
  1481
     * returned.  The ISO 639-2 language codes can be found on-line,
jtulach@1318
  1482
     * see "Codes for the Representation of Names of Languages Part 2:
jtulach@1318
  1483
     * Alpha-3 Code".  If the locale specifies a three-letter
jtulach@1318
  1484
     * language, the language is returned as is.  If the locale does
jtulach@1318
  1485
     * not specify a language the empty string is returned.
jtulach@1318
  1486
     *
jtulach@1318
  1487
     * @return A three-letter abbreviation of this locale's language.
jtulach@1318
  1488
     * @exception MissingResourceException Throws MissingResourceException if
jtulach@1318
  1489
     * three-letter language abbreviation is not available for this locale.
jtulach@1318
  1490
     */
jtulach@1318
  1491
    public String getISO3Language() throws MissingResourceException {
jtulach@1318
  1492
        String lang = baseLocale.getLanguage();
jtulach@1318
  1493
        if (lang.length() == 3) {
jtulach@1318
  1494
            return lang;
jtulach@1318
  1495
        }
jtulach@1318
  1496
jtulach@1318
  1497
        String language3 = getISO3Code(lang, LocaleISOData.isoLanguageTable);
jtulach@1318
  1498
        if (language3 == null) {
jtulach@1318
  1499
            throw new MissingResourceException("Couldn't find 3-letter language code for "
jtulach@1318
  1500
                    + lang, "FormatData_" + toString(), "ShortLanguage");
jtulach@1318
  1501
        }
jtulach@1318
  1502
        return language3;
jtulach@1318
  1503
    }
jtulach@1318
  1504
jtulach@1318
  1505
    /**
jtulach@1318
  1506
     * Returns a three-letter abbreviation for this locale's country.
jtulach@1318
  1507
     * If the country matches an ISO 3166-1 alpha-2 code, the
jtulach@1318
  1508
     * corresponding ISO 3166-1 alpha-3 uppercase code is returned.
jtulach@1318
  1509
     * If the locale doesn't specify a country, this will be the empty
jtulach@1318
  1510
     * string.
jtulach@1318
  1511
     *
jtulach@1318
  1512
     * <p>The ISO 3166-1 codes can be found on-line.
jtulach@1318
  1513
     *
jtulach@1318
  1514
     * @return A three-letter abbreviation of this locale's country.
jtulach@1318
  1515
     * @exception MissingResourceException Throws MissingResourceException if the
jtulach@1318
  1516
     * three-letter country abbreviation is not available for this locale.
jtulach@1318
  1517
     */
jtulach@1318
  1518
    public String getISO3Country() throws MissingResourceException {
jtulach@1318
  1519
        String country3 = getISO3Code(baseLocale.getRegion(), LocaleISOData.isoCountryTable);
jtulach@1318
  1520
        if (country3 == null) {
jtulach@1318
  1521
            throw new MissingResourceException("Couldn't find 3-letter country code for "
jtulach@1318
  1522
                    + baseLocale.getRegion(), "FormatData_" + toString(), "ShortCountry");
jtulach@1318
  1523
        }
jtulach@1318
  1524
        return country3;
jtulach@1318
  1525
    }
jtulach@1318
  1526
jtulach@1318
  1527
    private static final String getISO3Code(String iso2Code, String table) {
jtulach@1318
  1528
        int codeLength = iso2Code.length();
jtulach@1318
  1529
        if (codeLength == 0) {
jtulach@1318
  1530
            return "";
jtulach@1318
  1531
        }
jtulach@1318
  1532
jtulach@1318
  1533
        int tableLength = table.length();
jtulach@1318
  1534
        int index = tableLength;
jtulach@1318
  1535
        if (codeLength == 2) {
jtulach@1318
  1536
            char c1 = iso2Code.charAt(0);
jtulach@1318
  1537
            char c2 = iso2Code.charAt(1);
jtulach@1318
  1538
            for (index = 0; index < tableLength; index += 5) {
jtulach@1318
  1539
                if (table.charAt(index) == c1
jtulach@1318
  1540
                    && table.charAt(index + 1) == c2) {
jtulach@1318
  1541
                    break;
jtulach@1318
  1542
                }
jtulach@1318
  1543
            }
jtulach@1318
  1544
        }
jtulach@1318
  1545
        return index < tableLength ? table.substring(index + 2, index + 5) : null;
jtulach@1318
  1546
    }
jtulach@1318
  1547
jtulach@1318
  1548
    /**
jtulach@1318
  1549
     * Returns a name for the locale's language that is appropriate for display to the
jtulach@1318
  1550
     * user.
jtulach@1318
  1551
     * If possible, the name returned will be localized for the default locale.
jtulach@1318
  1552
     * For example, if the locale is fr_FR and the default locale
jtulach@1318
  1553
     * is en_US, getDisplayLanguage() will return "French"; if the locale is en_US and
jtulach@1318
  1554
     * the default locale is fr_FR, getDisplayLanguage() will return "anglais".
jtulach@1318
  1555
     * If the name returned cannot be localized for the default locale,
jtulach@1318
  1556
     * (say, we don't have a Japanese name for Croatian),
jtulach@1318
  1557
     * this function falls back on the English name, and uses the ISO code as a last-resort
jtulach@1318
  1558
     * value.  If the locale doesn't specify a language, this function returns the empty string.
jtulach@1318
  1559
     */
jtulach@1318
  1560
    public final String getDisplayLanguage() {
jtulach@1318
  1561
        return getDisplayLanguage(getDefault(Category.DISPLAY));
jtulach@1318
  1562
    }
jtulach@1318
  1563
jtulach@1318
  1564
    /**
jtulach@1318
  1565
     * Returns a name for the locale's language that is appropriate for display to the
jtulach@1318
  1566
     * user.
jtulach@1318
  1567
     * If possible, the name returned will be localized according to inLocale.
jtulach@1318
  1568
     * For example, if the locale is fr_FR and inLocale
jtulach@1318
  1569
     * is en_US, getDisplayLanguage() will return "French"; if the locale is en_US and
jtulach@1318
  1570
     * inLocale is fr_FR, getDisplayLanguage() will return "anglais".
jtulach@1318
  1571
     * If the name returned cannot be localized according to inLocale,
jtulach@1318
  1572
     * (say, we don't have a Japanese name for Croatian),
jtulach@1318
  1573
     * this function falls back on the English name, and finally
jtulach@1318
  1574
     * on the ISO code as a last-resort value.  If the locale doesn't specify a language,
jtulach@1318
  1575
     * this function returns the empty string.
jtulach@1318
  1576
     *
jtulach@1318
  1577
     * @exception NullPointerException if <code>inLocale</code> is <code>null</code>
jtulach@1318
  1578
     */
jtulach@1318
  1579
    public String getDisplayLanguage(Locale inLocale) {
jtulach@1318
  1580
        return getDisplayString(baseLocale.getLanguage(), inLocale, DISPLAY_LANGUAGE);
jtulach@1318
  1581
    }
jtulach@1318
  1582
jtulach@1318
  1583
    /**
jtulach@1318
  1584
     * Returns a name for the the locale's script that is appropriate for display to
jtulach@1318
  1585
     * the user. If possible, the name will be localized for the default locale.  Returns
jtulach@1318
  1586
     * the empty string if this locale doesn't specify a script code.
jtulach@1318
  1587
     *
jtulach@1318
  1588
     * @return the display name of the script code for the current default locale
jtulach@1318
  1589
     * @since 1.7
jtulach@1318
  1590
     */
jtulach@1318
  1591
    public String getDisplayScript() {
jtulach@1318
  1592
        return getDisplayScript(getDefault());
jtulach@1318
  1593
    }
jtulach@1318
  1594
jtulach@1318
  1595
    /**
jtulach@1318
  1596
     * Returns a name for the locale's script that is appropriate
jtulach@1318
  1597
     * for display to the user. If possible, the name will be
jtulach@1318
  1598
     * localized for the given locale. Returns the empty string if
jtulach@1318
  1599
     * this locale doesn't specify a script code.
jtulach@1318
  1600
     *
jtulach@1318
  1601
     * @return the display name of the script code for the current default locale
jtulach@1318
  1602
     * @throws NullPointerException if <code>inLocale</code> is <code>null</code>
jtulach@1318
  1603
     * @since 1.7
jtulach@1318
  1604
     */
jtulach@1318
  1605
    public String getDisplayScript(Locale inLocale) {
jtulach@1318
  1606
        return getDisplayString(baseLocale.getScript(), inLocale, DISPLAY_SCRIPT);
jtulach@1318
  1607
    }
jtulach@1318
  1608
jtulach@1318
  1609
    /**
jtulach@1318
  1610
     * Returns a name for the locale's country that is appropriate for display to the
jtulach@1318
  1611
     * user.
jtulach@1318
  1612
     * If possible, the name returned will be localized for the default locale.
jtulach@1318
  1613
     * For example, if the locale is fr_FR and the default locale
jtulach@1318
  1614
     * is en_US, getDisplayCountry() will return "France"; if the locale is en_US and
jtulach@1318
  1615
     * the default locale is fr_FR, getDisplayCountry() will return "Etats-Unis".
jtulach@1318
  1616
     * If the name returned cannot be localized for the default locale,
jtulach@1318
  1617
     * (say, we don't have a Japanese name for Croatia),
jtulach@1318
  1618
     * this function falls back on the English name, and uses the ISO code as a last-resort
jtulach@1318
  1619
     * value.  If the locale doesn't specify a country, this function returns the empty string.
jtulach@1318
  1620
     */
jtulach@1318
  1621
    public final String getDisplayCountry() {
jtulach@1318
  1622
        return getDisplayCountry(getDefault(Category.DISPLAY));
jtulach@1318
  1623
    }
jtulach@1318
  1624
jtulach@1318
  1625
    /**
jtulach@1318
  1626
     * Returns a name for the locale's country that is appropriate for display to the
jtulach@1318
  1627
     * user.
jtulach@1318
  1628
     * If possible, the name returned will be localized according to inLocale.
jtulach@1318
  1629
     * For example, if the locale is fr_FR and inLocale
jtulach@1318
  1630
     * is en_US, getDisplayCountry() will return "France"; if the locale is en_US and
jtulach@1318
  1631
     * inLocale is fr_FR, getDisplayCountry() will return "Etats-Unis".
jtulach@1318
  1632
     * If the name returned cannot be localized according to inLocale.
jtulach@1318
  1633
     * (say, we don't have a Japanese name for Croatia),
jtulach@1318
  1634
     * this function falls back on the English name, and finally
jtulach@1318
  1635
     * on the ISO code as a last-resort value.  If the locale doesn't specify a country,
jtulach@1318
  1636
     * this function returns the empty string.
jtulach@1318
  1637
     *
jtulach@1318
  1638
     * @exception NullPointerException if <code>inLocale</code> is <code>null</code>
jtulach@1318
  1639
     */
jtulach@1318
  1640
    public String getDisplayCountry(Locale inLocale) {
jtulach@1318
  1641
        return getDisplayString(baseLocale.getRegion(), inLocale, DISPLAY_COUNTRY);
jtulach@1318
  1642
    }
jtulach@1318
  1643
jtulach@1318
  1644
    private String getDisplayString(String code, Locale inLocale, int type) {
jtulach@1318
  1645
        if (code.length() == 0) {
jtulach@1318
  1646
            return "";
jtulach@1318
  1647
        }
jtulach@1318
  1648
jtulach@1318
  1649
        if (inLocale == null) {
jtulach@1318
  1650
            throw new NullPointerException();
jtulach@1318
  1651
        }
jtulach@1318
  1652
jtulach@1318
  1653
        try {
jtulach@1318
  1654
            OpenListResourceBundle bundle = LocaleData.getLocaleNames(inLocale);
jtulach@1318
  1655
            String key = (type == DISPLAY_VARIANT ? "%%"+code : code);
jtulach@1318
  1656
            String result = null;
jtulach@1318
  1657
jtulach@1318
  1658
            // Check whether a provider can provide an implementation that's closer
jtulach@1318
  1659
            // to the requested locale than what the Java runtime itself can provide.
jtulach@1318
  1660
            LocaleServiceProviderPool pool =
jtulach@1318
  1661
                LocaleServiceProviderPool.getPool(LocaleNameProvider.class);
jtulach@1318
  1662
            if (pool.hasProviders()) {
jtulach@1318
  1663
                result = pool.getLocalizedObject(
jtulach@1318
  1664
                                    LocaleNameGetter.INSTANCE,
jtulach@1318
  1665
                                    inLocale, bundle, key,
jtulach@1318
  1666
                                    type, code);
jtulach@1318
  1667
            }
jtulach@1318
  1668
jtulach@1318
  1669
            if (result == null) {
jtulach@1318
  1670
                result = bundle.getString(key);
jtulach@1318
  1671
            }
jtulach@1318
  1672
jtulach@1318
  1673
            if (result != null) {
jtulach@1318
  1674
                return result;
jtulach@1318
  1675
            }
jtulach@1318
  1676
        }
jtulach@1318
  1677
        catch (Exception e) {
jtulach@1318
  1678
            // just fall through
jtulach@1318
  1679
        }
jtulach@1318
  1680
        return code;
jtulach@1318
  1681
    }
jtulach@1318
  1682
jtulach@1318
  1683
    /**
jtulach@1318
  1684
     * Returns a name for the locale's variant code that is appropriate for display to the
jtulach@1318
  1685
     * user.  If possible, the name will be localized for the default locale.  If the locale
jtulach@1318
  1686
     * doesn't specify a variant code, this function returns the empty string.
jtulach@1318
  1687
     */
jtulach@1318
  1688
    public final String getDisplayVariant() {
jtulach@1318
  1689
        return getDisplayVariant(getDefault(Category.DISPLAY));
jtulach@1318
  1690
    }
jtulach@1318
  1691
jtulach@1318
  1692
    /**
jtulach@1318
  1693
     * Returns a name for the locale's variant code that is appropriate for display to the
jtulach@1318
  1694
     * user.  If possible, the name will be localized for inLocale.  If the locale
jtulach@1318
  1695
     * doesn't specify a variant code, this function returns the empty string.
jtulach@1318
  1696
     *
jtulach@1318
  1697
     * @exception NullPointerException if <code>inLocale</code> is <code>null</code>
jtulach@1318
  1698
     */
jtulach@1318
  1699
    public String getDisplayVariant(Locale inLocale) {
jtulach@1318
  1700
        if (baseLocale.getVariant().length() == 0)
jtulach@1318
  1701
            return "";
jtulach@1318
  1702
jtulach@1318
  1703
        OpenListResourceBundle bundle = LocaleData.getLocaleNames(inLocale);
jtulach@1318
  1704
jtulach@1318
  1705
        String names[] = getDisplayVariantArray(bundle, inLocale);
jtulach@1318
  1706
jtulach@1318
  1707
        // Get the localized patterns for formatting a list, and use
jtulach@1318
  1708
        // them to format the list.
jtulach@1318
  1709
        String listPattern = null;
jtulach@1318
  1710
        String listCompositionPattern = null;
jtulach@1318
  1711
        try {
jtulach@1318
  1712
            listPattern = bundle.getString("ListPattern");
jtulach@1318
  1713
            listCompositionPattern = bundle.getString("ListCompositionPattern");
jtulach@1318
  1714
        } catch (MissingResourceException e) {
jtulach@1318
  1715
        }
jtulach@1318
  1716
        return formatList(names, listPattern, listCompositionPattern);
jtulach@1318
  1717
    }
jtulach@1318
  1718
jtulach@1318
  1719
    /**
jtulach@1318
  1720
     * Returns a name for the locale that is appropriate for display to the
jtulach@1318
  1721
     * user. This will be the values returned by getDisplayLanguage(),
jtulach@1318
  1722
     * getDisplayScript(), getDisplayCountry(), and getDisplayVariant() assembled
jtulach@1318
  1723
     * into a single string. The the non-empty values are used in order,
jtulach@1318
  1724
     * with the second and subsequent names in parentheses.  For example:
jtulach@1318
  1725
     * <blockquote>
jtulach@1318
  1726
     * language (script, country, variant)<br>
jtulach@1318
  1727
     * language (country)<br>
jtulach@1318
  1728
     * language (variant)<br>
jtulach@1318
  1729
     * script (country)<br>
jtulach@1318
  1730
     * country<br>
jtulach@1318
  1731
     * </blockquote>
jtulach@1318
  1732
     * depending on which fields are specified in the locale.  If the
jtulach@1318
  1733
     * language, sacript, country, and variant fields are all empty,
jtulach@1318
  1734
     * this function returns the empty string.
jtulach@1318
  1735
     */
jtulach@1318
  1736
    public final String getDisplayName() {
jtulach@1318
  1737
        return getDisplayName(getDefault(Category.DISPLAY));
jtulach@1318
  1738
    }
jtulach@1318
  1739
jtulach@1318
  1740
    /**
jtulach@1318
  1741
     * Returns a name for the locale that is appropriate for display
jtulach@1318
  1742
     * to the user.  This will be the values returned by
jtulach@1318
  1743
     * getDisplayLanguage(), getDisplayScript(),getDisplayCountry(),
jtulach@1318
  1744
     * and getDisplayVariant() assembled into a single string.
jtulach@1318
  1745
     * The non-empty values are used in order,
jtulach@1318
  1746
     * with the second and subsequent names in parentheses.  For example:
jtulach@1318
  1747
     * <blockquote>
jtulach@1318
  1748
     * language (script, country, variant)<br>
jtulach@1318
  1749
     * language (country)<br>
jtulach@1318
  1750
     * language (variant)<br>
jtulach@1318
  1751
     * script (country)<br>
jtulach@1318
  1752
     * country<br>
jtulach@1318
  1753
     * </blockquote>
jtulach@1318
  1754
     * depending on which fields are specified in the locale.  If the
jtulach@1318
  1755
     * language, script, country, and variant fields are all empty,
jtulach@1318
  1756
     * this function returns the empty string.
jtulach@1318
  1757
     *
jtulach@1318
  1758
     * @throws NullPointerException if <code>inLocale</code> is <code>null</code>
jtulach@1318
  1759
     */
jtulach@1318
  1760
    public String getDisplayName(Locale inLocale) {
jtulach@1318
  1761
        OpenListResourceBundle bundle = LocaleData.getLocaleNames(inLocale);
jtulach@1318
  1762
jtulach@1318
  1763
        String languageName = getDisplayLanguage(inLocale);
jtulach@1318
  1764
        String scriptName = getDisplayScript(inLocale);
jtulach@1318
  1765
        String countryName = getDisplayCountry(inLocale);
jtulach@1318
  1766
        String[] variantNames = getDisplayVariantArray(bundle, inLocale);
jtulach@1318
  1767
jtulach@1318
  1768
        // Get the localized patterns for formatting a display name.
jtulach@1318
  1769
        String displayNamePattern = null;
jtulach@1318
  1770
        String listPattern = null;
jtulach@1318
  1771
        String listCompositionPattern = null;
jtulach@1318
  1772
        try {
jtulach@1318
  1773
            displayNamePattern = bundle.getString("DisplayNamePattern");
jtulach@1318
  1774
            listPattern = bundle.getString("ListPattern");
jtulach@1318
  1775
            listCompositionPattern = bundle.getString("ListCompositionPattern");
jtulach@1318
  1776
        } catch (MissingResourceException e) {
jtulach@1318
  1777
        }
jtulach@1318
  1778
jtulach@1318
  1779
        // The display name consists of a main name, followed by qualifiers.
jtulach@1318
  1780
        // Typically, the format is "MainName (Qualifier, Qualifier)" but this
jtulach@1318
  1781
        // depends on what pattern is stored in the display locale.
jtulach@1318
  1782
        String   mainName       = null;
jtulach@1318
  1783
        String[] qualifierNames = null;
jtulach@1318
  1784
jtulach@1318
  1785
        // The main name is the language, or if there is no language, the script,
jtulach@1318
  1786
        // then if no script, the country. If there is no language/script/country
jtulach@1318
  1787
        // (an anomalous situation) then the display name is simply the variant's
jtulach@1318
  1788
        // display name.
jtulach@1318
  1789
        if (languageName.length() == 0 && scriptName.length() == 0 && countryName.length() == 0) {
jtulach@1318
  1790
            if (variantNames.length == 0) {
jtulach@1318
  1791
                return "";
jtulach@1318
  1792
            } else {
jtulach@1318
  1793
                return formatList(variantNames, listPattern, listCompositionPattern);
jtulach@1318
  1794
            }
jtulach@1318
  1795
        }
jtulach@1318
  1796
        ArrayList<String> names = new ArrayList<>(4);
jtulach@1318
  1797
        if (languageName.length() != 0) {
jtulach@1318
  1798
            names.add(languageName);
jtulach@1318
  1799
        }
jtulach@1318
  1800
        if (scriptName.length() != 0) {
jtulach@1318
  1801
            names.add(scriptName);
jtulach@1318
  1802
        }
jtulach@1318
  1803
        if (countryName.length() != 0) {
jtulach@1318
  1804
            names.add(countryName);
jtulach@1318
  1805
        }
jtulach@1318
  1806
        if (variantNames.length != 0) {
jtulach@1318
  1807
            for (String var : variantNames) {
jtulach@1318
  1808
                names.add(var);
jtulach@1318
  1809
            }
jtulach@1318
  1810
        }
jtulach@1318
  1811
jtulach@1318
  1812
        // The first one in the main name
jtulach@1318
  1813
        mainName = names.get(0);
jtulach@1318
  1814
jtulach@1318
  1815
        // Others are qualifiers
jtulach@1318
  1816
        int numNames = names.size();
jtulach@1318
  1817
        qualifierNames = (numNames > 1) ?
jtulach@1318
  1818
                names.subList(1, numNames).toArray(new String[numNames - 1]) : new String[0];
jtulach@1318
  1819
jtulach@1318
  1820
        // Create an array whose first element is the number of remaining
jtulach@1318
  1821
        // elements.  This serves as a selector into a ChoiceFormat pattern from
jtulach@1318
  1822
        // the resource.  The second and third elements are the main name and
jtulach@1318
  1823
        // the qualifier; if there are no qualifiers, the third element is
jtulach@1318
  1824
        // unused by the format pattern.
jtulach@1318
  1825
        Object[] displayNames = {
jtulach@1318
  1826
            new Integer(qualifierNames.length != 0 ? 2 : 1),
jtulach@1318
  1827
            mainName,
jtulach@1318
  1828
            // We could also just call formatList() and have it handle the empty
jtulach@1318
  1829
            // list case, but this is more efficient, and we want it to be
jtulach@1318
  1830
            // efficient since all the language-only locales will not have any
jtulach@1318
  1831
            // qualifiers.
jtulach@1318
  1832
            qualifierNames.length != 0 ? formatList(qualifierNames, listPattern, listCompositionPattern) : null
jtulach@1318
  1833
        };
jtulach@1318
  1834
jtulach@1318
  1835
        if (displayNamePattern != null) {
jtulach@1318
  1836
            return new MessageFormat(displayNamePattern).format(displayNames);
jtulach@1318
  1837
        }
jtulach@1318
  1838
        else {
jtulach@1318
  1839
            // If we cannot get the message format pattern, then we use a simple
jtulach@1318
  1840
            // hard-coded pattern.  This should not occur in practice unless the
jtulach@1318
  1841
            // installation is missing some core files (FormatData etc.).
jtulach@1318
  1842
            StringBuilder result = new StringBuilder();
jtulach@1318
  1843
            result.append((String)displayNames[1]);
jtulach@1318
  1844
            if (displayNames.length > 2) {
jtulach@1318
  1845
                result.append(" (");
jtulach@1318
  1846
                result.append((String)displayNames[2]);
jtulach@1318
  1847
                result.append(')');
jtulach@1318
  1848
            }
jtulach@1318
  1849
            return result.toString();
jtulach@1318
  1850
        }
jtulach@1318
  1851
    }
jtulach@1318
  1852
jtulach@1318
  1853
    /**
jtulach@1318
  1854
     * Overrides Cloneable.
jtulach@1318
  1855
     */
jtulach@1318
  1856
    public Object clone()
jtulach@1318
  1857
    {
jtulach@1318
  1858
        try {
jtulach@1318
  1859
            Locale that = (Locale)super.clone();
jtulach@1318
  1860
            return that;
jtulach@1318
  1861
        } catch (CloneNotSupportedException e) {
jtulach@1318
  1862
            throw new InternalError();
jtulach@1318
  1863
        }
jtulach@1318
  1864
    }
jtulach@1318
  1865
jtulach@1318
  1866
    /**
jtulach@1318
  1867
     * Override hashCode.
jtulach@1318
  1868
     * Since Locales are often used in hashtables, caches the value
jtulach@1318
  1869
     * for speed.
jtulach@1318
  1870
     */
jtulach@1318
  1871
    @Override
jtulach@1318
  1872
    public int hashCode() {
jtulach@1318
  1873
        int hc = hashCodeValue;
jtulach@1318
  1874
        if (hc == 0) {
jtulach@1318
  1875
            hc = baseLocale.hashCode();
jtulach@1318
  1876
            if (localeExtensions != null) {
jtulach@1318
  1877
                hc ^= localeExtensions.hashCode();
jtulach@1318
  1878
            }
jtulach@1318
  1879
            hashCodeValue = hc;
jtulach@1318
  1880
        }
jtulach@1318
  1881
        return hc;
jtulach@1318
  1882
    }
jtulach@1318
  1883
jtulach@1318
  1884
    // Overrides
jtulach@1318
  1885
jtulach@1318
  1886
    /**
jtulach@1318
  1887
     * Returns true if this Locale is equal to another object.  A Locale is
jtulach@1318
  1888
     * deemed equal to another Locale with identical language, script, country,
jtulach@1318
  1889
     * variant and extensions, and unequal to all other objects.
jtulach@1318
  1890
     *
jtulach@1318
  1891
     * @return true if this Locale is equal to the specified object.
jtulach@1318
  1892
     */
jtulach@1318
  1893
    @Override
jtulach@1318
  1894
    public boolean equals(Object obj) {
jtulach@1318
  1895
        if (this == obj)                      // quick check
jtulach@1318
  1896
            return true;
jtulach@1318
  1897
        if (!(obj instanceof Locale))
jtulach@1318
  1898
            return false;
jtulach@1318
  1899
        BaseLocale otherBase = ((Locale)obj).baseLocale;
jtulach@1318
  1900
        if (!baseLocale.equals(otherBase)) {
jtulach@1318
  1901
            return false;
jtulach@1318
  1902
        }
jtulach@1318
  1903
        if (localeExtensions == null) {
jtulach@1318
  1904
            return ((Locale)obj).localeExtensions == null;
jtulach@1318
  1905
        }
jtulach@1318
  1906
        return localeExtensions.equals(((Locale)obj).localeExtensions);
jtulach@1318
  1907
    }
jtulach@1318
  1908
jtulach@1318
  1909
    // ================= privates =====================================
jtulach@1318
  1910
jtulach@1318
  1911
    private transient BaseLocale baseLocale;
jtulach@1318
  1912
    private transient LocaleExtensions localeExtensions;
jtulach@1318
  1913
jtulach@1318
  1914
    /**
jtulach@1318
  1915
     * Calculated hashcode
jtulach@1318
  1916
     */
jtulach@1318
  1917
    private transient volatile int hashCodeValue = 0;
jtulach@1318
  1918
jtulach@1318
  1919
    private static Locale defaultLocale = null;
jtulach@1318
  1920
    private static Locale defaultDisplayLocale = null;
jtulach@1318
  1921
    private static Locale defaultFormatLocale = null;
jtulach@1318
  1922
jtulach@1318
  1923
    /**
jtulach@1318
  1924
     * Return an array of the display names of the variant.
jtulach@1318
  1925
     * @param bundle the ResourceBundle to use to get the display names
jtulach@1318
  1926
     * @return an array of display names, possible of zero length.
jtulach@1318
  1927
     */
jtulach@1318
  1928
    private String[] getDisplayVariantArray(OpenListResourceBundle bundle, Locale inLocale) {
jtulach@1318
  1929
        // Split the variant name into tokens separated by '_'.
jtulach@1318
  1930
        StringTokenizer tokenizer = new StringTokenizer(baseLocale.getVariant(), "_");
jtulach@1318
  1931
        String[] names = new String[tokenizer.countTokens()];
jtulach@1318
  1932
jtulach@1318
  1933
        // For each variant token, lookup the display name.  If
jtulach@1318
  1934
        // not found, use the variant name itself.
jtulach@1318
  1935
        for (int i=0; i<names.length; ++i) {
jtulach@1318
  1936
            names[i] = getDisplayString(tokenizer.nextToken(),
jtulach@1318
  1937
                                inLocale, DISPLAY_VARIANT);
jtulach@1318
  1938
        }
jtulach@1318
  1939
jtulach@1318
  1940
        return names;
jtulach@1318
  1941
    }
jtulach@1318
  1942
jtulach@1318
  1943
    /**
jtulach@1318
  1944
     * Format a list using given pattern strings.
jtulach@1318
  1945
     * If either of the patterns is null, then a the list is
jtulach@1318
  1946
     * formatted by concatenation with the delimiter ','.
jtulach@1318
  1947
     * @param stringList the list of strings to be formatted.
jtulach@1318
  1948
     * @param listPattern should create a MessageFormat taking 0-3 arguments
jtulach@1318
  1949
     * and formatting them into a list.
jtulach@1318
  1950
     * @param listCompositionPattern should take 2 arguments
jtulach@1318
  1951
     * and is used by composeList.
jtulach@1318
  1952
     * @return a string representing the list.
jtulach@1318
  1953
     */
jtulach@1318
  1954
    private static String formatList(String[] stringList, String listPattern, String listCompositionPattern) {
jtulach@1318
  1955
        // If we have no list patterns, compose the list in a simple,
jtulach@1318
  1956
        // non-localized way.
jtulach@1318
  1957
        if (listPattern == null || listCompositionPattern == null) {
jtulach@1318
  1958
            StringBuffer result = new StringBuffer();
jtulach@1318
  1959
            for (int i=0; i<stringList.length; ++i) {
jtulach@1318
  1960
                if (i>0) result.append(',');
jtulach@1318
  1961
                result.append(stringList[i]);
jtulach@1318
  1962
            }
jtulach@1318
  1963
            return result.toString();
jtulach@1318
  1964
        }
jtulach@1318
  1965
jtulach@1318
  1966
        // Compose the list down to three elements if necessary
jtulach@1318
  1967
        if (stringList.length > 3) {
jtulach@1318
  1968
            MessageFormat format = new MessageFormat(listCompositionPattern);
jtulach@1318
  1969
            stringList = composeList(format, stringList);
jtulach@1318
  1970
        }
jtulach@1318
  1971
jtulach@1318
  1972
        // Rebuild the argument list with the list length as the first element
jtulach@1318
  1973
        Object[] args = new Object[stringList.length + 1];
jtulach@1318
  1974
        System.arraycopy(stringList, 0, args, 1, stringList.length);
jtulach@1318
  1975
        args[0] = new Integer(stringList.length);
jtulach@1318
  1976
jtulach@1318
  1977
        // Format it using the pattern in the resource
jtulach@1318
  1978
        MessageFormat format = new MessageFormat(listPattern);
jtulach@1318
  1979
        return format.format(args);
jtulach@1318
  1980
    }
jtulach@1318
  1981
jtulach@1318
  1982
    /**
jtulach@1318
  1983
     * Given a list of strings, return a list shortened to three elements.
jtulach@1318
  1984
     * Shorten it by applying the given format to the first two elements
jtulach@1318
  1985
     * recursively.
jtulach@1318
  1986
     * @param format a format which takes two arguments
jtulach@1318
  1987
     * @param list a list of strings
jtulach@1318
  1988
     * @return if the list is three elements or shorter, the same list;
jtulach@1318
  1989
     * otherwise, a new list of three elements.
jtulach@1318
  1990
     */
jtulach@1318
  1991
    private static String[] composeList(MessageFormat format, String[] list) {
jtulach@1318
  1992
        if (list.length <= 3) return list;
jtulach@1318
  1993
jtulach@1318
  1994
        // Use the given format to compose the first two elements into one
jtulach@1318
  1995
        String[] listItems = { list[0], list[1] };
jtulach@1318
  1996
        String newItem = format.format(listItems);
jtulach@1318
  1997
jtulach@1318
  1998
        // Form a new list one element shorter
jtulach@1318
  1999
        String[] newList = new String[list.length-1];
jtulach@1318
  2000
        System.arraycopy(list, 2, newList, 1, newList.length-1);
jtulach@1318
  2001
        newList[0] = newItem;
jtulach@1318
  2002
jtulach@1318
  2003
        // Recurse
jtulach@1318
  2004
        return composeList(format, newList);
jtulach@1318
  2005
    }
jtulach@1318
  2006
jtulach@1318
  2007
    /**
jtulach@1318
  2008
     * @serialField language    String
jtulach@1318
  2009
     *      language subtag in lower case. (See <a href="java/util/Locale.html#getLanguage()">getLanguage()</a>)
jtulach@1318
  2010
     * @serialField country     String
jtulach@1318
  2011
     *      country subtag in upper case. (See <a href="java/util/Locale.html#getCountry()">getCountry()</a>)
jtulach@1318
  2012
     * @serialField variant     String
jtulach@1318
  2013
     *      variant subtags separated by LOWLINE characters. (See <a href="java/util/Locale.html#getVariant()">getVariant()</a>)
jtulach@1318
  2014
     * @serialField hashcode    int
jtulach@1318
  2015
     *      deprecated, for forward compatibility only
jtulach@1318
  2016
     * @serialField script      String
jtulach@1318
  2017
     *      script subtag in title case (See <a href="java/util/Locale.html#getScript()">getScript()</a>)
jtulach@1318
  2018
     * @serialField extensions  String
jtulach@1318
  2019
     *      canonical representation of extensions, that is,
jtulach@1318
  2020
     *      BCP47 extensions in alphabetical order followed by
jtulach@1318
  2021
     *      BCP47 private use subtags, all in lower case letters
jtulach@1318
  2022
     *      separated by HYPHEN-MINUS characters.
jtulach@1318
  2023
     *      (See <a href="java/util/Locale.html#getExtensionKeys()">getExtensionKeys()</a>,
jtulach@1318
  2024
     *      <a href="java/util/Locale.html#getExtension(char)">getExtension(char)</a>)
jtulach@1318
  2025
     */
jtulach@1318
  2026
    private static final ObjectStreamField[] serialPersistentFields = {
jtulach@1318
  2027
        new ObjectStreamField("language", String.class),
jtulach@1318
  2028
        new ObjectStreamField("country", String.class),
jtulach@1318
  2029
        new ObjectStreamField("variant", String.class),
jtulach@1318
  2030
        new ObjectStreamField("hashcode", int.class),
jtulach@1318
  2031
        new ObjectStreamField("script", String.class),
jtulach@1318
  2032
        new ObjectStreamField("extensions", String.class),
jtulach@1318
  2033
    };
jtulach@1318
  2034
jtulach@1318
  2035
    /**
jtulach@1318
  2036
     * Serializes this <code>Locale</code> to the specified <code>ObjectOutputStream</code>.
jtulach@1318
  2037
     * @param out the <code>ObjectOutputStream</code> to write
jtulach@1318
  2038
     * @throws IOException
jtulach@1318
  2039
     * @since 1.7
jtulach@1318
  2040
     */
jtulach@1318
  2041
    private void writeObject(ObjectOutputStream out) throws IOException {
jtulach@1318
  2042
        ObjectOutputStream.PutField fields = out.putFields();
jtulach@1318
  2043
        fields.put("language", baseLocale.getLanguage());
jtulach@1318
  2044
        fields.put("script", baseLocale.getScript());
jtulach@1318
  2045
        fields.put("country", baseLocale.getRegion());
jtulach@1318
  2046
        fields.put("variant", baseLocale.getVariant());
jtulach@1318
  2047
        fields.put("extensions", localeExtensions == null ? "" : localeExtensions.getID());
jtulach@1318
  2048
        fields.put("hashcode", -1); // place holder just for backward support
jtulach@1318
  2049
        out.writeFields();
jtulach@1318
  2050
    }
jtulach@1318
  2051
jtulach@1318
  2052
    /**
jtulach@1318
  2053
     * Deserializes this <code>Locale</code>.
jtulach@1318
  2054
     * @param in the <code>ObjectInputStream</code> to read
jtulach@1318
  2055
     * @throws IOException
jtulach@1318
  2056
     * @throws ClassNotFoundException
jtulach@1318
  2057
     * @throws IllformdLocaleException
jtulach@1318
  2058
     * @since 1.7
jtulach@1318
  2059
     */
jtulach@1318
  2060
    private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
jtulach@1318
  2061
        ObjectInputStream.GetField fields = in.readFields();
jtulach@1318
  2062
        String language = (String)fields.get("language", "");
jtulach@1318
  2063
        String script = (String)fields.get("script", "");
jtulach@1318
  2064
        String country = (String)fields.get("country", "");
jtulach@1318
  2065
        String variant = (String)fields.get("variant", "");
jtulach@1318
  2066
        String extStr = (String)fields.get("extensions", "");
jtulach@1318
  2067
        baseLocale = BaseLocale.getInstance(convertOldISOCodes(language), script, country, variant);
jtulach@1318
  2068
        if (extStr.length() > 0) {
jtulach@1318
  2069
            try {
jtulach@1318
  2070
                InternalLocaleBuilder bldr = new InternalLocaleBuilder();
jtulach@1318
  2071
                bldr.setExtensions(extStr);
jtulach@1318
  2072
                localeExtensions = bldr.getLocaleExtensions();
jtulach@1318
  2073
            } catch (LocaleSyntaxException e) {
jtulach@1318
  2074
                throw new IllformedLocaleException(e.getMessage());
jtulach@1318
  2075
            }
jtulach@1318
  2076
        } else {
jtulach@1318
  2077
            localeExtensions = null;
jtulach@1318
  2078
        }
jtulach@1318
  2079
    }
jtulach@1318
  2080
jtulach@1318
  2081
    /**
jtulach@1318
  2082
     * Returns a cached <code>Locale</code> instance equivalent to
jtulach@1318
  2083
     * the deserialized <code>Locale</code>. When serialized
jtulach@1318
  2084
     * language, country and variant fields read from the object data stream
jtulach@1318
  2085
     * are exactly "ja", "JP", "JP" or "th", "TH", "TH" and script/extensions
jtulach@1318
  2086
     * fields are empty, this method supplies <code>UNICODE_LOCALE_EXTENSION</code>
jtulach@1318
  2087
     * "ca"/"japanese" (calendar type is "japanese") or "nu"/"thai" (number script
jtulach@1318
  2088
     * type is "thai"). See <a href="Locale.html#special_cases_constructor"/>Special Cases</a>
jtulach@1318
  2089
     * for more information.
jtulach@1318
  2090
     *
jtulach@1318
  2091
     * @return an instance of <code>Locale</code> equivalent to
jtulach@1318
  2092
     * the deserialized <code>Locale</code>.
jtulach@1318
  2093
     * @throws java.io.ObjectStreamException
jtulach@1318
  2094
     */
jtulach@1318
  2095
    private Object readResolve() throws java.io.ObjectStreamException {
jtulach@1318
  2096
        return getInstance(baseLocale.getLanguage(), baseLocale.getScript(),
jtulach@1318
  2097
                baseLocale.getRegion(), baseLocale.getVariant(), localeExtensions);
jtulach@1318
  2098
    }
jtulach@1318
  2099
jtulach@1318
  2100
    private static volatile String[] isoLanguages = null;
jtulach@1318
  2101
jtulach@1318
  2102
    private static volatile String[] isoCountries = null;
jtulach@1318
  2103
jtulach@1318
  2104
    private static String convertOldISOCodes(String language) {
jtulach@1318
  2105
        // we accept both the old and the new ISO codes for the languages whose ISO
jtulach@1318
  2106
        // codes have changed, but we always store the OLD code, for backward compatibility
jtulach@1318
  2107
        language = LocaleUtils.toLowerString(language).intern();
jtulach@1318
  2108
        if (language == "he") {
jtulach@1318
  2109
            return "iw";
jtulach@1318
  2110
        } else if (language == "yi") {
jtulach@1318
  2111
            return "ji";
jtulach@1318
  2112
        } else if (language == "id") {
jtulach@1318
  2113
            return "in";
jtulach@1318
  2114
        } else {
jtulach@1318
  2115
            return language;
jtulach@1318
  2116
        }
jtulach@1318
  2117
    }
jtulach@1318
  2118
jtulach@1318
  2119
    private static LocaleExtensions getCompatibilityExtensions(String language,
jtulach@1318
  2120
                                                               String script,
jtulach@1318
  2121
                                                               String country,
jtulach@1318
  2122
                                                               String variant) {
jtulach@1318
  2123
        LocaleExtensions extensions = null;
jtulach@1318
  2124
        // Special cases for backward compatibility support
jtulach@1318
  2125
        if (LocaleUtils.caseIgnoreMatch(language, "ja")
jtulach@1318
  2126
                && script.length() == 0
jtulach@1318
  2127
                && LocaleUtils.caseIgnoreMatch(country, "jp")
jtulach@1318
  2128
                && "JP".equals(variant)) {
jtulach@1318
  2129
            // ja_JP_JP -> u-ca-japanese (calendar = japanese)
jtulach@1318
  2130
            extensions = LocaleExtensions.CALENDAR_JAPANESE;
jtulach@1318
  2131
        } else if (LocaleUtils.caseIgnoreMatch(language, "th")
jtulach@1318
  2132
                && script.length() == 0
jtulach@1318
  2133
                && LocaleUtils.caseIgnoreMatch(country, "th")
jtulach@1318
  2134
                && "TH".equals(variant)) {
jtulach@1318
  2135
            // th_TH_TH -> u-nu-thai (numbersystem = thai)
jtulach@1318
  2136
            extensions = LocaleExtensions.NUMBER_THAI;
jtulach@1318
  2137
        }
jtulach@1318
  2138
        return extensions;
jtulach@1318
  2139
    }
jtulach@1318
  2140
jtulach@1318
  2141
    /**
jtulach@1318
  2142
     * Obtains a localized locale names from a LocaleNameProvider
jtulach@1318
  2143
     * implementation.
jtulach@1318
  2144
     */
jtulach@1318
  2145
    private static class LocaleNameGetter
jtulach@1318
  2146
        implements LocaleServiceProviderPool.LocalizedObjectGetter<LocaleNameProvider, String> {
jtulach@1318
  2147
        private static final LocaleNameGetter INSTANCE = new LocaleNameGetter();
jtulach@1318
  2148
jtulach@1318
  2149
        public String getObject(LocaleNameProvider localeNameProvider,
jtulach@1318
  2150
                                Locale locale,
jtulach@1318
  2151
                                String key,
jtulach@1318
  2152
                                Object... params) {
jtulach@1318
  2153
            assert params.length == 2;
jtulach@1318
  2154
            int type = (Integer)params[0];
jtulach@1318
  2155
            String code = (String)params[1];
jtulach@1318
  2156
jtulach@1318
  2157
            switch(type) {
jtulach@1318
  2158
            case DISPLAY_LANGUAGE:
jtulach@1318
  2159
                return localeNameProvider.getDisplayLanguage(code, locale);
jtulach@1318
  2160
            case DISPLAY_COUNTRY:
jtulach@1318
  2161
                return localeNameProvider.getDisplayCountry(code, locale);
jtulach@1318
  2162
            case DISPLAY_VARIANT:
jtulach@1318
  2163
                return localeNameProvider.getDisplayVariant(code, locale);
jtulach@1318
  2164
            case DISPLAY_SCRIPT:
jtulach@1318
  2165
                return localeNameProvider.getDisplayScript(code, locale);
jtulach@1318
  2166
            default:
jtulach@1318
  2167
                assert false; // shouldn't happen
jtulach@1318
  2168
            }
jtulach@1318
  2169
jtulach@1318
  2170
            return null;
jtulach@1318
  2171
        }
jtulach@1318
  2172
    }
jtulach@1318
  2173
jtulach@1318
  2174
    /**
jtulach@1318
  2175
     * Enum for locale categories.  These locale categories are used to get/set
jtulach@1318
  2176
     * the default locale for the specific functionality represented by the
jtulach@1318
  2177
     * category.
jtulach@1318
  2178
     *
jtulach@1318
  2179
     * @see #getDefault(Locale.Category)
jtulach@1318
  2180
     * @see #setDefault(Locale.Category, Locale)
jtulach@1318
  2181
     * @since 1.7
jtulach@1318
  2182
     */
jtulach@1318
  2183
    public enum Category {
jtulach@1318
  2184
jtulach@1318
  2185
        /**
jtulach@1318
  2186
         * Category used to represent the default locale for
jtulach@1318
  2187
         * displaying user interfaces.
jtulach@1318
  2188
         */
jtulach@1318
  2189
        DISPLAY("user.language.display",
jtulach@1318
  2190
                "user.script.display",
jtulach@1318
  2191
                "user.country.display",
jtulach@1318
  2192
                "user.variant.display"),
jtulach@1318
  2193
jtulach@1318
  2194
        /**
jtulach@1318
  2195
         * Category used to represent the default locale for
jtulach@1318
  2196
         * formatting dates, numbers, and/or currencies.
jtulach@1318
  2197
         */
jtulach@1318
  2198
        FORMAT("user.language.format",
jtulach@1318
  2199
               "user.script.format",
jtulach@1318
  2200
               "user.country.format",
jtulach@1318
  2201
               "user.variant.format");
jtulach@1318
  2202
jtulach@1318
  2203
        Category(String languageKey, String scriptKey, String countryKey, String variantKey) {
jtulach@1318
  2204
            this.languageKey = languageKey;
jtulach@1318
  2205
            this.scriptKey = scriptKey;
jtulach@1318
  2206
            this.countryKey = countryKey;
jtulach@1318
  2207
            this.variantKey = variantKey;
jtulach@1318
  2208
        }
jtulach@1318
  2209
jtulach@1318
  2210
        final String languageKey;
jtulach@1318
  2211
        final String scriptKey;
jtulach@1318
  2212
        final String countryKey;
jtulach@1318
  2213
        final String variantKey;
jtulach@1318
  2214
    }
jtulach@1318
  2215
jtulach@1318
  2216
    /**
jtulach@1318
  2217
     * <code>Builder</code> is used to build instances of <code>Locale</code>
jtulach@1318
  2218
     * from values configured by the setters.  Unlike the <code>Locale</code>
jtulach@1318
  2219
     * constructors, the <code>Builder</code> checks if a value configured by a
jtulach@1318
  2220
     * setter satisfies the syntax requirements defined by the <code>Locale</code>
jtulach@1318
  2221
     * class.  A <code>Locale</code> object created by a <code>Builder</code> is
jtulach@1318
  2222
     * well-formed and can be transformed to a well-formed IETF BCP 47 language tag
jtulach@1318
  2223
     * without losing information.
jtulach@1318
  2224
     *
jtulach@1318
  2225
     * <p><b>Note:</b> The <code>Locale</code> class does not provide any
jtulach@1318
  2226
     * syntactic restrictions on variant, while BCP 47 requires each variant
jtulach@1318
  2227
     * subtag to be 5 to 8 alphanumerics or a single numeric followed by 3
jtulach@1318
  2228
     * alphanumerics.  The method <code>setVariant</code> throws
jtulach@1318
  2229
     * <code>IllformedLocaleException</code> for a variant that does not satisfy
jtulach@1318
  2230
     * this restriction. If it is necessary to support such a variant, use a
jtulach@1318
  2231
     * Locale constructor.  However, keep in mind that a <code>Locale</code>
jtulach@1318
  2232
     * object created this way might lose the variant information when
jtulach@1318
  2233
     * transformed to a BCP 47 language tag.
jtulach@1318
  2234
     *
jtulach@1318
  2235
     * <p>The following example shows how to create a <code>Locale</code> object
jtulach@1318
  2236
     * with the <code>Builder</code>.
jtulach@1318
  2237
     * <blockquote>
jtulach@1318
  2238
     * <pre>
jtulach@1318
  2239
     *     Locale aLocale = new Builder().setLanguage("sr").setScript("Latn").setRegion("RS").build();
jtulach@1318
  2240
     * </pre>
jtulach@1318
  2241
     * </blockquote>
jtulach@1318
  2242
     *
jtulach@1318
  2243
     * <p>Builders can be reused; <code>clear()</code> resets all
jtulach@1318
  2244
     * fields to their default values.
jtulach@1318
  2245
     *
jtulach@1318
  2246
     * @see Locale#forLanguageTag
jtulach@1318
  2247
     * @since 1.7
jtulach@1318
  2248
     */
jtulach@1318
  2249
    public static final class Builder {
jtulach@1318
  2250
        private final InternalLocaleBuilder localeBuilder;
jtulach@1318
  2251
jtulach@1318
  2252
        /**
jtulach@1318
  2253
         * Constructs an empty Builder. The default value of all
jtulach@1318
  2254
         * fields, extensions, and private use information is the
jtulach@1318
  2255
         * empty string.
jtulach@1318
  2256
         */
jtulach@1318
  2257
        public Builder() {
jtulach@1318
  2258
            localeBuilder = new InternalLocaleBuilder();
jtulach@1318
  2259
        }
jtulach@1318
  2260
jtulach@1318
  2261
        /**
jtulach@1318
  2262
         * Resets the <code>Builder</code> to match the provided
jtulach@1318
  2263
         * <code>locale</code>.  Existing state is discarded.
jtulach@1318
  2264
         *
jtulach@1318
  2265
         * <p>All fields of the locale must be well-formed, see {@link Locale}.
jtulach@1318
  2266
         *
jtulach@1318
  2267
         * <p>Locales with any ill-formed fields cause
jtulach@1318
  2268
         * <code>IllformedLocaleException</code> to be thrown, except for the
jtulach@1318
  2269
         * following three cases which are accepted for compatibility
jtulach@1318
  2270
         * reasons:<ul>
jtulach@1318
  2271
         * <li>Locale("ja", "JP", "JP") is treated as "ja-JP-u-ca-japanese"
jtulach@1318
  2272
         * <li>Locale("th", "TH", "TH") is treated as "th-TH-u-nu-thai"
jtulach@1318
  2273
         * <li>Locale("no", "NO", "NY") is treated as "nn-NO"</ul>
jtulach@1318
  2274
         *
jtulach@1318
  2275
         * @param locale the locale
jtulach@1318
  2276
         * @return This builder.
jtulach@1318
  2277
         * @throws IllformedLocaleException if <code>locale</code> has
jtulach@1318
  2278
         * any ill-formed fields.
jtulach@1318
  2279
         * @throws NullPointerException if <code>locale</code> is null.
jtulach@1318
  2280
         */
jtulach@1318
  2281
        public Builder setLocale(Locale locale) {
jtulach@1318
  2282
            try {
jtulach@1318
  2283
                localeBuilder.setLocale(locale.baseLocale, locale.localeExtensions);
jtulach@1318
  2284
            } catch (LocaleSyntaxException e) {
jtulach@1318
  2285
                throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex());
jtulach@1318
  2286
            }
jtulach@1318
  2287
            return this;
jtulach@1318
  2288
        }
jtulach@1318
  2289
jtulach@1318
  2290
        /**
jtulach@1318
  2291
         * Resets the Builder to match the provided IETF BCP 47
jtulach@1318
  2292
         * language tag.  Discards the existing state.  Null and the
jtulach@1318
  2293
         * empty string cause the builder to be reset, like {@link
jtulach@1318
  2294
         * #clear}.  Grandfathered tags (see {@link
jtulach@1318
  2295
         * Locale#forLanguageTag}) are converted to their canonical
jtulach@1318
  2296
         * form before being processed.  Otherwise, the language tag
jtulach@1318
  2297
         * must be well-formed (see {@link Locale}) or an exception is
jtulach@1318
  2298
         * thrown (unlike <code>Locale.forLanguageTag</code>, which
jtulach@1318
  2299
         * just discards ill-formed and following portions of the
jtulach@1318
  2300
         * tag).
jtulach@1318
  2301
         *
jtulach@1318
  2302
         * @param languageTag the language tag
jtulach@1318
  2303
         * @return This builder.
jtulach@1318
  2304
         * @throws IllformedLocaleException if <code>languageTag</code> is ill-formed
jtulach@1318
  2305
         * @see Locale#forLanguageTag(String)
jtulach@1318
  2306
         */
jtulach@1318
  2307
        public Builder setLanguageTag(String languageTag) {
jtulach@1318
  2308
            ParseStatus sts = new ParseStatus();
jtulach@1318
  2309
            LanguageTag tag = LanguageTag.parse(languageTag, sts);
jtulach@1318
  2310
            if (sts.isError()) {
jtulach@1318
  2311
                throw new IllformedLocaleException(sts.getErrorMessage(), sts.getErrorIndex());
jtulach@1318
  2312
            }
jtulach@1318
  2313
            localeBuilder.setLanguageTag(tag);
jtulach@1318
  2314
            return this;
jtulach@1318
  2315
        }
jtulach@1318
  2316
jtulach@1318
  2317
        /**
jtulach@1318
  2318
         * Sets the language.  If <code>language</code> is the empty string or
jtulach@1318
  2319
         * null, the language in this <code>Builder</code> is removed.  Otherwise,
jtulach@1318
  2320
         * the language must be <a href="./Locale.html#def_language">well-formed</a>
jtulach@1318
  2321
         * or an exception is thrown.
jtulach@1318
  2322
         *
jtulach@1318
  2323
         * <p>The typical language value is a two or three-letter language
jtulach@1318
  2324
         * code as defined in ISO639.
jtulach@1318
  2325
         *
jtulach@1318
  2326
         * @param language the language
jtulach@1318
  2327
         * @return This builder.
jtulach@1318
  2328
         * @throws IllformedLocaleException if <code>language</code> is ill-formed
jtulach@1318
  2329
         */
jtulach@1318
  2330
        public Builder setLanguage(String language) {
jtulach@1318
  2331
            try {
jtulach@1318
  2332
                localeBuilder.setLanguage(language);
jtulach@1318
  2333
            } catch (LocaleSyntaxException e) {
jtulach@1318
  2334
                throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex());
jtulach@1318
  2335
            }
jtulach@1318
  2336
            return this;
jtulach@1318
  2337
        }
jtulach@1318
  2338
jtulach@1318
  2339
        /**
jtulach@1318
  2340
         * Sets the script. If <code>script</code> is null or the empty string,
jtulach@1318
  2341
         * the script in this <code>Builder</code> is removed.
jtulach@1318
  2342
         * Otherwise, the script must be <a href="./Locale.html#def_script">well-formed</a> or an
jtulach@1318
  2343
         * exception is thrown.
jtulach@1318
  2344
         *
jtulach@1318
  2345
         * <p>The typical script value is a four-letter script code as defined by ISO 15924.
jtulach@1318
  2346
         *
jtulach@1318
  2347
         * @param script the script
jtulach@1318
  2348
         * @return This builder.
jtulach@1318
  2349
         * @throws IllformedLocaleException if <code>script</code> is ill-formed
jtulach@1318
  2350
         */
jtulach@1318
  2351
        public Builder setScript(String script) {
jtulach@1318
  2352
            try {
jtulach@1318
  2353
                localeBuilder.setScript(script);
jtulach@1318
  2354
            } catch (LocaleSyntaxException e) {
jtulach@1318
  2355
                throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex());
jtulach@1318
  2356
            }
jtulach@1318
  2357
            return this;
jtulach@1318
  2358
        }
jtulach@1318
  2359
jtulach@1318
  2360
        /**
jtulach@1318
  2361
         * Sets the region.  If region is null or the empty string, the region
jtulach@1318
  2362
         * in this <code>Builder</code> is removed.  Otherwise,
jtulach@1318
  2363
         * the region must be <a href="./Locale.html#def_region">well-formed</a> or an
jtulach@1318
  2364
         * exception is thrown.
jtulach@1318
  2365
         *
jtulach@1318
  2366
         * <p>The typical region value is a two-letter ISO 3166 code or a
jtulach@1318
  2367
         * three-digit UN M.49 area code.
jtulach@1318
  2368
         *
jtulach@1318
  2369
         * <p>The country value in the <code>Locale</code> created by the
jtulach@1318
  2370
         * <code>Builder</code> is always normalized to upper case.
jtulach@1318
  2371
         *
jtulach@1318
  2372
         * @param region the region
jtulach@1318
  2373
         * @return This builder.
jtulach@1318
  2374
         * @throws IllformedLocaleException if <code>region</code> is ill-formed
jtulach@1318
  2375
         */
jtulach@1318
  2376
        public Builder setRegion(String region) {
jtulach@1318
  2377
            try {
jtulach@1318
  2378
                localeBuilder.setRegion(region);
jtulach@1318
  2379
            } catch (LocaleSyntaxException e) {
jtulach@1318
  2380
                throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex());
jtulach@1318
  2381
            }
jtulach@1318
  2382
            return this;
jtulach@1318
  2383
        }
jtulach@1318
  2384
jtulach@1318
  2385
        /**
jtulach@1318
  2386
         * Sets the variant.  If variant is null or the empty string, the
jtulach@1318
  2387
         * variant in this <code>Builder</code> is removed.  Otherwise, it
jtulach@1318
  2388
         * must consist of one or more <a href="./Locale.html#def_variant">well-formed</a>
jtulach@1318
  2389
         * subtags, or an exception is thrown.
jtulach@1318
  2390
         *
jtulach@1318
  2391
         * <p><b>Note:</b> This method checks if <code>variant</code>
jtulach@1318
  2392
         * satisfies the IETF BCP 47 variant subtag's syntax requirements,
jtulach@1318
  2393
         * and normalizes the value to lowercase letters.  However,
jtulach@1318
  2394
         * the <code>Locale</code> class does not impose any syntactic
jtulach@1318
  2395
         * restriction on variant, and the variant value in
jtulach@1318
  2396
         * <code>Locale</code> is case sensitive.  To set such a variant,
jtulach@1318
  2397
         * use a Locale constructor.
jtulach@1318
  2398
         *
jtulach@1318
  2399
         * @param variant the variant
jtulach@1318
  2400
         * @return This builder.
jtulach@1318
  2401
         * @throws IllformedLocaleException if <code>variant</code> is ill-formed
jtulach@1318
  2402
         */
jtulach@1318
  2403
        public Builder setVariant(String variant) {
jtulach@1318
  2404
            try {
jtulach@1318
  2405
                localeBuilder.setVariant(variant);
jtulach@1318
  2406
            } catch (LocaleSyntaxException e) {
jtulach@1318
  2407
                throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex());
jtulach@1318
  2408
            }
jtulach@1318
  2409
            return this;
jtulach@1318
  2410
        }
jtulach@1318
  2411
jtulach@1318
  2412
        /**
jtulach@1318
  2413
         * Sets the extension for the given key. If the value is null or the
jtulach@1318
  2414
         * empty string, the extension is removed.  Otherwise, the extension
jtulach@1318
  2415
         * must be <a href="./Locale.html#def_extensions">well-formed</a> or an exception
jtulach@1318
  2416
         * is thrown.
jtulach@1318
  2417
         *
jtulach@1318
  2418
         * <p><b>Note:</b> The key {@link Locale#UNICODE_LOCALE_EXTENSION
jtulach@1318
  2419
         * UNICODE_LOCALE_EXTENSION} ('u') is used for the Unicode locale extension.
jtulach@1318
  2420
         * Setting a value for this key replaces any existing Unicode locale key/type
jtulach@1318
  2421
         * pairs with those defined in the extension.
jtulach@1318
  2422
         *
jtulach@1318
  2423
         * <p><b>Note:</b> The key {@link Locale#PRIVATE_USE_EXTENSION
jtulach@1318
  2424
         * PRIVATE_USE_EXTENSION} ('x') is used for the private use code. To be
jtulach@1318
  2425
         * well-formed, the value for this key needs only to have subtags of one to
jtulach@1318
  2426
         * eight alphanumeric characters, not two to eight as in the general case.
jtulach@1318
  2427
         *
jtulach@1318
  2428
         * @param key the extension key
jtulach@1318
  2429
         * @param value the extension value
jtulach@1318
  2430
         * @return This builder.
jtulach@1318
  2431
         * @throws IllformedLocaleException if <code>key</code> is illegal
jtulach@1318
  2432
         * or <code>value</code> is ill-formed
jtulach@1318
  2433
         * @see #setUnicodeLocaleKeyword(String, String)
jtulach@1318
  2434
         */
jtulach@1318
  2435
        public Builder setExtension(char key, String value) {
jtulach@1318
  2436
            try {
jtulach@1318
  2437
                localeBuilder.setExtension(key, value);
jtulach@1318
  2438
            } catch (LocaleSyntaxException e) {
jtulach@1318
  2439
                throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex());
jtulach@1318
  2440
            }
jtulach@1318
  2441
            return this;
jtulach@1318
  2442
        }
jtulach@1318
  2443
jtulach@1318
  2444
        /**
jtulach@1318
  2445
         * Sets the Unicode locale keyword type for the given key.  If the type
jtulach@1318
  2446
         * is null, the Unicode keyword is removed.  Otherwise, the key must be
jtulach@1318
  2447
         * non-null and both key and type must be <a
jtulach@1318
  2448
         * href="./Locale.html#def_locale_extension">well-formed</a> or an exception
jtulach@1318
  2449
         * is thrown.
jtulach@1318
  2450
         *
jtulach@1318
  2451
         * <p>Keys and types are converted to lower case.
jtulach@1318
  2452
         *
jtulach@1318
  2453
         * <p><b>Note</b>:Setting the 'u' extension via {@link #setExtension}
jtulach@1318
  2454
         * replaces all Unicode locale keywords with those defined in the
jtulach@1318
  2455
         * extension.
jtulach@1318
  2456
         *
jtulach@1318
  2457
         * @param key the Unicode locale key
jtulach@1318
  2458
         * @param type the Unicode locale type
jtulach@1318
  2459
         * @return This builder.
jtulach@1318
  2460
         * @throws IllformedLocaleException if <code>key</code> or <code>type</code>
jtulach@1318
  2461
         * is ill-formed
jtulach@1318
  2462
         * @throws NullPointerException if <code>key</code> is null
jtulach@1318
  2463
         * @see #setExtension(char, String)
jtulach@1318
  2464
         */
jtulach@1318
  2465
        public Builder setUnicodeLocaleKeyword(String key, String type) {
jtulach@1318
  2466
            try {
jtulach@1318
  2467
                localeBuilder.setUnicodeLocaleKeyword(key, type);
jtulach@1318
  2468
            } catch (LocaleSyntaxException e) {
jtulach@1318
  2469
                throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex());
jtulach@1318
  2470
            }
jtulach@1318
  2471
            return this;
jtulach@1318
  2472
        }
jtulach@1318
  2473
jtulach@1318
  2474
        /**
jtulach@1318
  2475
         * Adds a unicode locale attribute, if not already present, otherwise
jtulach@1318
  2476
         * has no effect.  The attribute must not be null and must be <a
jtulach@1318
  2477
         * href="./Locale.html#def_locale_extension">well-formed</a> or an exception
jtulach@1318
  2478
         * is thrown.
jtulach@1318
  2479
         *
jtulach@1318
  2480
         * @param attribute the attribute
jtulach@1318
  2481
         * @return This builder.
jtulach@1318
  2482
         * @throws NullPointerException if <code>attribute</code> is null
jtulach@1318
  2483
         * @throws IllformedLocaleException if <code>attribute</code> is ill-formed
jtulach@1318
  2484
         * @see #setExtension(char, String)
jtulach@1318
  2485
         */
jtulach@1318
  2486
        public Builder addUnicodeLocaleAttribute(String attribute) {
jtulach@1318
  2487
            try {
jtulach@1318
  2488
                localeBuilder.addUnicodeLocaleAttribute(attribute);
jtulach@1318
  2489
            } catch (LocaleSyntaxException e) {
jtulach@1318
  2490
                throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex());
jtulach@1318
  2491
            }
jtulach@1318
  2492
            return this;
jtulach@1318
  2493
        }
jtulach@1318
  2494
jtulach@1318
  2495
        /**
jtulach@1318
  2496
         * Removes a unicode locale attribute, if present, otherwise has no
jtulach@1318
  2497
         * effect.  The attribute must not be null and must be <a
jtulach@1318
  2498
         * href="./Locale.html#def_locale_extension">well-formed</a> or an exception
jtulach@1318
  2499
         * is thrown.
jtulach@1318
  2500
         *
jtulach@1318
  2501
         * <p>Attribute comparision for removal is case-insensitive.
jtulach@1318
  2502
         *
jtulach@1318
  2503
         * @param attribute the attribute
jtulach@1318
  2504
         * @return This builder.
jtulach@1318
  2505
         * @throws NullPointerException if <code>attribute</code> is null
jtulach@1318
  2506
         * @throws IllformedLocaleException if <code>attribute</code> is ill-formed
jtulach@1318
  2507
         * @see #setExtension(char, String)
jtulach@1318
  2508
         */
jtulach@1318
  2509
        public Builder removeUnicodeLocaleAttribute(String attribute) {
jtulach@1318
  2510
            try {
jtulach@1318
  2511
                localeBuilder.removeUnicodeLocaleAttribute(attribute);
jtulach@1318
  2512
            } catch (LocaleSyntaxException e) {
jtulach@1318
  2513
                throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex());
jtulach@1318
  2514
            }
jtulach@1318
  2515
            return this;
jtulach@1318
  2516
        }
jtulach@1318
  2517
jtulach@1318
  2518
        /**
jtulach@1318
  2519
         * Resets the builder to its initial, empty state.
jtulach@1318
  2520
         *
jtulach@1318
  2521
         * @return This builder.
jtulach@1318
  2522
         */
jtulach@1318
  2523
        public Builder clear() {
jtulach@1318
  2524
            localeBuilder.clear();
jtulach@1318
  2525
            return this;
jtulach@1318
  2526
        }
jtulach@1318
  2527
jtulach@1318
  2528
        /**
jtulach@1318
  2529
         * Resets the extensions to their initial, empty state.
jtulach@1318
  2530
         * Language, script, region and variant are unchanged.
jtulach@1318
  2531
         *
jtulach@1318
  2532
         * @return This builder.
jtulach@1318
  2533
         * @see #setExtension(char, String)
jtulach@1318
  2534
         */
jtulach@1318
  2535
        public Builder clearExtensions() {
jtulach@1318
  2536
            localeBuilder.clearExtensions();
jtulach@1318
  2537
            return this;
jtulach@1318
  2538
        }
jtulach@1318
  2539
jtulach@1318
  2540
        /**
jtulach@1318
  2541
         * Returns an instance of <code>Locale</code> created from the fields set
jtulach@1318
  2542
         * on this builder.
jtulach@1318
  2543
         *
jtulach@1318
  2544
         * <p>This applies the conversions listed in {@link Locale#forLanguageTag}
jtulach@1318
  2545
         * when constructing a Locale. (Grandfathered tags are handled in
jtulach@1318
  2546
         * {@link #setLanguageTag}.)
jtulach@1318
  2547
         *
jtulach@1318
  2548
         * @return A Locale.
jtulach@1318
  2549
         */
jtulach@1318
  2550
        public Locale build() {
jtulach@1318
  2551
            BaseLocale baseloc = localeBuilder.getBaseLocale();
jtulach@1318
  2552
            LocaleExtensions extensions = localeBuilder.getLocaleExtensions();
jtulach@1318
  2553
            if (extensions == null && baseloc.getVariant().length() > 0) {
jtulach@1318
  2554
                extensions = getCompatibilityExtensions(baseloc.getLanguage(), baseloc.getScript(),
jtulach@1318
  2555
                        baseloc.getRegion(), baseloc.getVariant());
jtulach@1318
  2556
            }
jtulach@1318
  2557
            return Locale.getInstance(baseloc, extensions);
jtulach@1318
  2558
        }
jtulach@1318
  2559
    }
jtulach@1318
  2560
}