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