1 /*
   2  * Copyright (c) 1996, 2016, 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 java.text.MessageFormat;
  49 import java.util.spi.LocaleNameProvider;
  50 
  51 import sun.security.action.GetPropertyAction;
  52 import sun.util.locale.BaseLocale;
  53 import sun.util.locale.InternalLocaleBuilder;
  54 import sun.util.locale.LanguageTag;
  55 import sun.util.locale.LocaleExtensions;
  56 import sun.util.locale.LocaleMatcher;
  57 import sun.util.locale.LocaleObjectCache;
  58 import sun.util.locale.LocaleSyntaxException;
  59 import sun.util.locale.LocaleUtils;
  60 import sun.util.locale.ParseStatus;
  61 import sun.util.locale.provider.LocaleProviderAdapter;
  62 import sun.util.locale.provider.LocaleResources;
  63 import sun.util.locale.provider.LocaleServiceProviderPool;
  64 
  65 /**
  66  * A <code>Locale</code> object represents a specific geographical, political,
  67  * or cultural region. An operation that requires a <code>Locale</code> to perform
  68  * its task is called <em>locale-sensitive</em> and uses the <code>Locale</code>
  69  * to tailor information for the user. For example, displaying a number
  70  * is a locale-sensitive operation&mdash; the number should be formatted
  71  * according to the customs and conventions of the user's native country,
  72  * region, or culture.
  73  *
  74  * <p> The {@code Locale} class implements IETF BCP 47 which is composed of
  75  * <a href="http://tools.ietf.org/html/rfc4647">RFC 4647 "Matching of Language
  76  * Tags"</a> and <a href="http://tools.ietf.org/html/rfc5646">RFC 5646 "Tags
  77  * for Identifying Languages"</a> with support for the LDML (UTS#35, "Unicode
  78  * Locale Data Markup Language") BCP 47-compatible extensions for locale data
  79  * exchange.
  80  *
  81  * <p> A <code>Locale</code> object logically consists of the fields
  82  * described below.
  83  *
  84  * <dl>
  85  *   <dt><a name="def_language"><b>language</b></a></dt>
  86  *
  87  *   <dd>ISO 639 alpha-2 or alpha-3 language code, or registered
  88  *   language subtags up to 8 alpha letters (for future enhancements).
  89  *   When a language has both an alpha-2 code and an alpha-3 code, the
  90  *   alpha-2 code must be used.  You can find a full list of valid
  91  *   language codes in the IANA Language Subtag Registry (search for
  92  *   "Type: language").  The language field is case insensitive, but
  93  *   <code>Locale</code> always canonicalizes to lower case.</dd>
  94  *
  95  *   <dd>Well-formed language values have the form
  96  *   <code>[a-zA-Z]{2,8}</code>.  Note that this is not the full
  97  *   BCP47 language production, since it excludes extlang.  They are
  98  *   not needed since modern three-letter language codes replace
  99  *   them.</dd>
 100  *
 101  *   <dd>Example: "en" (English), "ja" (Japanese), "kok" (Konkani)</dd>
 102  *
 103  *   <dt><a name="def_script"><b>script</b></a></dt>
 104  *
 105  *   <dd>ISO 15924 alpha-4 script code.  You can find a full list of
 106  *   valid script codes in the IANA Language Subtag Registry (search
 107  *   for "Type: script").  The script field is case insensitive, but
 108  *   <code>Locale</code> always canonicalizes to title case (the first
 109  *   letter is upper case and the rest of the letters are lower
 110  *   case).</dd>
 111  *
 112  *   <dd>Well-formed script values have the form
 113  *   <code>[a-zA-Z]{4}</code></dd>
 114  *
 115  *   <dd>Example: "Latn" (Latin), "Cyrl" (Cyrillic)</dd>
 116  *
 117  *   <dt><a name="def_region"><b>country (region)</b></a></dt>
 118  *
 119  *   <dd>ISO 3166 alpha-2 country code or UN M.49 numeric-3 area code.
 120  *   You can find a full list of valid country and region codes in the
 121  *   IANA Language Subtag Registry (search for "Type: region").  The
 122  *   country (region) field is case insensitive, but
 123  *   <code>Locale</code> always canonicalizes to upper case.</dd>
 124  *
 125  *   <dd>Well-formed country/region values have
 126  *   the form <code>[a-zA-Z]{2} | [0-9]{3}</code></dd>
 127  *
 128  *   <dd>Example: "US" (United States), "FR" (France), "029"
 129  *   (Caribbean)</dd>
 130  *
 131  *   <dt><a name="def_variant"><b>variant</b></a></dt>
 132  *
 133  *   <dd>Any arbitrary value used to indicate a variation of a
 134  *   <code>Locale</code>.  Where there are two or more variant values
 135  *   each indicating its own semantics, these values should be ordered
 136  *   by importance, with most important first, separated by
 137  *   underscore('_').  The variant field is case sensitive.</dd>
 138  *
 139  *   <dd>Note: IETF BCP 47 places syntactic restrictions on variant
 140  *   subtags.  Also BCP 47 subtags are strictly used to indicate
 141  *   additional variations that define a language or its dialects that
 142  *   are not covered by any combinations of language, script and
 143  *   region subtags.  You can find a full list of valid variant codes
 144  *   in the IANA Language Subtag Registry (search for "Type: variant").
 145  *
 146  *   <p>However, the variant field in <code>Locale</code> has
 147  *   historically been used for any kind of variation, not just
 148  *   language variations.  For example, some supported variants
 149  *   available in Java SE Runtime Environments indicate alternative
 150  *   cultural behaviors such as calendar type or number script.  In
 151  *   BCP 47 this kind of information, which does not identify the
 152  *   language, is supported by extension subtags or private use
 153  *   subtags.</dd>
 154  *
 155  *   <dd>Well-formed variant values have the form <code>SUBTAG
 156  *   (('_'|'-') SUBTAG)*</code> where <code>SUBTAG =
 157  *   [0-9][0-9a-zA-Z]{3} | [0-9a-zA-Z]{5,8}</code>. (Note: BCP 47 only
 158  *   uses hyphen ('-') as a delimiter, this is more lenient).</dd>
 159  *
 160  *   <dd>Example: "polyton" (Polytonic Greek), "POSIX"</dd>
 161  *
 162  *   <dt><a name="def_extensions"><b>extensions</b></a></dt>
 163  *
 164  *   <dd>A map from single character keys to string values, indicating
 165  *   extensions apart from language identification.  The extensions in
 166  *   <code>Locale</code> implement the semantics and syntax of BCP 47
 167  *   extension subtags and private use subtags. The extensions are
 168  *   case insensitive, but <code>Locale</code> canonicalizes all
 169  *   extension keys and values to lower case. Note that extensions
 170  *   cannot have empty values.</dd>
 171  *
 172  *   <dd>Well-formed keys are single characters from the set
 173  *   <code>[0-9a-zA-Z]</code>.  Well-formed values have the form
 174  *   <code>SUBTAG ('-' SUBTAG)*</code> where for the key 'x'
 175  *   <code>SUBTAG = [0-9a-zA-Z]{1,8}</code> and for other keys
 176  *   <code>SUBTAG = [0-9a-zA-Z]{2,8}</code> (that is, 'x' allows
 177  *   single-character subtags).</dd>
 178  *
 179  *   <dd>Example: key="u"/value="ca-japanese" (Japanese Calendar),
 180  *   key="x"/value="java-1-7"</dd>
 181  * </dl>
 182  *
 183  * <b>Note:</b> Although BCP 47 requires field values to be registered
 184  * in the IANA Language Subtag Registry, the <code>Locale</code> class
 185  * does not provide any validation features.  The <code>Builder</code>
 186  * only checks if an individual field satisfies the syntactic
 187  * requirement (is well-formed), but does not validate the value
 188  * itself.  See {@link Builder} for details.
 189  *
 190  * <h3><a name="def_locale_extension">Unicode locale/language extension</a></h3>
 191  *
 192  * <p>UTS#35, "Unicode Locale Data Markup Language" defines optional
 193  * attributes and keywords to override or refine the default behavior
 194  * associated with a locale.  A keyword is represented by a pair of
 195  * key and type.  For example, "nu-thai" indicates that Thai local
 196  * digits (value:"thai") should be used for formatting numbers
 197  * (key:"nu").
 198  *
 199  * <p>The keywords are mapped to a BCP 47 extension value using the
 200  * extension key 'u' ({@link #UNICODE_LOCALE_EXTENSION}).  The above
 201  * example, "nu-thai", becomes the extension "u-nu-thai".
 202  *
 203  * <p>Thus, when a <code>Locale</code> object contains Unicode locale
 204  * attributes and keywords,
 205  * <code>getExtension(UNICODE_LOCALE_EXTENSION)</code> will return a
 206  * String representing this information, for example, "nu-thai".  The
 207  * <code>Locale</code> class also provides {@link
 208  * #getUnicodeLocaleAttributes}, {@link #getUnicodeLocaleKeys}, and
 209  * {@link #getUnicodeLocaleType} which allow you to access Unicode
 210  * locale attributes and key/type pairs directly.  When represented as
 211  * a string, the Unicode Locale Extension lists attributes
 212  * alphabetically, followed by key/type sequences with keys listed
 213  * alphabetically (the order of subtags comprising a key's type is
 214  * fixed when the type is defined)
 215  *
 216  * <p>A well-formed locale key has the form
 217  * <code>[0-9a-zA-Z]{2}</code>.  A well-formed locale type has the
 218  * form <code>"" | [0-9a-zA-Z]{3,8} ('-' [0-9a-zA-Z]{3,8})*</code> (it
 219  * can be empty, or a series of subtags 3-8 alphanums in length).  A
 220  * well-formed locale attribute has the form
 221  * <code>[0-9a-zA-Z]{3,8}</code> (it is a single subtag with the same
 222  * form as a locale type subtag).
 223  *
 224  * <p>The Unicode locale extension specifies optional behavior in
 225  * locale-sensitive services.  Although the LDML specification defines
 226  * various keys and values, actual locale-sensitive service
 227  * implementations in a Java Runtime Environment might not support any
 228  * particular Unicode locale attributes or key/type pairs.
 229  *
 230  * <h4>Creating a Locale</h4>
 231  *
 232  * <p>There are several different ways to create a <code>Locale</code>
 233  * object.
 234  *
 235  * <h5>Builder</h5>
 236  *
 237  * <p>Using {@link Builder} you can construct a <code>Locale</code> object
 238  * that conforms to BCP 47 syntax.
 239  *
 240  * <h5>Constructors</h5>
 241  *
 242  * <p>The <code>Locale</code> class provides three constructors:
 243  * <blockquote>
 244  * <pre>
 245  *     {@link #Locale(String language)}
 246  *     {@link #Locale(String language, String country)}
 247  *     {@link #Locale(String language, String country, String variant)}
 248  * </pre>
 249  * </blockquote>
 250  * These constructors allow you to create a <code>Locale</code> object
 251  * with language, country and variant, but you cannot specify
 252  * script or extensions.
 253  *
 254  * <h5>Factory Methods</h5>
 255  *
 256  * <p>The method {@link #forLanguageTag} creates a <code>Locale</code>
 257  * object for a well-formed BCP 47 language tag.
 258  *
 259  * <h5>Locale Constants</h5>
 260  *
 261  * <p>The <code>Locale</code> class provides a number of convenient constants
 262  * that you can use to create <code>Locale</code> objects for commonly used
 263  * locales. For example, the following creates a <code>Locale</code> object
 264  * for the United States:
 265  * <blockquote>
 266  * <pre>
 267  *     Locale.US
 268  * </pre>
 269  * </blockquote>
 270  *
 271  * <h4><a name="LocaleMatching">Locale Matching</a></h4>
 272  *
 273  * <p>If an application or a system is internationalized and provides localized
 274  * resources for multiple locales, it sometimes needs to find one or more
 275  * locales (or language tags) which meet each user's specific preferences. Note
 276  * that a term "language tag" is used interchangeably with "locale" in this
 277  * locale matching documentation.
 278  *
 279  * <p>In order to do matching a user's preferred locales to a set of language
 280  * tags, <a href="http://tools.ietf.org/html/rfc4647">RFC 4647 Matching of
 281  * Language Tags</a> defines two mechanisms: filtering and lookup.
 282  * <em>Filtering</em> is used to get all matching locales, whereas
 283  * <em>lookup</em> is to choose the best matching locale.
 284  * Matching is done case-insensitively. These matching mechanisms are described
 285  * in the following sections.
 286  *
 287  * <p>A user's preference is called a <em>Language Priority List</em> and is
 288  * expressed as a list of language ranges. There are syntactically two types of
 289  * language ranges: basic and extended. See
 290  * {@link Locale.LanguageRange Locale.LanguageRange} for details.
 291  *
 292  * <h5>Filtering</h5>
 293  *
 294  * <p>The filtering operation returns all matching language tags. It is defined
 295  * in RFC 4647 as follows:
 296  * "In filtering, each language range represents the least specific language
 297  * tag (that is, the language tag with fewest number of subtags) that is an
 298  * acceptable match. All of the language tags in the matching set of tags will
 299  * have an equal or greater number of subtags than the language range. Every
 300  * non-wildcard subtag in the language range will appear in every one of the
 301  * matching language tags."
 302  *
 303  * <p>There are two types of filtering: filtering for basic language ranges
 304  * (called "basic filtering") and filtering for extended language ranges
 305  * (called "extended filtering"). They may return different results by what
 306  * kind of language ranges are included in the given Language Priority List.
 307  * {@link Locale.FilteringMode} is a parameter to specify how filtering should
 308  * be done.
 309  *
 310  * <h5>Lookup</h5>
 311  *
 312  * <p>The lookup operation returns the best matching language tags. It is
 313  * defined in RFC 4647 as follows:
 314  * "By contrast with filtering, each language range represents the most
 315  * specific tag that is an acceptable match.  The first matching tag found,
 316  * according to the user's priority, is considered the closest match and is the
 317  * item returned."
 318  *
 319  * <p>For example, if a Language Priority List consists of two language ranges,
 320  * {@code "zh-Hant-TW"} and {@code "en-US"}, in prioritized order, lookup
 321  * method progressively searches the language tags below in order to find the
 322  * best matching language tag.
 323  * <blockquote>
 324  * <pre>
 325  *    1. zh-Hant-TW
 326  *    2. zh-Hant
 327  *    3. zh
 328  *    4. en-US
 329  *    5. en
 330  * </pre>
 331  * </blockquote>
 332  * If there is a language tag which matches completely to a language range
 333  * above, the language tag is returned.
 334  *
 335  * <p>{@code "*"} is the special language range, and it is ignored in lookup.
 336  *
 337  * <p>If multiple language tags match as a result of the subtag {@code '*'}
 338  * included in a language range, the first matching language tag returned by
 339  * an {@link Iterator} over a {@link Collection} of language tags is treated as
 340  * the best matching one.
 341  *
 342  * <h4>Use of Locale</h4>
 343  *
 344  * <p>Once you've created a <code>Locale</code> you can query it for information
 345  * about itself. Use <code>getCountry</code> to get the country (or region)
 346  * code and <code>getLanguage</code> to get the language code.
 347  * You can use <code>getDisplayCountry</code> to get the
 348  * name of the country suitable for displaying to the user. Similarly,
 349  * you can use <code>getDisplayLanguage</code> to get the name of
 350  * the language suitable for displaying to the user. Interestingly,
 351  * the <code>getDisplayXXX</code> methods are themselves locale-sensitive
 352  * and have two versions: one that uses the default
 353  * {@link Locale.Category#DISPLAY DISPLAY} locale and one
 354  * that uses the locale specified as an argument.
 355  *
 356  * <p>The Java Platform provides a number of classes that perform locale-sensitive
 357  * operations. For example, the <code>NumberFormat</code> class formats
 358  * numbers, currency, and percentages in a locale-sensitive manner. Classes
 359  * such as <code>NumberFormat</code> have several convenience methods
 360  * for creating a default object of that type. For example, the
 361  * <code>NumberFormat</code> class provides these three convenience methods
 362  * for creating a default <code>NumberFormat</code> object:
 363  * <blockquote>
 364  * <pre>
 365  *     NumberFormat.getInstance()
 366  *     NumberFormat.getCurrencyInstance()
 367  *     NumberFormat.getPercentInstance()
 368  * </pre>
 369  * </blockquote>
 370  * Each of these methods has two variants; one with an explicit locale
 371  * and one without; the latter uses the default
 372  * {@link Locale.Category#FORMAT FORMAT} locale:
 373  * <blockquote>
 374  * <pre>
 375  *     NumberFormat.getInstance(myLocale)
 376  *     NumberFormat.getCurrencyInstance(myLocale)
 377  *     NumberFormat.getPercentInstance(myLocale)
 378  * </pre>
 379  * </blockquote>
 380  * A <code>Locale</code> is the mechanism for identifying the kind of object
 381  * (<code>NumberFormat</code>) that you would like to get. The locale is
 382  * <STRONG>just</STRONG> a mechanism for identifying objects,
 383  * <STRONG>not</STRONG> a container for the objects themselves.
 384  *
 385  * <h4>Compatibility</h4>
 386  *
 387  * <p>In order to maintain compatibility with existing usage, Locale's
 388  * constructors retain their behavior prior to the Java Runtime
 389  * Environment version 1.7.  The same is largely true for the
 390  * <code>toString</code> method. Thus Locale objects can continue to
 391  * be used as they were. In particular, clients who parse the output
 392  * of toString into language, country, and variant fields can continue
 393  * to do so (although this is strongly discouraged), although the
 394  * variant field will have additional information in it if script or
 395  * extensions are present.
 396  *
 397  * <p>In addition, BCP 47 imposes syntax restrictions that are not
 398  * imposed by Locale's constructors. This means that conversions
 399  * between some Locales and BCP 47 language tags cannot be made without
 400  * losing information. Thus <code>toLanguageTag</code> cannot
 401  * represent the state of locales whose language, country, or variant
 402  * do not conform to BCP 47.
 403  *
 404  * <p>Because of these issues, it is recommended that clients migrate
 405  * away from constructing non-conforming locales and use the
 406  * <code>forLanguageTag</code> and <code>Locale.Builder</code> APIs instead.
 407  * Clients desiring a string representation of the complete locale can
 408  * then always rely on <code>toLanguageTag</code> for this purpose.
 409  *
 410  * <h5><a name="special_cases_constructor">Special cases</a></h5>
 411  *
 412  * <p>For compatibility reasons, two
 413  * non-conforming locales are treated as special cases.  These are
 414  * <b>{@code ja_JP_JP}</b> and <b>{@code th_TH_TH}</b>. These are ill-formed
 415  * in BCP 47 since the variants are too short. To ease migration to BCP 47,
 416  * these are treated specially during construction.  These two cases (and only
 417  * these) cause a constructor to generate an extension, all other values behave
 418  * exactly as they did prior to Java 7.
 419  *
 420  * <p>Java has used {@code ja_JP_JP} to represent Japanese as used in
 421  * Japan together with the Japanese Imperial calendar. This is now
 422  * representable using a Unicode locale extension, by specifying the
 423  * Unicode locale key {@code ca} (for "calendar") and type
 424  * {@code japanese}. When the Locale constructor is called with the
 425  * arguments "ja", "JP", "JP", the extension "u-ca-japanese" is
 426  * automatically added.
 427  *
 428  * <p>Java has used {@code th_TH_TH} to represent Thai as used in
 429  * Thailand together with Thai digits. This is also now representable using
 430  * a Unicode locale extension, by specifying the Unicode locale key
 431  * {@code nu} (for "number") and value {@code thai}. When the Locale
 432  * constructor is called with the arguments "th", "TH", "TH", the
 433  * extension "u-nu-thai" is automatically added.
 434  *
 435  * <h5>Serialization</h5>
 436  *
 437  * <p>During serialization, writeObject writes all fields to the output
 438  * stream, including extensions.
 439  *
 440  * <p>During deserialization, readResolve adds extensions as described
 441  * in <a href="#special_cases_constructor">Special Cases</a>, only
 442  * for the two cases th_TH_TH and ja_JP_JP.
 443  *
 444  * <h5>Legacy language codes</h5>
 445  *
 446  * <p>Locale's constructor has always converted three language codes to
 447  * their earlier, obsoleted forms: {@code he} maps to {@code iw},
 448  * {@code yi} maps to {@code ji}, and {@code id} maps to
 449  * {@code in}.  This continues to be the case, in order to not break
 450  * backwards compatibility.
 451  *
 452  * <p>The APIs added in 1.7 map between the old and new language codes,
 453  * maintaining the old codes internal to Locale (so that
 454  * <code>getLanguage</code> and <code>toString</code> reflect the old
 455  * code), but using the new codes in the BCP 47 language tag APIs (so
 456  * that <code>toLanguageTag</code> reflects the new one). This
 457  * preserves the equivalence between Locales no matter which code or
 458  * API is used to construct them. Java's default resource bundle
 459  * lookup mechanism also implements this mapping, so that resources
 460  * can be named using either convention, see {@link ResourceBundle.Control}.
 461  *
 462  * <h5>Three-letter language/country(region) codes</h5>
 463  *
 464  * <p>The Locale constructors have always specified that the language
 465  * and the country param be two characters in length, although in
 466  * practice they have accepted any length.  The specification has now
 467  * been relaxed to allow language codes of two to eight characters and
 468  * country (region) codes of two to three characters, and in
 469  * particular, three-letter language codes and three-digit region
 470  * codes as specified in the IANA Language Subtag Registry.  For
 471  * compatibility, the implementation still does not impose a length
 472  * constraint.
 473  *
 474  * @see Builder
 475  * @see ResourceBundle
 476  * @see java.text.Format
 477  * @see java.text.NumberFormat
 478  * @see java.text.Collator
 479  * @author Mark Davis
 480  * @since 1.1
 481  */
 482 public final class Locale implements Cloneable, Serializable {
 483 
 484     private static final  Cache LOCALECACHE = new Cache();
 485 
 486     /** Useful constant for language.
 487      */
 488     public static final Locale ENGLISH = createConstant("en", "");
 489 
 490     /** Useful constant for language.
 491      */
 492     public static final Locale FRENCH = createConstant("fr", "");
 493 
 494     /** Useful constant for language.
 495      */
 496     public static final Locale GERMAN = createConstant("de", "");
 497 
 498     /** Useful constant for language.
 499      */
 500     public static final Locale ITALIAN = createConstant("it", "");
 501 
 502     /** Useful constant for language.
 503      */
 504     public static final Locale JAPANESE = createConstant("ja", "");
 505 
 506     /** Useful constant for language.
 507      */
 508     public static final Locale KOREAN = createConstant("ko", "");
 509 
 510     /** Useful constant for language.
 511      */
 512     public static final Locale CHINESE = createConstant("zh", "");
 513 
 514     /** Useful constant for language.
 515      */
 516     public static final Locale SIMPLIFIED_CHINESE = createConstant("zh", "CN");
 517 
 518     /** Useful constant for language.
 519      */
 520     public static final Locale TRADITIONAL_CHINESE = createConstant("zh", "TW");
 521 
 522     /** Useful constant for country.
 523      */
 524     public static final Locale FRANCE = createConstant("fr", "FR");
 525 
 526     /** Useful constant for country.
 527      */
 528     public static final Locale GERMANY = createConstant("de", "DE");
 529 
 530     /** Useful constant for country.
 531      */
 532     public static final Locale ITALY = createConstant("it", "IT");
 533 
 534     /** Useful constant for country.
 535      */
 536     public static final Locale JAPAN = createConstant("ja", "JP");
 537 
 538     /** Useful constant for country.
 539      */
 540     public static final Locale KOREA = createConstant("ko", "KR");
 541 
 542     /** Useful constant for country.
 543      */
 544     public static final Locale CHINA = SIMPLIFIED_CHINESE;
 545 
 546     /** Useful constant for country.
 547      */
 548     public static final Locale PRC = SIMPLIFIED_CHINESE;
 549 
 550     /** Useful constant for country.
 551      */
 552     public static final Locale TAIWAN = TRADITIONAL_CHINESE;
 553 
 554     /** Useful constant for country.
 555      */
 556     public static final Locale UK = createConstant("en", "GB");
 557 
 558     /** Useful constant for country.
 559      */
 560     public static final Locale US = createConstant("en", "US");
 561 
 562     /** Useful constant for country.
 563      */
 564     public static final Locale CANADA = createConstant("en", "CA");
 565 
 566     /** Useful constant for country.
 567      */
 568     public static final Locale CANADA_FRENCH = createConstant("fr", "CA");
 569 
 570     /**
 571      * Useful constant for the root locale.  The root locale is the locale whose
 572      * language, country, and variant are empty ("") strings.  This is regarded
 573      * as the base locale of all locales, and is used as the language/country
 574      * neutral locale for the locale sensitive operations.
 575      *
 576      * @since 1.6
 577      */
 578     public static final Locale ROOT = createConstant("", "");
 579 
 580     /**
 581      * The key for the private use extension ('x').
 582      *
 583      * @see #getExtension(char)
 584      * @see Builder#setExtension(char, String)
 585      * @since 1.7
 586      */
 587     public static final char PRIVATE_USE_EXTENSION = 'x';
 588 
 589     /**
 590      * The key for Unicode locale extension ('u').
 591      *
 592      * @see #getExtension(char)
 593      * @see Builder#setExtension(char, String)
 594      * @since 1.7
 595      */
 596     public static final char UNICODE_LOCALE_EXTENSION = 'u';
 597 
 598     /** serialization ID
 599      */
 600     static final long serialVersionUID = 9149081749638150636L;
 601 
 602     /**
 603      * Display types for retrieving localized names from the name providers.
 604      */
 605     private static final int DISPLAY_LANGUAGE = 0;
 606     private static final int DISPLAY_COUNTRY  = 1;
 607     private static final int DISPLAY_VARIANT  = 2;
 608     private static final int DISPLAY_SCRIPT   = 3;
 609 
 610     /**
 611      * Private constructor used by getInstance method
 612      */
 613     private Locale(BaseLocale baseLocale, LocaleExtensions extensions) {
 614         this.baseLocale = baseLocale;
 615         this.localeExtensions = extensions;
 616     }
 617 
 618     /**
 619      * Construct a locale from language, country and variant.
 620      * This constructor normalizes the language value to lowercase and
 621      * the country value to uppercase.
 622      * <p>
 623      * <b>Note:</b>
 624      * <ul>
 625      * <li>ISO 639 is not a stable standard; some of the language codes it defines
 626      * (specifically "iw", "ji", and "in") have changed.  This constructor accepts both the
 627      * old codes ("iw", "ji", and "in") and the new codes ("he", "yi", and "id"), but all other
 628      * API on Locale will return only the OLD codes.
 629      * <li>For backward compatibility reasons, this constructor does not make
 630      * any syntactic checks on the input.
 631      * <li>The two cases ("ja", "JP", "JP") and ("th", "TH", "TH") are handled specially,
 632      * see <a href="#special_cases_constructor">Special Cases</a> for more information.
 633      * </ul>
 634      *
 635      * @param language An ISO 639 alpha-2 or alpha-3 language code, or a language subtag
 636      * up to 8 characters in length.  See the <code>Locale</code> class description about
 637      * valid language values.
 638      * @param country An ISO 3166 alpha-2 country code or a UN M.49 numeric-3 area code.
 639      * See the <code>Locale</code> class description about valid country values.
 640      * @param variant Any arbitrary value used to indicate a variation of a <code>Locale</code>.
 641      * See the <code>Locale</code> class description for the details.
 642      * @exception NullPointerException thrown if any argument is null.
 643      */
 644     public Locale(String language, String country, String variant) {
 645         if (language== null || country == null || variant == null) {
 646             throw new NullPointerException();
 647         }
 648         baseLocale = BaseLocale.getInstance(convertOldISOCodes(language), "", country, variant);
 649         localeExtensions = getCompatibilityExtensions(language, "", country, variant);
 650     }
 651 
 652     /**
 653      * Construct a locale from language and country.
 654      * This constructor normalizes the language value to lowercase and
 655      * the country value to uppercase.
 656      * <p>
 657      * <b>Note:</b>
 658      * <ul>
 659      * <li>ISO 639 is not a stable standard; some of the language codes it defines
 660      * (specifically "iw", "ji", and "in") have changed.  This constructor accepts both the
 661      * old codes ("iw", "ji", and "in") and the new codes ("he", "yi", and "id"), but all other
 662      * API on Locale will return only the OLD codes.
 663      * <li>For backward compatibility reasons, this constructor does not make
 664      * any syntactic checks on the input.
 665      * </ul>
 666      *
 667      * @param language An ISO 639 alpha-2 or alpha-3 language code, or a language subtag
 668      * up to 8 characters in length.  See the <code>Locale</code> class description about
 669      * valid language values.
 670      * @param country An ISO 3166 alpha-2 country code or a UN M.49 numeric-3 area code.
 671      * See the <code>Locale</code> class description about valid country values.
 672      * @exception NullPointerException thrown if either argument is null.
 673      */
 674     public Locale(String language, String country) {
 675         this(language, country, "");
 676     }
 677 
 678     /**
 679      * Construct a locale from a language code.
 680      * This constructor normalizes the language value to lowercase.
 681      * <p>
 682      * <b>Note:</b>
 683      * <ul>
 684      * <li>ISO 639 is not a stable standard; some of the language codes it defines
 685      * (specifically "iw", "ji", and "in") have changed.  This constructor accepts both the
 686      * old codes ("iw", "ji", and "in") and the new codes ("he", "yi", and "id"), but all other
 687      * API on Locale will return only the OLD codes.
 688      * <li>For backward compatibility reasons, this constructor does not make
 689      * any syntactic checks on the input.
 690      * </ul>
 691      *
 692      * @param language An ISO 639 alpha-2 or alpha-3 language code, or a language subtag
 693      * up to 8 characters in length.  See the <code>Locale</code> class description about
 694      * valid language values.
 695      * @exception NullPointerException thrown if argument is null.
 696      * @since 1.4
 697      */
 698     public Locale(String language) {
 699         this(language, "", "");
 700     }
 701 
 702     /**
 703      * This method must be called only for creating the Locale.*
 704      * constants due to making shortcuts.
 705      */
 706     private static Locale createConstant(String lang, String country) {
 707         BaseLocale base = BaseLocale.createInstance(lang, country);
 708         return getInstance(base, null);
 709     }
 710 
 711     /**
 712      * Returns a <code>Locale</code> constructed from the given
 713      * <code>language</code>, <code>country</code> and
 714      * <code>variant</code>. If the same <code>Locale</code> instance
 715      * is available in the cache, then that instance is
 716      * returned. Otherwise, a new <code>Locale</code> instance is
 717      * created and cached.
 718      *
 719      * @param language lowercase 2 to 8 language code.
 720      * @param country uppercase two-letter ISO-3166 code and numeric-3 UN M.49 area code.
 721      * @param variant vendor and browser specific code. See class description.
 722      * @return the <code>Locale</code> instance requested
 723      * @exception NullPointerException if any argument is null.
 724      */
 725     static Locale getInstance(String language, String country, String variant) {
 726         return getInstance(language, "", country, variant, null);
 727     }
 728 
 729     static Locale getInstance(String language, String script, String country,
 730                                       String variant, LocaleExtensions extensions) {
 731         if (language== null || script == null || country == null || variant == null) {
 732             throw new NullPointerException();
 733         }
 734 
 735         if (extensions == null) {
 736             extensions = getCompatibilityExtensions(language, script, country, variant);
 737         }
 738 
 739         BaseLocale baseloc = BaseLocale.getInstance(language, script, country, variant);
 740         return getInstance(baseloc, extensions);
 741     }
 742 
 743     static Locale getInstance(BaseLocale baseloc, LocaleExtensions extensions) {
 744         LocaleKey key = new LocaleKey(baseloc, extensions);
 745         return LOCALECACHE.get(key);
 746     }
 747 
 748     private static class Cache extends LocaleObjectCache<LocaleKey, Locale> {
 749         private Cache() {
 750         }
 751 
 752         @Override
 753         protected Locale createObject(LocaleKey key) {
 754             return new Locale(key.base, key.exts);
 755         }
 756     }
 757 
 758     private static final class LocaleKey {
 759         private final BaseLocale base;
 760         private final LocaleExtensions exts;
 761         private final int hash;
 762 
 763         private LocaleKey(BaseLocale baseLocale, LocaleExtensions extensions) {
 764             base = baseLocale;
 765             exts = extensions;
 766 
 767             // Calculate the hash value here because it's always used.
 768             int h = base.hashCode();
 769             if (exts != null) {
 770                 h ^= exts.hashCode();
 771             }
 772             hash = h;
 773         }
 774 
 775         @Override
 776         public boolean equals(Object obj) {
 777             if (this == obj) {
 778                 return true;
 779             }
 780             if (!(obj instanceof LocaleKey)) {
 781                 return false;
 782             }
 783             LocaleKey other = (LocaleKey)obj;
 784             if (hash != other.hash || !base.equals(other.base)) {
 785                 return false;
 786             }
 787             if (exts == null) {
 788                 return other.exts == null;
 789             }
 790             return exts.equals(other.exts);
 791         }
 792 
 793         @Override
 794         public int hashCode() {
 795             return hash;
 796         }
 797     }
 798 
 799     /**
 800      * Gets the current value of the default locale for this instance
 801      * of the Java Virtual Machine.
 802      * <p>
 803      * The Java Virtual Machine sets the default locale during startup
 804      * based on the host environment. It is used by many locale-sensitive
 805      * methods if no locale is explicitly specified.
 806      * It can be changed using the
 807      * {@link #setDefault(java.util.Locale) setDefault} method.
 808      *
 809      * @return the default locale for this instance of the Java Virtual Machine
 810      */
 811     public static Locale getDefault() {
 812         // do not synchronize this method - see 4071298
 813         return defaultLocale;
 814     }
 815 
 816     /**
 817      * Gets the current value of the default locale for the specified Category
 818      * for this instance of the Java Virtual Machine.
 819      * <p>
 820      * The Java Virtual Machine sets the default locale during startup based
 821      * on the host environment. It is used by many locale-sensitive methods
 822      * if no locale is explicitly specified. It can be changed using the
 823      * setDefault(Locale.Category, Locale) method.
 824      *
 825      * @param category - the specified category to get the default locale
 826      * @throws NullPointerException if category is null
 827      * @return the default locale for the specified Category for this instance
 828      *     of the Java Virtual Machine
 829      * @see #setDefault(Locale.Category, Locale)
 830      * @since 1.7
 831      */
 832     public static Locale getDefault(Locale.Category category) {
 833         // do not synchronize this method - see 4071298
 834         switch (category) {
 835         case DISPLAY:
 836             if (defaultDisplayLocale == null) {
 837                 synchronized(Locale.class) {
 838                     if (defaultDisplayLocale == null) {
 839                         defaultDisplayLocale = initDefault(category);
 840                     }
 841                 }
 842             }
 843             return defaultDisplayLocale;
 844         case FORMAT:
 845             if (defaultFormatLocale == null) {
 846                 synchronized(Locale.class) {
 847                     if (defaultFormatLocale == null) {
 848                         defaultFormatLocale = initDefault(category);
 849                     }
 850                 }
 851             }
 852             return defaultFormatLocale;
 853         default:
 854             assert false: "Unknown Category";
 855         }
 856         return getDefault();
 857     }
 858 
 859     private static Locale initDefault() {
 860         String language, region, script, country, variant;
 861         Properties props = GetPropertyAction.privilegedGetProperties();
 862         language = props.getProperty("user.language", "en");
 863         // for compatibility, check for old user.region property
 864         region = props.getProperty("user.region");
 865         if (region != null) {
 866             // region can be of form country, country_variant, or _variant
 867             int i = region.indexOf('_');
 868             if (i >= 0) {
 869                 country = region.substring(0, i);
 870                 variant = region.substring(i + 1);
 871             } else {
 872                 country = region;
 873                 variant = "";
 874             }
 875             script = "";
 876         } else {
 877             script = props.getProperty("user.script", "");
 878             country = props.getProperty("user.country", "");
 879             variant = props.getProperty("user.variant", "");
 880         }
 881 
 882         return getInstance(language, script, country, variant, null);
 883     }
 884 
 885     private static Locale initDefault(Locale.Category category) {
 886         Properties props = GetPropertyAction.privilegedGetProperties();
 887         return getInstance(
 888             props.getProperty(category.languageKey,
 889                     defaultLocale.getLanguage()),
 890             props.getProperty(category.scriptKey,
 891                     defaultLocale.getScript()),
 892             props.getProperty(category.countryKey,
 893                     defaultLocale.getCountry()),
 894             props.getProperty(category.variantKey,
 895                     defaultLocale.getVariant()),
 896             null);
 897     }
 898 
 899     /**
 900      * Sets the default locale for this instance of the Java Virtual Machine.
 901      * This does not affect the host locale.
 902      * <p>
 903      * If there is a security manager, its <code>checkPermission</code>
 904      * method is called with a <code>PropertyPermission("user.language", "write")</code>
 905      * permission before the default locale is changed.
 906      * <p>
 907      * The Java Virtual Machine sets the default locale during startup
 908      * based on the host environment. It is used by many locale-sensitive
 909      * methods if no locale is explicitly specified.
 910      * <p>
 911      * Since changing the default locale may affect many different areas
 912      * of functionality, this method should only be used if the caller
 913      * is prepared to reinitialize locale-sensitive code running
 914      * within the same Java Virtual Machine.
 915      * <p>
 916      * By setting the default locale with this method, all of the default
 917      * locales for each Category are also set to the specified default locale.
 918      *
 919      * @throws SecurityException
 920      *        if a security manager exists and its
 921      *        <code>checkPermission</code> method doesn't allow the operation.
 922      * @throws NullPointerException if <code>newLocale</code> is null
 923      * @param newLocale the new default locale
 924      * @see SecurityManager#checkPermission
 925      * @see java.util.PropertyPermission
 926      */
 927     public static synchronized void setDefault(Locale newLocale) {
 928         setDefault(Category.DISPLAY, newLocale);
 929         setDefault(Category.FORMAT, newLocale);
 930         defaultLocale = newLocale;
 931     }
 932 
 933     /**
 934      * Sets the default locale for the specified Category for this instance
 935      * of the Java Virtual Machine. This does not affect the host locale.
 936      * <p>
 937      * If there is a security manager, its checkPermission method is called
 938      * with a PropertyPermission("user.language", "write") permission before
 939      * the default locale is changed.
 940      * <p>
 941      * The Java Virtual Machine sets the default locale during startup based
 942      * on the host environment. It is used by many locale-sensitive methods
 943      * if no locale is explicitly specified.
 944      * <p>
 945      * Since changing the default locale may affect many different areas of
 946      * functionality, this method should only be used if the caller is
 947      * prepared to reinitialize locale-sensitive code running within the
 948      * same Java Virtual Machine.
 949      *
 950      * @param category - the specified category to set the default locale
 951      * @param newLocale - the new default locale
 952      * @throws SecurityException if a security manager exists and its
 953      *     checkPermission method doesn't allow the operation.
 954      * @throws NullPointerException if category and/or newLocale is null
 955      * @see SecurityManager#checkPermission(java.security.Permission)
 956      * @see PropertyPermission
 957      * @see #getDefault(Locale.Category)
 958      * @since 1.7
 959      */
 960     public static synchronized void setDefault(Locale.Category category,
 961         Locale newLocale) {
 962         if (category == null)
 963             throw new NullPointerException("Category cannot be NULL");
 964         if (newLocale == null)
 965             throw new NullPointerException("Can't set default locale to NULL");
 966 
 967         SecurityManager sm = System.getSecurityManager();
 968         if (sm != null) sm.checkPermission(new PropertyPermission
 969                         ("user.language", "write"));
 970         switch (category) {
 971         case DISPLAY:
 972             defaultDisplayLocale = newLocale;
 973             break;
 974         case FORMAT:
 975             defaultFormatLocale = newLocale;
 976             break;
 977         default:
 978             assert false: "Unknown Category";
 979         }
 980     }
 981 
 982     /**
 983      * Returns an array of all installed locales.
 984      * The returned array represents the union of locales supported
 985      * by the Java runtime environment and by installed
 986      * {@link java.util.spi.LocaleServiceProvider LocaleServiceProvider}
 987      * implementations.  It must contain at least a <code>Locale</code>
 988      * instance equal to {@link java.util.Locale#US Locale.US}.
 989      *
 990      * @return An array of installed locales.
 991      */
 992     public static Locale[] getAvailableLocales() {
 993         return LocaleServiceProviderPool.getAllAvailableLocales();
 994     }
 995 
 996     /**
 997      * Returns a list of all 2-letter country codes defined in ISO 3166.
 998      * Can be used to create Locales.
 999      * <p>
1000      * <b>Note:</b> The <code>Locale</code> class also supports other codes for
1001      * country (region), such as 3-letter numeric UN M.49 area codes.
1002      * Therefore, the list returned by this method does not contain ALL valid
1003      * codes that can be used to create Locales.
1004      *
1005      * @return An array of ISO 3166 two-letter country codes.
1006      */
1007     public static String[] getISOCountries() {
1008         if (isoCountries == null) {
1009             isoCountries = getISO2Table(LocaleISOData.isoCountryTable);
1010         }
1011         String[] result = new String[isoCountries.length];
1012         System.arraycopy(isoCountries, 0, result, 0, isoCountries.length);
1013         return result;
1014     }
1015 
1016     /**
1017      * Returns a list of all 2-letter language codes defined in ISO 639.
1018      * Can be used to create Locales.
1019      * <p>
1020      * <b>Note:</b>
1021      * <ul>
1022      * <li>ISO 639 is not a stable standard&mdash; some languages' codes have changed.
1023      * The list this function returns includes both the new and the old codes for the
1024      * languages whose codes have changed.
1025      * <li>The <code>Locale</code> class also supports language codes up to
1026      * 8 characters in length.  Therefore, the list returned by this method does
1027      * not contain ALL valid codes that can be used to create Locales.
1028      * </ul>
1029      *
1030      * @return An array of ISO 639 two-letter language codes.
1031      */
1032     public static String[] getISOLanguages() {
1033         if (isoLanguages == null) {
1034             isoLanguages = getISO2Table(LocaleISOData.isoLanguageTable);
1035         }
1036         String[] result = new String[isoLanguages.length];
1037         System.arraycopy(isoLanguages, 0, result, 0, isoLanguages.length);
1038         return result;
1039     }
1040 
1041     private static String[] getISO2Table(String table) {
1042         int len = table.length() / 5;
1043         String[] isoTable = new String[len];
1044         for (int i = 0, j = 0; i < len; i++, j += 5) {
1045             isoTable[i] = table.substring(j, j + 2);
1046         }
1047         return isoTable;
1048     }
1049 
1050     /**
1051      * Returns the language code of this Locale.
1052      *
1053      * <p><b>Note:</b> ISO 639 is not a stable standard&mdash; some languages' codes have changed.
1054      * Locale's constructor recognizes both the new and the old codes for the languages
1055      * whose codes have changed, but this function always returns the old code.  If you
1056      * want to check for a specific language whose code has changed, don't do
1057      * <pre>
1058      * if (locale.getLanguage().equals("he")) // BAD!
1059      *    ...
1060      * </pre>
1061      * Instead, do
1062      * <pre>
1063      * if (locale.getLanguage().equals(new Locale("he").getLanguage()))
1064      *    ...
1065      * </pre>
1066      * @return The language code, or the empty string if none is defined.
1067      * @see #getDisplayLanguage
1068      */
1069     public String getLanguage() {
1070         return baseLocale.getLanguage();
1071     }
1072 
1073     /**
1074      * Returns the script for this locale, which should
1075      * either be the empty string or an ISO 15924 4-letter script
1076      * code. The first letter is uppercase and the rest are
1077      * lowercase, for example, 'Latn', 'Cyrl'.
1078      *
1079      * @return The script code, or the empty string if none is defined.
1080      * @see #getDisplayScript
1081      * @since 1.7
1082      */
1083     public String getScript() {
1084         return baseLocale.getScript();
1085     }
1086 
1087     /**
1088      * Returns the country/region code for this locale, which should
1089      * either be the empty string, an uppercase ISO 3166 2-letter code,
1090      * or a UN M.49 3-digit code.
1091      *
1092      * @return The country/region code, or the empty string if none is defined.
1093      * @see #getDisplayCountry
1094      */
1095     public String getCountry() {
1096         return baseLocale.getRegion();
1097     }
1098 
1099     /**
1100      * Returns the variant code for this locale.
1101      *
1102      * @return The variant code, or the empty string if none is defined.
1103      * @see #getDisplayVariant
1104      */
1105     public String getVariant() {
1106         return baseLocale.getVariant();
1107     }
1108 
1109     /**
1110      * Returns {@code true} if this {@code Locale} has any <a href="#def_extensions">
1111      * extensions</a>.
1112      *
1113      * @return {@code true} if this {@code Locale} has any extensions
1114      * @since 1.8
1115      */
1116     public boolean hasExtensions() {
1117         return localeExtensions != null;
1118     }
1119 
1120     /**
1121      * Returns a copy of this {@code Locale} with no <a href="#def_extensions">
1122      * extensions</a>. If this {@code Locale} has no extensions, this {@code Locale}
1123      * is returned.
1124      *
1125      * @return a copy of this {@code Locale} with no extensions, or {@code this}
1126      *         if {@code this} has no extensions
1127      * @since 1.8
1128      */
1129     public Locale stripExtensions() {
1130         return hasExtensions() ? Locale.getInstance(baseLocale, null) : this;
1131     }
1132 
1133     /**
1134      * Returns the extension (or private use) value associated with
1135      * the specified key, or null if there is no extension
1136      * associated with the key. To be well-formed, the key must be one
1137      * of <code>[0-9A-Za-z]</code>. Keys are case-insensitive, so
1138      * for example 'z' and 'Z' represent the same extension.
1139      *
1140      * @param key the extension key
1141      * @return The extension, or null if this locale defines no
1142      * extension for the specified key.
1143      * @throws IllegalArgumentException if key is not well-formed
1144      * @see #PRIVATE_USE_EXTENSION
1145      * @see #UNICODE_LOCALE_EXTENSION
1146      * @since 1.7
1147      */
1148     public String getExtension(char key) {
1149         if (!LocaleExtensions.isValidKey(key)) {
1150             throw new IllegalArgumentException("Ill-formed extension key: " + key);
1151         }
1152         return hasExtensions() ? localeExtensions.getExtensionValue(key) : null;
1153     }
1154 
1155     /**
1156      * Returns the set of extension keys associated with this locale, or the
1157      * empty set if it has no extensions. The returned set is unmodifiable.
1158      * The keys will all be lower-case.
1159      *
1160      * @return The set of extension keys, or the empty set if this locale has
1161      * no extensions.
1162      * @since 1.7
1163      */
1164     public Set<Character> getExtensionKeys() {
1165         if (!hasExtensions()) {
1166             return Collections.emptySet();
1167         }
1168         return localeExtensions.getKeys();
1169     }
1170 
1171     /**
1172      * Returns the set of unicode locale attributes associated with
1173      * this locale, or the empty set if it has no attributes. The
1174      * returned set is unmodifiable.
1175      *
1176      * @return The set of attributes.
1177      * @since 1.7
1178      */
1179     public Set<String> getUnicodeLocaleAttributes() {
1180         if (!hasExtensions()) {
1181             return Collections.emptySet();
1182         }
1183         return localeExtensions.getUnicodeLocaleAttributes();
1184     }
1185 
1186     /**
1187      * Returns the Unicode locale type associated with the specified Unicode locale key
1188      * for this locale. Returns the empty string for keys that are defined with no type.
1189      * Returns null if the key is not defined. Keys are case-insensitive. The key must
1190      * be two alphanumeric characters ([0-9a-zA-Z]), or an IllegalArgumentException is
1191      * thrown.
1192      *
1193      * @param key the Unicode locale key
1194      * @return The Unicode locale type associated with the key, or null if the
1195      * locale does not define the key.
1196      * @throws IllegalArgumentException if the key is not well-formed
1197      * @throws NullPointerException if <code>key</code> is null
1198      * @since 1.7
1199      */
1200     public String getUnicodeLocaleType(String key) {
1201         if (!isUnicodeExtensionKey(key)) {
1202             throw new IllegalArgumentException("Ill-formed Unicode locale key: " + key);
1203         }
1204         return hasExtensions() ? localeExtensions.getUnicodeLocaleType(key) : null;
1205     }
1206 
1207     /**
1208      * Returns the set of Unicode locale keys defined by this locale, or the empty set if
1209      * this locale has none.  The returned set is immutable.  Keys are all lower case.
1210      *
1211      * @return The set of Unicode locale keys, or the empty set if this locale has
1212      * no Unicode locale keywords.
1213      * @since 1.7
1214      */
1215     public Set<String> getUnicodeLocaleKeys() {
1216         if (localeExtensions == null) {
1217             return Collections.emptySet();
1218         }
1219         return localeExtensions.getUnicodeLocaleKeys();
1220     }
1221 
1222     /**
1223      * Package locale method returning the Locale's BaseLocale,
1224      * used by ResourceBundle
1225      * @return base locale of this Locale
1226      */
1227     BaseLocale getBaseLocale() {
1228         return baseLocale;
1229     }
1230 
1231     /**
1232      * Package private method returning the Locale's LocaleExtensions,
1233      * used by ResourceBundle.
1234      * @return locale extensions of this Locale,
1235      *         or {@code null} if no extensions are defined
1236      */
1237      LocaleExtensions getLocaleExtensions() {
1238          return localeExtensions;
1239      }
1240 
1241     /**
1242      * Returns a string representation of this <code>Locale</code>
1243      * object, consisting of language, country, variant, script,
1244      * and extensions as below:
1245      * <blockquote>
1246      * language + "_" + country + "_" + (variant + "_#" | "#") + script + "_" + extensions
1247      * </blockquote>
1248      *
1249      * Language is always lower case, country is always upper case, script is always title
1250      * case, and extensions are always lower case.  Extensions and private use subtags
1251      * will be in canonical order as explained in {@link #toLanguageTag}.
1252      *
1253      * <p>When the locale has neither script nor extensions, the result is the same as in
1254      * Java 6 and prior.
1255      *
1256      * <p>If both the language and country fields are missing, this function will return
1257      * the empty string, even if the variant, script, or extensions field is present (you
1258      * can't have a locale with just a variant, the variant must accompany a well-formed
1259      * language or country code).
1260      *
1261      * <p>If script or extensions are present and variant is missing, no underscore is
1262      * added before the "#".
1263      *
1264      * <p>This behavior is designed to support debugging and to be compatible with
1265      * previous uses of <code>toString</code> that expected language, country, and variant
1266      * fields only.  To represent a Locale as a String for interchange purposes, use
1267      * {@link #toLanguageTag}.
1268      *
1269      * <p>Examples: <ul>
1270      * <li>{@code en}</li>
1271      * <li>{@code de_DE}</li>
1272      * <li>{@code _GB}</li>
1273      * <li>{@code en_US_WIN}</li>
1274      * <li>{@code de__POSIX}</li>
1275      * <li>{@code zh_CN_#Hans}</li>
1276      * <li>{@code zh_TW_#Hant_x-java}</li>
1277      * <li>{@code th_TH_TH_#u-nu-thai}</li></ul>
1278      *
1279      * @return A string representation of the Locale, for debugging.
1280      * @see #getDisplayName
1281      * @see #toLanguageTag
1282      */
1283     @Override
1284     public final String toString() {
1285         boolean l = (baseLocale.getLanguage().length() != 0);
1286         boolean s = (baseLocale.getScript().length() != 0);
1287         boolean r = (baseLocale.getRegion().length() != 0);
1288         boolean v = (baseLocale.getVariant().length() != 0);
1289         boolean e = (localeExtensions != null && localeExtensions.getID().length() != 0);
1290 
1291         StringBuilder result = new StringBuilder(baseLocale.getLanguage());
1292         if (r || (l && (v || s || e))) {
1293             result.append('_')
1294                 .append(baseLocale.getRegion()); // This may just append '_'
1295         }
1296         if (v && (l || r)) {
1297             result.append('_')
1298                 .append(baseLocale.getVariant());
1299         }
1300 
1301         if (s && (l || r)) {
1302             result.append("_#")
1303                 .append(baseLocale.getScript());
1304         }
1305 
1306         if (e && (l || r)) {
1307             result.append('_');
1308             if (!s) {
1309                 result.append('#');
1310             }
1311             result.append(localeExtensions.getID());
1312         }
1313 
1314         return result.toString();
1315     }
1316 
1317     /**
1318      * Returns a well-formed IETF BCP 47 language tag representing
1319      * this locale.
1320      *
1321      * <p>If this <code>Locale</code> has a language, country, or
1322      * variant that does not satisfy the IETF BCP 47 language tag
1323      * syntax requirements, this method handles these fields as
1324      * described below:
1325      *
1326      * <p><b>Language:</b> If language is empty, or not <a
1327      * href="#def_language" >well-formed</a> (for example "a" or
1328      * "e2"), it will be emitted as "und" (Undetermined).
1329      *
1330      * <p><b>Country:</b> If country is not <a
1331      * href="#def_region">well-formed</a> (for example "12" or "USA"),
1332      * it will be omitted.
1333      *
1334      * <p><b>Variant:</b> If variant <b>is</b> <a
1335      * href="#def_variant">well-formed</a>, each sub-segment
1336      * (delimited by '-' or '_') is emitted as a subtag.  Otherwise:
1337      * <ul>
1338      *
1339      * <li>if all sub-segments match <code>[0-9a-zA-Z]{1,8}</code>
1340      * (for example "WIN" or "Oracle_JDK_Standard_Edition"), the first
1341      * ill-formed sub-segment and all following will be appended to
1342      * the private use subtag.  The first appended subtag will be
1343      * "lvariant", followed by the sub-segments in order, separated by
1344      * hyphen. For example, "x-lvariant-WIN",
1345      * "Oracle-x-lvariant-JDK-Standard-Edition".
1346      *
1347      * <li>if any sub-segment does not match
1348      * <code>[0-9a-zA-Z]{1,8}</code>, the variant will be truncated
1349      * and the problematic sub-segment and all following sub-segments
1350      * will be omitted.  If the remainder is non-empty, it will be
1351      * emitted as a private use subtag as above (even if the remainder
1352      * turns out to be well-formed).  For example,
1353      * "Solaris_isjustthecoolestthing" is emitted as
1354      * "x-lvariant-Solaris", not as "solaris".</li></ul>
1355      *
1356      * <p><b>Special Conversions:</b> Java supports some old locale
1357      * representations, including deprecated ISO language codes,
1358      * for compatibility. This method performs the following
1359      * conversions:
1360      * <ul>
1361      *
1362      * <li>Deprecated ISO language codes "iw", "ji", and "in" are
1363      * converted to "he", "yi", and "id", respectively.
1364      *
1365      * <li>A locale with language "no", country "NO", and variant
1366      * "NY", representing Norwegian Nynorsk (Norway), is converted
1367      * to a language tag "nn-NO".</li></ul>
1368      *
1369      * <p><b>Note:</b> Although the language tag created by this
1370      * method is well-formed (satisfies the syntax requirements
1371      * defined by the IETF BCP 47 specification), it is not
1372      * necessarily a valid BCP 47 language tag.  For example,
1373      * <pre>
1374      *   new Locale("xx", "YY").toLanguageTag();</pre>
1375      *
1376      * will return "xx-YY", but the language subtag "xx" and the
1377      * region subtag "YY" are invalid because they are not registered
1378      * in the IANA Language Subtag Registry.
1379      *
1380      * @return a BCP47 language tag representing the locale
1381      * @see #forLanguageTag(String)
1382      * @since 1.7
1383      */
1384     public String toLanguageTag() {
1385         if (languageTag != null) {
1386             return languageTag;
1387         }
1388 
1389         LanguageTag tag = LanguageTag.parseLocale(baseLocale, localeExtensions);
1390         StringBuilder buf = new StringBuilder();
1391 
1392         String subtag = tag.getLanguage();
1393         if (subtag.length() > 0) {
1394             buf.append(LanguageTag.canonicalizeLanguage(subtag));
1395         }
1396 
1397         subtag = tag.getScript();
1398         if (subtag.length() > 0) {
1399             buf.append(LanguageTag.SEP);
1400             buf.append(LanguageTag.canonicalizeScript(subtag));
1401         }
1402 
1403         subtag = tag.getRegion();
1404         if (subtag.length() > 0) {
1405             buf.append(LanguageTag.SEP);
1406             buf.append(LanguageTag.canonicalizeRegion(subtag));
1407         }
1408 
1409         List<String>subtags = tag.getVariants();
1410         for (String s : subtags) {
1411             buf.append(LanguageTag.SEP);
1412             // preserve casing
1413             buf.append(s);
1414         }
1415 
1416         subtags = tag.getExtensions();
1417         for (String s : subtags) {
1418             buf.append(LanguageTag.SEP);
1419             buf.append(LanguageTag.canonicalizeExtension(s));
1420         }
1421 
1422         subtag = tag.getPrivateuse();
1423         if (subtag.length() > 0) {
1424             if (buf.length() > 0) {
1425                 buf.append(LanguageTag.SEP);
1426             }
1427             buf.append(LanguageTag.PRIVATEUSE).append(LanguageTag.SEP);
1428             // preserve casing
1429             buf.append(subtag);
1430         }
1431 
1432         String langTag = buf.toString();
1433         synchronized (this) {
1434             if (languageTag == null) {
1435                 languageTag = langTag;
1436             }
1437         }
1438         return languageTag;
1439     }
1440 
1441     /**
1442      * Returns a locale for the specified IETF BCP 47 language tag string.
1443      *
1444      * <p>If the specified language tag contains any ill-formed subtags,
1445      * the first such subtag and all following subtags are ignored.  Compare
1446      * to {@link Locale.Builder#setLanguageTag} which throws an exception
1447      * in this case.
1448      *
1449      * <p>The following <b>conversions</b> are performed:<ul>
1450      *
1451      * <li>The language code "und" is mapped to language "".
1452      *
1453      * <li>The language codes "he", "yi", and "id" are mapped to "iw",
1454      * "ji", and "in" respectively. (This is the same canonicalization
1455      * that's done in Locale's constructors.)
1456      *
1457      * <li>The portion of a private use subtag prefixed by "lvariant",
1458      * if any, is removed and appended to the variant field in the
1459      * result locale (without case normalization).  If it is then
1460      * empty, the private use subtag is discarded:
1461      *
1462      * <pre>
1463      *     Locale loc;
1464      *     loc = Locale.forLanguageTag("en-US-x-lvariant-POSIX");
1465      *     loc.getVariant(); // returns "POSIX"
1466      *     loc.getExtension('x'); // returns null
1467      *
1468      *     loc = Locale.forLanguageTag("de-POSIX-x-URP-lvariant-Abc-Def");
1469      *     loc.getVariant(); // returns "POSIX_Abc_Def"
1470      *     loc.getExtension('x'); // returns "urp"
1471      * </pre>
1472      *
1473      * <li>When the languageTag argument contains an extlang subtag,
1474      * the first such subtag is used as the language, and the primary
1475      * language subtag and other extlang subtags are ignored:
1476      *
1477      * <pre>
1478      *     Locale.forLanguageTag("ar-aao").getLanguage(); // returns "aao"
1479      *     Locale.forLanguageTag("en-abc-def-us").toString(); // returns "abc_US"
1480      * </pre>
1481      *
1482      * <li>Case is normalized except for variant tags, which are left
1483      * unchanged.  Language is normalized to lower case, script to
1484      * title case, country to upper case, and extensions to lower
1485      * case.
1486      *
1487      * <li>If, after processing, the locale would exactly match either
1488      * ja_JP_JP or th_TH_TH with no extensions, the appropriate
1489      * extensions are added as though the constructor had been called:
1490      *
1491      * <pre>
1492      *    Locale.forLanguageTag("ja-JP-x-lvariant-JP").toLanguageTag();
1493      *    // returns "ja-JP-u-ca-japanese-x-lvariant-JP"
1494      *    Locale.forLanguageTag("th-TH-x-lvariant-TH").toLanguageTag();
1495      *    // returns "th-TH-u-nu-thai-x-lvariant-TH"
1496      * </pre></ul>
1497      *
1498      * <p>This implements the 'Language-Tag' production of BCP47, and
1499      * so supports grandfathered (regular and irregular) as well as
1500      * private use language tags.  Stand alone private use tags are
1501      * represented as empty language and extension 'x-whatever',
1502      * and grandfathered tags are converted to their canonical replacements
1503      * where they exist.
1504      *
1505      * <p>Grandfathered tags with canonical replacements are as follows:
1506      *
1507      * <table summary="Grandfathered tags with canonical replacements">
1508      * <tbody align="center">
1509      * <tr><th>grandfathered tag</th><th>&nbsp;</th><th>modern replacement</th></tr>
1510      * <tr><td>art-lojban</td><td>&nbsp;</td><td>jbo</td></tr>
1511      * <tr><td>i-ami</td><td>&nbsp;</td><td>ami</td></tr>
1512      * <tr><td>i-bnn</td><td>&nbsp;</td><td>bnn</td></tr>
1513      * <tr><td>i-hak</td><td>&nbsp;</td><td>hak</td></tr>
1514      * <tr><td>i-klingon</td><td>&nbsp;</td><td>tlh</td></tr>
1515      * <tr><td>i-lux</td><td>&nbsp;</td><td>lb</td></tr>
1516      * <tr><td>i-navajo</td><td>&nbsp;</td><td>nv</td></tr>
1517      * <tr><td>i-pwn</td><td>&nbsp;</td><td>pwn</td></tr>
1518      * <tr><td>i-tao</td><td>&nbsp;</td><td>tao</td></tr>
1519      * <tr><td>i-tay</td><td>&nbsp;</td><td>tay</td></tr>
1520      * <tr><td>i-tsu</td><td>&nbsp;</td><td>tsu</td></tr>
1521      * <tr><td>no-bok</td><td>&nbsp;</td><td>nb</td></tr>
1522      * <tr><td>no-nyn</td><td>&nbsp;</td><td>nn</td></tr>
1523      * <tr><td>sgn-BE-FR</td><td>&nbsp;</td><td>sfb</td></tr>
1524      * <tr><td>sgn-BE-NL</td><td>&nbsp;</td><td>vgt</td></tr>
1525      * <tr><td>sgn-CH-DE</td><td>&nbsp;</td><td>sgg</td></tr>
1526      * <tr><td>zh-guoyu</td><td>&nbsp;</td><td>cmn</td></tr>
1527      * <tr><td>zh-hakka</td><td>&nbsp;</td><td>hak</td></tr>
1528      * <tr><td>zh-min-nan</td><td>&nbsp;</td><td>nan</td></tr>
1529      * <tr><td>zh-xiang</td><td>&nbsp;</td><td>hsn</td></tr>
1530      * </tbody>
1531      * </table>
1532      *
1533      * <p>Grandfathered tags with no modern replacement will be
1534      * converted as follows:
1535      *
1536      * <table summary="Grandfathered tags with no modern replacement">
1537      * <tbody align="center">
1538      * <tr><th>grandfathered tag</th><th>&nbsp;</th><th>converts to</th></tr>
1539      * <tr><td>cel-gaulish</td><td>&nbsp;</td><td>xtg-x-cel-gaulish</td></tr>
1540      * <tr><td>en-GB-oed</td><td>&nbsp;</td><td>en-GB-x-oed</td></tr>
1541      * <tr><td>i-default</td><td>&nbsp;</td><td>en-x-i-default</td></tr>
1542      * <tr><td>i-enochian</td><td>&nbsp;</td><td>und-x-i-enochian</td></tr>
1543      * <tr><td>i-mingo</td><td>&nbsp;</td><td>see-x-i-mingo</td></tr>
1544      * <tr><td>zh-min</td><td>&nbsp;</td><td>nan-x-zh-min</td></tr>
1545      * </tbody>
1546      * </table>
1547      *
1548      * <p>For a list of all grandfathered tags, see the
1549      * IANA Language Subtag Registry (search for "Type: grandfathered").
1550      *
1551      * <p><b>Note</b>: there is no guarantee that <code>toLanguageTag</code>
1552      * and <code>forLanguageTag</code> will round-trip.
1553      *
1554      * @param languageTag the language tag
1555      * @return The locale that best represents the language tag.
1556      * @throws NullPointerException if <code>languageTag</code> is <code>null</code>
1557      * @see #toLanguageTag()
1558      * @see java.util.Locale.Builder#setLanguageTag(String)
1559      * @since 1.7
1560      */
1561     public static Locale forLanguageTag(String languageTag) {
1562         LanguageTag tag = LanguageTag.parse(languageTag, null);
1563         InternalLocaleBuilder bldr = new InternalLocaleBuilder();
1564         bldr.setLanguageTag(tag);
1565         BaseLocale base = bldr.getBaseLocale();
1566         LocaleExtensions exts = bldr.getLocaleExtensions();
1567         if (exts == null && base.getVariant().length() > 0) {
1568             exts = getCompatibilityExtensions(base.getLanguage(), base.getScript(),
1569                                               base.getRegion(), base.getVariant());
1570         }
1571         return getInstance(base, exts);
1572     }
1573 
1574     /**
1575      * Returns a three-letter abbreviation of this locale's language.
1576      * If the language matches an ISO 639-1 two-letter code, the
1577      * corresponding ISO 639-2/T three-letter lowercase code is
1578      * returned.  The ISO 639-2 language codes can be found on-line,
1579      * see "Codes for the Representation of Names of Languages Part 2:
1580      * Alpha-3 Code".  If the locale specifies a three-letter
1581      * language, the language is returned as is.  If the locale does
1582      * not specify a language the empty string is returned.
1583      *
1584      * @return A three-letter abbreviation of this locale's language.
1585      * @exception MissingResourceException Throws MissingResourceException if
1586      * three-letter language abbreviation is not available for this locale.
1587      */
1588     public String getISO3Language() throws MissingResourceException {
1589         String lang = baseLocale.getLanguage();
1590         if (lang.length() == 3) {
1591             return lang;
1592         }
1593 
1594         String language3 = getISO3Code(lang, LocaleISOData.isoLanguageTable);
1595         if (language3 == null) {
1596             throw new MissingResourceException("Couldn't find 3-letter language code for "
1597                     + lang, "FormatData_" + toString(), "ShortLanguage");
1598         }
1599         return language3;
1600     }
1601 
1602     /**
1603      * Returns a three-letter abbreviation for this locale's country.
1604      * If the country matches an ISO 3166-1 alpha-2 code, the
1605      * corresponding ISO 3166-1 alpha-3 uppercase code is returned.
1606      * If the locale doesn't specify a country, this will be the empty
1607      * string.
1608      *
1609      * <p>The ISO 3166-1 codes can be found on-line.
1610      *
1611      * @return A three-letter abbreviation of this locale's country.
1612      * @exception MissingResourceException Throws MissingResourceException if the
1613      * three-letter country abbreviation is not available for this locale.
1614      */
1615     public String getISO3Country() throws MissingResourceException {
1616         String country3 = getISO3Code(baseLocale.getRegion(), LocaleISOData.isoCountryTable);
1617         if (country3 == null) {
1618             throw new MissingResourceException("Couldn't find 3-letter country code for "
1619                     + baseLocale.getRegion(), "FormatData_" + toString(), "ShortCountry");
1620         }
1621         return country3;
1622     }
1623 
1624     private static String getISO3Code(String iso2Code, String table) {
1625         int codeLength = iso2Code.length();
1626         if (codeLength == 0) {
1627             return "";
1628         }
1629 
1630         int tableLength = table.length();
1631         int index = tableLength;
1632         if (codeLength == 2) {
1633             char c1 = iso2Code.charAt(0);
1634             char c2 = iso2Code.charAt(1);
1635             for (index = 0; index < tableLength; index += 5) {
1636                 if (table.charAt(index) == c1
1637                     && table.charAt(index + 1) == c2) {
1638                     break;
1639                 }
1640             }
1641         }
1642         return index < tableLength ? table.substring(index + 2, index + 5) : null;
1643     }
1644 
1645     /**
1646      * Returns a name for the locale's language that is appropriate for display to the
1647      * user.
1648      * If possible, the name returned will be localized for the default
1649      * {@link Locale.Category#DISPLAY DISPLAY} locale.
1650      * For example, if the locale is fr_FR and the default
1651      * {@link Locale.Category#DISPLAY DISPLAY} locale
1652      * is en_US, getDisplayLanguage() will return "French"; if the locale is en_US and
1653      * the default {@link Locale.Category#DISPLAY DISPLAY} locale is fr_FR,
1654      * getDisplayLanguage() will return "anglais".
1655      * If the name returned cannot be localized for the default
1656      * {@link Locale.Category#DISPLAY DISPLAY} locale,
1657      * (say, we don't have a Japanese name for Croatian),
1658      * this function falls back on the English name, and uses the ISO code as a last-resort
1659      * value.  If the locale doesn't specify a language, this function returns the empty string.
1660      *
1661      * @return The name of the display language.
1662      */
1663     public final String getDisplayLanguage() {
1664         return getDisplayLanguage(getDefault(Category.DISPLAY));
1665     }
1666 
1667     /**
1668      * Returns a name for the locale's language that is appropriate for display to the
1669      * user.
1670      * If possible, the name returned will be localized according to inLocale.
1671      * For example, if the locale is fr_FR and inLocale
1672      * is en_US, getDisplayLanguage() will return "French"; if the locale is en_US and
1673      * inLocale is fr_FR, getDisplayLanguage() will return "anglais".
1674      * If the name returned cannot be localized according to inLocale,
1675      * (say, we don't have a Japanese name for Croatian),
1676      * this function falls back on the English name, and finally
1677      * on the ISO code as a last-resort value.  If the locale doesn't specify a language,
1678      * this function returns the empty string.
1679      *
1680      * @param inLocale The locale for which to retrieve the display language.
1681      * @return The name of the display language appropriate to the given locale.
1682      * @exception NullPointerException if <code>inLocale</code> is <code>null</code>
1683      */
1684     public String getDisplayLanguage(Locale inLocale) {
1685         return getDisplayString(baseLocale.getLanguage(), inLocale, DISPLAY_LANGUAGE);
1686     }
1687 
1688     /**
1689      * Returns a name for the locale's script that is appropriate for display to
1690      * the user. If possible, the name will be localized for the default
1691      * {@link Locale.Category#DISPLAY DISPLAY} locale.  Returns
1692      * the empty string if this locale doesn't specify a script code.
1693      *
1694      * @return the display name of the script code for the current default
1695      *     {@link Locale.Category#DISPLAY DISPLAY} locale
1696      * @since 1.7
1697      */
1698     public String getDisplayScript() {
1699         return getDisplayScript(getDefault(Category.DISPLAY));
1700     }
1701 
1702     /**
1703      * Returns a name for the locale's script that is appropriate
1704      * for display to the user. If possible, the name will be
1705      * localized for the given locale. Returns the empty string if
1706      * this locale doesn't specify a script code.
1707      *
1708      * @param inLocale The locale for which to retrieve the display script.
1709      * @return the display name of the script code for the current default
1710      * {@link Locale.Category#DISPLAY DISPLAY} locale
1711      * @throws NullPointerException if <code>inLocale</code> is <code>null</code>
1712      * @since 1.7
1713      */
1714     public String getDisplayScript(Locale inLocale) {
1715         return getDisplayString(baseLocale.getScript(), inLocale, DISPLAY_SCRIPT);
1716     }
1717 
1718     /**
1719      * Returns a name for the locale's country that is appropriate for display to the
1720      * user.
1721      * If possible, the name returned will be localized for the default
1722      * {@link Locale.Category#DISPLAY DISPLAY} locale.
1723      * For example, if the locale is fr_FR and the default
1724      * {@link Locale.Category#DISPLAY DISPLAY} locale
1725      * is en_US, getDisplayCountry() will return "France"; if the locale is en_US and
1726      * the default {@link Locale.Category#DISPLAY DISPLAY} locale is fr_FR,
1727      * getDisplayCountry() will return "Etats-Unis".
1728      * If the name returned cannot be localized for the default
1729      * {@link Locale.Category#DISPLAY DISPLAY} locale,
1730      * (say, we don't have a Japanese name for Croatia),
1731      * this function falls back on the English name, and uses the ISO code as a last-resort
1732      * value.  If the locale doesn't specify a country, this function returns the empty string.
1733      *
1734      * @return The name of the country appropriate to the locale.
1735      */
1736     public final String getDisplayCountry() {
1737         return getDisplayCountry(getDefault(Category.DISPLAY));
1738     }
1739 
1740     /**
1741      * Returns a name for the locale's country that is appropriate for display to the
1742      * user.
1743      * If possible, the name returned will be localized according to inLocale.
1744      * For example, if the locale is fr_FR and inLocale
1745      * is en_US, getDisplayCountry() will return "France"; if the locale is en_US and
1746      * inLocale is fr_FR, getDisplayCountry() will return "Etats-Unis".
1747      * If the name returned cannot be localized according to inLocale.
1748      * (say, we don't have a Japanese name for Croatia),
1749      * this function falls back on the English name, and finally
1750      * on the ISO code as a last-resort value.  If the locale doesn't specify a country,
1751      * this function returns the empty string.
1752      *
1753      * @param inLocale The locale for which to retrieve the display country.
1754      * @return The name of the country appropriate to the given locale.
1755      * @exception NullPointerException if <code>inLocale</code> is <code>null</code>
1756      */
1757     public String getDisplayCountry(Locale inLocale) {
1758         return getDisplayString(baseLocale.getRegion(), inLocale, DISPLAY_COUNTRY);
1759     }
1760 
1761     private String getDisplayString(String code, Locale inLocale, int type) {
1762         if (code.length() == 0) {
1763             return "";
1764         }
1765 
1766         if (inLocale == null) {
1767             throw new NullPointerException();
1768         }
1769 
1770         LocaleServiceProviderPool pool =
1771             LocaleServiceProviderPool.getPool(LocaleNameProvider.class);
1772         String key = (type == DISPLAY_VARIANT ? "%%"+code : code);
1773         String result = pool.getLocalizedObject(
1774                                 LocaleNameGetter.INSTANCE,
1775                                 inLocale, key, type, code);
1776             if (result != null) {
1777                 return result;
1778             }
1779 
1780         return code;
1781     }
1782 
1783     /**
1784      * Returns a name for the locale's variant code that is appropriate for display to the
1785      * user.  If possible, the name will be localized for the default
1786      * {@link Locale.Category#DISPLAY DISPLAY} locale.  If the locale
1787      * doesn't specify a variant code, this function returns the empty string.
1788      *
1789      * @return The name of the display variant code appropriate to the locale.
1790      */
1791     public final String getDisplayVariant() {
1792         return getDisplayVariant(getDefault(Category.DISPLAY));
1793     }
1794 
1795     /**
1796      * Returns a name for the locale's variant code that is appropriate for display to the
1797      * user.  If possible, the name will be localized for inLocale.  If the locale
1798      * doesn't specify a variant code, this function returns the empty string.
1799      *
1800      * @param inLocale The locale for which to retrieve the display variant code.
1801      * @return The name of the display variant code appropriate to the given locale.
1802      * @exception NullPointerException if <code>inLocale</code> is <code>null</code>
1803      */
1804     public String getDisplayVariant(Locale inLocale) {
1805         if (baseLocale.getVariant().length() == 0)
1806             return "";
1807 
1808         LocaleResources lr = LocaleProviderAdapter.forJRE().getLocaleResources(inLocale);
1809 
1810         String names[] = getDisplayVariantArray(inLocale);
1811 
1812         // Get the localized patterns for formatting a list, and use
1813         // them to format the list.
1814         return formatList(names,
1815                           lr.getLocaleName("ListPattern"),
1816                           lr.getLocaleName("ListCompositionPattern"));
1817     }
1818 
1819     /**
1820      * Returns a name for the locale that is appropriate for display to the
1821      * user. This will be the values returned by getDisplayLanguage(),
1822      * getDisplayScript(), getDisplayCountry(), and getDisplayVariant() assembled
1823      * into a single string. The the non-empty values are used in order,
1824      * with the second and subsequent names in parentheses.  For example:
1825      * <blockquote>
1826      * language (script, country, variant)<br>
1827      * language (country)<br>
1828      * language (variant)<br>
1829      * script (country)<br>
1830      * country<br>
1831      * </blockquote>
1832      * depending on which fields are specified in the locale.  If the
1833      * language, script, country, and variant fields are all empty,
1834      * this function returns the empty string.
1835      *
1836      * @return The name of the locale appropriate to display.
1837      */
1838     public final String getDisplayName() {
1839         return getDisplayName(getDefault(Category.DISPLAY));
1840     }
1841 
1842     /**
1843      * Returns a name for the locale that is appropriate for display
1844      * to the user.  This will be the values returned by
1845      * getDisplayLanguage(), getDisplayScript(),getDisplayCountry(),
1846      * and getDisplayVariant() assembled into a single string.
1847      * The non-empty values are used in order,
1848      * with the second and subsequent names in parentheses.  For example:
1849      * <blockquote>
1850      * language (script, country, variant)<br>
1851      * language (country)<br>
1852      * language (variant)<br>
1853      * script (country)<br>
1854      * country<br>
1855      * </blockquote>
1856      * depending on which fields are specified in the locale.  If the
1857      * language, script, country, and variant fields are all empty,
1858      * this function returns the empty string.
1859      *
1860      * @param inLocale The locale for which to retrieve the display name.
1861      * @return The name of the locale appropriate to display.
1862      * @throws NullPointerException if <code>inLocale</code> is <code>null</code>
1863      */
1864     public String getDisplayName(Locale inLocale) {
1865         LocaleResources lr =  LocaleProviderAdapter.forJRE().getLocaleResources(inLocale);
1866 
1867         String languageName = getDisplayLanguage(inLocale);
1868         String scriptName = getDisplayScript(inLocale);
1869         String countryName = getDisplayCountry(inLocale);
1870         String[] variantNames = getDisplayVariantArray(inLocale);
1871 
1872         // Get the localized patterns for formatting a display name.
1873         String displayNamePattern = lr.getLocaleName("DisplayNamePattern");
1874         String listPattern = lr.getLocaleName("ListPattern");
1875         String listCompositionPattern = lr.getLocaleName("ListCompositionPattern");
1876 
1877         // The display name consists of a main name, followed by qualifiers.
1878         // Typically, the format is "MainName (Qualifier, Qualifier)" but this
1879         // depends on what pattern is stored in the display locale.
1880         String   mainName       = null;
1881         String[] qualifierNames = null;
1882 
1883         // The main name is the language, or if there is no language, the script,
1884         // then if no script, the country. If there is no language/script/country
1885         // (an anomalous situation) then the display name is simply the variant's
1886         // display name.
1887         if (languageName.length() == 0 && scriptName.length() == 0 && countryName.length() == 0) {
1888             if (variantNames.length == 0) {
1889                 return "";
1890             } else {
1891                 return formatList(variantNames, listPattern, listCompositionPattern);
1892             }
1893         }
1894         ArrayList<String> names = new ArrayList<>(4);
1895         if (languageName.length() != 0) {
1896             names.add(languageName);
1897         }
1898         if (scriptName.length() != 0) {
1899             names.add(scriptName);
1900         }
1901         if (countryName.length() != 0) {
1902             names.add(countryName);
1903         }
1904         if (variantNames.length != 0) {
1905             names.addAll(Arrays.asList(variantNames));
1906         }
1907 
1908         // The first one in the main name
1909         mainName = names.get(0);
1910 
1911         // Others are qualifiers
1912         int numNames = names.size();
1913         qualifierNames = (numNames > 1) ?
1914                 names.subList(1, numNames).toArray(new String[numNames - 1]) : new String[0];
1915 
1916         // Create an array whose first element is the number of remaining
1917         // elements.  This serves as a selector into a ChoiceFormat pattern from
1918         // the resource.  The second and third elements are the main name and
1919         // the qualifier; if there are no qualifiers, the third element is
1920         // unused by the format pattern.
1921         Object[] displayNames = {
1922             qualifierNames.length != 0 ? 2 : 1,
1923             mainName,
1924             // We could also just call formatList() and have it handle the empty
1925             // list case, but this is more efficient, and we want it to be
1926             // efficient since all the language-only locales will not have any
1927             // qualifiers.
1928             qualifierNames.length != 0 ? formatList(qualifierNames, listPattern, listCompositionPattern) : null
1929         };
1930 
1931         if (displayNamePattern != null) {
1932             return new MessageFormat(displayNamePattern).format(displayNames);
1933         }
1934         else {
1935             // If we cannot get the message format pattern, then we use a simple
1936             // hard-coded pattern.  This should not occur in practice unless the
1937             // installation is missing some core files (FormatData etc.).
1938             StringBuilder result = new StringBuilder();
1939             result.append((String)displayNames[1]);
1940             if (displayNames.length > 2) {
1941                 result.append(" (");
1942                 result.append((String)displayNames[2]);
1943                 result.append(')');
1944             }
1945             return result.toString();
1946         }
1947     }
1948 
1949     /**
1950      * Overrides Cloneable.
1951      */
1952     @Override
1953     public Object clone()
1954     {
1955         try {
1956             Locale that = (Locale)super.clone();
1957             return that;
1958         } catch (CloneNotSupportedException e) {
1959             throw new InternalError(e);
1960         }
1961     }
1962 
1963     /**
1964      * Override hashCode.
1965      * Since Locales are often used in hashtables, caches the value
1966      * for speed.
1967      */
1968     @Override
1969     public int hashCode() {
1970         int hc = hashCodeValue;
1971         if (hc == 0) {
1972             hc = baseLocale.hashCode();
1973             if (localeExtensions != null) {
1974                 hc ^= localeExtensions.hashCode();
1975             }
1976             hashCodeValue = hc;
1977         }
1978         return hc;
1979     }
1980 
1981     // Overrides
1982 
1983     /**
1984      * Returns true if this Locale is equal to another object.  A Locale is
1985      * deemed equal to another Locale with identical language, script, country,
1986      * variant and extensions, and unequal to all other objects.
1987      *
1988      * @return true if this Locale is equal to the specified object.
1989      */
1990     @Override
1991     public boolean equals(Object obj) {
1992         if (this == obj)                      // quick check
1993             return true;
1994         if (!(obj instanceof Locale))
1995             return false;
1996         BaseLocale otherBase = ((Locale)obj).baseLocale;
1997         if (!baseLocale.equals(otherBase)) {
1998             return false;
1999         }
2000         if (localeExtensions == null) {
2001             return ((Locale)obj).localeExtensions == null;
2002         }
2003         return localeExtensions.equals(((Locale)obj).localeExtensions);
2004     }
2005 
2006     // ================= privates =====================================
2007 
2008     private transient BaseLocale baseLocale;
2009     private transient LocaleExtensions localeExtensions;
2010 
2011     /**
2012      * Calculated hashcode
2013      */
2014     private transient volatile int hashCodeValue;
2015 
2016     private static volatile Locale defaultLocale = initDefault();
2017     private static volatile Locale defaultDisplayLocale;
2018     private static volatile Locale defaultFormatLocale;
2019 
2020     private transient volatile String languageTag;
2021 
2022     /**
2023      * Return an array of the display names of the variant.
2024      * @param bundle the ResourceBundle to use to get the display names
2025      * @return an array of display names, possible of zero length.
2026      */
2027     private String[] getDisplayVariantArray(Locale inLocale) {
2028         // Split the variant name into tokens separated by '_'.
2029         StringTokenizer tokenizer = new StringTokenizer(baseLocale.getVariant(), "_");
2030         String[] names = new String[tokenizer.countTokens()];
2031 
2032         // For each variant token, lookup the display name.  If
2033         // not found, use the variant name itself.
2034         for (int i=0; i<names.length; ++i) {
2035             names[i] = getDisplayString(tokenizer.nextToken(),
2036                                 inLocale, DISPLAY_VARIANT);
2037         }
2038 
2039         return names;
2040     }
2041 
2042     /**
2043      * Format a list using given pattern strings.
2044      * If either of the patterns is null, then a the list is
2045      * formatted by concatenation with the delimiter ','.
2046      * @param stringList the list of strings to be formatted.
2047      * @param listPattern should create a MessageFormat taking 0-3 arguments
2048      * and formatting them into a list.
2049      * @param listCompositionPattern should take 2 arguments
2050      * and is used by composeList.
2051      * @return a string representing the list.
2052      */
2053     private static String formatList(String[] stringList, String listPattern, String listCompositionPattern) {
2054         // If we have no list patterns, compose the list in a simple,
2055         // non-localized way.
2056         if (listPattern == null || listCompositionPattern == null) {
2057             StringJoiner sj = new StringJoiner(",");
2058             for (int i = 0; i < stringList.length; ++i) {
2059                 sj.add(stringList[i]);
2060             }
2061             return sj.toString();
2062         }
2063 
2064         // Compose the list down to three elements if necessary
2065         if (stringList.length > 3) {
2066             MessageFormat format = new MessageFormat(listCompositionPattern);
2067             stringList = composeList(format, stringList);
2068         }
2069 
2070         // Rebuild the argument list with the list length as the first element
2071         Object[] args = new Object[stringList.length + 1];
2072         System.arraycopy(stringList, 0, args, 1, stringList.length);
2073         args[0] = stringList.length;
2074 
2075         // Format it using the pattern in the resource
2076         MessageFormat format = new MessageFormat(listPattern);
2077         return format.format(args);
2078     }
2079 
2080     /**
2081      * Given a list of strings, return a list shortened to three elements.
2082      * Shorten it by applying the given format to the first two elements
2083      * recursively.
2084      * @param format a format which takes two arguments
2085      * @param list a list of strings
2086      * @return if the list is three elements or shorter, the same list;
2087      * otherwise, a new list of three elements.
2088      */
2089     private static String[] composeList(MessageFormat format, String[] list) {
2090         if (list.length <= 3) return list;
2091 
2092         // Use the given format to compose the first two elements into one
2093         String[] listItems = { list[0], list[1] };
2094         String newItem = format.format(listItems);
2095 
2096         // Form a new list one element shorter
2097         String[] newList = new String[list.length-1];
2098         System.arraycopy(list, 2, newList, 1, newList.length-1);
2099         newList[0] = newItem;
2100 
2101         // Recurse
2102         return composeList(format, newList);
2103     }
2104 
2105     // Duplicate of sun.util.locale.UnicodeLocaleExtension.isKey in order to
2106     // avoid its class loading.
2107     private static boolean isUnicodeExtensionKey(String s) {
2108         // 2alphanum
2109         return (s.length() == 2) && LocaleUtils.isAlphaNumericString(s);
2110     }
2111 
2112     /**
2113      * @serialField language    String
2114      *      language subtag in lower case. (See <a href="java/util/Locale.html#getLanguage()">getLanguage()</a>)
2115      * @serialField country     String
2116      *      country subtag in upper case. (See <a href="java/util/Locale.html#getCountry()">getCountry()</a>)
2117      * @serialField variant     String
2118      *      variant subtags separated by LOWLINE characters. (See <a href="java/util/Locale.html#getVariant()">getVariant()</a>)
2119      * @serialField hashcode    int
2120      *      deprecated, for forward compatibility only
2121      * @serialField script      String
2122      *      script subtag in title case (See <a href="java/util/Locale.html#getScript()">getScript()</a>)
2123      * @serialField extensions  String
2124      *      canonical representation of extensions, that is,
2125      *      BCP47 extensions in alphabetical order followed by
2126      *      BCP47 private use subtags, all in lower case letters
2127      *      separated by HYPHEN-MINUS characters.
2128      *      (See <a href="java/util/Locale.html#getExtensionKeys()">getExtensionKeys()</a>,
2129      *      <a href="java/util/Locale.html#getExtension(char)">getExtension(char)</a>)
2130      */
2131     private static final ObjectStreamField[] serialPersistentFields = {
2132         new ObjectStreamField("language", String.class),
2133         new ObjectStreamField("country", String.class),
2134         new ObjectStreamField("variant", String.class),
2135         new ObjectStreamField("hashcode", int.class),
2136         new ObjectStreamField("script", String.class),
2137         new ObjectStreamField("extensions", String.class),
2138     };
2139 
2140     /**
2141      * Serializes this <code>Locale</code> to the specified <code>ObjectOutputStream</code>.
2142      * @param out the <code>ObjectOutputStream</code> to write
2143      * @throws IOException
2144      * @since 1.7
2145      */
2146     private void writeObject(ObjectOutputStream out) throws IOException {
2147         ObjectOutputStream.PutField fields = out.putFields();
2148         fields.put("language", baseLocale.getLanguage());
2149         fields.put("script", baseLocale.getScript());
2150         fields.put("country", baseLocale.getRegion());
2151         fields.put("variant", baseLocale.getVariant());
2152         fields.put("extensions", localeExtensions == null ? "" : localeExtensions.getID());
2153         fields.put("hashcode", -1); // place holder just for backward support
2154         out.writeFields();
2155     }
2156 
2157     /**
2158      * Deserializes this <code>Locale</code>.
2159      * @param in the <code>ObjectInputStream</code> to read
2160      * @throws IOException
2161      * @throws ClassNotFoundException
2162      * @throws IllformedLocaleException
2163      * @since 1.7
2164      */
2165     private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
2166         ObjectInputStream.GetField fields = in.readFields();
2167         String language = (String)fields.get("language", "");
2168         String script = (String)fields.get("script", "");
2169         String country = (String)fields.get("country", "");
2170         String variant = (String)fields.get("variant", "");
2171         String extStr = (String)fields.get("extensions", "");
2172         baseLocale = BaseLocale.getInstance(convertOldISOCodes(language), script, country, variant);
2173         if (extStr.length() > 0) {
2174             try {
2175                 InternalLocaleBuilder bldr = new InternalLocaleBuilder();
2176                 bldr.setExtensions(extStr);
2177                 localeExtensions = bldr.getLocaleExtensions();
2178             } catch (LocaleSyntaxException e) {
2179                 throw new IllformedLocaleException(e.getMessage());
2180             }
2181         } else {
2182             localeExtensions = null;
2183         }
2184     }
2185 
2186     /**
2187      * Returns a cached <code>Locale</code> instance equivalent to
2188      * the deserialized <code>Locale</code>. When serialized
2189      * language, country and variant fields read from the object data stream
2190      * are exactly "ja", "JP", "JP" or "th", "TH", "TH" and script/extensions
2191      * fields are empty, this method supplies <code>UNICODE_LOCALE_EXTENSION</code>
2192      * "ca"/"japanese" (calendar type is "japanese") or "nu"/"thai" (number script
2193      * type is "thai"). See <a href="Locale.html#special_cases_constructor">Special Cases</a>
2194      * for more information.
2195      *
2196      * @return an instance of <code>Locale</code> equivalent to
2197      * the deserialized <code>Locale</code>.
2198      * @throws java.io.ObjectStreamException
2199      */
2200     private Object readResolve() throws java.io.ObjectStreamException {
2201         return getInstance(baseLocale.getLanguage(), baseLocale.getScript(),
2202                 baseLocale.getRegion(), baseLocale.getVariant(), localeExtensions);
2203     }
2204 
2205     private static volatile String[] isoLanguages;
2206 
2207     private static volatile String[] isoCountries;
2208 
2209     private static String convertOldISOCodes(String language) {
2210         // we accept both the old and the new ISO codes for the languages whose ISO
2211         // codes have changed, but we always store the OLD code, for backward compatibility
2212         language = LocaleUtils.toLowerString(language).intern();
2213         if (language == "he") {
2214             return "iw";
2215         } else if (language == "yi") {
2216             return "ji";
2217         } else if (language == "id") {
2218             return "in";
2219         } else {
2220             return language;
2221         }
2222     }
2223 
2224     private static LocaleExtensions getCompatibilityExtensions(String language,
2225                                                                String script,
2226                                                                String country,
2227                                                                String variant) {
2228         LocaleExtensions extensions = null;
2229         // Special cases for backward compatibility support
2230         if (LocaleUtils.caseIgnoreMatch(language, "ja")
2231                 && script.length() == 0
2232                 && LocaleUtils.caseIgnoreMatch(country, "jp")
2233                 && "JP".equals(variant)) {
2234             // ja_JP_JP -> u-ca-japanese (calendar = japanese)
2235             extensions = LocaleExtensions.CALENDAR_JAPANESE;
2236         } else if (LocaleUtils.caseIgnoreMatch(language, "th")
2237                 && script.length() == 0
2238                 && LocaleUtils.caseIgnoreMatch(country, "th")
2239                 && "TH".equals(variant)) {
2240             // th_TH_TH -> u-nu-thai (numbersystem = thai)
2241             extensions = LocaleExtensions.NUMBER_THAI;
2242         }
2243         return extensions;
2244     }
2245 
2246     /**
2247      * Obtains a localized locale names from a LocaleNameProvider
2248      * implementation.
2249      */
2250     private static class LocaleNameGetter
2251         implements LocaleServiceProviderPool.LocalizedObjectGetter<LocaleNameProvider, String> {
2252         private static final LocaleNameGetter INSTANCE = new LocaleNameGetter();
2253 
2254         @Override
2255         public String getObject(LocaleNameProvider localeNameProvider,
2256                                 Locale locale,
2257                                 String key,
2258                                 Object... params) {
2259             assert params.length == 2;
2260             int type = (Integer)params[0];
2261             String code = (String)params[1];
2262 
2263             switch(type) {
2264             case DISPLAY_LANGUAGE:
2265                 return localeNameProvider.getDisplayLanguage(code, locale);
2266             case DISPLAY_COUNTRY:
2267                 return localeNameProvider.getDisplayCountry(code, locale);
2268             case DISPLAY_VARIANT:
2269                 return localeNameProvider.getDisplayVariant(code, locale);
2270             case DISPLAY_SCRIPT:
2271                 return localeNameProvider.getDisplayScript(code, locale);
2272             default:
2273                 assert false; // shouldn't happen
2274             }
2275 
2276             return null;
2277         }
2278     }
2279 
2280     /**
2281      * Enum for locale categories.  These locale categories are used to get/set
2282      * the default locale for the specific functionality represented by the
2283      * category.
2284      *
2285      * @see #getDefault(Locale.Category)
2286      * @see #setDefault(Locale.Category, Locale)
2287      * @since 1.7
2288      */
2289     public enum Category {
2290 
2291         /**
2292          * Category used to represent the default locale for
2293          * displaying user interfaces.
2294          */
2295         DISPLAY("user.language.display",
2296                 "user.script.display",
2297                 "user.country.display",
2298                 "user.variant.display"),
2299 
2300         /**
2301          * Category used to represent the default locale for
2302          * formatting dates, numbers, and/or currencies.
2303          */
2304         FORMAT("user.language.format",
2305                "user.script.format",
2306                "user.country.format",
2307                "user.variant.format");
2308 
2309         Category(String languageKey, String scriptKey, String countryKey, String variantKey) {
2310             this.languageKey = languageKey;
2311             this.scriptKey = scriptKey;
2312             this.countryKey = countryKey;
2313             this.variantKey = variantKey;
2314         }
2315 
2316         final String languageKey;
2317         final String scriptKey;
2318         final String countryKey;
2319         final String variantKey;
2320     }
2321 
2322     /**
2323      * <code>Builder</code> is used to build instances of <code>Locale</code>
2324      * from values configured by the setters.  Unlike the <code>Locale</code>
2325      * constructors, the <code>Builder</code> checks if a value configured by a
2326      * setter satisfies the syntax requirements defined by the <code>Locale</code>
2327      * class.  A <code>Locale</code> object created by a <code>Builder</code> is
2328      * well-formed and can be transformed to a well-formed IETF BCP 47 language tag
2329      * without losing information.
2330      *
2331      * <p><b>Note:</b> The <code>Locale</code> class does not provide any
2332      * syntactic restrictions on variant, while BCP 47 requires each variant
2333      * subtag to be 5 to 8 alphanumerics or a single numeric followed by 3
2334      * alphanumerics.  The method <code>setVariant</code> throws
2335      * <code>IllformedLocaleException</code> for a variant that does not satisfy
2336      * this restriction. If it is necessary to support such a variant, use a
2337      * Locale constructor.  However, keep in mind that a <code>Locale</code>
2338      * object created this way might lose the variant information when
2339      * transformed to a BCP 47 language tag.
2340      *
2341      * <p>The following example shows how to create a <code>Locale</code> object
2342      * with the <code>Builder</code>.
2343      * <blockquote>
2344      * <pre>
2345      *     Locale aLocale = new Builder().setLanguage("sr").setScript("Latn").setRegion("RS").build();
2346      * </pre>
2347      * </blockquote>
2348      *
2349      * <p>Builders can be reused; <code>clear()</code> resets all
2350      * fields to their default values.
2351      *
2352      * @see Locale#forLanguageTag
2353      * @since 1.7
2354      */
2355     public static final class Builder {
2356         private final InternalLocaleBuilder localeBuilder;
2357 
2358         /**
2359          * Constructs an empty Builder. The default value of all
2360          * fields, extensions, and private use information is the
2361          * empty string.
2362          */
2363         public Builder() {
2364             localeBuilder = new InternalLocaleBuilder();
2365         }
2366 
2367         /**
2368          * Resets the <code>Builder</code> to match the provided
2369          * <code>locale</code>.  Existing state is discarded.
2370          *
2371          * <p>All fields of the locale must be well-formed, see {@link Locale}.
2372          *
2373          * <p>Locales with any ill-formed fields cause
2374          * <code>IllformedLocaleException</code> to be thrown, except for the
2375          * following three cases which are accepted for compatibility
2376          * reasons:<ul>
2377          * <li>Locale("ja", "JP", "JP") is treated as "ja-JP-u-ca-japanese"
2378          * <li>Locale("th", "TH", "TH") is treated as "th-TH-u-nu-thai"
2379          * <li>Locale("no", "NO", "NY") is treated as "nn-NO"</ul>
2380          *
2381          * @param locale the locale
2382          * @return This builder.
2383          * @throws IllformedLocaleException if <code>locale</code> has
2384          * any ill-formed fields.
2385          * @throws NullPointerException if <code>locale</code> is null.
2386          */
2387         public Builder setLocale(Locale locale) {
2388             try {
2389                 localeBuilder.setLocale(locale.baseLocale, locale.localeExtensions);
2390             } catch (LocaleSyntaxException e) {
2391                 throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex());
2392             }
2393             return this;
2394         }
2395 
2396         /**
2397          * Resets the Builder to match the provided IETF BCP 47
2398          * language tag.  Discards the existing state.  Null and the
2399          * empty string cause the builder to be reset, like {@link
2400          * #clear}.  Grandfathered tags (see {@link
2401          * Locale#forLanguageTag}) are converted to their canonical
2402          * form before being processed.  Otherwise, the language tag
2403          * must be well-formed (see {@link Locale}) or an exception is
2404          * thrown (unlike <code>Locale.forLanguageTag</code>, which
2405          * just discards ill-formed and following portions of the
2406          * tag).
2407          *
2408          * @param languageTag the language tag
2409          * @return This builder.
2410          * @throws IllformedLocaleException if <code>languageTag</code> is ill-formed
2411          * @see Locale#forLanguageTag(String)
2412          */
2413         public Builder setLanguageTag(String languageTag) {
2414             ParseStatus sts = new ParseStatus();
2415             LanguageTag tag = LanguageTag.parse(languageTag, sts);
2416             if (sts.isError()) {
2417                 throw new IllformedLocaleException(sts.getErrorMessage(), sts.getErrorIndex());
2418             }
2419             localeBuilder.setLanguageTag(tag);
2420             return this;
2421         }
2422 
2423         /**
2424          * Sets the language.  If <code>language</code> is the empty string or
2425          * null, the language in this <code>Builder</code> is removed.  Otherwise,
2426          * the language must be <a href="./Locale.html#def_language">well-formed</a>
2427          * or an exception is thrown.
2428          *
2429          * <p>The typical language value is a two or three-letter language
2430          * code as defined in ISO639.
2431          *
2432          * @param language the language
2433          * @return This builder.
2434          * @throws IllformedLocaleException if <code>language</code> is ill-formed
2435          */
2436         public Builder setLanguage(String language) {
2437             try {
2438                 localeBuilder.setLanguage(language);
2439             } catch (LocaleSyntaxException e) {
2440                 throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex());
2441             }
2442             return this;
2443         }
2444 
2445         /**
2446          * Sets the script. If <code>script</code> is null or the empty string,
2447          * the script in this <code>Builder</code> is removed.
2448          * Otherwise, the script must be <a href="./Locale.html#def_script">well-formed</a> or an
2449          * exception is thrown.
2450          *
2451          * <p>The typical script value is a four-letter script code as defined by ISO 15924.
2452          *
2453          * @param script the script
2454          * @return This builder.
2455          * @throws IllformedLocaleException if <code>script</code> is ill-formed
2456          */
2457         public Builder setScript(String script) {
2458             try {
2459                 localeBuilder.setScript(script);
2460             } catch (LocaleSyntaxException e) {
2461                 throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex());
2462             }
2463             return this;
2464         }
2465 
2466         /**
2467          * Sets the region.  If region is null or the empty string, the region
2468          * in this <code>Builder</code> is removed.  Otherwise,
2469          * the region must be <a href="./Locale.html#def_region">well-formed</a> or an
2470          * exception is thrown.
2471          *
2472          * <p>The typical region value is a two-letter ISO 3166 code or a
2473          * three-digit UN M.49 area code.
2474          *
2475          * <p>The country value in the <code>Locale</code> created by the
2476          * <code>Builder</code> is always normalized to upper case.
2477          *
2478          * @param region the region
2479          * @return This builder.
2480          * @throws IllformedLocaleException if <code>region</code> is ill-formed
2481          */
2482         public Builder setRegion(String region) {
2483             try {
2484                 localeBuilder.setRegion(region);
2485             } catch (LocaleSyntaxException e) {
2486                 throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex());
2487             }
2488             return this;
2489         }
2490 
2491         /**
2492          * Sets the variant.  If variant is null or the empty string, the
2493          * variant in this <code>Builder</code> is removed.  Otherwise, it
2494          * must consist of one or more <a href="./Locale.html#def_variant">well-formed</a>
2495          * subtags, or an exception is thrown.
2496          *
2497          * <p><b>Note:</b> This method checks if <code>variant</code>
2498          * satisfies the IETF BCP 47 variant subtag's syntax requirements,
2499          * and normalizes the value to lowercase letters.  However,
2500          * the <code>Locale</code> class does not impose any syntactic
2501          * restriction on variant, and the variant value in
2502          * <code>Locale</code> is case sensitive.  To set such a variant,
2503          * use a Locale constructor.
2504          *
2505          * @param variant the variant
2506          * @return This builder.
2507          * @throws IllformedLocaleException if <code>variant</code> is ill-formed
2508          */
2509         public Builder setVariant(String variant) {
2510             try {
2511                 localeBuilder.setVariant(variant);
2512             } catch (LocaleSyntaxException e) {
2513                 throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex());
2514             }
2515             return this;
2516         }
2517 
2518         /**
2519          * Sets the extension for the given key. If the value is null or the
2520          * empty string, the extension is removed.  Otherwise, the extension
2521          * must be <a href="./Locale.html#def_extensions">well-formed</a> or an exception
2522          * is thrown.
2523          *
2524          * <p><b>Note:</b> The key {@link Locale#UNICODE_LOCALE_EXTENSION
2525          * UNICODE_LOCALE_EXTENSION} ('u') is used for the Unicode locale extension.
2526          * Setting a value for this key replaces any existing Unicode locale key/type
2527          * pairs with those defined in the extension.
2528          *
2529          * <p><b>Note:</b> The key {@link Locale#PRIVATE_USE_EXTENSION
2530          * PRIVATE_USE_EXTENSION} ('x') is used for the private use code. To be
2531          * well-formed, the value for this key needs only to have subtags of one to
2532          * eight alphanumeric characters, not two to eight as in the general case.
2533          *
2534          * @param key the extension key
2535          * @param value the extension value
2536          * @return This builder.
2537          * @throws IllformedLocaleException if <code>key</code> is illegal
2538          * or <code>value</code> is ill-formed
2539          * @see #setUnicodeLocaleKeyword(String, String)
2540          */
2541         public Builder setExtension(char key, String value) {
2542             try {
2543                 localeBuilder.setExtension(key, value);
2544             } catch (LocaleSyntaxException e) {
2545                 throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex());
2546             }
2547             return this;
2548         }
2549 
2550         /**
2551          * Sets the Unicode locale keyword type for the given key.  If the type
2552          * is null, the Unicode keyword is removed.  Otherwise, the key must be
2553          * non-null and both key and type must be <a
2554          * href="./Locale.html#def_locale_extension">well-formed</a> or an exception
2555          * is thrown.
2556          *
2557          * <p>Keys and types are converted to lower case.
2558          *
2559          * <p><b>Note</b>:Setting the 'u' extension via {@link #setExtension}
2560          * replaces all Unicode locale keywords with those defined in the
2561          * extension.
2562          *
2563          * @param key the Unicode locale key
2564          * @param type the Unicode locale type
2565          * @return This builder.
2566          * @throws IllformedLocaleException if <code>key</code> or <code>type</code>
2567          * is ill-formed
2568          * @throws NullPointerException if <code>key</code> is null
2569          * @see #setExtension(char, String)
2570          */
2571         public Builder setUnicodeLocaleKeyword(String key, String type) {
2572             try {
2573                 localeBuilder.setUnicodeLocaleKeyword(key, type);
2574             } catch (LocaleSyntaxException e) {
2575                 throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex());
2576             }
2577             return this;
2578         }
2579 
2580         /**
2581          * Adds a unicode locale attribute, if not already present, otherwise
2582          * has no effect.  The attribute must not be null and must be <a
2583          * href="./Locale.html#def_locale_extension">well-formed</a> or an exception
2584          * is thrown.
2585          *
2586          * @param attribute the attribute
2587          * @return This builder.
2588          * @throws NullPointerException if <code>attribute</code> is null
2589          * @throws IllformedLocaleException if <code>attribute</code> is ill-formed
2590          * @see #setExtension(char, String)
2591          */
2592         public Builder addUnicodeLocaleAttribute(String attribute) {
2593             try {
2594                 localeBuilder.addUnicodeLocaleAttribute(attribute);
2595             } catch (LocaleSyntaxException e) {
2596                 throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex());
2597             }
2598             return this;
2599         }
2600 
2601         /**
2602          * Removes a unicode locale attribute, if present, otherwise has no
2603          * effect.  The attribute must not be null and must be <a
2604          * href="./Locale.html#def_locale_extension">well-formed</a> or an exception
2605          * is thrown.
2606          *
2607          * <p>Attribute comparison for removal is case-insensitive.
2608          *
2609          * @param attribute the attribute
2610          * @return This builder.
2611          * @throws NullPointerException if <code>attribute</code> is null
2612          * @throws IllformedLocaleException if <code>attribute</code> is ill-formed
2613          * @see #setExtension(char, String)
2614          */
2615         public Builder removeUnicodeLocaleAttribute(String attribute) {
2616             try {
2617                 localeBuilder.removeUnicodeLocaleAttribute(attribute);
2618             } catch (LocaleSyntaxException e) {
2619                 throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex());
2620             }
2621             return this;
2622         }
2623 
2624         /**
2625          * Resets the builder to its initial, empty state.
2626          *
2627          * @return This builder.
2628          */
2629         public Builder clear() {
2630             localeBuilder.clear();
2631             return this;
2632         }
2633 
2634         /**
2635          * Resets the extensions to their initial, empty state.
2636          * Language, script, region and variant are unchanged.
2637          *
2638          * @return This builder.
2639          * @see #setExtension(char, String)
2640          */
2641         public Builder clearExtensions() {
2642             localeBuilder.clearExtensions();
2643             return this;
2644         }
2645 
2646         /**
2647          * Returns an instance of <code>Locale</code> created from the fields set
2648          * on this builder.
2649          *
2650          * <p>This applies the conversions listed in {@link Locale#forLanguageTag}
2651          * when constructing a Locale. (Grandfathered tags are handled in
2652          * {@link #setLanguageTag}.)
2653          *
2654          * @return A Locale.
2655          */
2656         public Locale build() {
2657             BaseLocale baseloc = localeBuilder.getBaseLocale();
2658             LocaleExtensions extensions = localeBuilder.getLocaleExtensions();
2659             if (extensions == null && baseloc.getVariant().length() > 0) {
2660                 extensions = getCompatibilityExtensions(baseloc.getLanguage(), baseloc.getScript(),
2661                         baseloc.getRegion(), baseloc.getVariant());
2662             }
2663             return Locale.getInstance(baseloc, extensions);
2664         }
2665     }
2666 
2667     /**
2668      * This enum provides constants to select a filtering mode for locale
2669      * matching. Refer to <a href="http://tools.ietf.org/html/rfc4647">RFC 4647
2670      * Matching of Language Tags</a> for details.
2671      *
2672      * <p>As an example, think of two Language Priority Lists each of which
2673      * includes only one language range and a set of following language tags:
2674      *
2675      * <pre>
2676      *    de (German)
2677      *    de-DE (German, Germany)
2678      *    de-Deva (German, in Devanagari script)
2679      *    de-Deva-DE (German, in Devanagari script, Germany)
2680      *    de-DE-1996 (German, Germany, orthography of 1996)
2681      *    de-Latn-DE (German, in Latin script, Germany)
2682      *    de-Latn-DE-1996 (German, in Latin script, Germany, orthography of 1996)
2683      * </pre>
2684      *
2685      * The filtering method will behave as follows:
2686      *
2687      * <table cellpadding=2 summary="Filtering method behavior">
2688      * <tr>
2689      * <th>Filtering Mode</th>
2690      * <th>Language Priority List: {@code "de-DE"}</th>
2691      * <th>Language Priority List: {@code "de-*-DE"}</th>
2692      * </tr>
2693      * <tr>
2694      * <td valign=top>
2695      * {@link FilteringMode#AUTOSELECT_FILTERING AUTOSELECT_FILTERING}
2696      * </td>
2697      * <td valign=top>
2698      * Performs <em>basic</em> filtering and returns {@code "de-DE"} and
2699      * {@code "de-DE-1996"}.
2700      * </td>
2701      * <td valign=top>
2702      * Performs <em>extended</em> filtering and returns {@code "de-DE"},
2703      * {@code "de-Deva-DE"}, {@code "de-DE-1996"}, {@code "de-Latn-DE"}, and
2704      * {@code "de-Latn-DE-1996"}.
2705      * </td>
2706      * </tr>
2707      * <tr>
2708      * <td valign=top>
2709      * {@link FilteringMode#EXTENDED_FILTERING EXTENDED_FILTERING}
2710      * </td>
2711      * <td valign=top>
2712      * Performs <em>extended</em> filtering and returns {@code "de-DE"},
2713      * {@code "de-Deva-DE"}, {@code "de-DE-1996"}, {@code "de-Latn-DE"}, and
2714      * {@code "de-Latn-DE-1996"}.
2715      * </td>
2716      * <td valign=top>Same as above.</td>
2717      * </tr>
2718      * <tr>
2719      * <td valign=top>
2720      * {@link FilteringMode#IGNORE_EXTENDED_RANGES IGNORE_EXTENDED_RANGES}
2721      * </td>
2722      * <td valign=top>
2723      * Performs <em>basic</em> filtering and returns {@code "de-DE"} and
2724      * {@code "de-DE-1996"}.
2725      * </td>
2726      * <td valign=top>
2727      * Performs <em>basic</em> filtering and returns {@code null} because
2728      * nothing matches.
2729      * </td>
2730      * </tr>
2731      * <tr>
2732      * <td valign=top>
2733      * {@link FilteringMode#MAP_EXTENDED_RANGES MAP_EXTENDED_RANGES}
2734      * </td>
2735      * <td valign=top>Same as above.</td>
2736      * <td valign=top>
2737      * Performs <em>basic</em> filtering and returns {@code "de-DE"} and
2738      * {@code "de-DE-1996"} because {@code "de-*-DE"} is mapped to
2739      * {@code "de-DE"}.
2740      * </td>
2741      * </tr>
2742      * <tr>
2743      * <td valign=top>
2744      * {@link FilteringMode#REJECT_EXTENDED_RANGES REJECT_EXTENDED_RANGES}
2745      * </td>
2746      * <td valign=top>Same as above.</td>
2747      * <td valign=top>
2748      * Throws {@link IllegalArgumentException} because {@code "de-*-DE"} is
2749      * not a valid basic language range.
2750      * </td>
2751      * </tr>
2752      * </table>
2753      *
2754      * @see #filter(List, Collection, FilteringMode)
2755      * @see #filterTags(List, Collection, FilteringMode)
2756      *
2757      * @since 1.8
2758      */
2759     public static enum FilteringMode {
2760         /**
2761          * Specifies automatic filtering mode based on the given Language
2762          * Priority List consisting of language ranges. If all of the ranges
2763          * are basic, basic filtering is selected. Otherwise, extended
2764          * filtering is selected.
2765          */
2766         AUTOSELECT_FILTERING,
2767 
2768         /**
2769          * Specifies extended filtering.
2770          */
2771         EXTENDED_FILTERING,
2772 
2773         /**
2774          * Specifies basic filtering: Note that any extended language ranges
2775          * included in the given Language Priority List are ignored.
2776          */
2777         IGNORE_EXTENDED_RANGES,
2778 
2779         /**
2780          * Specifies basic filtering: If any extended language ranges are
2781          * included in the given Language Priority List, they are mapped to the
2782          * basic language range. Specifically, a language range starting with a
2783          * subtag {@code "*"} is treated as a language range {@code "*"}. For
2784          * example, {@code "*-US"} is treated as {@code "*"}. If {@code "*"} is
2785          * not the first subtag, {@code "*"} and extra {@code "-"} are removed.
2786          * For example, {@code "ja-*-JP"} is mapped to {@code "ja-JP"}.
2787          */
2788         MAP_EXTENDED_RANGES,
2789 
2790         /**
2791          * Specifies basic filtering: If any extended language ranges are
2792          * included in the given Language Priority List, the list is rejected
2793          * and the filtering method throws {@link IllegalArgumentException}.
2794          */
2795         REJECT_EXTENDED_RANGES
2796     };
2797 
2798     /**
2799      * This class expresses a <em>Language Range</em> defined in
2800      * <a href="http://tools.ietf.org/html/rfc4647">RFC 4647 Matching of
2801      * Language Tags</a>. A language range is an identifier which is used to
2802      * select language tag(s) meeting specific requirements by using the
2803      * mechanisms described in <a href="Locale.html#LocaleMatching">Locale
2804      * Matching</a>. A list which represents a user's preferences and consists
2805      * of language ranges is called a <em>Language Priority List</em>.
2806      *
2807      * <p>There are two types of language ranges: basic and extended. In RFC
2808      * 4647, the syntax of language ranges is expressed in
2809      * <a href="http://tools.ietf.org/html/rfc4234">ABNF</a> as follows:
2810      * <blockquote>
2811      * <pre>
2812      *     basic-language-range    = (1*8ALPHA *("-" 1*8alphanum)) / "*"
2813      *     extended-language-range = (1*8ALPHA / "*")
2814      *                               *("-" (1*8alphanum / "*"))
2815      *     alphanum                = ALPHA / DIGIT
2816      * </pre>
2817      * </blockquote>
2818      * For example, {@code "en"} (English), {@code "ja-JP"} (Japanese, Japan),
2819      * {@code "*"} (special language range which matches any language tag) are
2820      * basic language ranges, whereas {@code "*-CH"} (any languages,
2821      * Switzerland), {@code "es-*"} (Spanish, any regions), and
2822      * {@code "zh-Hant-*"} (Traditional Chinese, any regions) are extended
2823      * language ranges.
2824      *
2825      * @see #filter
2826      * @see #filterTags
2827      * @see #lookup
2828      * @see #lookupTag
2829      *
2830      * @since 1.8
2831      */
2832     public static final class LanguageRange {
2833 
2834        /**
2835         * A constant holding the maximum value of weight, 1.0, which indicates
2836         * that the language range is a good fit for the user.
2837         */
2838         public static final double MAX_WEIGHT = 1.0;
2839 
2840        /**
2841         * A constant holding the minimum value of weight, 0.0, which indicates
2842         * that the language range is not a good fit for the user.
2843         */
2844         public static final double MIN_WEIGHT = 0.0;
2845 
2846         private final String range;
2847         private final double weight;
2848 
2849         private volatile int hash;
2850 
2851         /**
2852          * Constructs a {@code LanguageRange} using the given {@code range}.
2853          * Note that no validation is done against the IANA Language Subtag
2854          * Registry at time of construction.
2855          *
2856          * <p>This is equivalent to {@code LanguageRange(range, MAX_WEIGHT)}.
2857          *
2858          * @param range a language range
2859          * @throws NullPointerException if the given {@code range} is
2860          *     {@code null}
2861          * @throws IllegalArgumentException if the given {@code range} does not
2862          * comply with the syntax of the language range mentioned in RFC 4647
2863          */
2864         public LanguageRange(String range) {
2865             this(range, MAX_WEIGHT);
2866         }
2867 
2868         /**
2869          * Constructs a {@code LanguageRange} using the given {@code range} and
2870          * {@code weight}. Note that no validation is done against the IANA
2871          * Language Subtag Registry at time of construction.
2872          *
2873          * @param range  a language range
2874          * @param weight a weight value between {@code MIN_WEIGHT} and
2875          *     {@code MAX_WEIGHT}
2876          * @throws NullPointerException if the given {@code range} is
2877          *     {@code null}
2878          * @throws IllegalArgumentException if the given {@code range} does not
2879          * comply with the syntax of the language range mentioned in RFC 4647
2880          * or if the given {@code weight} is less than {@code MIN_WEIGHT}
2881          * or greater than {@code MAX_WEIGHT}
2882          */
2883         public LanguageRange(String range, double weight) {
2884             if (range == null) {
2885                 throw new NullPointerException();
2886             }
2887             if (weight < MIN_WEIGHT || weight > MAX_WEIGHT) {
2888                 throw new IllegalArgumentException("weight=" + weight);
2889             }
2890 
2891             range = range.toLowerCase(Locale.ROOT);
2892 
2893             // Do syntax check.
2894             boolean isIllFormed = false;
2895             String[] subtags = range.split("-");
2896             if (isSubtagIllFormed(subtags[0], true)
2897                 || range.endsWith("-")) {
2898                 isIllFormed = true;
2899             } else {
2900                 for (int i = 1; i < subtags.length; i++) {
2901                     if (isSubtagIllFormed(subtags[i], false)) {
2902                         isIllFormed = true;
2903                         break;
2904                     }
2905                 }
2906             }
2907             if (isIllFormed) {
2908                 throw new IllegalArgumentException("range=" + range);
2909             }
2910 
2911             this.range = range;
2912             this.weight = weight;
2913         }
2914 
2915         private static boolean isSubtagIllFormed(String subtag,
2916                                                  boolean isFirstSubtag) {
2917             if (subtag.equals("") || subtag.length() > 8) {
2918                 return true;
2919             } else if (subtag.equals("*")) {
2920                 return false;
2921             }
2922             char[] charArray = subtag.toCharArray();
2923             if (isFirstSubtag) { // ALPHA
2924                 for (char c : charArray) {
2925                     if (c < 'a' || c > 'z') {
2926                         return true;
2927                     }
2928                 }
2929             } else { // ALPHA / DIGIT
2930                 for (char c : charArray) {
2931                     if (c < '0' || (c > '9' && c < 'a') || c > 'z') {
2932                         return true;
2933                     }
2934                 }
2935             }
2936             return false;
2937         }
2938 
2939         /**
2940          * Returns the language range of this {@code LanguageRange}.
2941          *
2942          * @return the language range.
2943          */
2944         public String getRange() {
2945             return range;
2946         }
2947 
2948         /**
2949          * Returns the weight of this {@code LanguageRange}.
2950          *
2951          * @return the weight value.
2952          */
2953         public double getWeight() {
2954             return weight;
2955         }
2956 
2957         /**
2958          * Parses the given {@code ranges} to generate a Language Priority List.
2959          *
2960          * <p>This method performs a syntactic check for each language range in
2961          * the given {@code ranges} but doesn't do validation using the IANA
2962          * Language Subtag Registry.
2963          *
2964          * <p>The {@code ranges} to be given can take one of the following
2965          * forms:
2966          *
2967          * <pre>
2968          *   "Accept-Language: ja,en;q=0.4"  (weighted list with Accept-Language prefix)
2969          *   "ja,en;q=0.4"                   (weighted list)
2970          *   "ja,en"                         (prioritized list)
2971          * </pre>
2972          *
2973          * In a weighted list, each language range is given a weight value.
2974          * The weight value is identical to the "quality value" in
2975          * <a href="http://tools.ietf.org/html/rfc2616">RFC 2616</a>, and it
2976          * expresses how much the user prefers  the language. A weight value is
2977          * specified after a corresponding language range followed by
2978          * {@code ";q="}, and the default weight value is {@code MAX_WEIGHT}
2979          * when it is omitted.
2980          *
2981          * <p>Unlike a weighted list, language ranges in a prioritized list
2982          * are sorted in the descending order based on its priority. The first
2983          * language range has the highest priority and meets the user's
2984          * preference most.
2985          *
2986          * <p>In either case, language ranges are sorted in descending order in
2987          * the Language Priority List based on priority or weight. If a
2988          * language range appears in the given {@code ranges} more than once,
2989          * only the first one is included on the Language Priority List.
2990          *
2991          * <p>The returned list consists of language ranges from the given
2992          * {@code ranges} and their equivalents found in the IANA Language
2993          * Subtag Registry. For example, if the given {@code ranges} is
2994          * {@code "Accept-Language: iw,en-us;q=0.7,en;q=0.3"}, the elements in
2995          * the list to be returned are:
2996          *
2997          * <pre>
2998          *  <b>Range</b>                                   <b>Weight</b>
2999          *    "iw" (older tag for Hebrew)             1.0
3000          *    "he" (new preferred code for Hebrew)    1.0
3001          *    "en-us" (English, United States)        0.7
3002          *    "en" (English)                          0.3
3003          * </pre>
3004          *
3005          * Two language ranges, {@code "iw"} and {@code "he"}, have the same
3006          * highest priority in the list. By adding {@code "he"} to the user's
3007          * Language Priority List, locale-matching method can find Hebrew as a
3008          * matching locale (or language tag) even if the application or system
3009          * offers only {@code "he"} as a supported locale (or language tag).
3010          *
3011          * @param ranges a list of comma-separated language ranges or a list of
3012          *     language ranges in the form of the "Accept-Language" header
3013          *     defined in <a href="http://tools.ietf.org/html/rfc2616">RFC
3014          *     2616</a>
3015          * @return a Language Priority List consisting of language ranges
3016          *     included in the given {@code ranges} and their equivalent
3017          *     language ranges if available. The list is modifiable.
3018          * @throws NullPointerException if {@code ranges} is null
3019          * @throws IllegalArgumentException if a language range or a weight
3020          *     found in the given {@code ranges} is ill-formed
3021          */
3022         public static List<LanguageRange> parse(String ranges) {
3023             return LocaleMatcher.parse(ranges);
3024         }
3025 
3026         /**
3027          * Parses the given {@code ranges} to generate a Language Priority
3028          * List, and then customizes the list using the given {@code map}.
3029          * This method is equivalent to
3030          * {@code mapEquivalents(parse(ranges), map)}.
3031          *
3032          * @param ranges a list of comma-separated language ranges or a list
3033          *     of language ranges in the form of the "Accept-Language" header
3034          *     defined in <a href="http://tools.ietf.org/html/rfc2616">RFC
3035          *     2616</a>
3036          * @param map a map containing information to customize language ranges
3037          * @return a Language Priority List with customization. The list is
3038          *     modifiable.
3039          * @throws NullPointerException if {@code ranges} is null
3040          * @throws IllegalArgumentException if a language range or a weight
3041          *     found in the given {@code ranges} is ill-formed
3042          * @see #parse(String)
3043          * @see #mapEquivalents
3044          */
3045         public static List<LanguageRange> parse(String ranges,
3046                                                 Map<String, List<String>> map) {
3047             return mapEquivalents(parse(ranges), map);
3048         }
3049 
3050         /**
3051          * Generates a new customized Language Priority List using the given
3052          * {@code priorityList} and {@code map}. If the given {@code map} is
3053          * empty, this method returns a copy of the given {@code priorityList}.
3054          *
3055          * <p>In the map, a key represents a language range whereas a value is
3056          * a list of equivalents of it. {@code '*'} cannot be used in the map.
3057          * Each equivalent language range has the same weight value as its
3058          * original language range.
3059          *
3060          * <pre>
3061          *  An example of map:
3062          *    <b>Key</b>                            <b>Value</b>
3063          *      "zh" (Chinese)                 "zh",
3064          *                                     "zh-Hans"(Simplified Chinese)
3065          *      "zh-HK" (Chinese, Hong Kong)   "zh-HK"
3066          *      "zh-TW" (Chinese, Taiwan)      "zh-TW"
3067          * </pre>
3068          *
3069          * The customization is performed after modification using the IANA
3070          * Language Subtag Registry.
3071          *
3072          * <p>For example, if a user's Language Priority List consists of five
3073          * language ranges ({@code "zh"}, {@code "zh-CN"}, {@code "en"},
3074          * {@code "zh-TW"}, and {@code "zh-HK"}), the newly generated Language
3075          * Priority List which is customized using the above map example will
3076          * consists of {@code "zh"}, {@code "zh-Hans"}, {@code "zh-CN"},
3077          * {@code "zh-Hans-CN"}, {@code "en"}, {@code "zh-TW"}, and
3078          * {@code "zh-HK"}.
3079          *
3080          * <p>{@code "zh-HK"} and {@code "zh-TW"} aren't converted to
3081          * {@code "zh-Hans-HK"} nor {@code "zh-Hans-TW"} even if they are
3082          * included in the Language Priority List. In this example, mapping
3083          * is used to clearly distinguish Simplified Chinese and Traditional
3084          * Chinese.
3085          *
3086          * <p>If the {@code "zh"}-to-{@code "zh"} mapping isn't included in the
3087          * map, a simple replacement will be performed and the customized list
3088          * won't include {@code "zh"} and {@code "zh-CN"}.
3089          *
3090          * @param priorityList user's Language Priority List
3091          * @param map a map containing information to customize language ranges
3092          * @return a new Language Priority List with customization. The list is
3093          *     modifiable.
3094          * @throws NullPointerException if {@code priorityList} is {@code null}
3095          * @see #parse(String, Map)
3096          */
3097         public static List<LanguageRange> mapEquivalents(
3098                                               List<LanguageRange>priorityList,
3099                                               Map<String, List<String>> map) {
3100             return LocaleMatcher.mapEquivalents(priorityList, map);
3101         }
3102 
3103         /**
3104          * Returns a hash code value for the object.
3105          *
3106          * @return  a hash code value for this object.
3107          */
3108         @Override
3109         public int hashCode() {
3110             int h = hash;
3111             if (h == 0) {
3112                 h = 17;
3113                 h = 37*h + range.hashCode();
3114                 long bitsWeight = Double.doubleToLongBits(weight);
3115                 h = 37*h + (int)(bitsWeight ^ (bitsWeight >>> 32));
3116                 if (h != 0) {
3117                     hash = h;
3118                 }
3119             }
3120             return h;
3121         }
3122 
3123         /**
3124          * Compares this object to the specified object. The result is true if
3125          * and only if the argument is not {@code null} and is a
3126          * {@code LanguageRange} object that contains the same {@code range}
3127          * and {@code weight} values as this object.
3128          *
3129          * @param obj the object to compare with
3130          * @return  {@code true} if this object's {@code range} and
3131          *     {@code weight} are the same as the {@code obj}'s; {@code false}
3132          *     otherwise.
3133          */
3134         @Override
3135         public boolean equals(Object obj) {
3136             if (this == obj) {
3137                 return true;
3138             }
3139             if (!(obj instanceof LanguageRange)) {
3140                 return false;
3141             }
3142             LanguageRange other = (LanguageRange)obj;
3143             return hash == other.hash
3144                    && range.equals(other.range)
3145                    && weight == other.weight;
3146         }
3147 
3148         /**
3149          * Returns an informative string representation of this {@code LanguageRange}
3150          * object, consisting of language range and weight if the range is
3151          * weighted and the weight is less than the max weight.
3152          *
3153          * @return a string representation of this {@code LanguageRange} object.
3154          */
3155         @Override
3156         public String toString() {
3157             return (weight == MAX_WEIGHT) ? range : range + ";q=" + weight;
3158         }
3159     }
3160 
3161     /**
3162      * Returns a list of matching {@code Locale} instances using the filtering
3163      * mechanism defined in RFC 4647.
3164      *
3165      * @param priorityList user's Language Priority List in which each language
3166      *     tag is sorted in descending order based on priority or weight
3167      * @param locales {@code Locale} instances used for matching
3168      * @param mode filtering mode
3169      * @return a list of {@code Locale} instances for matching language tags
3170      *     sorted in descending order based on priority or weight, or an empty
3171      *     list if nothing matches. The list is modifiable.
3172      * @throws NullPointerException if {@code priorityList} or {@code locales}
3173      *     is {@code null}
3174      * @throws IllegalArgumentException if one or more extended language ranges
3175      *     are included in the given list when
3176      *     {@link FilteringMode#REJECT_EXTENDED_RANGES} is specified
3177      *
3178      * @since 1.8
3179      */
3180     public static List<Locale> filter(List<LanguageRange> priorityList,
3181                                       Collection<Locale> locales,
3182                                       FilteringMode mode) {
3183         return LocaleMatcher.filter(priorityList, locales, mode);
3184     }
3185 
3186     /**
3187      * Returns a list of matching {@code Locale} instances using the filtering
3188      * mechanism defined in RFC 4647. This is equivalent to
3189      * {@link #filter(List, Collection, FilteringMode)} when {@code mode} is
3190      * {@link FilteringMode#AUTOSELECT_FILTERING}.
3191      *
3192      * @param priorityList user's Language Priority List in which each language
3193      *     tag is sorted in descending order based on priority or weight
3194      * @param locales {@code Locale} instances used for matching
3195      * @return a list of {@code Locale} instances for matching language tags
3196      *     sorted in descending order based on priority or weight, or an empty
3197      *     list if nothing matches. The list is modifiable.
3198      * @throws NullPointerException if {@code priorityList} or {@code locales}
3199      *     is {@code null}
3200      *
3201      * @since 1.8
3202      */
3203     public static List<Locale> filter(List<LanguageRange> priorityList,
3204                                       Collection<Locale> locales) {
3205         return filter(priorityList, locales, FilteringMode.AUTOSELECT_FILTERING);
3206     }
3207 
3208     /**
3209      * Returns a list of matching languages tags using the basic filtering
3210      * mechanism defined in RFC 4647.
3211      *
3212      * @param priorityList user's Language Priority List in which each language
3213      *     tag is sorted in descending order based on priority or weight
3214      * @param tags language tags
3215      * @param mode filtering mode
3216      * @return a list of matching language tags sorted in descending order
3217      *     based on priority or weight, or an empty list if nothing matches.
3218      *     The list is modifiable.
3219      * @throws NullPointerException if {@code priorityList} or {@code tags} is
3220      *     {@code null}
3221      * @throws IllegalArgumentException if one or more extended language ranges
3222      *     are included in the given list when
3223      *     {@link FilteringMode#REJECT_EXTENDED_RANGES} is specified
3224      *
3225      * @since 1.8
3226      */
3227     public static List<String> filterTags(List<LanguageRange> priorityList,
3228                                           Collection<String> tags,
3229                                           FilteringMode mode) {
3230         return LocaleMatcher.filterTags(priorityList, tags, mode);
3231     }
3232 
3233     /**
3234      * Returns a list of matching languages tags using the basic filtering
3235      * mechanism defined in RFC 4647. This is equivalent to
3236      * {@link #filterTags(List, Collection, FilteringMode)} when {@code mode}
3237      * is {@link FilteringMode#AUTOSELECT_FILTERING}.
3238      *
3239      * @param priorityList user's Language Priority List in which each language
3240      *     tag is sorted in descending order based on priority or weight
3241      * @param tags language tags
3242      * @return a list of matching language tags sorted in descending order
3243      *     based on priority or weight, or an empty list if nothing matches.
3244      *     The list is modifiable.
3245      * @throws NullPointerException if {@code priorityList} or {@code tags} is
3246      *     {@code null}
3247      *
3248      * @since 1.8
3249      */
3250     public static List<String> filterTags(List<LanguageRange> priorityList,
3251                                           Collection<String> tags) {
3252         return filterTags(priorityList, tags, FilteringMode.AUTOSELECT_FILTERING);
3253     }
3254 
3255     /**
3256      * Returns a {@code Locale} instance for the best-matching language
3257      * tag using the lookup mechanism defined in RFC 4647.
3258      *
3259      * @param priorityList user's Language Priority List in which each language
3260      *     tag is sorted in descending order based on priority or weight
3261      * @param locales {@code Locale} instances used for matching
3262      * @return the best matching <code>Locale</code> instance chosen based on
3263      *     priority or weight, or {@code null} if nothing matches.
3264      * @throws NullPointerException if {@code priorityList} or {@code tags} is
3265      *     {@code null}
3266      *
3267      * @since 1.8
3268      */
3269     public static Locale lookup(List<LanguageRange> priorityList,
3270                                 Collection<Locale> locales) {
3271         return LocaleMatcher.lookup(priorityList, locales);
3272     }
3273 
3274     /**
3275      * Returns the best-matching language tag using the lookup mechanism
3276      * defined in RFC 4647.
3277      *
3278      * @param priorityList user's Language Priority List in which each language
3279      *     tag is sorted in descending order based on priority or weight
3280      * @param tags language tangs used for matching
3281      * @return the best matching language tag chosen based on priority or
3282      *     weight, or {@code null} if nothing matches.
3283      * @throws NullPointerException if {@code priorityList} or {@code tags} is
3284      *     {@code null}
3285      *
3286      * @since 1.8
3287      */
3288     public static String lookupTag(List<LanguageRange> priorityList,
3289                                    Collection<String> tags) {
3290         return LocaleMatcher.lookupTag(priorityList, tags);
3291     }
3292 
3293 }