jdk/src/share/classes/java/util/Locale.java
author okutsu
Thu, 14 Apr 2011 15:59:47 +0900
changeset 9224 75c0420badef
parent 8780 c88e159a3e5c
child 9232 9e29d6359705
permissions -rw-r--r--
7028818: (lc) Lazily initialize locale extension 7029740: (lc) New Locale class implementation doesn't follow the Java coding conventions 7032820: (lc) sun.util.locale.InternalLocaleBuilder.CaseInsensitiveChar.equals problems 7033503: (lc) Restore optimization code for Locale class initialization 7033504: (lc) incompatible behavior change for ja_JP_JP and th_TH_TH locales Reviewed-by: naoto
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
     2
 * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * The original version of this source code and documentation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * is copyrighted and owned by Taligent, Inc., a wholly-owned
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * subsidiary of IBM. These materials are provided under terms
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * of a License Agreement between Taligent and Sun. This technology
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * is protected by multiple US and International patents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * This notice and attribution to Taligent may not be removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Taligent is a registered trademark of Taligent, Inc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
package java.util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
    43
import java.io.IOException;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
    44
import java.io.ObjectInputStream;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
    45
import java.io.ObjectOutputStream;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
    46
import java.io.ObjectStreamField;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
    47
import java.io.Serializable;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.text.MessageFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import java.util.spi.LocaleNameProvider;
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
    51
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import sun.security.action.GetPropertyAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import sun.util.LocaleServiceProviderPool;
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
    54
import sun.util.locale.BaseLocale;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
    55
import sun.util.locale.InternalLocaleBuilder;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
    56
import sun.util.locale.LanguageTag;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
    57
import sun.util.locale.LocaleExtensions;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
    58
import sun.util.locale.LocaleObjectCache;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
    59
import sun.util.locale.LocaleSyntaxException;
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
    60
import sun.util.locale.LocaleUtils;
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
    61
import sun.util.locale.ParseStatus;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
    62
import sun.util.locale.UnicodeLocaleExtension;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
import sun.util.resources.LocaleData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
import sun.util.resources.OpenListResourceBundle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * A <code>Locale</code> object represents a specific geographical, political,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * or cultural region. An operation that requires a <code>Locale</code> to perform
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * its task is called <em>locale-sensitive</em> and uses the <code>Locale</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * to tailor information for the user. For example, displaying a number
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
    71
 * is a locale-sensitive operation&mdash; the number should be formatted
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
    72
 * according to the customs and conventions of the user's native country,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * region, or culture.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
    75
 * <p> The <code>Locale</code> class implements identifiers
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
    76
 * interchangeable with BCP 47 (IETF BCP 47, "Tags for Identifying
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
    77
 * Languages"), with support for the LDML (UTS#35, "Unicode Locale
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
    78
 * Data Markup Language") BCP 47-compatible extensions for locale data
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
    79
 * exchange.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
    80
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
    81
 * <p> A <code>Locale</code> object logically consists of the fields
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
    82
 * described below.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
    83
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
    84
 * <dl>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
    85
 *   <dt><a name="def_language"/><b>language</b></dt>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
    86
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
    87
 *   <dd>ISO 639 alpha-2 or alpha-3 language code, or registered
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
    88
 *   language subtags up to 8 alpha letters (for future enhancements).
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
    89
 *   When a language has both an alpha-2 code and an alpha-3 code, the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
    90
 *   alpha-2 code must be used.  You can find a full list of valid
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
    91
 *   language codes in the IANA Language Subtag Registry (search for
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
    92
 *   "Type: language").  The language field is case insensitive, but
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
    93
 *   <code>Locale</code> always canonicalizes to lower case.</dd><br>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
    94
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
    95
 *   <dd>Well-formed language values have the form
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
    96
 *   <code>[a-zA-Z]{2,8}</code>.  Note that this is not the the full
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
    97
 *   BCP47 language production, since it excludes extlang.  They are
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
    98
 *   not needed since modern three-letter language codes replace
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
    99
 *   them.</dd><br>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   100
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   101
 *   <dd>Example: "en" (English), "ja" (Japanese), "kok" (Konkani)</dd><br>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   102
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   103
 *   <dt><a name="def_script"/><b>script</b></dt>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   104
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   105
 *   <dd>ISO 15924 alpha-4 script code.  You can find a full list of
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   106
 *   valid script codes in the IANA Language Subtag Registry (search
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   107
 *   for "Type: script").  The script field is case insensitive, but
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   108
 *   <code>Locale</code> always canonicalizes to title case (the first
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   109
 *   letter is upper case and the rest of the letters are lower
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   110
 *   case).</dd><br>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   111
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   112
 *   <dd>Well-formed script values have the form
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   113
 *   <code>[a-zA-Z]{4}</code></dd><br>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   114
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   115
 *   <dd>Example: "Latn" (Latin), "Cyrl" (Cyrillic)</dd><br>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   116
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   117
 *   <dt><a name="def_region"/><b>country (region)</b></dt>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   118
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   119
 *   <dd>ISO 3166 alpha-2 country code or UN M.49 numeric-3 area code.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   120
 *   You can find a full list of valid country and region codes in the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   121
 *   IANA Language Subtag Registry (search for "Type: region").  The
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   122
 *   country (region) field is case insensitive, but
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   123
 *   <code>Locale</code> always canonicalizes to upper case.</dd><br>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   124
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   125
 *   <dd>Well-formed country/region values have
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   126
 *   the form <code>[a-zA-Z]{2} | [0-9]{3}</code></dd><br>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   127
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   128
 *   <dd>Example: "US" (United States), "FR" (France), "029"
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   129
 *   (Caribbean)</dd><br>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   130
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   131
 *   <dt><a name="def_variant"/><b>variant</b></dt>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   132
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   133
 *   <dd>Any arbitrary value used to indicate a variation of a
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   134
 *   <code>Locale</code>.  Where there are two or more variant values
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   135
 *   each indicating its own semantics, these values should be ordered
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   136
 *   by importance, with most important first, separated by
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   137
 *   underscore('_').  The variant field is case sensitive.</dd><br>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   138
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   139
 *   <dd>Note: IETF BCP 47 places syntactic restrictions on variant
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   140
 *   subtags.  Also BCP 47 subtags are strictly used to indicate
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   141
 *   additional variations that define a language or its dialects that
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   142
 *   are not covered by any combinations of language, script and
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   143
 *   region subtags.  You can find a full list of valid variant codes
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   144
 *   in the IANA Language Subtag Registry (search for "Type: variant").
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   145
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   146
 *   <p>However, the variant field in <code>Locale</code> has
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   147
 *   historically been used for any kind of variation, not just
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   148
 *   language variations.  For example, some supported variants
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   149
 *   available in Java SE Runtime Environments indicate alternative
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   150
 *   cultural behaviors such as calendar type or number script.  In
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   151
 *   BCP 47 this kind of information, which does not identify the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   152
 *   language, is supported by extension subtags or private use
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   153
 *   subtags.</dd><br>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   154
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   155
 *   <dd>Well-formed variant values have the form <code>SUBTAG
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   156
 *   (('_'|'-') SUBTAG)*</code> where <code>SUBTAG =
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   157
 *   [0-9][0-9a-zA-Z]{3} | [0-9a-zA-Z]{5,8}</code>. (Note: BCP 47 only
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   158
 *   uses hyphen ('-') as a delimiter, this is more lenient).</dd><br>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   159
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   160
 *   <dd>Example: "polyton" (Polytonic Greek), "POSIX"</dd><br>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   161
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   162
 *   <dt><a name="def_extensions"/><b>extensions</b></dt>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   163
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   164
 *   <dd>A map from single character keys to string values, indicating
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   165
 *   extensions apart from language identification.  The extensions in
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   166
 *   <code>Locale</code> implement the semantics and syntax of BCP 47
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   167
 *   extension subtags and private use subtags. The extensions are
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   168
 *   case insensitive, but <code>Locale</code> canonicalizes all
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   169
 *   extension keys and values to lower case. Note that extensions
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   170
 *   cannot have empty values.</dd><br>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   171
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   172
 *   <dd>Well-formed keys are single characters from the set
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   173
 *   <code>[0-9a-zA-Z]</code>.  Well-formed values have the form
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   174
 *   <code>SUBTAG ('-' SUBTAG)*</code> where for the key 'x'
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   175
 *   <code>SUBTAG = [0-9a-zA-Z]{1,8}</code> and for other keys
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   176
 *   <code>SUBTAG = [0-9a-zA-Z]{2,8}</code> (that is, 'x' allows
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   177
 *   single-character subtags).</dd><br>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   178
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   179
 *   <dd>Example: key="u"/value="ca-japanese" (Japanese Calendar),
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   180
 *   key="x"/value="java-1-7"</dd>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   181
 * </dl>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   182
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   183
 * <b>Note:</b> Although BCP 47 requires field values to be registered
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   184
 * in the IANA Language Subtag Registry, the <code>Locale</code> class
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   185
 * does not provide any validation features.  The <code>Builder</code>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   186
 * only checks if an individual field satisfies the syntactic
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   187
 * requirement (is well-formed), but does not validate the value
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   188
 * itself.  See {@link Builder} for details.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   189
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   190
 * <h4><a name="def_locale_extension">Unicode locale/language extension</h4>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   191
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   192
 * <p>UTS#35, "Unicode Locale Data Markup Language" defines optional
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   193
 * attributes and keywords to override or refine the default behavior
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   194
 * associated with a locale.  A keyword is represented by a pair of
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   195
 * key and type.  For example, "nu-thai" indicates that Thai local
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   196
 * digits (value:"thai") should be used for formatting numbers
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   197
 * (key:"nu").
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   198
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   199
 * <p>The keywords are mapped to a BCP 47 extension value using the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   200
 * extension key 'u' ({@link #UNICODE_LOCALE_EXTENSION}).  The above
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   201
 * example, "nu-thai", becomes the extension "u-nu-thai".code
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   202
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   203
 * <p>Thus, when a <code>Locale</code> object contains Unicode locale
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   204
 * attributes and keywords,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   205
 * <code>getExtension(UNICODE_LOCALE_EXTENSION)</code> will return a
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   206
 * String representing this information, for example, "nu-thai".  The
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   207
 * <code>Locale</code> class also provides {@link
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   208
 * #getUnicodeLocaleAttributes}, {@link #getUnicodeLocaleKeys}, and
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   209
 * {@link #getUnicodeLocaleType} which allow you to access Unicode
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   210
 * locale attributes and key/type pairs directly.  When represented as
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   211
 * a string, the Unicode Locale Extension lists attributes
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   212
 * alphabetically, followed by key/type sequences with keys listed
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   213
 * alphabetically (the order of subtags comprising a key's type is
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   214
 * fixed when the type is defined)
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   215
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   216
 * <p>A well-formed locale key has the form
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   217
 * <code>[0-9a-zA-Z]{2}</code>.  A well-formed locale type has the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   218
 * form <code>"" | [0-9a-zA-Z]{3,8} ('-' [0-9a-zA-Z]{3,8})*</code> (it
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   219
 * can be empty, or a series of subtags 3-8 alphanums in length).  A
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   220
 * well-formed locale attribute has the form
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   221
 * <code>[0-9a-zA-Z]{3,8}</code> (it is a single subtag with the same
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   222
 * form as a locale type subtag).
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   223
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   224
 * <p>The Unicode locale extension specifies optional behavior in
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   225
 * locale-sensitive services.  Although the LDML specification defines
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   226
 * various keys and values, actual locale-sensitive service
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   227
 * implementations in a Java Runtime Environment might not support any
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   228
 * particular Unicode locale attributes or key/type pairs.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   229
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   230
 * <h4>Creating a Locale</h4>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   231
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   232
 * <p>There are several different ways to create a <code>Locale</code>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   233
 * object.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   234
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   235
 * <h5>Builder</h5>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   236
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   237
 * <p>Using {@link Builder} you can construct a <code>Locale</code> object
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   238
 * that conforms to BCP 47 syntax.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   239
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   240
 * <h5>Constructors</h5>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   241
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   242
 * <p>The <code>Locale</code> class provides three constructors:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
 * <pre>
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   245
 *     {@link #Locale(String language)}
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   246
 *     {@link #Locale(String language, String country)}
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   247
 *     {@link #Locale(String language, String country, String variant)}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
 * </blockquote>
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   250
 * These constructors allow you to create a <code>Locale</code> object
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   251
 * with language, country and variant, but you cannot specify
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   252
 * script or extensions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
 *
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   254
 * <h5>Factory Methods</h5>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
 *
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   256
 * <p>The method {@link #forLanguageTag} creates a <code>Locale</code>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   257
 * object for a well-formed BCP 47 language tag.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
 *
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   259
 * <h5>Locale Constants</h5>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
 *
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   261
 * <p>The <code>Locale</code> class provides a number of convenient constants
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
 * that you can use to create <code>Locale</code> objects for commonly used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
 * locales. For example, the following creates a <code>Locale</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
 * for the United States:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
 * <pre>
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   267
 *     Locale.US
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
 *
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   271
 * <h4>Use of Locale</h4>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   272
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   273
 * <p>Once you've created a <code>Locale</code> you can query it for information
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   274
 * about itself. Use <code>getCountry</code> to get the country (or region)
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   275
 * code and <code>getLanguage</code> to get the language code.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   276
 * You can use <code>getDisplayCountry</code> to get the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
 * name of the country suitable for displaying to the user. Similarly,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
 * you can use <code>getDisplayLanguage</code> to get the name of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
 * the language suitable for displaying to the user. Interestingly,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
 * the <code>getDisplayXXX</code> methods are themselves locale-sensitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
 * and have two versions: one that uses the default locale and one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
 * that uses the locale specified as an argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
 *
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   284
 * <p>The Java Platform provides a number of classes that perform locale-sensitive
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
 * operations. For example, the <code>NumberFormat</code> class formats
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   286
 * numbers, currency, and percentages in a locale-sensitive manner. Classes
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   287
 * such as <code>NumberFormat</code> have several convenience methods
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
 * for creating a default object of that type. For example, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
 * <code>NumberFormat</code> class provides these three convenience methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
 * for creating a default <code>NumberFormat</code> object:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
 * <pre>
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   293
 *     NumberFormat.getInstance()
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   294
 *     NumberFormat.getCurrencyInstance()
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   295
 *     NumberFormat.getPercentInstance()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
 * </blockquote>
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   298
 * Each of these methods has two variants; one with an explicit locale
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   299
 * and one without; the latter uses the default locale:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
 * <pre>
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   302
 *     NumberFormat.getInstance(myLocale)
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   303
 *     NumberFormat.getCurrencyInstance(myLocale)
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   304
 *     NumberFormat.getPercentInstance(myLocale)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
 * A <code>Locale</code> is the mechanism for identifying the kind of object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
 * (<code>NumberFormat</code>) that you would like to get. The locale is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
 * <STRONG>just</STRONG> a mechanism for identifying objects,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
 * <STRONG>not</STRONG> a container for the objects themselves.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
 *
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   312
 * <h4>Compatibility</h4>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   313
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   314
 * <p>In order to maintain compatibility with existing usage, Locale's
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   315
 * constructors retain their behavior prior to the Java Runtime
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   316
 * Environment version 1.7.  The same is largely true for the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   317
 * <code>toString</code> method. Thus Locale objects can continue to
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   318
 * be used as they were. In particular, clients who parse the output
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   319
 * of toString into language, country, and variant fields can continue
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   320
 * to do so (although this is strongly discouraged), although the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   321
 * variant field will have additional information in it if script or
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   322
 * extensions are present.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   323
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   324
 * <p>In addition, BCP 47 imposes syntax restrictions that are not
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   325
 * imposed by Locale's constructors. This means that conversions
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   326
 * between some Locales and BCP 47 language tags cannot be made without
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   327
 * losing information. Thus <code>toLanguageTag</code> cannot
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   328
 * represent the state of locales whose language, country, or variant
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   329
 * do not conform to BCP 47.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   330
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   331
 * <p>Because of these issues, it is recommended that clients migrate
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   332
 * away from constructing non-conforming locales and use the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   333
 * <code>forLanguageTag</code> and <code>Locale.Builder</code> APIs instead.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   334
 * Clients desiring a string representation of the complete locale can
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   335
 * then always rely on <code>toLanguageTag</code> for this purpose.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   336
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   337
 * <h5><a name="special_cases_constructor"/>Special cases</h5>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   338
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   339
 * <p>For compatibility reasons, two
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   340
 * non-conforming locales are treated as special cases.  These are
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   341
 * <b><tt>ja_JP_JP</tt></b> and <b><tt>th_TH_TH</tt></b>. These are ill-formed
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   342
 * in BCP 47 since the variants are too short. To ease migration to BCP 47,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   343
 * these are treated specially during construction.  These two cases (and only
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   344
 * these) cause a constructor to generate an extension, all other values behave
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   345
 * exactly as they did prior to Java 7.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   346
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   347
 * <p>Java has used <tt>ja_JP_JP</tt> to represent Japanese as used in
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   348
 * Japan together with the Japanese Imperial calendar. This is now
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   349
 * representable using a Unicode locale extension, by specifying the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   350
 * Unicode locale key <tt>ca</tt> (for "calendar") and type
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   351
 * <tt>japanese</tt>. When the Locale constructor is called with the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   352
 * arguments "ja", "JP", "JP", the extension "u-ca-japanese" is
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   353
 * automatically added.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   354
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   355
 * <p>Java has used <tt>th_TH_TH</tt> to represent Thai as used in
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   356
 * Thailand together with Thai digits. This is also now representable using
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   357
 * a Unicode locale extension, by specifying the Unicode locale key
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   358
 * <tt>nu</tt> (for "number") and value <tt>thai</tt>. When the Locale
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   359
 * constructor is called with the arguments "th", "TH", "TH", the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   360
 * extension "u-nu-thai" is automatically added.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   361
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   362
 * <h5>Serialization</h5>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   363
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   364
 * <p>During serialization, writeObject writes all fields to the output
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   365
 * stream, including extensions.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   366
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   367
 * <p>During deserialization, readResolve adds extensions as described
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   368
 * in <a href="#special_cases_constructor">Special Cases</a>, only
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   369
 * for the two cases th_TH_TH and ja_JP_JP.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   370
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   371
 * <h5>Legacy language codes</h5>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   372
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   373
 * <p>Locale's constructor has always converted three language codes to
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   374
 * their earlier, obsoleted forms: <tt>he</tt> maps to <tt>iw</tt>,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   375
 * <tt>yi</tt> maps to <tt>ji</tt>, and <tt>id</tt> maps to
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   376
 * <tt>in</tt>.  This continues to be the case, in order to not break
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   377
 * backwards compatibility.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   378
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   379
 * <p>The APIs added in 1.7 map between the old and new language codes,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   380
 * maintaining the old codes internal to Locale (so that
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   381
 * <code>getLanguage</code> and <code>toString</code> reflect the old
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   382
 * code), but using the new codes in the BCP 47 language tag APIs (so
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   383
 * that <code>toLanguageTag</code> reflects the new one). This
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   384
 * preserves the equivalence between Locales no matter which code or
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   385
 * API is used to construct them. Java's default resource bundle
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   386
 * lookup mechanism also implements this mapping, so that resources
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   387
 * can be named using either convention, see {@link ResourceBundle.Control}.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   388
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   389
 * <h5>Three-letter language/country(region) codes</h5>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   390
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   391
 * <p>The Locale constructors have always specified that the language
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   392
 * and the country param be two characters in length, although in
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   393
 * practice they have accepted any length.  The specification has now
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   394
 * been relaxed to allow language codes of two to eight characters and
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   395
 * country (region) codes of two to three characters, and in
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   396
 * particular, three-letter language codes and three-digit region
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   397
 * codes as specified in the IANA Language Subtag Registry.  For
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   398
 * compatibility, the implementation still does not impose a length
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   399
 * constraint.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   400
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   401
 * @see Builder
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   402
 * @see ResourceBundle
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   403
 * @see java.text.Format
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   404
 * @see java.text.NumberFormat
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   405
 * @see java.text.Collator
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   406
 * @author Mark Davis
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   407
 * @since 1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
public final class Locale implements Cloneable, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   411
    static private final  Cache LOCALECACHE = new Cache();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    /** Useful constant for language.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     */
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
   415
    static public final Locale ENGLISH = createConstant("en", "");
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   416
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   417
    /** Useful constant for language.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   418
     */
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
   419
    static public final Locale FRENCH = createConstant("fr", "");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    /** Useful constant for language.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     */
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
   423
    static public final Locale GERMAN = createConstant("de", "");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    /** Useful constant for language.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     */
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
   427
    static public final Locale ITALIAN = createConstant("it", "");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    /** Useful constant for language.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     */
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
   431
    static public final Locale JAPANESE = createConstant("ja", "");
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   432
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   433
    /** Useful constant for language.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   434
     */
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
   435
    static public final Locale KOREAN = createConstant("ko", "");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    /** Useful constant for language.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     */
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
   439
    static public final Locale CHINESE = createConstant("zh", "");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   441
    /** Useful constant for language.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     */
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
   443
    static public final Locale SIMPLIFIED_CHINESE = createConstant("zh", "CN");
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   444
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   445
    /** Useful constant for language.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   446
     */
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
   447
    static public final Locale TRADITIONAL_CHINESE = createConstant("zh", "TW");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    /** Useful constant for country.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     */
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
   451
    static public final Locale FRANCE = createConstant("fr", "FR");
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   452
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   453
    /** Useful constant for country.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   454
     */
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
   455
    static public final Locale GERMANY = createConstant("de", "DE");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    /** Useful constant for country.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     */
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
   459
    static public final Locale ITALY = createConstant("it", "IT");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    /** Useful constant for country.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     */
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
   463
    static public final Locale JAPAN = createConstant("ja", "JP");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    /** Useful constant for country.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     */
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
   467
    static public final Locale KOREA = createConstant("ko", "KR");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    /** Useful constant for country.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    static public final Locale CHINA = SIMPLIFIED_CHINESE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    /** Useful constant for country.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    static public final Locale PRC = SIMPLIFIED_CHINESE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    /** Useful constant for country.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    static public final Locale TAIWAN = TRADITIONAL_CHINESE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    /** Useful constant for country.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     */
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
   483
    static public final Locale UK = createConstant("en", "GB");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    /** Useful constant for country.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     */
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
   487
    static public final Locale US = createConstant("en", "US");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    /** Useful constant for country.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     */
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
   491
    static public final Locale CANADA = createConstant("en", "CA");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    /** Useful constant for country.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     */
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
   495
    static public final Locale CANADA_FRENCH = createConstant("fr", "CA");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * Useful constant for the root locale.  The root locale is the locale whose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * language, country, and variant are empty ("") strings.  This is regarded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * as the base locale of all locales, and is used as the language/country
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * neutral locale for the locale sensitive operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     */
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
   505
    static public final Locale ROOT = createConstant("", "");
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   506
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   507
    /**
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   508
     * The key for the private use extension ('x').
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   509
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   510
     * @see #getExtension(char)
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   511
     * @see Builder#setExtension(char, String)
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   512
     * @since 1.7
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   513
     */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   514
    static public final char PRIVATE_USE_EXTENSION = 'x';
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   515
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   516
    /**
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   517
     * The key for Unicode locale extension ('u').
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   518
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   519
     * @see #getExtension(char)
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   520
     * @see Builder#setExtension(char, String)
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   521
     * @since 1.7
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   522
     */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   523
    static public final char UNICODE_LOCALE_EXTENSION = 'u';
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    /** serialization ID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    static final long serialVersionUID = 9149081749638150636L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * Display types for retrieving localized names from the name providers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    private static final int DISPLAY_LANGUAGE = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    private static final int DISPLAY_COUNTRY  = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    private static final int DISPLAY_VARIANT  = 2;
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
   535
    private static final int DISPLAY_SCRIPT   = 3;
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   536
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   537
    /**
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   538
     * Private constructor used by getInstance method
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   539
     */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   540
    private Locale(BaseLocale baseLocale, LocaleExtensions extensions) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
   541
        this.baseLocale = baseLocale;
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
   542
        this.localeExtensions = extensions;
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   543
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    /**
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   546
     * Construct a locale from language, country and variant.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   547
     * This constructor normalizes the language value to lowercase and
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   548
     * the country value to uppercase.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   549
     * <p>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   550
     * <b>Note:</b>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   551
     * <ul>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   552
     * <li>ISO 639 is not a stable standard; some of the language codes it defines
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   553
     * (specifically "iw", "ji", and "in") have changed.  This constructor accepts both the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   554
     * old codes ("iw", "ji", and "in") and the new codes ("he", "yi", and "id"), but all other
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * API on Locale will return only the OLD codes.
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   556
     * <li>For backward compatibility reasons, this constructor does not make
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   557
     * any syntactic checks on the input.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   558
     * <li>The two cases ("ja", "JP", "JP") and ("th", "TH", "TH") are handled specially,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   559
     * see <a href="#special_cases_constructor">Special Cases</a> for more information.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   560
     * </ul>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   561
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   562
     * @param language An ISO 639 alpha-2 or alpha-3 language code, or a language subtag
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   563
     * up to 8 characters in length.  See the <code>Locale</code> class description about
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   564
     * valid language values.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   565
     * @param country An ISO 3166 alpha-2 country code or a UN M.49 numeric-3 area code.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   566
     * See the <code>Locale</code> class description about valid country values.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   567
     * @param variant Any arbitrary value used to indicate a variation of a <code>Locale</code>.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   568
     * See the <code>Locale</code> class description for the details.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * @exception NullPointerException thrown if any argument is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    public Locale(String language, String country, String variant) {
6655
b4130d85e450 6986612: pit jdk7 b112: java.util.Locale getDisplayVariant() sqe test getDisplayVariantTests.java fails
naoto
parents: 6501
diff changeset
   572
        if (language== null || country == null || variant == null) {
b4130d85e450 6986612: pit jdk7 b112: java.util.Locale getDisplayVariant() sqe test getDisplayVariantTests.java fails
naoto
parents: 6501
diff changeset
   573
            throw new NullPointerException();
b4130d85e450 6986612: pit jdk7 b112: java.util.Locale getDisplayVariant() sqe test getDisplayVariantTests.java fails
naoto
parents: 6501
diff changeset
   574
        }
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
   575
        baseLocale = BaseLocale.getInstance(convertOldISOCodes(language), "", country, variant);
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
   576
        localeExtensions = getCompatibilityExtensions(language, "", country, variant);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    /**
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   580
     * Construct a locale from language and country.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   581
     * This constructor normalizes the language value to lowercase and
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   582
     * the country value to uppercase.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   583
     * <p>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   584
     * <b>Note:</b>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   585
     * <ul>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   586
     * <li>ISO 639 is not a stable standard; some of the language codes it defines
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   587
     * (specifically "iw", "ji", and "in") have changed.  This constructor accepts both the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   588
     * old codes ("iw", "ji", and "in") and the new codes ("he", "yi", and "id"), but all other
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * API on Locale will return only the OLD codes.
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   590
     * <li>For backward compatibility reasons, this constructor does not make
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   591
     * any syntactic checks on the input.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   592
     * </ul>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   593
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   594
     * @param language An ISO 639 alpha-2 or alpha-3 language code, or a language subtag
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   595
     * up to 8 characters in length.  See the <code>Locale</code> class description about
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   596
     * valid language values.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   597
     * @param country An ISO 3166 alpha-2 country code or a UN M.49 numeric-3 area code.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   598
     * See the <code>Locale</code> class description about valid country values.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * @exception NullPointerException thrown if either argument is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    public Locale(String language, String country) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        this(language, country, "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * Construct a locale from a language code.
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   607
     * This constructor normalizes the language value to lowercase.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   608
     * <p>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   609
     * <b>Note:</b>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   610
     * <ul>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   611
     * <li>ISO 639 is not a stable standard; some of the language codes it defines
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   612
     * (specifically "iw", "ji", and "in") have changed.  This constructor accepts both the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   613
     * old codes ("iw", "ji", and "in") and the new codes ("he", "yi", and "id"), but all other
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * API on Locale will return only the OLD codes.
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   615
     * <li>For backward compatibility reasons, this constructor does not make
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   616
     * any syntactic checks on the input.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   617
     * </ul>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   618
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   619
     * @param language An ISO 639 alpha-2 or alpha-3 language code, or a language subtag
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   620
     * up to 8 characters in length.  See the <code>Locale</code> class description about
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   621
     * valid language values.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * @exception NullPointerException thrown if argument is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
    public Locale(String language) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        this(language, "", "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    /**
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
   630
     * This method must be called only for creating the Locale.*
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
   631
     * constants due to making shortcuts.
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
   632
     */
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
   633
    private static Locale createConstant(String lang, String country) {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
   634
        BaseLocale base = BaseLocale.createInstance(lang, country);
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
   635
        return getInstance(base, null);
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
   636
    }
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
   637
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
   638
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * Returns a <code>Locale</code> constructed from the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * <code>language</code>, <code>country</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * <code>variant</code>. If the same <code>Locale</code> instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * is available in the cache, then that instance is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * returned. Otherwise, a new <code>Locale</code> instance is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * created and cached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     *
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   646
     * @param language lowercase 2 to 8 language code.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   647
     * @param country uppercase two-letter ISO-3166 code and numric-3 UN M.49 area code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * @param variant vendor and browser specific code. See class description.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * @return the <code>Locale</code> instance requested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * @exception NullPointerException if any argument is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    static Locale getInstance(String language, String country, String variant) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
   653
        return getInstance(language, "", country, variant, null);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   654
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   655
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   656
    static Locale getInstance(String language, String script, String country,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   657
                                      String variant, LocaleExtensions extensions) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   658
        if (language== null || script == null || country == null || variant == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   662
        if (extensions == null) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   663
            extensions = getCompatibilityExtensions(language, script, country, variant);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   664
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   665
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   666
        BaseLocale baseloc = BaseLocale.getInstance(language, script, country, variant);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   667
        return getInstance(baseloc, extensions);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   668
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   669
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   670
    static Locale getInstance(BaseLocale baseloc, LocaleExtensions extensions) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   671
        LocaleKey key = new LocaleKey(baseloc, extensions);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   672
        return LOCALECACHE.get(key);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   673
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   674
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   675
    private static class Cache extends LocaleObjectCache<LocaleKey, Locale> {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
   676
        private Cache() {
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   677
        }
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
   678
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
   679
        @Override
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   680
        protected Locale createObject(LocaleKey key) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
   681
            return new Locale(key.base, key.exts);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   682
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   683
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   684
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
   685
    private static final class LocaleKey {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
   686
        private final BaseLocale base;
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
   687
        private final LocaleExtensions exts;
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
   688
        private final int hash;
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   689
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   690
        private LocaleKey(BaseLocale baseLocale, LocaleExtensions extensions) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
   691
            base = baseLocale;
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
   692
            exts = extensions;
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
   693
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
   694
            // Calculate the hash value here because it's always used.
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
   695
            int h = base.hashCode();
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
   696
            if (exts != null) {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
   697
                h ^= exts.hashCode();
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
   698
            }
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
   699
            hash = h;
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   700
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   701
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
   702
        @Override
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   703
        public boolean equals(Object obj) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   704
            if (this == obj) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   705
                return true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
            }
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   707
            if (!(obj instanceof LocaleKey)) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   708
                return false;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   709
            }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   710
            LocaleKey other = (LocaleKey)obj;
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
   711
            if (hash != other.hash || !base.equals(other.base)) {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
   712
                return false;
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
   713
            }
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
   714
            if (exts == null) {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
   715
                return other.exts == null;
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
   716
            }
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
   717
            return exts.equals(other.exts);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        }
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   719
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
   720
        @Override
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   721
        public int hashCode() {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
   722
            return hash;
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   723
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     * Gets the current value of the default locale for this instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     * of the Java Virtual Machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     * The Java Virtual Machine sets the default locale during startup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     * based on the host environment. It is used by many locale-sensitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     * methods if no locale is explicitly specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     * It can be changed using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     * {@link #setDefault(java.util.Locale) setDefault} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     * @return the default locale for this instance of the Java Virtual Machine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
    public static Locale getDefault() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        // do not synchronize this method - see 4071298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        // it's OK if more than one default locale happens to be created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        if (defaultLocale == null) {
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   742
            initDefault();
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   743
        }
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   744
        return defaultLocale;
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   745
    }
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   746
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   747
    /**
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   748
     * Gets the current value of the default locale for the specified Category
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   749
     * for this instance of the Java Virtual Machine.
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   750
     * <p>
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   751
     * The Java Virtual Machine sets the default locale during startup based
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   752
     * on the host environment. It is used by many locale-sensitive methods
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   753
     * if no locale is explicitly specified. It can be changed using the
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   754
     * setDefault(Locale.Category, Locale) method.
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   755
     *
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   756
     * @param category - the specified category to get the default locale
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   757
     * @throws NullPointerException - if category is null
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   758
     * @return the default locale for the specified Category for this instance
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   759
     *     of the Java Virtual Machine
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   760
     * @see #setDefault(Locale.Category, Locale)
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   761
     * @since 1.7
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   762
     */
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   763
    public static Locale getDefault(Locale.Category category) {
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   764
        // do not synchronize this method - see 4071298
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   765
        // it's OK if more than one default locale happens to be created
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   766
        switch (category) {
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   767
        case DISPLAY:
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   768
            if (defaultDisplayLocale == null) {
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   769
                initDefault(category);
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   770
            }
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   771
            return defaultDisplayLocale;
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   772
        case FORMAT:
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   773
            if (defaultFormatLocale == null) {
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   774
                initDefault(category);
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   775
            }
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   776
            return defaultFormatLocale;
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   777
        default:
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   778
            assert false: "Unknown Category";
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   779
        }
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   780
        return getDefault();
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   781
    }
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   782
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   783
    private static void initDefault() {
7017
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
   784
        String language, region, script, country, variant;
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   785
        language = AccessController.doPrivileged(
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   786
            new GetPropertyAction("user.language", "en"));
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   787
        // for compatibility, check for old user.region property
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   788
        region = AccessController.doPrivileged(
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   789
            new GetPropertyAction("user.region"));
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   790
        if (region != null) {
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   791
            // region can be of form country, country_variant, or _variant
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   792
            int i = region.indexOf('_');
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   793
            if (i >= 0) {
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   794
                country = region.substring(0, i);
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   795
                variant = region.substring(i + 1);
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   796
            } else {
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   797
                country = region;
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   798
                variant = "";
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   799
            }
7017
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
   800
            script = "";
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   801
        } else {
7017
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
   802
            script = AccessController.doPrivileged(
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
   803
                new GetPropertyAction("user.script", ""));
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   804
            country = AccessController.doPrivileged(
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   805
                new GetPropertyAction("user.country", ""));
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   806
            variant = AccessController.doPrivileged(
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   807
                new GetPropertyAction("user.variant", ""));
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   808
        }
7017
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
   809
        defaultLocale = getInstance(language, script, country, variant, null);
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   810
    }
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   811
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   812
    private static void initDefault(Locale.Category category) {
7017
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
   813
        // make sure defaultLocale is initialized
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
   814
        if (defaultLocale == null) {
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
   815
            initDefault();
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
   816
        }
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
   817
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
   818
        Locale defaultCategoryLocale = getInstance(
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
   819
            AccessController.doPrivileged(
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
   820
                new GetPropertyAction(category.languageKey, defaultLocale.getLanguage())),
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
   821
            AccessController.doPrivileged(
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
   822
                new GetPropertyAction(category.scriptKey, defaultLocale.getScript())),
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
   823
            AccessController.doPrivileged(
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
   824
                new GetPropertyAction(category.countryKey, defaultLocale.getCountry())),
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
   825
            AccessController.doPrivileged(
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
   826
                new GetPropertyAction(category.variantKey, defaultLocale.getVariant())),
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
   827
            null);
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
   828
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   829
        switch (category) {
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   830
        case DISPLAY:
7017
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
   831
            defaultDisplayLocale = defaultCategoryLocale;
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   832
            break;
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   833
        case FORMAT:
7017
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
   834
            defaultFormatLocale = defaultCategoryLocale;
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   835
            break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     * Sets the default locale for this instance of the Java Virtual Machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     * This does not affect the host locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     * If there is a security manager, its <code>checkPermission</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     * method is called with a <code>PropertyPermission("user.language", "write")</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     * permission before the default locale is changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     * The Java Virtual Machine sets the default locale during startup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     * based on the host environment. It is used by many locale-sensitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     * methods if no locale is explicitly specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     * Since changing the default locale may affect many different areas
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     * of functionality, this method should only be used if the caller
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     * is prepared to reinitialize locale-sensitive code running
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     * within the same Java Virtual Machine.
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   855
     * <p>
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   856
     * By setting the default locale with this method, all of the default
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   857
     * locales for each Category are also set to the specified default locale.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * @throws SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     *        if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     *        <code>checkPermission</code> method doesn't allow the operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     * @throws NullPointerException if <code>newLocale</code> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     * @param newLocale the new default locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     * @see SecurityManager#checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     * @see java.util.PropertyPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
    public static synchronized void setDefault(Locale newLocale) {
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   868
        setDefault(Category.DISPLAY, newLocale);
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   869
        setDefault(Category.FORMAT, newLocale);
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   870
        defaultLocale = newLocale;
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   871
    }
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   872
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   873
    /**
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   874
     * Sets the default locale for the specified Category for this instance
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   875
     * of the Java Virtual Machine. This does not affect the host locale.
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   876
     * <p>
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   877
     * If there is a security manager, its checkPermission method is called
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   878
     * with a PropertyPermission("user.language", "write") permission before
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   879
     * the default locale is changed.
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   880
     * <p>
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   881
     * The Java Virtual Machine sets the default locale during startup based
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   882
     * on the host environment. It is used by many locale-sensitive methods
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   883
     * if no locale is explicitly specified.
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   884
     * <p>
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   885
     * Since changing the default locale may affect many different areas of
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   886
     * functionality, this method should only be used if the caller is
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   887
     * prepared to reinitialize locale-sensitive code running within the
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   888
     * same Java Virtual Machine.
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   889
     * <p>
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   890
     *
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   891
     * @param category - the specified category to set the default locale
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   892
     * @param newLocale - the new default locale
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   893
     * @throws SecurityException - if a security manager exists and its
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   894
     *     checkPermission method doesn't allow the operation.
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   895
     * @throws NullPointerException - if category and/or newLocale is null
7508
9782d88e18a3 7000507: javadoc warnings in java.util.Locale
naoto
parents: 7017
diff changeset
   896
     * @see SecurityManager#checkPermission(java.security.Permission)
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   897
     * @see PropertyPermission
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   898
     * @see #getDefault(Locale.Category)
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   899
     * @since 1.7
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   900
     */
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   901
    public static synchronized void setDefault(Locale.Category category,
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   902
        Locale newLocale) {
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   903
        if (category == null)
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   904
            throw new NullPointerException("Category cannot be NULL");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        if (newLocale == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
            throw new NullPointerException("Can't set default locale to NULL");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        if (sm != null) sm.checkPermission(new PropertyPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
                        ("user.language", "write"));
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   911
        switch (category) {
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   912
        case DISPLAY:
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   913
            defaultDisplayLocale = newLocale;
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   914
            break;
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   915
        case FORMAT:
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   916
            defaultFormatLocale = newLocale;
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   917
            break;
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   918
        default:
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   919
            assert false: "Unknown Category";
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   920
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     * Returns an array of all installed locales.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     * The returned array represents the union of locales supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     * by the Java runtime environment and by installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     * {@link java.util.spi.LocaleServiceProvider LocaleServiceProvider}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     * implementations.  It must contain at least a <code>Locale</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     * instance equal to {@link java.util.Locale#US Locale.US}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     * @return An array of installed locales.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
    public static Locale[] getAvailableLocales() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
        return LocaleServiceProviderPool.getAllAvailableLocales();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     * Returns a list of all 2-letter country codes defined in ISO 3166.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     * Can be used to create Locales.
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   940
     * <p>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   941
     * <b>Note:</b> The <code>Locale</code> class also supports other codes for
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   942
     * country (region), such as 3-letter numeric UN M.49 area codes.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   943
     * Therefore, the list returned by this method does not contain ALL valid
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   944
     * codes that can be used to create Locales.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
    public static String[] getISOCountries() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
        if (isoCountries == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
            isoCountries = getISO2Table(LocaleISOData.isoCountryTable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
        String[] result = new String[isoCountries.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
        System.arraycopy(isoCountries, 0, result, 0, isoCountries.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     * Returns a list of all 2-letter language codes defined in ISO 639.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     * Can be used to create Locales.
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   958
     * <p>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   959
     * <b>Note:</b>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   960
     * <ul>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   961
     * <li>ISO 639 is not a stable standard&mdash; some languages' codes have changed.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     * The list this function returns includes both the new and the old codes for the
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   963
     * languages whose codes have changed.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   964
     * <li>The <code>Locale</code> class also supports language codes up to
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   965
     * 8 characters in length.  Therefore, the list returned by this method does
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   966
     * not contain ALL valid codes that can be used to create Locales.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   967
     * </ul>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
    public static String[] getISOLanguages() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        if (isoLanguages == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
            isoLanguages = getISO2Table(LocaleISOData.isoLanguageTable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
        String[] result = new String[isoLanguages.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
        System.arraycopy(isoLanguages, 0, result, 0, isoLanguages.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
    private static final String[] getISO2Table(String table) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
        int len = table.length() / 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        String[] isoTable = new String[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
        for (int i = 0, j = 0; i < len; i++, j += 5) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
            isoTable[i] = table.substring(j, j + 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        return isoTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
    /**
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   988
     * Returns the language code of this Locale.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   989
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   990
     * <p><b>Note:</b> ISO 639 is not a stable standard&mdash; some languages' codes have changed.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     * Locale's constructor recognizes both the new and the old codes for the languages
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     * whose codes have changed, but this function always returns the old code.  If you
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   993
     * want to check for a specific language whose code has changed, don't do
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   994
     * <pre>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   995
     * if (locale.getLanguage().equals("he")) // BAD!
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
     *    ...
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   997
     * </pre>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   998
     * Instead, do
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   999
     * <pre>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1000
     * if (locale.getLanguage().equals(new Locale("he").getLanguage()))
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1001
     *    ...
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1002
     * </pre>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1003
     * @return The language code, or the empty string if none is defined.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     * @see #getDisplayLanguage
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
    public String getLanguage() {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1007
        return baseLocale.getLanguage();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
    /**
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1011
     * Returns the script for this locale, which should
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1012
     * either be the empty string or an ISO 15924 4-letter script
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1013
     * code. The first letter is uppercase and the rest are
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1014
     * lowercase, for example, 'Latn', 'Cyrl'.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1015
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1016
     * @return The script code, or the empty string if none is defined.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1017
     * @see #getDisplayScript
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1018
     * @since 1.7
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1019
     */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1020
    public String getScript() {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1021
        return baseLocale.getScript();
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1022
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1023
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1024
    /**
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1025
     * Returns the country/region code for this locale, which should
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1026
     * either be the empty string, an uppercase ISO 3166 2-letter code,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1027
     * or a UN M.49 3-digit code.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1028
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1029
     * @return The country/region code, or the empty string if none is defined.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     * @see #getDisplayCountry
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
    public String getCountry() {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1033
        return baseLocale.getRegion();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
     * Returns the variant code for this locale.
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1038
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1039
     * @return The variant code, or the empty string if none is defined.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
     * @see #getDisplayVariant
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
    public String getVariant() {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1043
        return baseLocale.getVariant();
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1044
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1045
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1046
    /**
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1047
     * Returns the extension (or private use) value associated with
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1048
     * the specified key, or null if there is no extension
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1049
     * associated with the key. To be well-formed, the key must be one
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1050
     * of <code>[0-9A-Za-z]</code>. Keys are case-insensitive, so
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1051
     * for example 'z' and 'Z' represent the same extension.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1052
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1053
     * @param key the extension key
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1054
     * @return The extension, or null if this locale defines no
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1055
     * extension for the specified key.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1056
     * @throws IllegalArgumentException if key is not well-formed
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1057
     * @see #PRIVATE_USE_EXTENSION
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1058
     * @see #UNICODE_LOCALE_EXTENSION
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1059
     * @since 1.7
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1060
     */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1061
    public String getExtension(char key) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1062
        if (!LocaleExtensions.isValidKey(key)) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1063
            throw new IllegalArgumentException("Ill-formed extension key: " + key);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1064
        }
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1065
        return (localeExtensions == null) ? null : localeExtensions.getExtensionValue(key);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1066
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1067
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1068
    /**
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1069
     * Returns the set of extension keys associated with this locale, or the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1070
     * empty set if it has no extensions. The returned set is unmodifiable.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1071
     * The keys will all be lower-case.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1072
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1073
     * @return The set of extension keys, or the empty set if this locale has
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1074
     * no extensions.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1075
     * @since 1.7
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1076
     */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1077
    public Set<Character> getExtensionKeys() {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1078
        if (localeExtensions == null) {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1079
            return Collections.emptySet();
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1080
        }
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1081
        return localeExtensions.getKeys();
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1082
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1083
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1084
    /**
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1085
     * Returns the set of unicode locale attributes associated with
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1086
     * this locale, or the empty set if it has no attributes. The
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1087
     * returned set is unmodifiable.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1088
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1089
     * @return The set of attributes.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1090
     * @since 1.7
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1091
     */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1092
    public Set<String> getUnicodeLocaleAttributes() {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1093
        if (localeExtensions == null) {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1094
            return Collections.emptySet();
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1095
        }
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1096
        return localeExtensions.getUnicodeLocaleAttributes();
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1097
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1098
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1099
    /**
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1100
     * Returns the Unicode locale type associated with the specified Unicode locale key
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1101
     * for this locale. Returns the empty string for keys that are defined with no type.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1102
     * Returns null if the key is not defined. Keys are case-insensitive. The key must
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1103
     * be two alphanumeric characters ([0-9a-zA-Z]), or an IllegalArgumentException is
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1104
     * thrown.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1105
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1106
     * @param key the Unicode locale key
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1107
     * @return The Unicode locale type associated with the key, or null if the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1108
     * locale does not define the key.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1109
     * @throws IllegalArgumentException if the key is not well-formed
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1110
     * @throws NullPointerException if <code>key</code> is null
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1111
     * @since 1.7
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1112
     */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1113
    public String getUnicodeLocaleType(String key) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1114
        if (!UnicodeLocaleExtension.isKey(key)) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1115
            throw new IllegalArgumentException("Ill-formed Unicode locale key: " + key);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1116
        }
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1117
        return (localeExtensions == null) ? null : localeExtensions.getUnicodeLocaleType(key);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1118
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1119
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1120
    /**
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1121
     * Returns the set of Unicode locale keys defined by this locale, or the empty set if
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1122
     * this locale has none.  The returned set is immutable.  Keys are all lower case.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1123
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1124
     * @return The set of Unicode locale keys, or the empty set if this locale has
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1125
     * no Unicode locale keywords.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1126
     * @since 1.7
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1127
     */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1128
    public Set<String> getUnicodeLocaleKeys() {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1129
        if (localeExtensions == null) {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1130
            return Collections.emptySet();
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1131
        }
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1132
        return localeExtensions.getUnicodeLocaleKeys();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
    /**
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1136
     * Package locale method returning the Locale's BaseLocale,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1137
     * used by ResourceBundle
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1138
     * @return base locale of this Locale
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1139
     */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1140
    BaseLocale getBaseLocale() {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1141
        return baseLocale;
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1142
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1143
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1144
    /**
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1145
     * Package private method returning the Locale's LocaleExtensions,
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1146
     * used by ResourceBundle.
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1147
     * @return locale exnteions of this Locale,
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1148
     *         or {@code null} if no extensions are defined
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1149
     */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1150
     LocaleExtensions getLocaleExtensions() {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1151
         return localeExtensions;
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1152
     }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1153
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1154
    /**
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1155
     * Returns a string representation of this <code>Locale</code>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1156
     * object, consisting of language, country, variant, script,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1157
     * and extensions as below:
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1158
     * <p><blockquote>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1159
     * language + "_" + country + "_" + (variant + "_#" | "#") + script + "-" + extensions
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1160
     * </blockquote>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1161
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1162
     * Language is always lower case, country is always upper case, script is always title
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1163
     * case, and extensions are always lower case.  Extensions and private use subtags
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1164
     * will be in canonical order as explained in {@link #toLanguageTag}.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1165
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1166
     * <p>When the locale has neither script nor extensions, the result is the same as in
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1167
     * Java 6 and prior.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1168
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1169
     * <p>If both the language and country fields are missing, this function will return
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1170
     * the empty string, even if the variant, script, or extensions field is present (you
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1171
     * can't have a locale with just a variant, the variant must accompany a well-formed
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1172
     * language or country code).
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1173
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1174
     * <p>If script or extensions are present and variant is missing, no underscore is
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1175
     * added before the "#".
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1176
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1177
     * <p>This behavior is designed to support debugging and to be compatible with
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1178
     * previous uses of <code>toString</code> that expected language, country, and variant
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1179
     * fields only.  To represent a Locale as a String for interchange purposes, use
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1180
     * {@link #toLanguageTag}.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1181
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1182
     * <p>Examples: <ul><tt>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1183
     * <li>en
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1184
     * <li>de_DE
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1185
     * <li>_GB
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1186
     * <li>en_US_WIN
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1187
     * <li>de__POSIX
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1188
     * <li>zh_CN_#Hans
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1189
     * <li>zh_TW_#Hant-x-java
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1190
     * <li>th_TH_TH_#u-nu-thai</tt></ul>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1191
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1192
     * @return A string representation of the Locale, for debugging.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
     * @see #getDisplayName
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1194
     * @see #toLanguageTag
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
     */
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1196
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
    public final String toString() {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1198
        boolean l = (baseLocale.getLanguage().length() != 0);
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1199
        boolean s = (baseLocale.getScript().length() != 0);
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1200
        boolean r = (baseLocale.getRegion().length() != 0);
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1201
        boolean v = (baseLocale.getVariant().length() != 0);
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1202
        boolean e = (localeExtensions != null && localeExtensions.getID().length() != 0);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1203
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1204
        StringBuilder result = new StringBuilder(baseLocale.getLanguage());
8780
c88e159a3e5c 7023613: (lc) Incorrect string returned by Locale.toString() with non-empty script field
naoto
parents: 8149
diff changeset
  1205
        if (r || (l && (v || s || e))) {
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1206
            result.append('_')
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1207
                .append(baseLocale.getRegion()); // This may just append '_'
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1208
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1209
        if (v && (l || r)) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1210
            result.append('_')
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1211
                .append(baseLocale.getVariant());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
        }
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1213
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1214
        if (s && (l || r)) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1215
            result.append("_#")
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1216
                .append(baseLocale.getScript());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
        }
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1218
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1219
        if (e && (l || r)) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1220
            result.append('_');
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1221
            if (!s) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1222
                result.append('#');
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1223
            }
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1224
            result.append(localeExtensions.getID());
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1225
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1226
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
        return result.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
    /**
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1231
     * Returns a well-formed IETF BCP 47 language tag representing
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1232
     * this locale.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1233
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1234
     * <p>If this <code>Locale</code> has a language, country, or
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1235
     * variant that does not satisfy the IETF BCP 47 language tag
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1236
     * syntax requirements, this method handles these fields as
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1237
     * described below:
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1238
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1239
     * <p><b>Language:</b> If language is empty, or not <a
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1240
     * href="#def_language" >well-formed</a> (for example "a" or
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1241
     * "e2"), it will be emitted as "und" (Undetermined).
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1242
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1243
     * <p><b>Country:</b> If country is not <a
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1244
     * href="#def_region">well-formed</a> (for example "12" or "USA"),
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1245
     * it will be omitted.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1246
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1247
     * <p><b>Variant:</b> If variant <b>is</b> <a
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1248
     * href="#def_variant">well-formed</a>, each sub-segment
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1249
     * (delimited by '-' or '_') is emitted as a subtag.  Otherwise:
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1250
     * <ul>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1251
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1252
     * <li>if all sub-segments match <code>[0-9a-zA-Z]{1,8}</code>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1253
     * (for example "WIN" or "Oracle_JDK_Standard_Edition"), the first
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1254
     * ill-formed sub-segment and all following will be appended to
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1255
     * the private use subtag.  The first appended subtag will be
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1256
     * "lvariant", followed by the sub-segments in order, separated by
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1257
     * hyphen. For example, "x-lvariant-WIN",
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1258
     * "Oracle-x-lvariant-JDK-Standard-Edition".
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1259
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1260
     * <li>if any sub-segment does not match
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1261
     * <code>[0-9a-zA-Z]{1,8}</code>, the variant will be truncated
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1262
     * and the problematic sub-segment and all following sub-segments
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1263
     * will be omitted.  If the remainder is non-empty, it will be
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1264
     * emitted as a private use subtag as above (even if the remainder
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1265
     * turns out to be well-formed).  For example,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1266
     * "Solaris_isjustthecoolestthing" is emitted as
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1267
     * "x-lvariant-Solaris", not as "solaris".</li></ul>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1268
     *
7508
9782d88e18a3 7000507: javadoc warnings in java.util.Locale
naoto
parents: 7017
diff changeset
  1269
     * <p><b>Special Conversions:</b> Java supports some old locale
9782d88e18a3 7000507: javadoc warnings in java.util.Locale
naoto
parents: 7017
diff changeset
  1270
     * representations, including deprecated ISO language codes,
9782d88e18a3 7000507: javadoc warnings in java.util.Locale
naoto
parents: 7017
diff changeset
  1271
     * for compatibility. This method performs the following
9782d88e18a3 7000507: javadoc warnings in java.util.Locale
naoto
parents: 7017
diff changeset
  1272
     * conversions:
9782d88e18a3 7000507: javadoc warnings in java.util.Locale
naoto
parents: 7017
diff changeset
  1273
     * <ul>
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1274
     *
7508
9782d88e18a3 7000507: javadoc warnings in java.util.Locale
naoto
parents: 7017
diff changeset
  1275
     * <li>Deprecated ISO language codes "iw", "ji", and "in" are
9782d88e18a3 7000507: javadoc warnings in java.util.Locale
naoto
parents: 7017
diff changeset
  1276
     * converted to "he", "yi", and "id", respectively.
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1277
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1278
     * <li>A locale with language "no", country "NO", and variant
7508
9782d88e18a3 7000507: javadoc warnings in java.util.Locale
naoto
parents: 7017
diff changeset
  1279
     * "NY", representing Norwegian Nynorsk (Norway), is converted
9782d88e18a3 7000507: javadoc warnings in java.util.Locale
naoto
parents: 7017
diff changeset
  1280
     * to a language tag "nn-NO".</li></ul>
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1281
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1282
     * <p><b>Note:</b> Although the language tag created by this
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1283
     * method is well-formed (satisfies the syntax requirements
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1284
     * defined by the IETF BCP 47 specification), it is not
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1285
     * necessarily a valid BCP 47 language tag.  For example,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1286
     * <pre>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1287
     *   new Locale("xx", "YY").toLanguageTag();</pre>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1288
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1289
     * will return "xx-YY", but the language subtag "xx" and the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1290
     * region subtag "YY" are invalid because they are not registered
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1291
     * in the IANA Language Subtag Registry.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1292
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1293
     * @return a BCP47 language tag representing the locale
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1294
     * @see #forLanguageTag(String)
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1295
     * @since 1.7
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1296
     */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1297
    public String toLanguageTag() {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1298
        LanguageTag tag = LanguageTag.parseLocale(baseLocale, localeExtensions);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1299
        StringBuilder buf = new StringBuilder();
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1300
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1301
        String subtag = tag.getLanguage();
8149
768769e3cddd 7015500: Locale.toLanguageTag() uses "und" as lang subtag for private use only Locale
naoto
parents: 7783
diff changeset
  1302
        if (subtag.length() > 0) {
768769e3cddd 7015500: Locale.toLanguageTag() uses "und" as lang subtag for private use only Locale
naoto
parents: 7783
diff changeset
  1303
            buf.append(LanguageTag.canonicalizeLanguage(subtag));
768769e3cddd 7015500: Locale.toLanguageTag() uses "und" as lang subtag for private use only Locale
naoto
parents: 7783
diff changeset
  1304
        }
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1305
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1306
        subtag = tag.getScript();
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1307
        if (subtag.length() > 0) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1308
            buf.append(LanguageTag.SEP);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1309
            buf.append(LanguageTag.canonicalizeScript(subtag));
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1310
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1311
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1312
        subtag = tag.getRegion();
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1313
        if (subtag.length() > 0) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1314
            buf.append(LanguageTag.SEP);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1315
            buf.append(LanguageTag.canonicalizeRegion(subtag));
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1316
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1317
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1318
        List<String>subtags = tag.getVariants();
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1319
        for (String s : subtags) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1320
            buf.append(LanguageTag.SEP);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1321
            // preserve casing
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1322
            buf.append(s);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1323
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1324
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1325
        subtags = tag.getExtensions();
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1326
        for (String s : subtags) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1327
            buf.append(LanguageTag.SEP);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1328
            buf.append(LanguageTag.canonicalizeExtension(s));
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1329
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1330
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1331
        subtag = tag.getPrivateuse();
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1332
        if (subtag.length() > 0) {
8149
768769e3cddd 7015500: Locale.toLanguageTag() uses "und" as lang subtag for private use only Locale
naoto
parents: 7783
diff changeset
  1333
            if (buf.length() > 0) {
768769e3cddd 7015500: Locale.toLanguageTag() uses "und" as lang subtag for private use only Locale
naoto
parents: 7783
diff changeset
  1334
                buf.append(LanguageTag.SEP);
768769e3cddd 7015500: Locale.toLanguageTag() uses "und" as lang subtag for private use only Locale
naoto
parents: 7783
diff changeset
  1335
            }
768769e3cddd 7015500: Locale.toLanguageTag() uses "und" as lang subtag for private use only Locale
naoto
parents: 7783
diff changeset
  1336
            buf.append(LanguageTag.PRIVATEUSE).append(LanguageTag.SEP);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1337
            // preserve casing
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1338
            buf.append(subtag);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1339
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1340
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1341
        return buf.toString();
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1342
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1343
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1344
    /**
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1345
     * Returns a locale for the specified IETF BCP 47 language tag string.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1346
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1347
     * <p>If the specified language tag contains any ill-formed subtags,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1348
     * the first such subtag and all following subtags are ignored.  Compare
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1349
     * to {@link Locale.Builder#setLanguageTag} which throws an exception
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1350
     * in this case.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1351
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1352
     * <p>The following <b>conversions</b> are performed:<ul>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1353
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1354
     * <li>The language code "und" is mapped to language "".
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1355
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1356
     * <li>The language codes "he", "yi", and "id" are mapped to "iw",
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1357
     * "ji", and "in" respectively. (This is the same canonicalization
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1358
     * that's done in Locale's constructors.)
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1359
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1360
     * <li>The portion of a private use subtag prefixed by "lvariant",
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1361
     * if any, is removed and appended to the variant field in the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1362
     * result locale (without case normalization).  If it is then
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1363
     * empty, the private use subtag is discarded:
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1364
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1365
     * <pre>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1366
     *     Locale loc;
7508
9782d88e18a3 7000507: javadoc warnings in java.util.Locale
naoto
parents: 7017
diff changeset
  1367
     *     loc = Locale.forLanguageTag("en-US-x-lvariant-POSIX");
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1368
     *     loc.getVariant(); // returns "POSIX"
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1369
     *     loc.getExtension('x'); // returns null
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1370
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1371
     *     loc = Locale.forLanguageTag("de-POSIX-x-URP-lvariant-Abc-Def");
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1372
     *     loc.getVariant(); // returns "POSIX_Abc_Def"
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1373
     *     loc.getExtension('x'); // returns "urp"
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1374
     * </pre>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1375
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1376
     * <li>When the languageTag argument contains an extlang subtag,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1377
     * the first such subtag is used as the language, and the primary
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1378
     * language subtag and other extlang subtags are ignored:
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1379
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1380
     * <pre>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1381
     *     Locale.forLanguageTag("ar-aao").getLanguage(); // returns "aao"
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1382
     *     Locale.forLanguageTag("en-abc-def-us").toString(); // returns "abc_US"
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1383
     * </pre>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1384
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1385
     * <li>Case is normalized except for variant tags, which are left
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1386
     * unchanged.  Language is normalized to lower case, script to
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1387
     * title case, country to upper case, and extensions to lower
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1388
     * case.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1389
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1390
     * <li>If, after processing, the locale would exactly match either
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1391
     * ja_JP_JP or th_TH_TH with no extensions, the appropriate
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1392
     * extensions are added as though the constructor had been called:
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1393
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1394
     * <pre>
7508
9782d88e18a3 7000507: javadoc warnings in java.util.Locale
naoto
parents: 7017
diff changeset
  1395
     *    Locale.forLanguageTag("ja-JP-x-lvariant-JP").toLanguageTag();
9782d88e18a3 7000507: javadoc warnings in java.util.Locale
naoto
parents: 7017
diff changeset
  1396
     *    // returns "ja-JP-u-ca-japanese-x-lvariant-JP"
9782d88e18a3 7000507: javadoc warnings in java.util.Locale
naoto
parents: 7017
diff changeset
  1397
     *    Locale.forLanguageTag("th-TH-x-lvariant-TH").toLanguageTag();
9782d88e18a3 7000507: javadoc warnings in java.util.Locale
naoto
parents: 7017
diff changeset
  1398
     *    // returns "th-TH-u-nu-thai-x-lvariant-TH"
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1399
     * <pre></ul>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1400
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1401
     * <p>This implements the 'Language-Tag' production of BCP47, and
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1402
     * so supports grandfathered (regular and irregular) as well as
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1403
     * private use language tags.  Stand alone private use tags are
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1404
     * represented as empty language and extension 'x-whatever',
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1405
     * and grandfathered tags are converted to their canonical replacements
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1406
     * where they exist.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1407
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1408
     * <p>Grandfathered tags with canonical replacements are as follows:
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1409
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1410
     * <table>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1411
     * <tbody align="center">
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1412
     * <tr><th>grandfathered tag</th><th>&nbsp;</th><th>modern replacement</th></tr>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1413
     * <tr><td>art-lojban</td><td>&nbsp;</td><td>jbo</td></tr>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1414
     * <tr><td>i-ami</td><td>&nbsp;</td><td>ami</td></tr>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1415
     * <tr><td>i-bnn</td><td>&nbsp;</td><td>bnn</td></tr>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1416
     * <tr><td>i-hak</td><td>&nbsp;</td><td>hak</td></tr>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1417
     * <tr><td>i-klingon</td><td>&nbsp;</td><td>tlh</td></tr>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1418
     * <tr><td>i-lux</td><td>&nbsp;</td><td>lb</td></tr>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1419
     * <tr><td>i-navajo</td><td>&nbsp;</td><td>nv</td></tr>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1420
     * <tr><td>i-pwn</td><td>&nbsp;</td><td>pwn</td></tr>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1421
     * <tr><td>i-tao</td><td>&nbsp;</td><td>tao</td></tr>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1422
     * <tr><td>i-tay</td><td>&nbsp;</td><td>tay</td></tr>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1423
     * <tr><td>i-tsu</td><td>&nbsp;</td><td>tsu</td></tr>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1424
     * <tr><td>no-bok</td><td>&nbsp;</td><td>nb</td></tr>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1425
     * <tr><td>no-nyn</td><td>&nbsp;</td><td>nn</td></tr>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1426
     * <tr><td>sgn-BE-FR</td><td>&nbsp;</td><td>sfb</td></tr>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1427
     * <tr><td>sgn-BE-NL</td><td>&nbsp;</td><td>vgt</td></tr>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1428
     * <tr><td>sgn-CH-DE</td><td>&nbsp;</td><td>sgg</td></tr>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1429
     * <tr><td>zh-guoyu</td><td>&nbsp;</td><td>cmn</td></tr>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1430
     * <tr><td>zh-hakka</td><td>&nbsp;</td><td>hak</td></tr>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1431
     * <tr><td>zh-min-nan</td><td>&nbsp;</td><td>nan</td></tr>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1432
     * <tr><td>zh-xiang</td><td>&nbsp;</td><td>hsn</td></tr>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1433
     * </tbody>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1434
     * </table>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1435
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1436
     * <p>Grandfathered tags with no modern replacement will be
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1437
     * converted as follows:
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1438
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1439
     * <table>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1440
     * <tbody align="center">
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1441
     * <tr><th>grandfathered tag</th><th>&nbsp;</th><th>converts to</th></tr>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1442
     * <tr><td>cel-gaulish</td><td>&nbsp;</td><td>xtg-x-cel-gaulish</td></tr>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1443
     * <tr><td>en-GB-oed</td><td>&nbsp;</td><td>en-GB-x-oed</td></tr>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1444
     * <tr><td>i-default</td><td>&nbsp;</td><td>en-x-i-default</td></tr>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1445
     * <tr><td>i-enochian</td><td>&nbsp;</td><td>und-x-i-enochian</td></tr>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1446
     * <tr><td>i-mingo</td><td>&nbsp;</td><td>see-x-i-mingo</td></tr>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1447
     * <tr><td>zh-min</td><td>&nbsp;</td><td>nan-x-zh-min</td></tr>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1448
     * </tbody>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1449
     * </table>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1450
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1451
     * <p>For a list of all grandfathered tags, see the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1452
     * IANA Language Subtag Registry (search for "Type: grandfathered").
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1453
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1454
     * <p><b>Note</b>: there is no guarantee that <code>toLanguageTag</code>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1455
     * and <code>forLanguageTag</code> will round-trip.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1456
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1457
     * @param languageTag the language tag
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1458
     * @return The locale that best represents the language tag.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1459
     * @throws NullPointerException if <code>languageTag</code> is <code>null</code>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1460
     * @see #toLanguageTag()
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1461
     * @see java.util.Locale.Builder#setLanguageTag(String)
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1462
     * @since 1.7
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1463
     */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1464
    public static Locale forLanguageTag(String languageTag) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1465
        LanguageTag tag = LanguageTag.parse(languageTag, null);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1466
        InternalLocaleBuilder bldr = new InternalLocaleBuilder();
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1467
        bldr.setLanguageTag(tag);
7513
7091fccbc86d 7002320: Locale.forLanguageTag()/toLanguageTag() not working properly with ja_JP_JP locale
naoto
parents: 7508
diff changeset
  1468
        BaseLocale base = bldr.getBaseLocale();
7091fccbc86d 7002320: Locale.forLanguageTag()/toLanguageTag() not working properly with ja_JP_JP locale
naoto
parents: 7508
diff changeset
  1469
        LocaleExtensions exts = bldr.getLocaleExtensions();
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1470
        if (exts == null && base.getVariant().length() > 0) {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1471
            exts = getCompatibilityExtensions(base.getLanguage(), base.getScript(),
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1472
                                              base.getRegion(), base.getVariant());
7513
7091fccbc86d 7002320: Locale.forLanguageTag()/toLanguageTag() not working properly with ja_JP_JP locale
naoto
parents: 7508
diff changeset
  1473
        }
7091fccbc86d 7002320: Locale.forLanguageTag()/toLanguageTag() not working properly with ja_JP_JP locale
naoto
parents: 7508
diff changeset
  1474
        return getInstance(base, exts);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1475
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1476
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1477
    /**
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1478
     * Returns a three-letter abbreviation of this locale's language.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1479
     * If the language matches an ISO 639-1 two-letter code, the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1480
     * corresponding ISO 639-2/T three-letter lowercase code is
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1481
     * returned.  The ISO 639-2 language codes can be found on-line,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1482
     * see "Codes for the Representation of Names of Languages Part 2:
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1483
     * Alpha-3 Code".  If the locale specifies a three-letter
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1484
     * language, the language is returned as is.  If the locale does
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1485
     * not specify a language the empty string is returned.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1486
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1487
     * @return A three-letter abbreviation of this locale's language.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1488
     * @exception MissingResourceException Throws MissingResourceException if
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
     * three-letter language abbreviation is not available for this locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
    public String getISO3Language() throws MissingResourceException {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1492
        String lang = baseLocale.getLanguage();
7783
8eb31199b5ed 4940539: Constructor of java.util.Locale should handle ISO 639-2 Language Codes
naoto
parents: 7513
diff changeset
  1493
        if (lang.length() == 3) {
8eb31199b5ed 4940539: Constructor of java.util.Locale should handle ISO 639-2 Language Codes
naoto
parents: 7513
diff changeset
  1494
            return lang;
8eb31199b5ed 4940539: Constructor of java.util.Locale should handle ISO 639-2 Language Codes
naoto
parents: 7513
diff changeset
  1495
        }
8eb31199b5ed 4940539: Constructor of java.util.Locale should handle ISO 639-2 Language Codes
naoto
parents: 7513
diff changeset
  1496
8eb31199b5ed 4940539: Constructor of java.util.Locale should handle ISO 639-2 Language Codes
naoto
parents: 7513
diff changeset
  1497
        String language3 = getISO3Code(lang, LocaleISOData.isoLanguageTable);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
        if (language3 == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
            throw new MissingResourceException("Couldn't find 3-letter language code for "
7783
8eb31199b5ed 4940539: Constructor of java.util.Locale should handle ISO 639-2 Language Codes
naoto
parents: 7513
diff changeset
  1500
                    + lang, "FormatData_" + toString(), "ShortLanguage");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
        return language3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
    /**
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1506
     * Returns a three-letter abbreviation for this locale's country.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1507
     * If the country matches an ISO 3166-1 alpha-2 code, the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1508
     * corresponding ISO 3166-1 alpha-3 uppercase code is returned.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1509
     * If the locale doesn't specify a country, this will be the empty
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1510
     * string.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1511
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1512
     * <p>The ISO 3166-1 codes can be found on-line.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1513
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1514
     * @return A three-letter abbreviation of this locale's country.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
     * @exception MissingResourceException Throws MissingResourceException if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
     * three-letter country abbreviation is not available for this locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
    public String getISO3Country() throws MissingResourceException {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1519
        String country3 = getISO3Code(baseLocale.getRegion(), LocaleISOData.isoCountryTable);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
        if (country3 == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
            throw new MissingResourceException("Couldn't find 3-letter country code for "
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1522
                    + baseLocale.getRegion(), "FormatData_" + toString(), "ShortCountry");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
        return country3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
    private static final String getISO3Code(String iso2Code, String table) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
        int codeLength = iso2Code.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
        if (codeLength == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
            return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
        int tableLength = table.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
        int index = tableLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
        if (codeLength == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
            char c1 = iso2Code.charAt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
            char c2 = iso2Code.charAt(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
            for (index = 0; index < tableLength; index += 5) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
                if (table.charAt(index) == c1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
                    && table.charAt(index + 1) == c2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
        return index < tableLength ? table.substring(index + 2, index + 5) : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
     * Returns a name for the locale's language that is appropriate for display to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
     * user.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
     * If possible, the name returned will be localized for the default locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
     * For example, if the locale is fr_FR and the default locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
     * is en_US, getDisplayLanguage() will return "French"; if the locale is en_US and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
     * the default locale is fr_FR, getDisplayLanguage() will return "anglais".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
     * If the name returned cannot be localized for the default locale,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
     * (say, we don't have a Japanese name for Croatian),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
     * this function falls back on the English name, and uses the ISO code as a last-resort
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
     * value.  If the locale doesn't specify a language, this function returns the empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
    public final String getDisplayLanguage() {
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  1561
        return getDisplayLanguage(getDefault(Category.DISPLAY));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
     * Returns a name for the locale's language that is appropriate for display to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
     * user.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
     * If possible, the name returned will be localized according to inLocale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
     * For example, if the locale is fr_FR and inLocale
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
     * is en_US, getDisplayLanguage() will return "French"; if the locale is en_US and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
     * inLocale is fr_FR, getDisplayLanguage() will return "anglais".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
     * If the name returned cannot be localized according to inLocale,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
     * (say, we don't have a Japanese name for Croatian),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
     * this function falls back on the English name, and finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
     * on the ISO code as a last-resort value.  If the locale doesn't specify a language,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
     * this function returns the empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
     * @exception NullPointerException if <code>inLocale</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
    public String getDisplayLanguage(Locale inLocale) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1580
        return getDisplayString(baseLocale.getLanguage(), inLocale, DISPLAY_LANGUAGE);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1581
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1582
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1583
    /**
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1584
     * Returns a name for the the locale's script that is appropriate for display to
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1585
     * the user. If possible, the name will be localized for the default locale.  Returns
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1586
     * the empty string if this locale doesn't specify a script code.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1587
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1588
     * @return the display name of the script code for the current default locale
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1589
     * @since 1.7
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1590
     */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1591
    public String getDisplayScript() {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1592
        return getDisplayScript(getDefault());
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1593
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1594
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1595
    /**
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1596
     * Returns a name for the locale's script that is appropriate
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1597
     * for display to the user. If possible, the name will be
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1598
     * localized for the given locale. Returns the empty string if
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1599
     * this locale doesn't specify a script code.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1600
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1601
     * @return the display name of the script code for the current default locale
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1602
     * @throws NullPointerException if <code>inLocale</code> is <code>null</code>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1603
     * @since 1.7
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1604
     */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1605
    public String getDisplayScript(Locale inLocale) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1606
        return getDisplayString(baseLocale.getScript(), inLocale, DISPLAY_SCRIPT);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
     * Returns a name for the locale's country that is appropriate for display to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
     * user.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
     * If possible, the name returned will be localized for the default locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
     * For example, if the locale is fr_FR and the default locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
     * is en_US, getDisplayCountry() will return "France"; if the locale is en_US and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
     * the default locale is fr_FR, getDisplayCountry() will return "Etats-Unis".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
     * If the name returned cannot be localized for the default locale,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
     * (say, we don't have a Japanese name for Croatia),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
     * this function falls back on the English name, and uses the ISO code as a last-resort
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
     * value.  If the locale doesn't specify a country, this function returns the empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
    public final String getDisplayCountry() {
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  1622
        return getDisplayCountry(getDefault(Category.DISPLAY));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
     * Returns a name for the locale's country that is appropriate for display to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
     * user.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
     * If possible, the name returned will be localized according to inLocale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
     * For example, if the locale is fr_FR and inLocale
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
     * is en_US, getDisplayCountry() will return "France"; if the locale is en_US and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
     * inLocale is fr_FR, getDisplayCountry() will return "Etats-Unis".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
     * If the name returned cannot be localized according to inLocale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
     * (say, we don't have a Japanese name for Croatia),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
     * this function falls back on the English name, and finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
     * on the ISO code as a last-resort value.  If the locale doesn't specify a country,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
     * this function returns the empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
     * @exception NullPointerException if <code>inLocale</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
    public String getDisplayCountry(Locale inLocale) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1641
        return getDisplayString(baseLocale.getRegion(), inLocale, DISPLAY_COUNTRY);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
    private String getDisplayString(String code, Locale inLocale, int type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
        if (code.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
            return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
        if (inLocale == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
            OpenListResourceBundle bundle = LocaleData.getLocaleNames(inLocale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
            String key = (type == DISPLAY_VARIANT ? "%%"+code : code);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
            String result = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
            // Check whether a provider can provide an implementation that's closer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
            // to the requested locale than what the Java runtime itself can provide.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
            LocaleServiceProviderPool pool =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
                LocaleServiceProviderPool.getPool(LocaleNameProvider.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
            if (pool.hasProviders()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
                result = pool.getLocalizedObject(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
                                    LocaleNameGetter.INSTANCE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
                                    inLocale, bundle, key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
                                    type, code);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
            if (result == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
                result = bundle.getString(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
            if (result != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
                return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
        catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
            // just fall through
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
        return code;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
     * Returns a name for the locale's variant code that is appropriate for display to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
     * user.  If possible, the name will be localized for the default locale.  If the locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
     * doesn't specify a variant code, this function returns the empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
    public final String getDisplayVariant() {
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  1689
        return getDisplayVariant(getDefault(Category.DISPLAY));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
     * Returns a name for the locale's variant code that is appropriate for display to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
     * user.  If possible, the name will be localized for inLocale.  If the locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
     * doesn't specify a variant code, this function returns the empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
     * @exception NullPointerException if <code>inLocale</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
    public String getDisplayVariant(Locale inLocale) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1700
        if (baseLocale.getVariant().length() == 0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
            return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
        OpenListResourceBundle bundle = LocaleData.getLocaleNames(inLocale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
        String names[] = getDisplayVariantArray(bundle, inLocale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
        // Get the localized patterns for formatting a list, and use
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
        // them to format the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
        String listPattern = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
        String listCompositionPattern = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
            listPattern = bundle.getString("ListPattern");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
            listCompositionPattern = bundle.getString("ListCompositionPattern");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
        } catch (MissingResourceException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
        return formatList(names, listPattern, listCompositionPattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
     * Returns a name for the locale that is appropriate for display to the
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1721
     * user. This will be the values returned by getDisplayLanguage(),
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1722
     * getDisplayScript(), getDisplayCountry(), and getDisplayVariant() assembled
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1723
     * into a single string. The the non-empty values are used in order,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1724
     * with the second and subsequent names in parentheses.  For example:
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1725
     * <blockquote>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1726
     * language (script, country, variant)<br>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1727
     * language (country)<br>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1728
     * language (variant)<br>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1729
     * script (country)<br>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1730
     * country<br>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1731
     * </blockquote>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1732
     * depending on which fields are specified in the locale.  If the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1733
     * language, sacript, country, and variant fields are all empty,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1734
     * this function returns the empty string.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
    public final String getDisplayName() {
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  1737
        return getDisplayName(getDefault(Category.DISPLAY));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
    /**
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1741
     * Returns a name for the locale that is appropriate for display
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1742
     * to the user.  This will be the values returned by
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1743
     * getDisplayLanguage(), getDisplayScript(),getDisplayCountry(),
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1744
     * and getDisplayVariant() assembled into a single string.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1745
     * The non-empty values are used in order,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1746
     * with the second and subsequent names in parentheses.  For example:
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1747
     * <blockquote>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1748
     * language (script, country, variant)<br>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1749
     * language (country)<br>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1750
     * language (variant)<br>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1751
     * script (country)<br>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1752
     * country<br>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1753
     * </blockquote>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1754
     * depending on which fields are specified in the locale.  If the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1755
     * language, script, country, and variant fields are all empty,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1756
     * this function returns the empty string.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
     *
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1758
     * @throws NullPointerException if <code>inLocale</code> is <code>null</code>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
    public String getDisplayName(Locale inLocale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
        OpenListResourceBundle bundle = LocaleData.getLocaleNames(inLocale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
        String languageName = getDisplayLanguage(inLocale);
6843
6ab7e78c51eb 6992272: I18N: Locale.getDisplayName() and toString() return empty if just script is set
naoto
parents: 6840
diff changeset
  1764
        String scriptName = getDisplayScript(inLocale);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
        String countryName = getDisplayCountry(inLocale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
        String[] variantNames = getDisplayVariantArray(bundle, inLocale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
        // Get the localized patterns for formatting a display name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
        String displayNamePattern = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
        String listPattern = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
        String listCompositionPattern = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
            displayNamePattern = bundle.getString("DisplayNamePattern");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
            listPattern = bundle.getString("ListPattern");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
            listCompositionPattern = bundle.getString("ListCompositionPattern");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
        } catch (MissingResourceException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
        // The display name consists of a main name, followed by qualifiers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
        // Typically, the format is "MainName (Qualifier, Qualifier)" but this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
        // depends on what pattern is stored in the display locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
        String   mainName       = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
        String[] qualifierNames = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
6843
6ab7e78c51eb 6992272: I18N: Locale.getDisplayName() and toString() return empty if just script is set
naoto
parents: 6840
diff changeset
  1785
        // The main name is the language, or if there is no language, the script,
6ab7e78c51eb 6992272: I18N: Locale.getDisplayName() and toString() return empty if just script is set
naoto
parents: 6840
diff changeset
  1786
        // then if no script, the country. If there is no language/script/country
6ab7e78c51eb 6992272: I18N: Locale.getDisplayName() and toString() return empty if just script is set
naoto
parents: 6840
diff changeset
  1787
        // (an anomalous situation) then the display name is simply the variant's
6ab7e78c51eb 6992272: I18N: Locale.getDisplayName() and toString() return empty if just script is set
naoto
parents: 6840
diff changeset
  1788
        // display name.
6ab7e78c51eb 6992272: I18N: Locale.getDisplayName() and toString() return empty if just script is set
naoto
parents: 6840
diff changeset
  1789
        if (languageName.length() == 0 && scriptName.length() == 0 && countryName.length() == 0) {
6ab7e78c51eb 6992272: I18N: Locale.getDisplayName() and toString() return empty if just script is set
naoto
parents: 6840
diff changeset
  1790
            if (variantNames.length == 0) {
6ab7e78c51eb 6992272: I18N: Locale.getDisplayName() and toString() return empty if just script is set
naoto
parents: 6840
diff changeset
  1791
                return "";
6ab7e78c51eb 6992272: I18N: Locale.getDisplayName() and toString() return empty if just script is set
naoto
parents: 6840
diff changeset
  1792
            } else {
6ab7e78c51eb 6992272: I18N: Locale.getDisplayName() and toString() return empty if just script is set
naoto
parents: 6840
diff changeset
  1793
                return formatList(variantNames, listPattern, listCompositionPattern);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
            }
6843
6ab7e78c51eb 6992272: I18N: Locale.getDisplayName() and toString() return empty if just script is set
naoto
parents: 6840
diff changeset
  1795
        }
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1796
        ArrayList<String> names = new ArrayList<>(4);
6843
6ab7e78c51eb 6992272: I18N: Locale.getDisplayName() and toString() return empty if just script is set
naoto
parents: 6840
diff changeset
  1797
        if (languageName.length() != 0) {
6ab7e78c51eb 6992272: I18N: Locale.getDisplayName() and toString() return empty if just script is set
naoto
parents: 6840
diff changeset
  1798
            names.add(languageName);
6ab7e78c51eb 6992272: I18N: Locale.getDisplayName() and toString() return empty if just script is set
naoto
parents: 6840
diff changeset
  1799
        }
6ab7e78c51eb 6992272: I18N: Locale.getDisplayName() and toString() return empty if just script is set
naoto
parents: 6840
diff changeset
  1800
        if (scriptName.length() != 0) {
6ab7e78c51eb 6992272: I18N: Locale.getDisplayName() and toString() return empty if just script is set
naoto
parents: 6840
diff changeset
  1801
            names.add(scriptName);
6ab7e78c51eb 6992272: I18N: Locale.getDisplayName() and toString() return empty if just script is set
naoto
parents: 6840
diff changeset
  1802
        }
6ab7e78c51eb 6992272: I18N: Locale.getDisplayName() and toString() return empty if just script is set
naoto
parents: 6840
diff changeset
  1803
        if (countryName.length() != 0) {
6ab7e78c51eb 6992272: I18N: Locale.getDisplayName() and toString() return empty if just script is set
naoto
parents: 6840
diff changeset
  1804
            names.add(countryName);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
        }
6843
6ab7e78c51eb 6992272: I18N: Locale.getDisplayName() and toString() return empty if just script is set
naoto
parents: 6840
diff changeset
  1806
        if (variantNames.length != 0) {
6ab7e78c51eb 6992272: I18N: Locale.getDisplayName() and toString() return empty if just script is set
naoto
parents: 6840
diff changeset
  1807
            for (String var : variantNames) {
6ab7e78c51eb 6992272: I18N: Locale.getDisplayName() and toString() return empty if just script is set
naoto
parents: 6840
diff changeset
  1808
                names.add(var);
6ab7e78c51eb 6992272: I18N: Locale.getDisplayName() and toString() return empty if just script is set
naoto
parents: 6840
diff changeset
  1809
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
        }
6843
6ab7e78c51eb 6992272: I18N: Locale.getDisplayName() and toString() return empty if just script is set
naoto
parents: 6840
diff changeset
  1811
6ab7e78c51eb 6992272: I18N: Locale.getDisplayName() and toString() return empty if just script is set
naoto
parents: 6840
diff changeset
  1812
        // The first one in the main name
6ab7e78c51eb 6992272: I18N: Locale.getDisplayName() and toString() return empty if just script is set
naoto
parents: 6840
diff changeset
  1813
        mainName = names.get(0);
6ab7e78c51eb 6992272: I18N: Locale.getDisplayName() and toString() return empty if just script is set
naoto
parents: 6840
diff changeset
  1814
6ab7e78c51eb 6992272: I18N: Locale.getDisplayName() and toString() return empty if just script is set
naoto
parents: 6840
diff changeset
  1815
        // Others are qualifiers
6ab7e78c51eb 6992272: I18N: Locale.getDisplayName() and toString() return empty if just script is set
naoto
parents: 6840
diff changeset
  1816
        int numNames = names.size();
6ab7e78c51eb 6992272: I18N: Locale.getDisplayName() and toString() return empty if just script is set
naoto
parents: 6840
diff changeset
  1817
        qualifierNames = (numNames > 1) ?
6ab7e78c51eb 6992272: I18N: Locale.getDisplayName() and toString() return empty if just script is set
naoto
parents: 6840
diff changeset
  1818
                names.subList(1, numNames).toArray(new String[numNames - 1]) : new String[0];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
        // Create an array whose first element is the number of remaining
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
        // elements.  This serves as a selector into a ChoiceFormat pattern from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
        // the resource.  The second and third elements are the main name and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
        // the qualifier; if there are no qualifiers, the third element is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
        // unused by the format pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
        Object[] displayNames = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
            new Integer(qualifierNames.length != 0 ? 2 : 1),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
            mainName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
            // We could also just call formatList() and have it handle the empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
            // list case, but this is more efficient, and we want it to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
            // efficient since all the language-only locales will not have any
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
            // qualifiers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
            qualifierNames.length != 0 ? formatList(qualifierNames, listPattern, listCompositionPattern) : null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
        if (displayNamePattern != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
            return new MessageFormat(displayNamePattern).format(displayNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
            // If we cannot get the message format pattern, then we use a simple
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
            // hard-coded pattern.  This should not occur in practice unless the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
            // installation is missing some core files (FormatData etc.).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
            StringBuilder result = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
            result.append((String)displayNames[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
            if (displayNames.length > 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
                result.append(" (");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
                result.append((String)displayNames[2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
                result.append(')');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
            return result.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
    /**
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1854
     * Overrides Cloneable.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
    public Object clone()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
            Locale that = (Locale)super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
            return that;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
        } catch (CloneNotSupportedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
            throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
     * Override hashCode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
     * Since Locales are often used in hashtables, caches the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
     * for speed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
     */
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1871
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
        int hc = hashCodeValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
        if (hc == 0) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1875
            hc = baseLocale.hashCode();
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1876
            if (localeExtensions != null) {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1877
                hc ^= localeExtensions.hashCode();
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1878
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
            hashCodeValue = hc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
        return hc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
    // Overrides
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
     * Returns true if this Locale is equal to another object.  A Locale is
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1888
     * deemed equal to another Locale with identical language, script, country,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1889
     * variant and extensions, and unequal to all other objects.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
     * @return true if this Locale is equal to the specified object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
     */
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1893
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
        if (this == obj)                      // quick check
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
        if (!(obj instanceof Locale))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
            return false;
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1899
        BaseLocale otherBase = ((Locale)obj).baseLocale;
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1900
        if (!baseLocale.equals(otherBase)) {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1901
            return false;
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1902
        }
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1903
        if (localeExtensions == null) {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1904
            return ((Locale)obj).localeExtensions == null;
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1905
        }
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1906
        return localeExtensions.equals(((Locale)obj).localeExtensions);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
    // ================= privates =====================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1911
    private transient BaseLocale baseLocale;
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1912
    private transient LocaleExtensions localeExtensions;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
    /**
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1915
     * Calculated hashcode
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
    private transient volatile int hashCodeValue = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
    private static Locale defaultLocale = null;
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  1920
    private static Locale defaultDisplayLocale = null;
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  1921
    private static Locale defaultFormatLocale = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
     * Return an array of the display names of the variant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
     * @param bundle the ResourceBundle to use to get the display names
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
     * @return an array of display names, possible of zero length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
    private String[] getDisplayVariantArray(OpenListResourceBundle bundle, Locale inLocale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
        // Split the variant name into tokens separated by '_'.
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1930
        StringTokenizer tokenizer = new StringTokenizer(baseLocale.getVariant(), "_");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
        String[] names = new String[tokenizer.countTokens()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
        // For each variant token, lookup the display name.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
        // not found, use the variant name itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
        for (int i=0; i<names.length; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
            names[i] = getDisplayString(tokenizer.nextToken(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
                                inLocale, DISPLAY_VARIANT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
        return names;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
     * Format a list using given pattern strings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
     * If either of the patterns is null, then a the list is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
     * formatted by concatenation with the delimiter ','.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
     * @param stringList the list of strings to be formatted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
     * @param listPattern should create a MessageFormat taking 0-3 arguments
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
     * and formatting them into a list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
     * @param listCompositionPattern should take 2 arguments
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
     * and is used by composeList.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
     * @return a string representing the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
    private static String formatList(String[] stringList, String listPattern, String listCompositionPattern) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
        // If we have no list patterns, compose the list in a simple,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
        // non-localized way.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
        if (listPattern == null || listCompositionPattern == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
            StringBuffer result = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
            for (int i=0; i<stringList.length; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
                if (i>0) result.append(',');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
                result.append(stringList[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
            return result.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
        // Compose the list down to three elements if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
        if (stringList.length > 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
            MessageFormat format = new MessageFormat(listCompositionPattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
            stringList = composeList(format, stringList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
        // Rebuild the argument list with the list length as the first element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
        Object[] args = new Object[stringList.length + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
        System.arraycopy(stringList, 0, args, 1, stringList.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
        args[0] = new Integer(stringList.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
        // Format it using the pattern in the resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
        MessageFormat format = new MessageFormat(listPattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
        return format.format(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
     * Given a list of strings, return a list shortened to three elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
     * Shorten it by applying the given format to the first two elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
     * recursively.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
     * @param format a format which takes two arguments
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
     * @param list a list of strings
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
     * @return if the list is three elements or shorter, the same list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
     * otherwise, a new list of three elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
    private static String[] composeList(MessageFormat format, String[] list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
        if (list.length <= 3) return list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
        // Use the given format to compose the first two elements into one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
        String[] listItems = { list[0], list[1] };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
        String newItem = format.format(listItems);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
        // Form a new list one element shorter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
        String[] newList = new String[list.length-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
        System.arraycopy(list, 2, newList, 1, newList.length-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
        newList[0] = newItem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
        // Recurse
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
        return composeList(format, newList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
    /**
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2008
     * @serialField language    String
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2009
     *      language subtag in lower case. (See <a href="java/util/Locale.html#getLanguage()">getLanguage()</a>)
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2010
     * @serialField country     String
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2011
     *      country subtag in upper case. (See <a href="java/util/Locale.html#getCountry()">getCountry()</a>)
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2012
     * @serialField variant     String
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2013
     *      variant subtags separated by LOWLINE characters. (See <a href="java/util/Locale.html#getVariant()">getVariant()</a>)
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2014
     * @serialField hashcode    int
6840
51af1a50aec6 6991013: Serialized form for java.util.Locale contains typos
naoto
parents: 6655
diff changeset
  2015
     *      deprecated, for forward compatibility only
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2016
     * @serialField script      String
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2017
     *      script subtag in title case (See <a href="java/util/Locale.html#getScript()">getScript()</a>)
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2018
     * @serialField extensions  String
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2019
     *      canonical representation of extensions, that is,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2020
     *      BCP47 extensions in alphabetical order followed by
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2021
     *      BCP47 private use subtags, all in lower case letters
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2022
     *      separated by HYPHEN-MINUS characters.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2023
     *      (See <a href="java/util/Locale.html#getExtensionKeys()">getExtensionKeys()</a>,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2024
     *      <a href="java/util/Locale.html#getExtension(char)">getExtension(char)</a>)
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2025
     */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2026
    private static final ObjectStreamField[] serialPersistentFields = {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2027
        new ObjectStreamField("language", String.class),
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2028
        new ObjectStreamField("country", String.class),
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2029
        new ObjectStreamField("variant", String.class),
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2030
        new ObjectStreamField("hashcode", int.class),
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2031
        new ObjectStreamField("script", String.class),
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2032
        new ObjectStreamField("extensions", String.class),
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2033
    };
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2034
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2035
    /**
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2036
     * Serializes this <code>Locale</code> to the specified <code>ObjectOutputStream</code>.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2037
     * @param out the <code>ObjectOutputStream</code> to write
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2038
     * @throws IOException
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2039
     * @since 1.7
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2040
     */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2041
    private void writeObject(ObjectOutputStream out) throws IOException {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2042
        ObjectOutputStream.PutField fields = out.putFields();
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2043
        fields.put("language", baseLocale.getLanguage());
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2044
        fields.put("script", baseLocale.getScript());
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2045
        fields.put("country", baseLocale.getRegion());
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2046
        fields.put("variant", baseLocale.getVariant());
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2047
        fields.put("extensions", localeExtensions == null ? "" : localeExtensions.getID());
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2048
        fields.put("hashcode", -1); // place holder just for backward support
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2049
        out.writeFields();
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2050
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2051
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2052
    /**
6840
51af1a50aec6 6991013: Serialized form for java.util.Locale contains typos
naoto
parents: 6655
diff changeset
  2053
     * Deserializes this <code>Locale</code>.
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2054
     * @param in the <code>ObjectInputStream</code> to read
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2055
     * @throws IOException
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2056
     * @throws ClassNotFoundException
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2057
     * @throws IllformdLocaleException
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2058
     * @since 1.7
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2059
     */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2060
    private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2061
        ObjectInputStream.GetField fields = in.readFields();
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2062
        String language = (String)fields.get("language", "");
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2063
        String script = (String)fields.get("script", "");
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2064
        String country = (String)fields.get("country", "");
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2065
        String variant = (String)fields.get("variant", "");
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2066
        String extStr = (String)fields.get("extensions", "");
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2067
        baseLocale = BaseLocale.getInstance(convertOldISOCodes(language), script, country, variant);
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2068
        if (extStr.length() > 0) {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2069
            try {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2070
                InternalLocaleBuilder bldr = new InternalLocaleBuilder();
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2071
                bldr.setExtensions(extStr);
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2072
                localeExtensions = bldr.getLocaleExtensions();
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2073
            } catch (LocaleSyntaxException e) {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2074
                throw new IllformedLocaleException(e.getMessage());
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2075
            }
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2076
        } else {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2077
            localeExtensions = null;
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2078
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2079
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2080
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2081
    /**
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2082
     * Returns a cached <code>Locale</code> instance equivalent to
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2083
     * the deserialized <code>Locale</code>. When serialized
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2084
     * language, country and variant fields read from the object data stream
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2085
     * are exactly "ja", "JP", "JP" or "th", "TH", "TH" and script/extensions
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2086
     * fields are empty, this method supplies <code>UNICODE_LOCALE_EXTENSION</code>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2087
     * "ca"/"japanese" (calendar type is "japanese") or "nu"/"thai" (number script
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2088
     * type is "thai"). See <a href="Locale.html#special_cases_constructor"/>Special Cases</a>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2089
     * for more information.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2090
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2091
     * @return an instance of <code>Locale</code> equivalent to
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2092
     * the deserialized <code>Locale</code>.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2093
     * @throws java.io.ObjectStreamException
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
    private Object readResolve() throws java.io.ObjectStreamException {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2096
        return getInstance(baseLocale.getLanguage(), baseLocale.getScript(),
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2097
                baseLocale.getRegion(), baseLocale.getVariant(), localeExtensions);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
    private static volatile String[] isoLanguages = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
    private static volatile String[] isoCountries = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2104
    private static String convertOldISOCodes(String language) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
        // we accept both the old and the new ISO codes for the languages whose ISO
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
        // codes have changed, but we always store the OLD code, for backward compatibility
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2107
        language = LocaleUtils.toLowerString(language).intern();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
        if (language == "he") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
            return "iw";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
        } else if (language == "yi") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
            return "ji";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
        } else if (language == "id") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
            return "in";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
            return language;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2119
    private static LocaleExtensions getCompatibilityExtensions(String language,
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2120
                                                               String script,
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2121
                                                               String country,
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2122
                                                               String variant) {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2123
        LocaleExtensions extensions = null;
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2124
        // Special cases for backward compatibility support
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2125
        if (LocaleUtils.caseIgnoreMatch(language, "ja")
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2126
                && script.length() == 0
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2127
                && LocaleUtils.caseIgnoreMatch(country, "jp")
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2128
                && "JP".equals(variant)) {
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2129
            // ja_JP_JP -> u-ca-japanese (calendar = japanese)
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2130
            extensions = LocaleExtensions.CALENDAR_JAPANESE;
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2131
        } else if (LocaleUtils.caseIgnoreMatch(language, "th")
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2132
                && script.length() == 0
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2133
                && LocaleUtils.caseIgnoreMatch(country, "th")
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2134
                && "TH".equals(variant)) {
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2135
            // th_TH_TH -> u-nu-thai (numbersystem = thai)
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2136
            extensions = LocaleExtensions.NUMBER_THAI;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2137
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2138
        return extensions;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2139
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2140
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
     * Obtains a localized locale names from a LocaleNameProvider
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
     * implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
    private static class LocaleNameGetter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
        implements LocaleServiceProviderPool.LocalizedObjectGetter<LocaleNameProvider, String> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
        private static final LocaleNameGetter INSTANCE = new LocaleNameGetter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
        public String getObject(LocaleNameProvider localeNameProvider,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
                                Locale locale,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
                                String key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
                                Object... params) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
            assert params.length == 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
            int type = (Integer)params[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
            String code = (String)params[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
            switch(type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
            case DISPLAY_LANGUAGE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
                return localeNameProvider.getDisplayLanguage(code, locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
            case DISPLAY_COUNTRY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
                return localeNameProvider.getDisplayCountry(code, locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
            case DISPLAY_VARIANT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
                return localeNameProvider.getDisplayVariant(code, locale);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2164
            case DISPLAY_SCRIPT:
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2165
                return localeNameProvider.getDisplayScript(code, locale);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
                assert false; // shouldn't happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
    }
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  2173
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  2174
    /**
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  2175
     * Enum for locale categories.  These locale categories are used to get/set
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  2176
     * the default locale for the specific functionality represented by the
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  2177
     * category.
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  2178
     *
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  2179
     * @see #getDefault(Locale.Category)
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  2180
     * @see #setDefault(Locale.Category, Locale)
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  2181
     * @since 1.7
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  2182
     */
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  2183
    public enum Category {
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  2184
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  2185
        /**
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  2186
         * Category used to represent the default locale for
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  2187
         * displaying user interfaces.
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  2188
         */
7017
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
  2189
        DISPLAY("user.language.display",
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
  2190
                "user.script.display",
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
  2191
                "user.country.display",
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
  2192
                "user.variant.display"),
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  2193
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  2194
        /**
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  2195
         * Category used to represent the default locale for
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  2196
         * formatting dates, numbers, and/or currencies.
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  2197
         */
7017
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
  2198
        FORMAT("user.language.format",
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
  2199
               "user.script.format",
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
  2200
               "user.country.format",
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
  2201
               "user.variant.format");
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
  2202
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
  2203
        Category(String languageKey, String scriptKey, String countryKey, String variantKey) {
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
  2204
            this.languageKey = languageKey;
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
  2205
            this.scriptKey = scriptKey;
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
  2206
            this.countryKey = countryKey;
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
  2207
            this.variantKey = variantKey;
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
  2208
        }
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
  2209
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
  2210
        final String languageKey;
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
  2211
        final String scriptKey;
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
  2212
        final String countryKey;
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
  2213
        final String variantKey;
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  2214
    }
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2215
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2216
    /**
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2217
     * <code>Builder</code> is used to build instances of <code>Locale</code>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2218
     * from values configured by the setters.  Unlike the <code>Locale</code>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2219
     * constructors, the <code>Builder</code> checks if a value configured by a
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2220
     * setter satisfies the syntax requirements defined by the <code>Locale</code>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2221
     * class.  A <code>Locale</code> object created by a <code>Builder</code> is
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2222
     * well-formed and can be transformed to a well-formed IETF BCP 47 language tag
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2223
     * without losing information.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2224
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2225
     * <p><b>Note:</b> The <code>Locale</code> class does not provide any
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2226
     * syntactic restrictions on variant, while BCP 47 requires each variant
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2227
     * subtag to be 5 to 8 alphanumerics or a single numeric followed by 3
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2228
     * alphanumerics.  The method <code>setVariant</code> throws
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2229
     * <code>IllformedLocaleException</code> for a variant that does not satisfy
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2230
     * this restriction. If it is necessary to support such a variant, use a
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2231
     * Locale constructor.  However, keep in mind that a <code>Locale</code>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2232
     * object created this way might lose the variant information when
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2233
     * transformed to a BCP 47 language tag.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2234
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2235
     * <p>The following example shows how to create a <code>Locale</code> object
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2236
     * with the <code>Builder</code>.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2237
     * <blockquote>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2238
     * <pre>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2239
     *     Locale aLocale = new Builder().setLanguage("sr").setScript("Latn").setRegion("RS").build();
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2240
     * </pre>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2241
     * </blockquote>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2242
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2243
     * <p>Builders can be reused; <code>clear()</code> resets all
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2244
     * fields to their default values.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2245
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2246
     * @see Locale#forLanguageTag
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2247
     * @since 1.7
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2248
     */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2249
    public static final class Builder {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2250
        private final InternalLocaleBuilder localeBuilder;
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2251
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2252
        /**
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2253
         * Constructs an empty Builder. The default value of all
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2254
         * fields, extensions, and private use information is the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2255
         * empty string.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2256
         */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2257
        public Builder() {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2258
            localeBuilder = new InternalLocaleBuilder();
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2259
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2260
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2261
        /**
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2262
         * Resets the <code>Builder</code> to match the provided
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2263
         * <code>locale</code>.  Existing state is discarded.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2264
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2265
         * <p>All fields of the locale must be well-formed, see {@link Locale}.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2266
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2267
         * <p>Locales with any ill-formed fields cause
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2268
         * <code>IllformedLocaleException</code> to be thrown, except for the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2269
         * following three cases which are accepted for compatibility
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2270
         * reasons:<ul>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2271
         * <li>Locale("ja", "JP", "JP") is treated as "ja-JP-u-ca-japanese"
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2272
         * <li>Locale("th", "TH", "TH") is treated as "th-TH-u-nu-thai"
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2273
         * <li>Locale("no", "NO", "NY") is treated as "nn-NO"</ul>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2274
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2275
         * @param locale the locale
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2276
         * @return This builder.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2277
         * @throws IllformedLocaleException if <code>locale</code> has
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2278
         * any ill-formed fields.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2279
         * @throws NullPointerException if <code>locale</code> is null.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2280
         */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2281
        public Builder setLocale(Locale locale) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2282
            try {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2283
                localeBuilder.setLocale(locale.baseLocale, locale.localeExtensions);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2284
            } catch (LocaleSyntaxException e) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2285
                throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex());
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2286
            }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2287
            return this;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2288
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2289
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2290
        /**
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2291
         * Resets the Builder to match the provided IETF BCP 47
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2292
         * language tag.  Discards the existing state.  Null and the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2293
         * empty string cause the builder to be reset, like {@link
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2294
         * #clear}.  Grandfathered tags (see {@link
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2295
         * Locale#forLanguageTag}) are converted to their canonical
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2296
         * form before being processed.  Otherwise, the language tag
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2297
         * must be well-formed (see {@link Locale}) or an exception is
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2298
         * thrown (unlike <code>Locale.forLanguageTag</code>, which
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2299
         * just discards ill-formed and following portions of the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2300
         * tag).
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2301
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2302
         * @param languageTag the language tag
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2303
         * @return This builder.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2304
         * @throws IllformedLocaleException if <code>languageTag</code> is ill-formed
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2305
         * @see Locale#forLanguageTag(String)
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2306
         */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2307
        public Builder setLanguageTag(String languageTag) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2308
            ParseStatus sts = new ParseStatus();
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2309
            LanguageTag tag = LanguageTag.parse(languageTag, sts);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2310
            if (sts.isError()) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2311
                throw new IllformedLocaleException(sts.getErrorMessage(), sts.getErrorIndex());
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2312
            }
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2313
            localeBuilder.setLanguageTag(tag);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2314
            return this;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2315
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2316
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2317
        /**
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2318
         * Sets the language.  If <code>language</code> is the empty string or
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2319
         * null, the language in this <code>Builder</code> is removed.  Otherwise,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2320
         * the language must be <a href="./Locale.html#def_language">well-formed</a>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2321
         * or an exception is thrown.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2322
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2323
         * <p>The typical language value is a two or three-letter language
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2324
         * code as defined in ISO639.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2325
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2326
         * @param language the language
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2327
         * @return This builder.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2328
         * @throws IllformedLocaleException if <code>language</code> is ill-formed
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2329
         */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2330
        public Builder setLanguage(String language) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2331
            try {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2332
                localeBuilder.setLanguage(language);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2333
            } catch (LocaleSyntaxException e) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2334
                throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex());
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2335
            }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2336
            return this;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2337
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2338
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2339
        /**
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2340
         * Sets the script. If <code>script</code> is null or the empty string,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2341
         * the script in this <code>Builder</code> is removed.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2342
         * Otherwise, the script must be <a href="./Locale.html#def_script">well-formed</a> or an
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2343
         * exception is thrown.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2344
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2345
         * <p>The typical script value is a four-letter script code as defined by ISO 15924.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2346
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2347
         * @param script the script
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2348
         * @return This builder.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2349
         * @throws IllformedLocaleException if <code>script</code> is ill-formed
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2350
         */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2351
        public Builder setScript(String script) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2352
            try {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2353
                localeBuilder.setScript(script);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2354
            } catch (LocaleSyntaxException e) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2355
                throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex());
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2356
            }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2357
            return this;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2358
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2359
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2360
        /**
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2361
         * Sets the region.  If region is null or the empty string, the region
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2362
         * in this <code>Builder</code> is removed.  Otherwise,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2363
         * the region must be <a href="./Locale.html#def_region">well-formed</a> or an
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2364
         * exception is thrown.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2365
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2366
         * <p>The typical region value is a two-letter ISO 3166 code or a
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2367
         * three-digit UN M.49 area code.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2368
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2369
         * <p>The country value in the <code>Locale</code> created by the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2370
         * <code>Builder</code> is always normalized to upper case.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2371
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2372
         * @param region the region
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2373
         * @return This builder.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2374
         * @throws IllformedLocaleException if <code>region</code> is ill-formed
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2375
         */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2376
        public Builder setRegion(String region) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2377
            try {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2378
                localeBuilder.setRegion(region);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2379
            } catch (LocaleSyntaxException e) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2380
                throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex());
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2381
            }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2382
            return this;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2383
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2384
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2385
        /**
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2386
         * Sets the variant.  If variant is null or the empty string, the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2387
         * variant in this <code>Builder</code> is removed.  Otherwise, it
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2388
         * must consist of one or more <a href="./Locale.html#def_variant">well-formed</a>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2389
         * subtags, or an exception is thrown.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2390
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2391
         * <p><b>Note:</b> This method checks if <code>variant</code>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2392
         * satisfies the IETF BCP 47 variant subtag's syntax requirements,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2393
         * and normalizes the value to lowercase letters.  However,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2394
         * the <code>Locale</code> class does not impose any syntactic
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2395
         * restriction on variant, and the variant value in
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2396
         * <code>Locale</code> is case sensitive.  To set such a variant,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2397
         * use a Locale constructor.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2398
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2399
         * @param variant the variant
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2400
         * @return This builder.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2401
         * @throws IllformedLocaleException if <code>variant</code> is ill-formed
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2402
         */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2403
        public Builder setVariant(String variant) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2404
            try {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2405
                localeBuilder.setVariant(variant);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2406
            } catch (LocaleSyntaxException e) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2407
                throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex());
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2408
            }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2409
            return this;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2410
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2411
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2412
        /**
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2413
         * Sets the extension for the given key. If the value is null or the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2414
         * empty string, the extension is removed.  Otherwise, the extension
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2415
         * must be <a href="./Locale.html#def_extensions">well-formed</a> or an exception
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2416
         * is thrown.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2417
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2418
         * <p><b>Note:</b> The key {@link Locale#UNICODE_LOCALE_EXTENSION
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2419
         * UNICODE_LOCALE_EXTENSION} ('u') is used for the Unicode locale extension.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2420
         * Setting a value for this key replaces any existing Unicode locale key/type
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2421
         * pairs with those defined in the extension.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2422
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2423
         * <p><b>Note:</b> The key {@link Locale#PRIVATE_USE_EXTENSION
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2424
         * PRIVATE_USE_EXTENSION} ('x') is used for the private use code. To be
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2425
         * well-formed, the value for this key needs only to have subtags of one to
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2426
         * eight alphanumeric characters, not two to eight as in the general case.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2427
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2428
         * @param key the extension key
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2429
         * @param value the extension value
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2430
         * @return This builder.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2431
         * @throws IllformedLocaleException if <code>key</code> is illegal
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2432
         * or <code>value</code> is ill-formed
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2433
         * @see #setUnicodeLocaleKeyword(String, String)
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2434
         */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2435
        public Builder setExtension(char key, String value) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2436
            try {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2437
                localeBuilder.setExtension(key, value);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2438
            } catch (LocaleSyntaxException e) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2439
                throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex());
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2440
            }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2441
            return this;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2442
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2443
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2444
        /**
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2445
         * Sets the Unicode locale keyword type for the given key.  If the type
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2446
         * is null, the Unicode keyword is removed.  Otherwise, the key must be
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2447
         * non-null and both key and type must be <a
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2448
         * href="./Locale.html#def_locale_extension">well-formed</a> or an exception
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2449
         * is thrown.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2450
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2451
         * <p>Keys and types are converted to lower case.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2452
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2453
         * <p><b>Note</b>:Setting the 'u' extension via {@link #setExtension}
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2454
         * replaces all Unicode locale keywords with those defined in the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2455
         * extension.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2456
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2457
         * @param key the Unicode locale key
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2458
         * @param type the Unicode locale type
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2459
         * @return This builder.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2460
         * @throws IllformedLocaleException if <code>key</code> or <code>type</code>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2461
         * is ill-formed
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2462
         * @throws NullPointerException if <code>key</code> is null
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2463
         * @see #setExtension(char, String)
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2464
         */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2465
        public Builder setUnicodeLocaleKeyword(String key, String type) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2466
            try {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2467
                localeBuilder.setUnicodeLocaleKeyword(key, type);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2468
            } catch (LocaleSyntaxException e) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2469
                throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex());
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2470
            }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2471
            return this;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2472
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2473
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2474
        /**
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2475
         * Adds a unicode locale attribute, if not already present, otherwise
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2476
         * has no effect.  The attribute must not be null and must be <a
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2477
         * href="./Locale.html#def_locale_extension">well-formed</a> or an exception
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2478
         * is thrown.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2479
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2480
         * @param attribute the attribute
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2481
         * @return This builder.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2482
         * @throws NullPointerException if <code>attribute</code> is null
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2483
         * @throws IllformedLocaleException if <code>attribute</code> is ill-formed
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2484
         * @see #setExtension(char, String)
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2485
         */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2486
        public Builder addUnicodeLocaleAttribute(String attribute) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2487
            try {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2488
                localeBuilder.addUnicodeLocaleAttribute(attribute);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2489
            } catch (LocaleSyntaxException e) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2490
                throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex());
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2491
            }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2492
            return this;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2493
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2494
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2495
        /**
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2496
         * Removes a unicode locale attribute, if present, otherwise has no
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2497
         * effect.  The attribute must not be null and must be <a
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2498
         * href="./Locale.html#def_locale_extension">well-formed</a> or an exception
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2499
         * is thrown.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2500
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2501
         * <p>Attribute comparision for removal is case-insensitive.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2502
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2503
         * @param attribute the attribute
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2504
         * @return This builder.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2505
         * @throws NullPointerException if <code>attribute</code> is null
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2506
         * @throws IllformedLocaleException if <code>attribute</code> is ill-formed
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2507
         * @see #setExtension(char, String)
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2508
         */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2509
        public Builder removeUnicodeLocaleAttribute(String attribute) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2510
            try {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2511
                localeBuilder.removeUnicodeLocaleAttribute(attribute);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2512
            } catch (LocaleSyntaxException e) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2513
                throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex());
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2514
            }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2515
            return this;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2516
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2517
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2518
        /**
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2519
         * Resets the builder to its initial, empty state.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2520
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2521
         * @return This builder.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2522
         */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2523
        public Builder clear() {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2524
            localeBuilder.clear();
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2525
            return this;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2526
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2527
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2528
        /**
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2529
         * Resets the extensions to their initial, empty state.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2530
         * Language, script, region and variant are unchanged.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2531
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2532
         * @return This builder.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2533
         * @see #setExtension(char, String)
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2534
         */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2535
        public Builder clearExtensions() {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2536
            localeBuilder.clearExtensions();
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2537
            return this;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2538
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2539
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2540
        /**
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2541
         * Returns an instance of <code>Locale</code> created from the fields set
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2542
         * on this builder.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2543
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2544
         * <p>This applies the conversions listed in {@link Locale#forLanguageTag}
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2545
         * when constructing a Locale. (Grandfathered tags are handled in
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2546
         * {@link #setLanguageTag}.)
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2547
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2548
         * @return A Locale.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2549
         */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2550
        public Locale build() {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2551
            BaseLocale baseloc = localeBuilder.getBaseLocale();
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2552
            LocaleExtensions extensions = localeBuilder.getLocaleExtensions();
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2553
            if (extensions == null && baseloc.getVariant().length() > 0) {
7513
7091fccbc86d 7002320: Locale.forLanguageTag()/toLanguageTag() not working properly with ja_JP_JP locale
naoto
parents: 7508
diff changeset
  2554
                extensions = getCompatibilityExtensions(baseloc.getLanguage(), baseloc.getScript(),
7091fccbc86d 7002320: Locale.forLanguageTag()/toLanguageTag() not working properly with ja_JP_JP locale
naoto
parents: 7508
diff changeset
  2555
                        baseloc.getRegion(), baseloc.getVariant());
7091fccbc86d 7002320: Locale.forLanguageTag()/toLanguageTag() not working properly with ja_JP_JP locale
naoto
parents: 7508
diff changeset
  2556
            }
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2557
            return Locale.getInstance(baseloc, extensions);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2558
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2559
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2560
}