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