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