rt/emul/compact/src/main/java/java/util/Locale.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 22 Feb 2015 09:00:14 +0100
changeset 1781 681e61714a1d
parent 1337 c794024954b5
child 1784 11679a57a895
permissions -rw-r--r--
Locale.getDefault() reads real value from navigator.language & co.
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;
jaroslav@1781
    48
import org.apidesign.bck2brwsr.core.JavaScriptBody;
jtulach@1318
    49
jtulach@1318
    50
/**
jtulach@1318
    51
 * A <code>Locale</code> object represents a specific geographical, political,
jtulach@1318
    52
 * or cultural region. An operation that requires a <code>Locale</code> to perform
jtulach@1318
    53
 * its task is called <em>locale-sensitive</em> and uses the <code>Locale</code>
jtulach@1318
    54
 * to tailor information for the user. For example, displaying a number
jtulach@1318
    55
 * is a locale-sensitive operation&mdash; the number should be formatted
jtulach@1318
    56
 * according to the customs and conventions of the user's native country,
jtulach@1318
    57
 * region, or culture.
jtulach@1318
    58
 *
jtulach@1318
    59
 * <p> The <code>Locale</code> class implements identifiers
jtulach@1318
    60
 * interchangeable with BCP 47 (IETF BCP 47, "Tags for Identifying
jtulach@1318
    61
 * Languages"), with support for the LDML (UTS#35, "Unicode Locale
jtulach@1318
    62
 * Data Markup Language") BCP 47-compatible extensions for locale data
jtulach@1318
    63
 * exchange.
jtulach@1318
    64
 *
jtulach@1318
    65
 * <p> A <code>Locale</code> object logically consists of the fields
jtulach@1318
    66
 * described below.
jtulach@1318
    67
 *
jtulach@1318
    68
 * <dl>
jtulach@1318
    69
 *   <dt><a name="def_language"/><b>language</b></dt>
jtulach@1318
    70
 *
jtulach@1318
    71
 *   <dd>ISO 639 alpha-2 or alpha-3 language code, or registered
jtulach@1318
    72
 *   language subtags up to 8 alpha letters (for future enhancements).
jtulach@1318
    73
 *   When a language has both an alpha-2 code and an alpha-3 code, the
jtulach@1318
    74
 *   alpha-2 code must be used.  You can find a full list of valid
jtulach@1318
    75
 *   language codes in the IANA Language Subtag Registry (search for
jtulach@1318
    76
 *   "Type: language").  The language field is case insensitive, but
jtulach@1318
    77
 *   <code>Locale</code> always canonicalizes to lower case.</dd><br>
jtulach@1318
    78
 *
jtulach@1318
    79
 *   <dd>Well-formed language values have the form
jtulach@1318
    80
 *   <code>[a-zA-Z]{2,8}</code>.  Note that this is not the the full
jtulach@1318
    81
 *   BCP47 language production, since it excludes extlang.  They are
jtulach@1318
    82
 *   not needed since modern three-letter language codes replace
jtulach@1318
    83
 *   them.</dd><br>
jtulach@1318
    84
 *
jtulach@1318
    85
 *   <dd>Example: "en" (English), "ja" (Japanese), "kok" (Konkani)</dd><br>
jtulach@1318
    86
 *
jtulach@1318
    87
 *   <dt><a name="def_script"/><b>script</b></dt>
jtulach@1318
    88
 *
jtulach@1318
    89
 *   <dd>ISO 15924 alpha-4 script code.  You can find a full list of
jtulach@1318
    90
 *   valid script codes in the IANA Language Subtag Registry (search
jtulach@1318
    91
 *   for "Type: script").  The script field is case insensitive, but
jtulach@1318
    92
 *   <code>Locale</code> always canonicalizes to title case (the first
jtulach@1318
    93
 *   letter is upper case and the rest of the letters are lower
jtulach@1318
    94
 *   case).</dd><br>
jtulach@1318
    95
 *
jtulach@1318
    96
 *   <dd>Well-formed script values have the form
jtulach@1318
    97
 *   <code>[a-zA-Z]{4}</code></dd><br>
jtulach@1318
    98
 *
jtulach@1318
    99
 *   <dd>Example: "Latn" (Latin), "Cyrl" (Cyrillic)</dd><br>
jtulach@1318
   100
 *
jtulach@1318
   101
 *   <dt><a name="def_region"/><b>country (region)</b></dt>
jtulach@1318
   102
 *
jtulach@1318
   103
 *   <dd>ISO 3166 alpha-2 country code or UN M.49 numeric-3 area code.
jtulach@1318
   104
 *   You can find a full list of valid country and region codes in the
jtulach@1318
   105
 *   IANA Language Subtag Registry (search for "Type: region").  The
jtulach@1318
   106
 *   country (region) field is case insensitive, but
jtulach@1318
   107
 *   <code>Locale</code> always canonicalizes to upper case.</dd><br>
jtulach@1318
   108
 *
jtulach@1318
   109
 *   <dd>Well-formed country/region values have
jtulach@1318
   110
 *   the form <code>[a-zA-Z]{2} | [0-9]{3}</code></dd><br>
jtulach@1318
   111
 *
jtulach@1318
   112
 *   <dd>Example: "US" (United States), "FR" (France), "029"
jtulach@1318
   113
 *   (Caribbean)</dd><br>
jtulach@1318
   114
 *
jtulach@1318
   115
 *   <dt><a name="def_variant"/><b>variant</b></dt>
jtulach@1318
   116
 *
jtulach@1318
   117
 *   <dd>Any arbitrary value used to indicate a variation of a
jtulach@1318
   118
 *   <code>Locale</code>.  Where there are two or more variant values
jtulach@1318
   119
 *   each indicating its own semantics, these values should be ordered
jtulach@1318
   120
 *   by importance, with most important first, separated by
jtulach@1318
   121
 *   underscore('_').  The variant field is case sensitive.</dd><br>
jtulach@1318
   122
 *
jtulach@1318
   123
 *   <dd>Note: IETF BCP 47 places syntactic restrictions on variant
jtulach@1318
   124
 *   subtags.  Also BCP 47 subtags are strictly used to indicate
jtulach@1318
   125
 *   additional variations that define a language or its dialects that
jtulach@1318
   126
 *   are not covered by any combinations of language, script and
jtulach@1318
   127
 *   region subtags.  You can find a full list of valid variant codes
jtulach@1318
   128
 *   in the IANA Language Subtag Registry (search for "Type: variant").
jtulach@1318
   129
 *
jtulach@1318
   130
 *   <p>However, the variant field in <code>Locale</code> has
jtulach@1318
   131
 *   historically been used for any kind of variation, not just
jtulach@1318
   132
 *   language variations.  For example, some supported variants
jtulach@1318
   133
 *   available in Java SE Runtime Environments indicate alternative
jtulach@1318
   134
 *   cultural behaviors such as calendar type or number script.  In
jtulach@1318
   135
 *   BCP 47 this kind of information, which does not identify the
jtulach@1318
   136
 *   language, is supported by extension subtags or private use
jtulach@1318
   137
 *   subtags.</dd><br>
jtulach@1318
   138
 *
jtulach@1318
   139
 *   <dd>Well-formed variant values have the form <code>SUBTAG
jtulach@1318
   140
 *   (('_'|'-') SUBTAG)*</code> where <code>SUBTAG =
jtulach@1318
   141
 *   [0-9][0-9a-zA-Z]{3} | [0-9a-zA-Z]{5,8}</code>. (Note: BCP 47 only
jtulach@1318
   142
 *   uses hyphen ('-') as a delimiter, this is more lenient).</dd><br>
jtulach@1318
   143
 *
jtulach@1318
   144
 *   <dd>Example: "polyton" (Polytonic Greek), "POSIX"</dd><br>
jtulach@1318
   145
 *
jtulach@1318
   146
 *   <dt><a name="def_extensions"/><b>extensions</b></dt>
jtulach@1318
   147
 *
jtulach@1318
   148
 *   <dd>A map from single character keys to string values, indicating
jtulach@1318
   149
 *   extensions apart from language identification.  The extensions in
jtulach@1318
   150
 *   <code>Locale</code> implement the semantics and syntax of BCP 47
jtulach@1318
   151
 *   extension subtags and private use subtags. The extensions are
jtulach@1318
   152
 *   case insensitive, but <code>Locale</code> canonicalizes all
jtulach@1318
   153
 *   extension keys and values to lower case. Note that extensions
jtulach@1318
   154
 *   cannot have empty values.</dd><br>
jtulach@1318
   155
 *
jtulach@1318
   156
 *   <dd>Well-formed keys are single characters from the set
jtulach@1318
   157
 *   <code>[0-9a-zA-Z]</code>.  Well-formed values have the form
jtulach@1318
   158
 *   <code>SUBTAG ('-' SUBTAG)*</code> where for the key 'x'
jtulach@1318
   159
 *   <code>SUBTAG = [0-9a-zA-Z]{1,8}</code> and for other keys
jtulach@1318
   160
 *   <code>SUBTAG = [0-9a-zA-Z]{2,8}</code> (that is, 'x' allows
jtulach@1318
   161
 *   single-character subtags).</dd><br>
jtulach@1318
   162
 *
jtulach@1318
   163
 *   <dd>Example: key="u"/value="ca-japanese" (Japanese Calendar),
jtulach@1318
   164
 *   key="x"/value="java-1-7"</dd>
jtulach@1318
   165
 * </dl>
jtulach@1318
   166
 *
jtulach@1318
   167
 * <b>Note:</b> Although BCP 47 requires field values to be registered
jtulach@1318
   168
 * in the IANA Language Subtag Registry, the <code>Locale</code> class
jtulach@1318
   169
 * does not provide any validation features.  The <code>Builder</code>
jtulach@1318
   170
 * only checks if an individual field satisfies the syntactic
jtulach@1318
   171
 * requirement (is well-formed), but does not validate the value
jtulach@1318
   172
 * itself.  See {@link Builder} for details.
jtulach@1318
   173
 *
jtulach@1318
   174
 * <h4><a name="def_locale_extension">Unicode locale/language extension</h4>
jtulach@1318
   175
 *
jtulach@1318
   176
 * <p>UTS#35, "Unicode Locale Data Markup Language" defines optional
jtulach@1318
   177
 * attributes and keywords to override or refine the default behavior
jtulach@1318
   178
 * associated with a locale.  A keyword is represented by a pair of
jtulach@1318
   179
 * key and type.  For example, "nu-thai" indicates that Thai local
jtulach@1318
   180
 * digits (value:"thai") should be used for formatting numbers
jtulach@1318
   181
 * (key:"nu").
jtulach@1318
   182
 *
jtulach@1318
   183
 * <p>The keywords are mapped to a BCP 47 extension value using the
jtulach@1318
   184
 * extension key 'u' ({@link #UNICODE_LOCALE_EXTENSION}).  The above
jtulach@1318
   185
 * example, "nu-thai", becomes the extension "u-nu-thai".code
jtulach@1318
   186
 *
jtulach@1318
   187
 * <p>Thus, when a <code>Locale</code> object contains Unicode locale
jtulach@1318
   188
 * attributes and keywords,
jtulach@1318
   189
 * <code>getExtension(UNICODE_LOCALE_EXTENSION)</code> will return a
jtulach@1318
   190
 * String representing this information, for example, "nu-thai".  The
jtulach@1318
   191
 * <code>Locale</code> class also provides {@link
jtulach@1318
   192
 * #getUnicodeLocaleAttributes}, {@link #getUnicodeLocaleKeys}, and
jtulach@1318
   193
 * {@link #getUnicodeLocaleType} which allow you to access Unicode
jtulach@1318
   194
 * locale attributes and key/type pairs directly.  When represented as
jtulach@1318
   195
 * a string, the Unicode Locale Extension lists attributes
jtulach@1318
   196
 * alphabetically, followed by key/type sequences with keys listed
jtulach@1318
   197
 * alphabetically (the order of subtags comprising a key's type is
jtulach@1318
   198
 * fixed when the type is defined)
jtulach@1318
   199
 *
jtulach@1318
   200
 * <p>A well-formed locale key has the form
jtulach@1318
   201
 * <code>[0-9a-zA-Z]{2}</code>.  A well-formed locale type has the
jtulach@1318
   202
 * form <code>"" | [0-9a-zA-Z]{3,8} ('-' [0-9a-zA-Z]{3,8})*</code> (it
jtulach@1318
   203
 * can be empty, or a series of subtags 3-8 alphanums in length).  A
jtulach@1318
   204
 * well-formed locale attribute has the form
jtulach@1318
   205
 * <code>[0-9a-zA-Z]{3,8}</code> (it is a single subtag with the same
jtulach@1318
   206
 * form as a locale type subtag).
jtulach@1318
   207
 *
jtulach@1318
   208
 * <p>The Unicode locale extension specifies optional behavior in
jtulach@1318
   209
 * locale-sensitive services.  Although the LDML specification defines
jtulach@1318
   210
 * various keys and values, actual locale-sensitive service
jtulach@1318
   211
 * implementations in a Java Runtime Environment might not support any
jtulach@1318
   212
 * particular Unicode locale attributes or key/type pairs.
jtulach@1318
   213
 *
jtulach@1318
   214
 * <h4>Creating a Locale</h4>
jtulach@1318
   215
 *
jtulach@1318
   216
 * <p>There are several different ways to create a <code>Locale</code>
jtulach@1318
   217
 * object.
jtulach@1318
   218
 *
jtulach@1318
   219
 * <h5>Builder</h5>
jtulach@1318
   220
 *
jtulach@1318
   221
 * <p>Using {@link Builder} you can construct a <code>Locale</code> object
jtulach@1318
   222
 * that conforms to BCP 47 syntax.
jtulach@1318
   223
 *
jtulach@1318
   224
 * <h5>Constructors</h5>
jtulach@1318
   225
 *
jtulach@1318
   226
 * <p>The <code>Locale</code> class provides three constructors:
jtulach@1318
   227
 * <blockquote>
jtulach@1318
   228
 * <pre>
jtulach@1318
   229
 *     {@link #Locale(String language)}
jtulach@1318
   230
 *     {@link #Locale(String language, String country)}
jtulach@1318
   231
 *     {@link #Locale(String language, String country, String variant)}
jtulach@1318
   232
 * </pre>
jtulach@1318
   233
 * </blockquote>
jtulach@1318
   234
 * These constructors allow you to create a <code>Locale</code> object
jtulach@1318
   235
 * with language, country and variant, but you cannot specify
jtulach@1318
   236
 * script or extensions.
jtulach@1318
   237
 *
jtulach@1318
   238
 * <h5>Factory Methods</h5>
jtulach@1318
   239
 *
jtulach@1318
   240
 * <p>The method {@link #forLanguageTag} creates a <code>Locale</code>
jtulach@1318
   241
 * object for a well-formed BCP 47 language tag.
jtulach@1318
   242
 *
jtulach@1318
   243
 * <h5>Locale Constants</h5>
jtulach@1318
   244
 *
jtulach@1318
   245
 * <p>The <code>Locale</code> class provides a number of convenient constants
jtulach@1318
   246
 * that you can use to create <code>Locale</code> objects for commonly used
jtulach@1318
   247
 * locales. For example, the following creates a <code>Locale</code> object
jtulach@1318
   248
 * for the United States:
jtulach@1318
   249
 * <blockquote>
jtulach@1318
   250
 * <pre>
jtulach@1318
   251
 *     Locale.US
jtulach@1318
   252
 * </pre>
jtulach@1318
   253
 * </blockquote>
jtulach@1318
   254
 *
jtulach@1318
   255
 * <h4>Use of Locale</h4>
jtulach@1318
   256
 *
jtulach@1318
   257
 * <p>Once you've created a <code>Locale</code> you can query it for information
jtulach@1318
   258
 * about itself. Use <code>getCountry</code> to get the country (or region)
jtulach@1318
   259
 * code and <code>getLanguage</code> to get the language code.
jtulach@1318
   260
 * You can use <code>getDisplayCountry</code> to get the
jtulach@1318
   261
 * name of the country suitable for displaying to the user. Similarly,
jtulach@1318
   262
 * you can use <code>getDisplayLanguage</code> to get the name of
jtulach@1318
   263
 * the language suitable for displaying to the user. Interestingly,
jtulach@1318
   264
 * the <code>getDisplayXXX</code> methods are themselves locale-sensitive
jtulach@1318
   265
 * and have two versions: one that uses the default locale and one
jtulach@1318
   266
 * that uses the locale specified as an argument.
jtulach@1318
   267
 *
jtulach@1318
   268
 * <p>The Java Platform provides a number of classes that perform locale-sensitive
jtulach@1318
   269
 * operations. For example, the <code>NumberFormat</code> class formats
jtulach@1318
   270
 * numbers, currency, and percentages in a locale-sensitive manner. Classes
jtulach@1318
   271
 * such as <code>NumberFormat</code> have several convenience methods
jtulach@1318
   272
 * for creating a default object of that type. For example, the
jtulach@1318
   273
 * <code>NumberFormat</code> class provides these three convenience methods
jtulach@1318
   274
 * for creating a default <code>NumberFormat</code> object:
jtulach@1318
   275
 * <blockquote>
jtulach@1318
   276
 * <pre>
jtulach@1318
   277
 *     NumberFormat.getInstance()
jtulach@1318
   278
 *     NumberFormat.getCurrencyInstance()
jtulach@1318
   279
 *     NumberFormat.getPercentInstance()
jtulach@1318
   280
 * </pre>
jtulach@1318
   281
 * </blockquote>
jtulach@1318
   282
 * Each of these methods has two variants; one with an explicit locale
jtulach@1318
   283
 * and one without; the latter uses the default locale:
jtulach@1318
   284
 * <blockquote>
jtulach@1318
   285
 * <pre>
jtulach@1318
   286
 *     NumberFormat.getInstance(myLocale)
jtulach@1318
   287
 *     NumberFormat.getCurrencyInstance(myLocale)
jtulach@1318
   288
 *     NumberFormat.getPercentInstance(myLocale)
jtulach@1318
   289
 * </pre>
jtulach@1318
   290
 * </blockquote>
jtulach@1318
   291
 * A <code>Locale</code> is the mechanism for identifying the kind of object
jtulach@1318
   292
 * (<code>NumberFormat</code>) that you would like to get. The locale is
jtulach@1318
   293
 * <STRONG>just</STRONG> a mechanism for identifying objects,
jtulach@1318
   294
 * <STRONG>not</STRONG> a container for the objects themselves.
jtulach@1318
   295
 *
jtulach@1318
   296
 * <h4>Compatibility</h4>
jtulach@1318
   297
 *
jtulach@1318
   298
 * <p>In order to maintain compatibility with existing usage, Locale's
jtulach@1318
   299
 * constructors retain their behavior prior to the Java Runtime
jtulach@1318
   300
 * Environment version 1.7.  The same is largely true for the
jtulach@1318
   301
 * <code>toString</code> method. Thus Locale objects can continue to
jtulach@1318
   302
 * be used as they were. In particular, clients who parse the output
jtulach@1318
   303
 * of toString into language, country, and variant fields can continue
jtulach@1318
   304
 * to do so (although this is strongly discouraged), although the
jtulach@1318
   305
 * variant field will have additional information in it if script or
jtulach@1318
   306
 * extensions are present.
jtulach@1318
   307
 *
jtulach@1318
   308
 * <p>In addition, BCP 47 imposes syntax restrictions that are not
jtulach@1318
   309
 * imposed by Locale's constructors. This means that conversions
jtulach@1318
   310
 * between some Locales and BCP 47 language tags cannot be made without
jtulach@1318
   311
 * losing information. Thus <code>toLanguageTag</code> cannot
jtulach@1318
   312
 * represent the state of locales whose language, country, or variant
jtulach@1318
   313
 * do not conform to BCP 47.
jtulach@1318
   314
 *
jtulach@1318
   315
 * <p>Because of these issues, it is recommended that clients migrate
jtulach@1318
   316
 * away from constructing non-conforming locales and use the
jtulach@1318
   317
 * <code>forLanguageTag</code> and <code>Locale.Builder</code> APIs instead.
jtulach@1318
   318
 * Clients desiring a string representation of the complete locale can
jtulach@1318
   319
 * then always rely on <code>toLanguageTag</code> for this purpose.
jtulach@1318
   320
 *
jtulach@1318
   321
 * <h5><a name="special_cases_constructor"/>Special cases</h5>
jtulach@1318
   322
 *
jtulach@1318
   323
 * <p>For compatibility reasons, two
jtulach@1318
   324
 * non-conforming locales are treated as special cases.  These are
jtulach@1318
   325
 * <b><tt>ja_JP_JP</tt></b> and <b><tt>th_TH_TH</tt></b>. These are ill-formed
jtulach@1318
   326
 * in BCP 47 since the variants are too short. To ease migration to BCP 47,
jtulach@1318
   327
 * these are treated specially during construction.  These two cases (and only
jtulach@1318
   328
 * these) cause a constructor to generate an extension, all other values behave
jtulach@1318
   329
 * exactly as they did prior to Java 7.
jtulach@1318
   330
 *
jtulach@1318
   331
 * <p>Java has used <tt>ja_JP_JP</tt> to represent Japanese as used in
jtulach@1318
   332
 * Japan together with the Japanese Imperial calendar. This is now
jtulach@1318
   333
 * representable using a Unicode locale extension, by specifying the
jtulach@1318
   334
 * Unicode locale key <tt>ca</tt> (for "calendar") and type
jtulach@1318
   335
 * <tt>japanese</tt>. When the Locale constructor is called with the
jtulach@1318
   336
 * arguments "ja", "JP", "JP", the extension "u-ca-japanese" is
jtulach@1318
   337
 * automatically added.
jtulach@1318
   338
 *
jtulach@1318
   339
 * <p>Java has used <tt>th_TH_TH</tt> to represent Thai as used in
jtulach@1318
   340
 * Thailand together with Thai digits. This is also now representable using
jtulach@1318
   341
 * a Unicode locale extension, by specifying the Unicode locale key
jtulach@1318
   342
 * <tt>nu</tt> (for "number") and value <tt>thai</tt>. When the Locale
jtulach@1318
   343
 * constructor is called with the arguments "th", "TH", "TH", the
jtulach@1318
   344
 * extension "u-nu-thai" is automatically added.
jtulach@1318
   345
 *
jtulach@1318
   346
 * <h5>Serialization</h5>
jtulach@1318
   347
 *
jtulach@1318
   348
 * <p>During serialization, writeObject writes all fields to the output
jtulach@1318
   349
 * stream, including extensions.
jtulach@1318
   350
 *
jtulach@1318
   351
 * <p>During deserialization, readResolve adds extensions as described
jtulach@1318
   352
 * in <a href="#special_cases_constructor">Special Cases</a>, only
jtulach@1318
   353
 * for the two cases th_TH_TH and ja_JP_JP.
jtulach@1318
   354
 *
jtulach@1318
   355
 * <h5>Legacy language codes</h5>
jtulach@1318
   356
 *
jtulach@1318
   357
 * <p>Locale's constructor has always converted three language codes to
jtulach@1318
   358
 * their earlier, obsoleted forms: <tt>he</tt> maps to <tt>iw</tt>,
jtulach@1318
   359
 * <tt>yi</tt> maps to <tt>ji</tt>, and <tt>id</tt> maps to
jtulach@1318
   360
 * <tt>in</tt>.  This continues to be the case, in order to not break
jtulach@1318
   361
 * backwards compatibility.
jtulach@1318
   362
 *
jtulach@1318
   363
 * <p>The APIs added in 1.7 map between the old and new language codes,
jtulach@1318
   364
 * maintaining the old codes internal to Locale (so that
jtulach@1318
   365
 * <code>getLanguage</code> and <code>toString</code> reflect the old
jtulach@1318
   366
 * code), but using the new codes in the BCP 47 language tag APIs (so
jtulach@1318
   367
 * that <code>toLanguageTag</code> reflects the new one). This
jtulach@1318
   368
 * preserves the equivalence between Locales no matter which code or
jtulach@1318
   369
 * API is used to construct them. Java's default resource bundle
jtulach@1318
   370
 * lookup mechanism also implements this mapping, so that resources
jtulach@1318
   371
 * can be named using either convention, see {@link ResourceBundle.Control}.
jtulach@1318
   372
 *
jtulach@1318
   373
 * <h5>Three-letter language/country(region) codes</h5>
jtulach@1318
   374
 *
jtulach@1318
   375
 * <p>The Locale constructors have always specified that the language
jtulach@1318
   376
 * and the country param be two characters in length, although in
jtulach@1318
   377
 * practice they have accepted any length.  The specification has now
jtulach@1318
   378
 * been relaxed to allow language codes of two to eight characters and
jtulach@1318
   379
 * country (region) codes of two to three characters, and in
jtulach@1318
   380
 * particular, three-letter language codes and three-digit region
jtulach@1318
   381
 * codes as specified in the IANA Language Subtag Registry.  For
jtulach@1318
   382
 * compatibility, the implementation still does not impose a length
jtulach@1318
   383
 * constraint.
jtulach@1318
   384
 *
jtulach@1318
   385
 * @see Builder
jtulach@1318
   386
 * @see ResourceBundle
jtulach@1318
   387
 * @see java.text.Format
jtulach@1318
   388
 * @see java.text.NumberFormat
jtulach@1318
   389
 * @see java.text.Collator
jtulach@1318
   390
 * @author Mark Davis
jtulach@1318
   391
 * @since 1.1
jtulach@1318
   392
 */
jtulach@1318
   393
public final class Locale implements Cloneable, Serializable {
jtulach@1318
   394
jtulach@1318
   395
    /** Useful constant for language.
jtulach@1318
   396
     */
jtulach@1318
   397
    static public final Locale ENGLISH = createConstant("en", "");
jtulach@1318
   398
jtulach@1318
   399
    /** Useful constant for language.
jtulach@1318
   400
     */
jtulach@1318
   401
    static public final Locale FRENCH = createConstant("fr", "");
jtulach@1318
   402
jtulach@1318
   403
    /** Useful constant for language.
jtulach@1318
   404
     */
jtulach@1318
   405
    static public final Locale GERMAN = createConstant("de", "");
jtulach@1318
   406
jtulach@1318
   407
    /** Useful constant for language.
jtulach@1318
   408
     */
jtulach@1318
   409
    static public final Locale ITALIAN = createConstant("it", "");
jtulach@1318
   410
jtulach@1318
   411
    /** Useful constant for language.
jtulach@1318
   412
     */
jtulach@1318
   413
    static public final Locale JAPANESE = createConstant("ja", "");
jtulach@1318
   414
jtulach@1318
   415
    /** Useful constant for language.
jtulach@1318
   416
     */
jtulach@1318
   417
    static public final Locale KOREAN = createConstant("ko", "");
jtulach@1318
   418
jtulach@1318
   419
    /** Useful constant for language.
jtulach@1318
   420
     */
jtulach@1318
   421
    static public final Locale CHINESE = createConstant("zh", "");
jtulach@1318
   422
jtulach@1318
   423
    /** Useful constant for language.
jtulach@1318
   424
     */
jtulach@1318
   425
    static public final Locale SIMPLIFIED_CHINESE = createConstant("zh", "CN");
jtulach@1318
   426
jtulach@1318
   427
    /** Useful constant for language.
jtulach@1318
   428
     */
jtulach@1318
   429
    static public final Locale TRADITIONAL_CHINESE = createConstant("zh", "TW");
jtulach@1318
   430
jtulach@1318
   431
    /** Useful constant for country.
jtulach@1318
   432
     */
jtulach@1318
   433
    static public final Locale FRANCE = createConstant("fr", "FR");
jtulach@1318
   434
jtulach@1318
   435
    /** Useful constant for country.
jtulach@1318
   436
     */
jtulach@1318
   437
    static public final Locale GERMANY = createConstant("de", "DE");
jtulach@1318
   438
jtulach@1318
   439
    /** Useful constant for country.
jtulach@1318
   440
     */
jtulach@1318
   441
    static public final Locale ITALY = createConstant("it", "IT");
jtulach@1318
   442
jtulach@1318
   443
    /** Useful constant for country.
jtulach@1318
   444
     */
jtulach@1318
   445
    static public final Locale JAPAN = createConstant("ja", "JP");
jtulach@1318
   446
jtulach@1318
   447
    /** Useful constant for country.
jtulach@1318
   448
     */
jtulach@1318
   449
    static public final Locale KOREA = createConstant("ko", "KR");
jtulach@1318
   450
jtulach@1318
   451
    /** Useful constant for country.
jtulach@1318
   452
     */
jtulach@1318
   453
    static public final Locale CHINA = SIMPLIFIED_CHINESE;
jtulach@1318
   454
jtulach@1318
   455
    /** Useful constant for country.
jtulach@1318
   456
     */
jtulach@1318
   457
    static public final Locale PRC = SIMPLIFIED_CHINESE;
jtulach@1318
   458
jtulach@1318
   459
    /** Useful constant for country.
jtulach@1318
   460
     */
jtulach@1318
   461
    static public final Locale TAIWAN = TRADITIONAL_CHINESE;
jtulach@1318
   462
jtulach@1318
   463
    /** Useful constant for country.
jtulach@1318
   464
     */
jtulach@1318
   465
    static public final Locale UK = createConstant("en", "GB");
jtulach@1318
   466
jtulach@1318
   467
    /** Useful constant for country.
jtulach@1318
   468
     */
jtulach@1318
   469
    static public final Locale US = createConstant("en", "US");
jtulach@1318
   470
jtulach@1318
   471
    /** Useful constant for country.
jtulach@1318
   472
     */
jtulach@1318
   473
    static public final Locale CANADA = createConstant("en", "CA");
jtulach@1318
   474
jtulach@1318
   475
    /** Useful constant for country.
jtulach@1318
   476
     */
jtulach@1318
   477
    static public final Locale CANADA_FRENCH = createConstant("fr", "CA");
jtulach@1318
   478
jtulach@1318
   479
    /**
jtulach@1318
   480
     * Useful constant for the root locale.  The root locale is the locale whose
jtulach@1318
   481
     * language, country, and variant are empty ("") strings.  This is regarded
jtulach@1318
   482
     * as the base locale of all locales, and is used as the language/country
jtulach@1318
   483
     * neutral locale for the locale sensitive operations.
jtulach@1318
   484
     *
jtulach@1318
   485
     * @since 1.6
jtulach@1318
   486
     */
jtulach@1318
   487
    static public final Locale ROOT = createConstant("", "");
jtulach@1318
   488
jtulach@1318
   489
    /**
jtulach@1318
   490
     * The key for the private use extension ('x').
jtulach@1318
   491
     *
jtulach@1318
   492
     * @see #getExtension(char)
jtulach@1318
   493
     * @see Builder#setExtension(char, String)
jtulach@1318
   494
     * @since 1.7
jtulach@1318
   495
     */
jtulach@1318
   496
    static public final char PRIVATE_USE_EXTENSION = 'x';
jtulach@1318
   497
jtulach@1318
   498
    /**
jtulach@1318
   499
     * The key for Unicode locale extension ('u').
jtulach@1318
   500
     *
jtulach@1318
   501
     * @see #getExtension(char)
jtulach@1318
   502
     * @see Builder#setExtension(char, String)
jtulach@1318
   503
     * @since 1.7
jtulach@1318
   504
     */
jtulach@1318
   505
    static public final char UNICODE_LOCALE_EXTENSION = 'u';
jtulach@1318
   506
jtulach@1318
   507
    /** serialization ID
jtulach@1318
   508
     */
jtulach@1318
   509
    static final long serialVersionUID = 9149081749638150636L;
jtulach@1318
   510
jtulach@1318
   511
    /**
jtulach@1318
   512
     * Display types for retrieving localized names from the name providers.
jtulach@1318
   513
     */
jtulach@1318
   514
    private static final int DISPLAY_LANGUAGE = 0;
jtulach@1318
   515
    private static final int DISPLAY_COUNTRY  = 1;
jtulach@1318
   516
    private static final int DISPLAY_VARIANT  = 2;
jtulach@1318
   517
    private static final int DISPLAY_SCRIPT   = 3;
jaroslav@1337
   518
jaroslav@1337
   519
    static Locale getInstance(String language, String script, String region, String v, Object object) {
jaroslav@1337
   520
        return new Locale(language, script, region);
jaroslav@1337
   521
    }
jaroslav@1337
   522
jaroslav@1337
   523
    static Locale getInstance(String no, String no0, String ny) {
jaroslav@1337
   524
        return new Locale(no, no0, ny);
jaroslav@1337
   525
    }
jaroslav@1337
   526
    
jaroslav@1320
   527
    private String language;
jaroslav@1320
   528
    private String country;
jaroslav@1320
   529
    private String variant;
jtulach@1318
   530
jtulach@1318
   531
jtulach@1318
   532
    /**
jtulach@1318
   533
     * Construct a locale from language, country and variant.
jtulach@1318
   534
     * This constructor normalizes the language value to lowercase and
jtulach@1318
   535
     * the country value to uppercase.
jtulach@1318
   536
     * <p>
jtulach@1318
   537
     * <b>Note:</b>
jtulach@1318
   538
     * <ul>
jtulach@1318
   539
     * <li>ISO 639 is not a stable standard; some of the language codes it defines
jtulach@1318
   540
     * (specifically "iw", "ji", and "in") have changed.  This constructor accepts both the
jtulach@1318
   541
     * old codes ("iw", "ji", and "in") and the new codes ("he", "yi", and "id"), but all other
jtulach@1318
   542
     * API on Locale will return only the OLD codes.
jtulach@1318
   543
     * <li>For backward compatibility reasons, this constructor does not make
jtulach@1318
   544
     * any syntactic checks on the input.
jtulach@1318
   545
     * <li>The two cases ("ja", "JP", "JP") and ("th", "TH", "TH") are handled specially,
jtulach@1318
   546
     * see <a href="#special_cases_constructor">Special Cases</a> for more information.
jtulach@1318
   547
     * </ul>
jtulach@1318
   548
     *
jtulach@1318
   549
     * @param language An ISO 639 alpha-2 or alpha-3 language code, or a language subtag
jtulach@1318
   550
     * up to 8 characters in length.  See the <code>Locale</code> class description about
jtulach@1318
   551
     * valid language values.
jtulach@1318
   552
     * @param country An ISO 3166 alpha-2 country code or a UN M.49 numeric-3 area code.
jtulach@1318
   553
     * See the <code>Locale</code> class description about valid country values.
jtulach@1318
   554
     * @param variant Any arbitrary value used to indicate a variation of a <code>Locale</code>.
jtulach@1318
   555
     * See the <code>Locale</code> class description for the details.
jtulach@1318
   556
     * @exception NullPointerException thrown if any argument is null.
jtulach@1318
   557
     */
jtulach@1318
   558
    public Locale(String language, String country, String variant) {
jtulach@1318
   559
        if (language== null || country == null || variant == null) {
jtulach@1318
   560
            throw new NullPointerException();
jtulach@1318
   561
        }
jaroslav@1320
   562
        this.language = language;
jaroslav@1320
   563
        this.country = country;
jaroslav@1320
   564
        this.variant = variant;
jtulach@1318
   565
    }
jtulach@1318
   566
jtulach@1318
   567
    /**
jtulach@1318
   568
     * Construct a locale from language and country.
jtulach@1318
   569
     * This constructor normalizes the language value to lowercase and
jtulach@1318
   570
     * the country value to uppercase.
jtulach@1318
   571
     * <p>
jtulach@1318
   572
     * <b>Note:</b>
jtulach@1318
   573
     * <ul>
jtulach@1318
   574
     * <li>ISO 639 is not a stable standard; some of the language codes it defines
jtulach@1318
   575
     * (specifically "iw", "ji", and "in") have changed.  This constructor accepts both the
jtulach@1318
   576
     * old codes ("iw", "ji", and "in") and the new codes ("he", "yi", and "id"), but all other
jtulach@1318
   577
     * API on Locale will return only the OLD codes.
jtulach@1318
   578
     * <li>For backward compatibility reasons, this constructor does not make
jtulach@1318
   579
     * any syntactic checks on the input.
jtulach@1318
   580
     * </ul>
jtulach@1318
   581
     *
jtulach@1318
   582
     * @param language An ISO 639 alpha-2 or alpha-3 language code, or a language subtag
jtulach@1318
   583
     * up to 8 characters in length.  See the <code>Locale</code> class description about
jtulach@1318
   584
     * valid language values.
jtulach@1318
   585
     * @param country An ISO 3166 alpha-2 country code or a UN M.49 numeric-3 area code.
jtulach@1318
   586
     * See the <code>Locale</code> class description about valid country values.
jtulach@1318
   587
     * @exception NullPointerException thrown if either argument is null.
jtulach@1318
   588
     */
jtulach@1318
   589
    public Locale(String language, String country) {
jtulach@1318
   590
        this(language, country, "");
jtulach@1318
   591
    }
jtulach@1318
   592
jtulach@1318
   593
    /**
jtulach@1318
   594
     * Construct a locale from a language code.
jtulach@1318
   595
     * This constructor normalizes the language value to lowercase.
jtulach@1318
   596
     * <p>
jtulach@1318
   597
     * <b>Note:</b>
jtulach@1318
   598
     * <ul>
jtulach@1318
   599
     * <li>ISO 639 is not a stable standard; some of the language codes it defines
jtulach@1318
   600
     * (specifically "iw", "ji", and "in") have changed.  This constructor accepts both the
jtulach@1318
   601
     * old codes ("iw", "ji", and "in") and the new codes ("he", "yi", and "id"), but all other
jtulach@1318
   602
     * API on Locale will return only the OLD codes.
jtulach@1318
   603
     * <li>For backward compatibility reasons, this constructor does not make
jtulach@1318
   604
     * any syntactic checks on the input.
jtulach@1318
   605
     * </ul>
jtulach@1318
   606
     *
jtulach@1318
   607
     * @param language An ISO 639 alpha-2 or alpha-3 language code, or a language subtag
jtulach@1318
   608
     * up to 8 characters in length.  See the <code>Locale</code> class description about
jtulach@1318
   609
     * valid language values.
jtulach@1318
   610
     * @exception NullPointerException thrown if argument is null.
jtulach@1318
   611
     * @since 1.4
jtulach@1318
   612
     */
jtulach@1318
   613
    public Locale(String language) {
jtulach@1318
   614
        this(language, "", "");
jtulach@1318
   615
    }
jtulach@1318
   616
jtulach@1318
   617
    /**
jtulach@1318
   618
     * This method must be called only for creating the Locale.*
jtulach@1318
   619
     * constants due to making shortcuts.
jtulach@1318
   620
     */
jtulach@1318
   621
    private static Locale createConstant(String lang, String country) {
jaroslav@1320
   622
        return new Locale(lang, country);
jtulach@1318
   623
    }
jtulach@1318
   624
jtulach@1318
   625
    /**
jtulach@1318
   626
     * Gets the current value of the default locale for this instance
jtulach@1318
   627
     * of the Java Virtual Machine.
jtulach@1318
   628
     * <p>
jtulach@1318
   629
     * The Java Virtual Machine sets the default locale during startup
jtulach@1318
   630
     * based on the host environment. It is used by many locale-sensitive
jtulach@1318
   631
     * methods if no locale is explicitly specified.
jtulach@1318
   632
     * It can be changed using the
jtulach@1318
   633
     * {@link #setDefault(java.util.Locale) setDefault} method.
jtulach@1318
   634
     *
jtulach@1318
   635
     * @return the default locale for this instance of the Java Virtual Machine
jtulach@1318
   636
     */
jtulach@1318
   637
    public static Locale getDefault() {
jaroslav@1781
   638
        String lang = language();
jaroslav@1781
   639
        if (lang != null) {
jaroslav@1781
   640
            String[] arr = lang.split("-");
jaroslav@1781
   641
            return new Locale(arr[0], arr[1]);
jaroslav@1781
   642
        }
jaroslav@1320
   643
        return Locale.US;
jtulach@1318
   644
    }
jaroslav@1781
   645
    
jaroslav@1781
   646
    @JavaScriptBody(args = {}, body = ""
jaroslav@1781
   647
        + "if (navigator.language) return navigator.language;\n"
jaroslav@1781
   648
        + "if (navigator.userLangage) return navigator.userLangage;\n"
jaroslav@1781
   649
        + "return null;\n"
jaroslav@1781
   650
    )
jaroslav@1781
   651
    private static native String language();
jtulach@1318
   652
jtulach@1318
   653
    /**
jaroslav@1337
   654
     * Gets the current value of the default locale for the specified Category
jaroslav@1337
   655
     * for this instance of the Java Virtual Machine.
jaroslav@1337
   656
     * <p>
jaroslav@1337
   657
     * The Java Virtual Machine sets the default locale during startup based
jaroslav@1337
   658
     * on the host environment. It is used by many locale-sensitive methods
jaroslav@1337
   659
     * if no locale is explicitly specified. It can be changed using the
jaroslav@1337
   660
     * setDefault(Locale.Category, Locale) method.
jaroslav@1337
   661
     *
jaroslav@1337
   662
     * @param category - the specified category to get the default locale
jaroslav@1337
   663
     * @throws NullPointerException - if category is null
jaroslav@1337
   664
     * @return the default locale for the specified Category for this instance
jaroslav@1337
   665
     *     of the Java Virtual Machine
jaroslav@1337
   666
     * @see #setDefault(Locale.Category, Locale)
jaroslav@1337
   667
     * @since 1.7
jaroslav@1337
   668
     */
jaroslav@1337
   669
    public static Locale getDefault(Locale.Category category) {
jaroslav@1781
   670
        return getDefault();
jaroslav@1337
   671
    }
jaroslav@1337
   672
    
jaroslav@1337
   673
    /**
jtulach@1318
   674
     * Sets the default locale for this instance of the Java Virtual Machine.
jtulach@1318
   675
     * This does not affect the host locale.
jtulach@1318
   676
     * <p>
jtulach@1318
   677
     * If there is a security manager, its <code>checkPermission</code>
jtulach@1318
   678
     * method is called with a <code>PropertyPermission("user.language", "write")</code>
jtulach@1318
   679
     * permission before the default locale is changed.
jtulach@1318
   680
     * <p>
jtulach@1318
   681
     * The Java Virtual Machine sets the default locale during startup
jtulach@1318
   682
     * based on the host environment. It is used by many locale-sensitive
jtulach@1318
   683
     * methods if no locale is explicitly specified.
jtulach@1318
   684
     * <p>
jtulach@1318
   685
     * Since changing the default locale may affect many different areas
jtulach@1318
   686
     * of functionality, this method should only be used if the caller
jtulach@1318
   687
     * is prepared to reinitialize locale-sensitive code running
jtulach@1318
   688
     * within the same Java Virtual Machine.
jtulach@1318
   689
     * <p>
jtulach@1318
   690
     * By setting the default locale with this method, all of the default
jtulach@1318
   691
     * locales for each Category are also set to the specified default locale.
jtulach@1318
   692
     *
jtulach@1318
   693
     * @throws SecurityException
jtulach@1318
   694
     *        if a security manager exists and its
jtulach@1318
   695
     *        <code>checkPermission</code> method doesn't allow the operation.
jtulach@1318
   696
     * @throws NullPointerException if <code>newLocale</code> is null
jtulach@1318
   697
     * @param newLocale the new default locale
jtulach@1318
   698
     * @see SecurityManager#checkPermission
jtulach@1318
   699
     * @see java.util.PropertyPermission
jtulach@1318
   700
     */
jaroslav@1320
   701
    public static void setDefault(Locale newLocale) {
jaroslav@1320
   702
        throw new SecurityException();
jtulach@1318
   703
    }
jtulach@1318
   704
jtulach@1318
   705
    /**
jtulach@1318
   706
     * Returns an array of all installed locales.
jtulach@1318
   707
     * The returned array represents the union of locales supported
jtulach@1318
   708
     * by the Java runtime environment and by installed
jtulach@1318
   709
     * {@link java.util.spi.LocaleServiceProvider LocaleServiceProvider}
jtulach@1318
   710
     * implementations.  It must contain at least a <code>Locale</code>
jtulach@1318
   711
     * instance equal to {@link java.util.Locale#US Locale.US}.
jtulach@1318
   712
     *
jtulach@1318
   713
     * @return An array of installed locales.
jtulach@1318
   714
     */
jtulach@1318
   715
    public static Locale[] getAvailableLocales() {
jaroslav@1320
   716
        return new Locale[] { Locale.US };
jtulach@1318
   717
    }
jtulach@1318
   718
jtulach@1318
   719
    /**
jtulach@1318
   720
     * Returns the language code of this Locale.
jtulach@1318
   721
     *
jtulach@1318
   722
     * <p><b>Note:</b> ISO 639 is not a stable standard&mdash; some languages' codes have changed.
jtulach@1318
   723
     * Locale's constructor recognizes both the new and the old codes for the languages
jtulach@1318
   724
     * whose codes have changed, but this function always returns the old code.  If you
jtulach@1318
   725
     * want to check for a specific language whose code has changed, don't do
jtulach@1318
   726
     * <pre>
jtulach@1318
   727
     * if (locale.getLanguage().equals("he")) // BAD!
jtulach@1318
   728
     *    ...
jtulach@1318
   729
     * </pre>
jtulach@1318
   730
     * Instead, do
jtulach@1318
   731
     * <pre>
jtulach@1318
   732
     * if (locale.getLanguage().equals(new Locale("he").getLanguage()))
jtulach@1318
   733
     *    ...
jtulach@1318
   734
     * </pre>
jtulach@1318
   735
     * @return The language code, or the empty string if none is defined.
jtulach@1318
   736
     * @see #getDisplayLanguage
jtulach@1318
   737
     */
jtulach@1318
   738
    public String getLanguage() {
jaroslav@1320
   739
        return language;
jtulach@1318
   740
    }
jtulach@1318
   741
jtulach@1318
   742
    /**
jtulach@1318
   743
     * Returns the script for this locale, which should
jtulach@1318
   744
     * either be the empty string or an ISO 15924 4-letter script
jtulach@1318
   745
     * code. The first letter is uppercase and the rest are
jtulach@1318
   746
     * lowercase, for example, 'Latn', 'Cyrl'.
jtulach@1318
   747
     *
jtulach@1318
   748
     * @return The script code, or the empty string if none is defined.
jtulach@1318
   749
     * @see #getDisplayScript
jtulach@1318
   750
     * @since 1.7
jtulach@1318
   751
     */
jtulach@1318
   752
    public String getScript() {
jaroslav@1320
   753
        return "";
jtulach@1318
   754
    }
jtulach@1318
   755
jtulach@1318
   756
    /**
jtulach@1318
   757
     * Returns the country/region code for this locale, which should
jtulach@1318
   758
     * either be the empty string, an uppercase ISO 3166 2-letter code,
jtulach@1318
   759
     * or a UN M.49 3-digit code.
jtulach@1318
   760
     *
jtulach@1318
   761
     * @return The country/region code, or the empty string if none is defined.
jtulach@1318
   762
     * @see #getDisplayCountry
jtulach@1318
   763
     */
jtulach@1318
   764
    public String getCountry() {
jaroslav@1320
   765
        return country;
jtulach@1318
   766
    }
jtulach@1318
   767
jtulach@1318
   768
    /**
jtulach@1318
   769
     * Returns the variant code for this locale.
jtulach@1318
   770
     *
jtulach@1318
   771
     * @return The variant code, or the empty string if none is defined.
jtulach@1318
   772
     * @see #getDisplayVariant
jtulach@1318
   773
     */
jtulach@1318
   774
    public String getVariant() {
jaroslav@1320
   775
        return variant;
jaroslav@1320
   776
    }
jaroslav@1320
   777
    
jaroslav@1320
   778
    String getRegion() {
jaroslav@1320
   779
        return getCountry();
jtulach@1318
   780
    }
jtulach@1318
   781
jtulach@1318
   782
    /**
jtulach@1318
   783
     * Returns the extension (or private use) value associated with
jtulach@1318
   784
     * the specified key, or null if there is no extension
jtulach@1318
   785
     * associated with the key. To be well-formed, the key must be one
jtulach@1318
   786
     * of <code>[0-9A-Za-z]</code>. Keys are case-insensitive, so
jtulach@1318
   787
     * for example 'z' and 'Z' represent the same extension.
jtulach@1318
   788
     *
jtulach@1318
   789
     * @param key the extension key
jtulach@1318
   790
     * @return The extension, or null if this locale defines no
jtulach@1318
   791
     * extension for the specified key.
jtulach@1318
   792
     * @throws IllegalArgumentException if key is not well-formed
jtulach@1318
   793
     * @see #PRIVATE_USE_EXTENSION
jtulach@1318
   794
     * @see #UNICODE_LOCALE_EXTENSION
jtulach@1318
   795
     * @since 1.7
jtulach@1318
   796
     */
jtulach@1318
   797
    public String getExtension(char key) {
jaroslav@1320
   798
        return null;
jtulach@1318
   799
    }
jtulach@1318
   800
jtulach@1318
   801
    /**
jtulach@1318
   802
     * Returns the set of extension keys associated with this locale, or the
jtulach@1318
   803
     * empty set if it has no extensions. The returned set is unmodifiable.
jtulach@1318
   804
     * The keys will all be lower-case.
jtulach@1318
   805
     *
jtulach@1318
   806
     * @return The set of extension keys, or the empty set if this locale has
jtulach@1318
   807
     * no extensions.
jtulach@1318
   808
     * @since 1.7
jtulach@1318
   809
     */
jtulach@1318
   810
    public Set<Character> getExtensionKeys() {
jaroslav@1320
   811
        return Collections.emptySet();
jtulach@1318
   812
    }
jtulach@1318
   813
jtulach@1318
   814
    /**
jtulach@1318
   815
     * Returns the set of unicode locale attributes associated with
jtulach@1318
   816
     * this locale, or the empty set if it has no attributes. The
jtulach@1318
   817
     * returned set is unmodifiable.
jtulach@1318
   818
     *
jtulach@1318
   819
     * @return The set of attributes.
jtulach@1318
   820
     * @since 1.7
jtulach@1318
   821
     */
jtulach@1318
   822
    public Set<String> getUnicodeLocaleAttributes() {
jaroslav@1320
   823
        return Collections.emptySet();
jtulach@1318
   824
    }
jtulach@1318
   825
jtulach@1318
   826
    /**
jtulach@1318
   827
     * Returns the Unicode locale type associated with the specified Unicode locale key
jtulach@1318
   828
     * for this locale. Returns the empty string for keys that are defined with no type.
jtulach@1318
   829
     * Returns null if the key is not defined. Keys are case-insensitive. The key must
jtulach@1318
   830
     * be two alphanumeric characters ([0-9a-zA-Z]), or an IllegalArgumentException is
jtulach@1318
   831
     * thrown.
jtulach@1318
   832
     *
jtulach@1318
   833
     * @param key the Unicode locale key
jtulach@1318
   834
     * @return The Unicode locale type associated with the key, or null if the
jtulach@1318
   835
     * locale does not define the key.
jtulach@1318
   836
     * @throws IllegalArgumentException if the key is not well-formed
jtulach@1318
   837
     * @throws NullPointerException if <code>key</code> is null
jtulach@1318
   838
     * @since 1.7
jtulach@1318
   839
     */
jtulach@1318
   840
    public String getUnicodeLocaleType(String key) {
jaroslav@1320
   841
        return null;
jtulach@1318
   842
    }
jtulach@1318
   843
jtulach@1318
   844
    /**
jtulach@1318
   845
     * Returns the set of Unicode locale keys defined by this locale, or the empty set if
jtulach@1318
   846
     * this locale has none.  The returned set is immutable.  Keys are all lower case.
jtulach@1318
   847
     *
jtulach@1318
   848
     * @return The set of Unicode locale keys, or the empty set if this locale has
jtulach@1318
   849
     * no Unicode locale keywords.
jtulach@1318
   850
     * @since 1.7
jtulach@1318
   851
     */
jtulach@1318
   852
    public Set<String> getUnicodeLocaleKeys() {
jaroslav@1320
   853
        return Collections.emptySet();
jtulach@1318
   854
    }
jtulach@1318
   855
jtulach@1318
   856
    /**
jtulach@1318
   857
     * Returns a string representation of this <code>Locale</code>
jtulach@1318
   858
     * object, consisting of language, country, variant, script,
jtulach@1318
   859
     * and extensions as below:
jtulach@1318
   860
     * <p><blockquote>
jtulach@1318
   861
     * language + "_" + country + "_" + (variant + "_#" | "#") + script + "-" + extensions
jtulach@1318
   862
     * </blockquote>
jtulach@1318
   863
     *
jtulach@1318
   864
     * Language is always lower case, country is always upper case, script is always title
jtulach@1318
   865
     * case, and extensions are always lower case.  Extensions and private use subtags
jtulach@1318
   866
     * will be in canonical order as explained in {@link #toLanguageTag}.
jtulach@1318
   867
     *
jtulach@1318
   868
     * <p>When the locale has neither script nor extensions, the result is the same as in
jtulach@1318
   869
     * Java 6 and prior.
jtulach@1318
   870
     *
jtulach@1318
   871
     * <p>If both the language and country fields are missing, this function will return
jtulach@1318
   872
     * the empty string, even if the variant, script, or extensions field is present (you
jtulach@1318
   873
     * can't have a locale with just a variant, the variant must accompany a well-formed
jtulach@1318
   874
     * language or country code).
jtulach@1318
   875
     *
jtulach@1318
   876
     * <p>If script or extensions are present and variant is missing, no underscore is
jtulach@1318
   877
     * added before the "#".
jtulach@1318
   878
     *
jtulach@1318
   879
     * <p>This behavior is designed to support debugging and to be compatible with
jtulach@1318
   880
     * previous uses of <code>toString</code> that expected language, country, and variant
jtulach@1318
   881
     * fields only.  To represent a Locale as a String for interchange purposes, use
jtulach@1318
   882
     * {@link #toLanguageTag}.
jtulach@1318
   883
     *
jtulach@1318
   884
     * <p>Examples: <ul><tt>
jtulach@1318
   885
     * <li>en
jtulach@1318
   886
     * <li>de_DE
jtulach@1318
   887
     * <li>_GB
jtulach@1318
   888
     * <li>en_US_WIN
jtulach@1318
   889
     * <li>de__POSIX
jtulach@1318
   890
     * <li>zh_CN_#Hans
jtulach@1318
   891
     * <li>zh_TW_#Hant-x-java
jtulach@1318
   892
     * <li>th_TH_TH_#u-nu-thai</tt></ul>
jtulach@1318
   893
     *
jtulach@1318
   894
     * @return A string representation of the Locale, for debugging.
jtulach@1318
   895
     * @see #getDisplayName
jtulach@1318
   896
     * @see #toLanguageTag
jtulach@1318
   897
     */
jtulach@1318
   898
    @Override
jtulach@1318
   899
    public final String toString() {
jaroslav@1320
   900
        Locale baseLocale = this;
jtulach@1318
   901
        boolean l = (baseLocale.getLanguage().length() != 0);
jtulach@1318
   902
        boolean s = (baseLocale.getScript().length() != 0);
jtulach@1318
   903
        boolean r = (baseLocale.getRegion().length() != 0);
jtulach@1318
   904
        boolean v = (baseLocale.getVariant().length() != 0);
jaroslav@1320
   905
        boolean e = false; //(localeExtensions != null && localeExtensions.getID().length() != 0);
jtulach@1318
   906
jtulach@1318
   907
        StringBuilder result = new StringBuilder(baseLocale.getLanguage());
jtulach@1318
   908
        if (r || (l && (v || s || e))) {
jtulach@1318
   909
            result.append('_')
jtulach@1318
   910
                .append(baseLocale.getRegion()); // This may just append '_'
jtulach@1318
   911
        }
jtulach@1318
   912
        if (v && (l || r)) {
jtulach@1318
   913
            result.append('_')
jtulach@1318
   914
                .append(baseLocale.getVariant());
jtulach@1318
   915
        }
jtulach@1318
   916
jtulach@1318
   917
        if (s && (l || r)) {
jtulach@1318
   918
            result.append("_#")
jtulach@1318
   919
                .append(baseLocale.getScript());
jtulach@1318
   920
        }
jtulach@1318
   921
jtulach@1318
   922
        if (e && (l || r)) {
jtulach@1318
   923
            result.append('_');
jtulach@1318
   924
            if (!s) {
jtulach@1318
   925
                result.append('#');
jtulach@1318
   926
            }
jaroslav@1320
   927
//            result.append(localeExtensions.getID());
jtulach@1318
   928
        }
jtulach@1318
   929
jtulach@1318
   930
        return result.toString();
jtulach@1318
   931
    }
jtulach@1318
   932
jaroslav@1320
   933
    
jtulach@1318
   934
    /**
jtulach@1318
   935
     * Overrides Cloneable.
jtulach@1318
   936
     */
jtulach@1318
   937
    public Object clone()
jtulach@1318
   938
    {
jtulach@1318
   939
        try {
jtulach@1318
   940
            Locale that = (Locale)super.clone();
jtulach@1318
   941
            return that;
jtulach@1318
   942
        } catch (CloneNotSupportedException e) {
jtulach@1318
   943
            throw new InternalError();
jtulach@1318
   944
        }
jtulach@1318
   945
    }
jtulach@1318
   946
jtulach@1318
   947
    /**
jtulach@1318
   948
     * Override hashCode.
jtulach@1318
   949
     * Since Locales are often used in hashtables, caches the value
jtulach@1318
   950
     * for speed.
jtulach@1318
   951
     */
jtulach@1318
   952
    @Override
jtulach@1318
   953
    public int hashCode() {
jaroslav@1320
   954
        int hash = 3;
jaroslav@1320
   955
        hash = 43 * hash + Objects.hashCode(this.language);
jaroslav@1320
   956
        hash = 43 * hash + Objects.hashCode(this.country);
jaroslav@1320
   957
        hash = 43 * hash + Objects.hashCode(this.variant);
jaroslav@1320
   958
        return hash;
jtulach@1318
   959
    }
jtulach@1318
   960
jtulach@1318
   961
    // Overrides
jtulach@1318
   962
    @Override
jtulach@1318
   963
    public boolean equals(Object obj) {
jaroslav@1320
   964
        if (obj == null) {
jtulach@1318
   965
            return false;
jtulach@1318
   966
        }
jaroslav@1320
   967
        if (getClass() != obj.getClass()) {
jaroslav@1320
   968
            return false;
jtulach@1318
   969
        }
jaroslav@1320
   970
        final Locale other = (Locale) obj;
jaroslav@1320
   971
        if (!Objects.equals(this.language, other.language)) {
jaroslav@1320
   972
            return false;
jaroslav@1320
   973
        }
jaroslav@1320
   974
        if (!Objects.equals(this.country, other.country)) {
jaroslav@1320
   975
            return false;
jaroslav@1320
   976
        }
jaroslav@1320
   977
        if (!Objects.equals(this.variant, other.variant)) {
jaroslav@1320
   978
            return false;
jaroslav@1320
   979
        }
jaroslav@1320
   980
        return true;
jtulach@1318
   981
    }
jtulach@1318
   982
jaroslav@1337
   983
    /**
jaroslav@1337
   984
     * Enum for locale categories.  These locale categories are used to get/set
jaroslav@1337
   985
     * the default locale for the specific functionality represented by the
jaroslav@1337
   986
     * category.
jaroslav@1337
   987
     *
jaroslav@1337
   988
     * @see #getDefault(Locale.Category)
jaroslav@1337
   989
     * @see #setDefault(Locale.Category, Locale)
jaroslav@1337
   990
     * @since 1.7
jaroslav@1337
   991
     */
jaroslav@1337
   992
    public enum Category {
jaroslav@1337
   993
jaroslav@1337
   994
        /**
jaroslav@1337
   995
         * Category used to represent the default locale for
jaroslav@1337
   996
         * displaying user interfaces.
jaroslav@1337
   997
         */
jaroslav@1337
   998
        DISPLAY("user.language.display",
jaroslav@1337
   999
                "user.script.display",
jaroslav@1337
  1000
                "user.country.display",
jaroslav@1337
  1001
                "user.variant.display"),
jaroslav@1337
  1002
jaroslav@1337
  1003
        /**
jaroslav@1337
  1004
         * Category used to represent the default locale for
jaroslav@1337
  1005
         * formatting dates, numbers, and/or currencies.
jaroslav@1337
  1006
         */
jaroslav@1337
  1007
        FORMAT("user.language.format",
jaroslav@1337
  1008
               "user.script.format",
jaroslav@1337
  1009
               "user.country.format",
jaroslav@1337
  1010
               "user.variant.format");
jaroslav@1337
  1011
jaroslav@1337
  1012
        Category(String languageKey, String scriptKey, String countryKey, String variantKey) {
jaroslav@1337
  1013
            this.languageKey = languageKey;
jaroslav@1337
  1014
            this.scriptKey = scriptKey;
jaroslav@1337
  1015
            this.countryKey = countryKey;
jaroslav@1337
  1016
            this.variantKey = variantKey;
jaroslav@1337
  1017
        }
jaroslav@1337
  1018
jaroslav@1337
  1019
        final String languageKey;
jaroslav@1337
  1020
        final String scriptKey;
jaroslav@1337
  1021
        final String countryKey;
jaroslav@1337
  1022
        final String variantKey;
jaroslav@1337
  1023
    }
jtulach@1318
  1024
jtulach@1318
  1025
}