jdk/src/share/classes/java/util/Locale.java
author yhuang
Wed, 14 Mar 2012 23:07:21 -0700
changeset 12135 078775fa4b30
parent 11282 2c8538c9daa6
child 13583 dc0017b1a452
permissions -rw-r--r--
7085757: Currency Data: ISO 4217 Amendment 152 Reviewed-by: naoto
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
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
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   740
        return defaultLocale;
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   741
    }
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   742
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
     * 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
   745
     * for this instance of the Java Virtual Machine.
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   746
     * <p>
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   747
     * 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
   748
     * 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
   749
     * 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
   750
     * setDefault(Locale.Category, Locale) method.
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   751
     *
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   752
     * @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
   753
     * @throws NullPointerException - if category is null
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   754
     * @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
   755
     *     of the Java Virtual Machine
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   756
     * @see #setDefault(Locale.Category, Locale)
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   757
     * @since 1.7
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   758
     */
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   759
    public static Locale getDefault(Locale.Category category) {
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   760
        // do not synchronize this method - see 4071298
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   761
        switch (category) {
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   762
        case DISPLAY:
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   763
            if (defaultDisplayLocale == null) {
11282
2c8538c9daa6 4808233: "Locale" not thread-safe
naoto
parents: 10419
diff changeset
   764
                synchronized(Locale.class) {
2c8538c9daa6 4808233: "Locale" not thread-safe
naoto
parents: 10419
diff changeset
   765
                    if (defaultDisplayLocale == null) {
2c8538c9daa6 4808233: "Locale" not thread-safe
naoto
parents: 10419
diff changeset
   766
                        defaultDisplayLocale = initDefault(category);
2c8538c9daa6 4808233: "Locale" not thread-safe
naoto
parents: 10419
diff changeset
   767
                    }
2c8538c9daa6 4808233: "Locale" not thread-safe
naoto
parents: 10419
diff changeset
   768
                }
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   769
            }
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   770
            return defaultDisplayLocale;
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   771
        case FORMAT:
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   772
            if (defaultFormatLocale == null) {
11282
2c8538c9daa6 4808233: "Locale" not thread-safe
naoto
parents: 10419
diff changeset
   773
                synchronized(Locale.class) {
2c8538c9daa6 4808233: "Locale" not thread-safe
naoto
parents: 10419
diff changeset
   774
                    if (defaultFormatLocale == null) {
2c8538c9daa6 4808233: "Locale" not thread-safe
naoto
parents: 10419
diff changeset
   775
                        defaultFormatLocale = initDefault(category);
2c8538c9daa6 4808233: "Locale" not thread-safe
naoto
parents: 10419
diff changeset
   776
                    }
2c8538c9daa6 4808233: "Locale" not thread-safe
naoto
parents: 10419
diff changeset
   777
                }
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   778
            }
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   779
            return defaultFormatLocale;
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   780
        default:
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   781
            assert false: "Unknown Category";
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
        return getDefault();
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   784
    }
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   785
11282
2c8538c9daa6 4808233: "Locale" not thread-safe
naoto
parents: 10419
diff changeset
   786
    private static Locale initDefault() {
7017
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
   787
        String language, region, script, country, variant;
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   788
        language = AccessController.doPrivileged(
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   789
            new GetPropertyAction("user.language", "en"));
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   790
        // for compatibility, check for old user.region property
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   791
        region = AccessController.doPrivileged(
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   792
            new GetPropertyAction("user.region"));
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   793
        if (region != null) {
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   794
            // 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
   795
            int i = region.indexOf('_');
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   796
            if (i >= 0) {
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   797
                country = region.substring(0, i);
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   798
                variant = region.substring(i + 1);
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   799
            } else {
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   800
                country = region;
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   801
                variant = "";
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   802
            }
7017
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
   803
            script = "";
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   804
        } else {
7017
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
   805
            script = AccessController.doPrivileged(
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
   806
                new GetPropertyAction("user.script", ""));
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   807
            country = AccessController.doPrivileged(
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   808
                new GetPropertyAction("user.country", ""));
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   809
            variant = AccessController.doPrivileged(
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   810
                new GetPropertyAction("user.variant", ""));
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   811
        }
11282
2c8538c9daa6 4808233: "Locale" not thread-safe
naoto
parents: 10419
diff changeset
   812
2c8538c9daa6 4808233: "Locale" not thread-safe
naoto
parents: 10419
diff changeset
   813
        return getInstance(language, script, country, variant, null);
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   814
    }
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   815
11282
2c8538c9daa6 4808233: "Locale" not thread-safe
naoto
parents: 10419
diff changeset
   816
    private static Locale initDefault(Locale.Category category) {
2c8538c9daa6 4808233: "Locale" not thread-safe
naoto
parents: 10419
diff changeset
   817
        return getInstance(
7017
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
   818
            AccessController.doPrivileged(
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
   819
                new GetPropertyAction(category.languageKey, defaultLocale.getLanguage())),
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
   820
            AccessController.doPrivileged(
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
   821
                new GetPropertyAction(category.scriptKey, defaultLocale.getScript())),
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
   822
            AccessController.doPrivileged(
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
   823
                new GetPropertyAction(category.countryKey, defaultLocale.getCountry())),
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
   824
            AccessController.doPrivileged(
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
   825
                new GetPropertyAction(category.variantKey, defaultLocale.getVariant())),
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
   826
            null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     * Sets the default locale for this instance of the Java Virtual Machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     * This does not affect the host locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     * If there is a security manager, its <code>checkPermission</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     * method is called with a <code>PropertyPermission("user.language", "write")</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     * permission before the default locale is changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     * The Java Virtual Machine sets the default locale during startup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     * based on the host environment. It is used by many locale-sensitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     * methods if no locale is explicitly specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     * Since changing the default locale may affect many different areas
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     * of functionality, this method should only be used if the caller
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     * is prepared to reinitialize locale-sensitive code running
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     * within the same Java Virtual Machine.
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   845
     * <p>
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   846
     * 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
   847
     * locales for each Category are also set to the specified default locale.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     * @throws SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     *        if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     *        <code>checkPermission</code> method doesn't allow the operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     * @throws NullPointerException if <code>newLocale</code> is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     * @param newLocale the new default locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     * @see SecurityManager#checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * @see java.util.PropertyPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
    public static synchronized void setDefault(Locale newLocale) {
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   858
        setDefault(Category.DISPLAY, newLocale);
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   859
        setDefault(Category.FORMAT, newLocale);
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   860
        defaultLocale = newLocale;
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   861
    }
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   862
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   863
    /**
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   864
     * 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
   865
     * 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
   866
     * <p>
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   867
     * 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
   868
     * with a PropertyPermission("user.language", "write") permission before
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   869
     * the default locale is changed.
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   870
     * <p>
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   871
     * 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
   872
     * 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
   873
     * if no locale is explicitly specified.
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   874
     * <p>
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   875
     * 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
   876
     * 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
   877
     * prepared to reinitialize locale-sensitive code running within the
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   878
     * same Java Virtual Machine.
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   879
     * <p>
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   880
     *
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   881
     * @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
   882
     * @param newLocale - the new default locale
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   883
     * @throws SecurityException - if a security manager exists and its
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   884
     *     checkPermission method doesn't allow the operation.
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   885
     * @throws NullPointerException - if category and/or newLocale is null
7508
9782d88e18a3 7000507: javadoc warnings in java.util.Locale
naoto
parents: 7017
diff changeset
   886
     * @see SecurityManager#checkPermission(java.security.Permission)
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   887
     * @see PropertyPermission
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   888
     * @see #getDefault(Locale.Category)
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   889
     * @since 1.7
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
    public static synchronized void setDefault(Locale.Category category,
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   892
        Locale newLocale) {
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   893
        if (category == null)
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   894
            throw new NullPointerException("Category cannot be NULL");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        if (newLocale == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
            throw new NullPointerException("Can't set default locale to NULL");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        if (sm != null) sm.checkPermission(new PropertyPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                        ("user.language", "write"));
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   901
        switch (category) {
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   902
        case DISPLAY:
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   903
            defaultDisplayLocale = newLocale;
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   904
            break;
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   905
        case FORMAT:
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   906
            defaultFormatLocale = newLocale;
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   907
            break;
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   908
        default:
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   909
            assert false: "Unknown Category";
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   910
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
     * Returns an array of all installed locales.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     * The returned array represents the union of locales supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     * by the Java runtime environment and by installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     * {@link java.util.spi.LocaleServiceProvider LocaleServiceProvider}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     * implementations.  It must contain at least a <code>Locale</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     * instance equal to {@link java.util.Locale#US Locale.US}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     * @return An array of installed locales.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
    public static Locale[] getAvailableLocales() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        return LocaleServiceProviderPool.getAllAvailableLocales();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     * Returns a list of all 2-letter country codes defined in ISO 3166.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     * Can be used to create Locales.
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   930
     * <p>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   931
     * <b>Note:</b> The <code>Locale</code> class also supports other codes for
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   932
     * country (region), such as 3-letter numeric UN M.49 area codes.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   933
     * Therefore, the list returned by this method does not contain ALL valid
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   934
     * codes that can be used to create Locales.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
    public static String[] getISOCountries() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
        if (isoCountries == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
            isoCountries = getISO2Table(LocaleISOData.isoCountryTable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        String[] result = new String[isoCountries.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
        System.arraycopy(isoCountries, 0, result, 0, isoCountries.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     * Returns a list of all 2-letter language codes defined in ISO 639.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     * Can be used to create Locales.
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   948
     * <p>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   949
     * <b>Note:</b>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   950
     * <ul>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   951
     * <li>ISO 639 is not a stable standard&mdash; some languages' codes have changed.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     * 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
   953
     * languages whose codes have changed.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   954
     * <li>The <code>Locale</code> class also supports language codes up to
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   955
     * 8 characters in length.  Therefore, the list returned by this method does
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   956
     * not contain ALL valid codes that can be used to create Locales.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   957
     * </ul>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
    public static String[] getISOLanguages() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
        if (isoLanguages == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
            isoLanguages = getISO2Table(LocaleISOData.isoLanguageTable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
        String[] result = new String[isoLanguages.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
        System.arraycopy(isoLanguages, 0, result, 0, isoLanguages.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
    private static final String[] getISO2Table(String table) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        int len = table.length() / 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        String[] isoTable = new String[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
        for (int i = 0, j = 0; i < len; i++, j += 5) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
            isoTable[i] = table.substring(j, j + 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
        return isoTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
    /**
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   978
     * Returns the language code of this Locale.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   979
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   980
     * <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
   981
     * Locale's constructor recognizes both the new and the old codes for the languages
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     * 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
   983
     * want to check for a specific language whose code has changed, don't do
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   984
     * <pre>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   985
     * if (locale.getLanguage().equals("he")) // BAD!
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
     *    ...
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   987
     * </pre>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   988
     * Instead, do
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   989
     * <pre>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   990
     * if (locale.getLanguage().equals(new Locale("he").getLanguage()))
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   991
     *    ...
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   992
     * </pre>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
   993
     * @return The language code, or the empty string if none is defined.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
     * @see #getDisplayLanguage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
    public String getLanguage() {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
   997
        return baseLocale.getLanguage();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
    /**
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1001
     * Returns the script for this locale, which should
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1002
     * either be the empty string or an ISO 15924 4-letter script
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1003
     * code. The first letter is uppercase and the rest are
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1004
     * lowercase, for example, 'Latn', 'Cyrl'.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1005
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1006
     * @return The script code, or the empty string if none is defined.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1007
     * @see #getDisplayScript
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1008
     * @since 1.7
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1009
     */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1010
    public String getScript() {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1011
        return baseLocale.getScript();
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1012
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1013
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1014
    /**
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1015
     * Returns the country/region code for this locale, which should
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1016
     * either be the empty string, an uppercase ISO 3166 2-letter code,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1017
     * or a UN M.49 3-digit code.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1018
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1019
     * @return The country/region code, or the empty string if none is defined.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     * @see #getDisplayCountry
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
    public String getCountry() {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1023
        return baseLocale.getRegion();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     * Returns the variant code for this locale.
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1028
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1029
     * @return The variant code, or the empty string if none is defined.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     * @see #getDisplayVariant
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
    public String getVariant() {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1033
        return baseLocale.getVariant();
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1034
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1035
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1036
    /**
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1037
     * Returns the extension (or private use) value associated with
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1038
     * the specified key, or null if there is no extension
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1039
     * associated with the key. To be well-formed, the key must be one
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1040
     * of <code>[0-9A-Za-z]</code>. Keys are case-insensitive, so
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1041
     * for example 'z' and 'Z' represent the same extension.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1042
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1043
     * @param key the extension key
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1044
     * @return The extension, or null if this locale defines no
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1045
     * extension for the specified key.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1046
     * @throws IllegalArgumentException if key is not well-formed
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1047
     * @see #PRIVATE_USE_EXTENSION
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1048
     * @see #UNICODE_LOCALE_EXTENSION
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1049
     * @since 1.7
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1050
     */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1051
    public String getExtension(char key) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1052
        if (!LocaleExtensions.isValidKey(key)) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1053
            throw new IllegalArgumentException("Ill-formed extension key: " + key);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1054
        }
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1055
        return (localeExtensions == null) ? null : localeExtensions.getExtensionValue(key);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1056
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1057
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1058
    /**
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1059
     * Returns the set of extension keys associated with this locale, or the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1060
     * empty set if it has no extensions. The returned set is unmodifiable.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1061
     * The keys will all be lower-case.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1062
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1063
     * @return The set of extension keys, or the empty set if this locale has
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1064
     * no extensions.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1065
     * @since 1.7
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1066
     */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1067
    public Set<Character> getExtensionKeys() {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1068
        if (localeExtensions == null) {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1069
            return Collections.emptySet();
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1070
        }
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1071
        return localeExtensions.getKeys();
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1072
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1073
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1074
    /**
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1075
     * Returns the set of unicode locale attributes associated with
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1076
     * this locale, or the empty set if it has no attributes. The
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1077
     * returned set is unmodifiable.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1078
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1079
     * @return The set of attributes.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1080
     * @since 1.7
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1081
     */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1082
    public Set<String> getUnicodeLocaleAttributes() {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1083
        if (localeExtensions == null) {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1084
            return Collections.emptySet();
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1085
        }
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1086
        return localeExtensions.getUnicodeLocaleAttributes();
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1087
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1088
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1089
    /**
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1090
     * Returns the Unicode locale type associated with the specified Unicode locale key
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1091
     * 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
  1092
     * 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
  1093
     * be two alphanumeric characters ([0-9a-zA-Z]), or an IllegalArgumentException is
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1094
     * thrown.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1095
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1096
     * @param key the Unicode locale key
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1097
     * @return The Unicode locale type associated with the key, or null if the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1098
     * locale does not define the key.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1099
     * @throws IllegalArgumentException if the key is not well-formed
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1100
     * @throws NullPointerException if <code>key</code> is null
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1101
     * @since 1.7
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1102
     */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1103
    public String getUnicodeLocaleType(String key) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1104
        if (!UnicodeLocaleExtension.isKey(key)) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1105
            throw new IllegalArgumentException("Ill-formed Unicode locale key: " + key);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1106
        }
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1107
        return (localeExtensions == null) ? null : localeExtensions.getUnicodeLocaleType(key);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1108
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1109
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1110
    /**
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1111
     * 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
  1112
     * this locale has none.  The returned set is immutable.  Keys are all lower case.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1113
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1114
     * @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
  1115
     * no Unicode locale keywords.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1116
     * @since 1.7
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1117
     */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1118
    public Set<String> getUnicodeLocaleKeys() {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1119
        if (localeExtensions == null) {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1120
            return Collections.emptySet();
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1121
        }
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1122
        return localeExtensions.getUnicodeLocaleKeys();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
    /**
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1126
     * Package locale method returning the Locale's BaseLocale,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1127
     * used by ResourceBundle
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1128
     * @return base locale of this Locale
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1129
     */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1130
    BaseLocale getBaseLocale() {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1131
        return baseLocale;
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1132
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1133
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1134
    /**
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1135
     * Package private method returning the Locale's LocaleExtensions,
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1136
     * used by ResourceBundle.
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1137
     * @return locale exnteions of this Locale,
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1138
     *         or {@code null} if no extensions are defined
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1139
     */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1140
     LocaleExtensions getLocaleExtensions() {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1141
         return localeExtensions;
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
    /**
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1145
     * Returns a string representation of this <code>Locale</code>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1146
     * object, consisting of language, country, variant, script,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1147
     * and extensions as below:
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1148
     * <p><blockquote>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1149
     * language + "_" + country + "_" + (variant + "_#" | "#") + script + "-" + extensions
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1150
     * </blockquote>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1151
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1152
     * Language is always lower case, country is always upper case, script is always title
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1153
     * case, and extensions are always lower case.  Extensions and private use subtags
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1154
     * will be in canonical order as explained in {@link #toLanguageTag}.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1155
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1156
     * <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
  1157
     * Java 6 and prior.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1158
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1159
     * <p>If both the language and country fields are missing, this function will return
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1160
     * the empty string, even if the variant, script, or extensions field is present (you
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1161
     * 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
  1162
     * language or country code).
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1163
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1164
     * <p>If script or extensions are present and variant is missing, no underscore is
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1165
     * added before the "#".
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1166
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1167
     * <p>This behavior is designed to support debugging and to be compatible with
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1168
     * previous uses of <code>toString</code> that expected language, country, and variant
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1169
     * fields only.  To represent a Locale as a String for interchange purposes, use
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1170
     * {@link #toLanguageTag}.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1171
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1172
     * <p>Examples: <ul><tt>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1173
     * <li>en
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1174
     * <li>de_DE
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1175
     * <li>_GB
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1176
     * <li>en_US_WIN
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1177
     * <li>de__POSIX
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1178
     * <li>zh_CN_#Hans
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1179
     * <li>zh_TW_#Hant-x-java
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1180
     * <li>th_TH_TH_#u-nu-thai</tt></ul>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1181
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1182
     * @return A string representation of the Locale, for debugging.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
     * @see #getDisplayName
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1184
     * @see #toLanguageTag
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
     */
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1186
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
    public final String toString() {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1188
        boolean l = (baseLocale.getLanguage().length() != 0);
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1189
        boolean s = (baseLocale.getScript().length() != 0);
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1190
        boolean r = (baseLocale.getRegion().length() != 0);
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1191
        boolean v = (baseLocale.getVariant().length() != 0);
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1192
        boolean e = (localeExtensions != null && localeExtensions.getID().length() != 0);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1193
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1194
        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
  1195
        if (r || (l && (v || s || e))) {
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1196
            result.append('_')
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1197
                .append(baseLocale.getRegion()); // This may just append '_'
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1198
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1199
        if (v && (l || r)) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1200
            result.append('_')
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1201
                .append(baseLocale.getVariant());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
        }
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1203
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1204
        if (s && (l || r)) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1205
            result.append("_#")
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1206
                .append(baseLocale.getScript());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
        }
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1208
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1209
        if (e && (l || r)) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1210
            result.append('_');
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1211
            if (!s) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1212
                result.append('#');
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1213
            }
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1214
            result.append(localeExtensions.getID());
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1215
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1216
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
        return result.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
    /**
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1221
     * Returns a well-formed IETF BCP 47 language tag representing
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1222
     * this locale.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1223
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1224
     * <p>If this <code>Locale</code> has a language, country, or
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1225
     * variant that does not satisfy the IETF BCP 47 language tag
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1226
     * syntax requirements, this method handles these fields as
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1227
     * described below:
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1228
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1229
     * <p><b>Language:</b> If language is empty, or not <a
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1230
     * href="#def_language" >well-formed</a> (for example "a" or
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1231
     * "e2"), it will be emitted as "und" (Undetermined).
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1232
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1233
     * <p><b>Country:</b> If country is not <a
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1234
     * href="#def_region">well-formed</a> (for example "12" or "USA"),
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1235
     * it will be omitted.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1236
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1237
     * <p><b>Variant:</b> If variant <b>is</b> <a
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1238
     * href="#def_variant">well-formed</a>, each sub-segment
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1239
     * (delimited by '-' or '_') is emitted as a subtag.  Otherwise:
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1240
     * <ul>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1241
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1242
     * <li>if all sub-segments match <code>[0-9a-zA-Z]{1,8}</code>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1243
     * (for example "WIN" or "Oracle_JDK_Standard_Edition"), the first
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1244
     * ill-formed sub-segment and all following will be appended to
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1245
     * the private use subtag.  The first appended subtag will be
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1246
     * "lvariant", followed by the sub-segments in order, separated by
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1247
     * hyphen. For example, "x-lvariant-WIN",
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1248
     * "Oracle-x-lvariant-JDK-Standard-Edition".
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1249
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1250
     * <li>if any sub-segment does not match
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1251
     * <code>[0-9a-zA-Z]{1,8}</code>, the variant will be truncated
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1252
     * and the problematic sub-segment and all following sub-segments
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1253
     * will be omitted.  If the remainder is non-empty, it will be
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1254
     * emitted as a private use subtag as above (even if the remainder
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1255
     * turns out to be well-formed).  For example,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1256
     * "Solaris_isjustthecoolestthing" is emitted as
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1257
     * "x-lvariant-Solaris", not as "solaris".</li></ul>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1258
     *
7508
9782d88e18a3 7000507: javadoc warnings in java.util.Locale
naoto
parents: 7017
diff changeset
  1259
     * <p><b>Special Conversions:</b> Java supports some old locale
9782d88e18a3 7000507: javadoc warnings in java.util.Locale
naoto
parents: 7017
diff changeset
  1260
     * representations, including deprecated ISO language codes,
9782d88e18a3 7000507: javadoc warnings in java.util.Locale
naoto
parents: 7017
diff changeset
  1261
     * for compatibility. This method performs the following
9782d88e18a3 7000507: javadoc warnings in java.util.Locale
naoto
parents: 7017
diff changeset
  1262
     * conversions:
9782d88e18a3 7000507: javadoc warnings in java.util.Locale
naoto
parents: 7017
diff changeset
  1263
     * <ul>
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1264
     *
7508
9782d88e18a3 7000507: javadoc warnings in java.util.Locale
naoto
parents: 7017
diff changeset
  1265
     * <li>Deprecated ISO language codes "iw", "ji", and "in" are
9782d88e18a3 7000507: javadoc warnings in java.util.Locale
naoto
parents: 7017
diff changeset
  1266
     * converted to "he", "yi", and "id", respectively.
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1267
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1268
     * <li>A locale with language "no", country "NO", and variant
7508
9782d88e18a3 7000507: javadoc warnings in java.util.Locale
naoto
parents: 7017
diff changeset
  1269
     * "NY", representing Norwegian Nynorsk (Norway), is converted
9782d88e18a3 7000507: javadoc warnings in java.util.Locale
naoto
parents: 7017
diff changeset
  1270
     * to a language tag "nn-NO".</li></ul>
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1271
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1272
     * <p><b>Note:</b> Although the language tag created by this
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1273
     * method is well-formed (satisfies the syntax requirements
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1274
     * defined by the IETF BCP 47 specification), it is not
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1275
     * necessarily a valid BCP 47 language tag.  For example,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1276
     * <pre>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1277
     *   new Locale("xx", "YY").toLanguageTag();</pre>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1278
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1279
     * will return "xx-YY", but the language subtag "xx" and the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1280
     * region subtag "YY" are invalid because they are not registered
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1281
     * in the IANA Language Subtag Registry.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1282
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1283
     * @return a BCP47 language tag representing the locale
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1284
     * @see #forLanguageTag(String)
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1285
     * @since 1.7
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1286
     */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1287
    public String toLanguageTag() {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1288
        LanguageTag tag = LanguageTag.parseLocale(baseLocale, localeExtensions);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1289
        StringBuilder buf = new StringBuilder();
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1290
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1291
        String subtag = tag.getLanguage();
8149
768769e3cddd 7015500: Locale.toLanguageTag() uses "und" as lang subtag for private use only Locale
naoto
parents: 7783
diff changeset
  1292
        if (subtag.length() > 0) {
768769e3cddd 7015500: Locale.toLanguageTag() uses "und" as lang subtag for private use only Locale
naoto
parents: 7783
diff changeset
  1293
            buf.append(LanguageTag.canonicalizeLanguage(subtag));
768769e3cddd 7015500: Locale.toLanguageTag() uses "und" as lang subtag for private use only Locale
naoto
parents: 7783
diff changeset
  1294
        }
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1295
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1296
        subtag = tag.getScript();
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1297
        if (subtag.length() > 0) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1298
            buf.append(LanguageTag.SEP);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1299
            buf.append(LanguageTag.canonicalizeScript(subtag));
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1300
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1301
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1302
        subtag = tag.getRegion();
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1303
        if (subtag.length() > 0) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1304
            buf.append(LanguageTag.SEP);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1305
            buf.append(LanguageTag.canonicalizeRegion(subtag));
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1306
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1307
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1308
        List<String>subtags = tag.getVariants();
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1309
        for (String s : subtags) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1310
            buf.append(LanguageTag.SEP);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1311
            // preserve casing
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1312
            buf.append(s);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1313
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1314
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1315
        subtags = tag.getExtensions();
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1316
        for (String s : subtags) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1317
            buf.append(LanguageTag.SEP);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1318
            buf.append(LanguageTag.canonicalizeExtension(s));
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1319
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1320
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1321
        subtag = tag.getPrivateuse();
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1322
        if (subtag.length() > 0) {
8149
768769e3cddd 7015500: Locale.toLanguageTag() uses "und" as lang subtag for private use only Locale
naoto
parents: 7783
diff changeset
  1323
            if (buf.length() > 0) {
768769e3cddd 7015500: Locale.toLanguageTag() uses "und" as lang subtag for private use only Locale
naoto
parents: 7783
diff changeset
  1324
                buf.append(LanguageTag.SEP);
768769e3cddd 7015500: Locale.toLanguageTag() uses "und" as lang subtag for private use only Locale
naoto
parents: 7783
diff changeset
  1325
            }
768769e3cddd 7015500: Locale.toLanguageTag() uses "und" as lang subtag for private use only Locale
naoto
parents: 7783
diff changeset
  1326
            buf.append(LanguageTag.PRIVATEUSE).append(LanguageTag.SEP);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1327
            // preserve casing
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1328
            buf.append(subtag);
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
        return buf.toString();
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1332
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1333
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1334
    /**
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1335
     * Returns a locale for the specified IETF BCP 47 language tag string.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1336
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1337
     * <p>If the specified language tag contains any ill-formed subtags,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1338
     * the first such subtag and all following subtags are ignored.  Compare
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1339
     * to {@link Locale.Builder#setLanguageTag} which throws an exception
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1340
     * in this case.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1341
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1342
     * <p>The following <b>conversions</b> are performed:<ul>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1343
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1344
     * <li>The language code "und" is mapped to language "".
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1345
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1346
     * <li>The language codes "he", "yi", and "id" are mapped to "iw",
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1347
     * "ji", and "in" respectively. (This is the same canonicalization
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1348
     * that's done in Locale's constructors.)
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1349
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1350
     * <li>The portion of a private use subtag prefixed by "lvariant",
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1351
     * if any, is removed and appended to the variant field in the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1352
     * result locale (without case normalization).  If it is then
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1353
     * empty, the private use subtag is discarded:
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1354
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1355
     * <pre>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1356
     *     Locale loc;
7508
9782d88e18a3 7000507: javadoc warnings in java.util.Locale
naoto
parents: 7017
diff changeset
  1357
     *     loc = Locale.forLanguageTag("en-US-x-lvariant-POSIX");
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1358
     *     loc.getVariant(); // returns "POSIX"
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1359
     *     loc.getExtension('x'); // returns null
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1360
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1361
     *     loc = Locale.forLanguageTag("de-POSIX-x-URP-lvariant-Abc-Def");
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1362
     *     loc.getVariant(); // returns "POSIX_Abc_Def"
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1363
     *     loc.getExtension('x'); // returns "urp"
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1364
     * </pre>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1365
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1366
     * <li>When the languageTag argument contains an extlang subtag,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1367
     * the first such subtag is used as the language, and the primary
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1368
     * language subtag and other extlang subtags are ignored:
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1369
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1370
     * <pre>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1371
     *     Locale.forLanguageTag("ar-aao").getLanguage(); // returns "aao"
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1372
     *     Locale.forLanguageTag("en-abc-def-us").toString(); // returns "abc_US"
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1373
     * </pre>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1374
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1375
     * <li>Case is normalized except for variant tags, which are left
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1376
     * unchanged.  Language is normalized to lower case, script to
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1377
     * title case, country to upper case, and extensions to lower
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1378
     * case.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1379
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1380
     * <li>If, after processing, the locale would exactly match either
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1381
     * ja_JP_JP or th_TH_TH with no extensions, the appropriate
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1382
     * extensions are added as though the constructor had been called:
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1383
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1384
     * <pre>
7508
9782d88e18a3 7000507: javadoc warnings in java.util.Locale
naoto
parents: 7017
diff changeset
  1385
     *    Locale.forLanguageTag("ja-JP-x-lvariant-JP").toLanguageTag();
9782d88e18a3 7000507: javadoc warnings in java.util.Locale
naoto
parents: 7017
diff changeset
  1386
     *    // returns "ja-JP-u-ca-japanese-x-lvariant-JP"
9782d88e18a3 7000507: javadoc warnings in java.util.Locale
naoto
parents: 7017
diff changeset
  1387
     *    Locale.forLanguageTag("th-TH-x-lvariant-TH").toLanguageTag();
9782d88e18a3 7000507: javadoc warnings in java.util.Locale
naoto
parents: 7017
diff changeset
  1388
     *    // returns "th-TH-u-nu-thai-x-lvariant-TH"
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1389
     * <pre></ul>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1390
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1391
     * <p>This implements the 'Language-Tag' production of BCP47, and
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1392
     * so supports grandfathered (regular and irregular) as well as
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1393
     * private use language tags.  Stand alone private use tags are
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1394
     * represented as empty language and extension 'x-whatever',
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1395
     * and grandfathered tags are converted to their canonical replacements
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1396
     * where they exist.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1397
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1398
     * <p>Grandfathered tags with canonical replacements are as follows:
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1399
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1400
     * <table>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1401
     * <tbody align="center">
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1402
     * <tr><th>grandfathered tag</th><th>&nbsp;</th><th>modern replacement</th></tr>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1403
     * <tr><td>art-lojban</td><td>&nbsp;</td><td>jbo</td></tr>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1404
     * <tr><td>i-ami</td><td>&nbsp;</td><td>ami</td></tr>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1405
     * <tr><td>i-bnn</td><td>&nbsp;</td><td>bnn</td></tr>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1406
     * <tr><td>i-hak</td><td>&nbsp;</td><td>hak</td></tr>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1407
     * <tr><td>i-klingon</td><td>&nbsp;</td><td>tlh</td></tr>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1408
     * <tr><td>i-lux</td><td>&nbsp;</td><td>lb</td></tr>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1409
     * <tr><td>i-navajo</td><td>&nbsp;</td><td>nv</td></tr>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1410
     * <tr><td>i-pwn</td><td>&nbsp;</td><td>pwn</td></tr>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1411
     * <tr><td>i-tao</td><td>&nbsp;</td><td>tao</td></tr>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1412
     * <tr><td>i-tay</td><td>&nbsp;</td><td>tay</td></tr>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1413
     * <tr><td>i-tsu</td><td>&nbsp;</td><td>tsu</td></tr>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1414
     * <tr><td>no-bok</td><td>&nbsp;</td><td>nb</td></tr>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1415
     * <tr><td>no-nyn</td><td>&nbsp;</td><td>nn</td></tr>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1416
     * <tr><td>sgn-BE-FR</td><td>&nbsp;</td><td>sfb</td></tr>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1417
     * <tr><td>sgn-BE-NL</td><td>&nbsp;</td><td>vgt</td></tr>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1418
     * <tr><td>sgn-CH-DE</td><td>&nbsp;</td><td>sgg</td></tr>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1419
     * <tr><td>zh-guoyu</td><td>&nbsp;</td><td>cmn</td></tr>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1420
     * <tr><td>zh-hakka</td><td>&nbsp;</td><td>hak</td></tr>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1421
     * <tr><td>zh-min-nan</td><td>&nbsp;</td><td>nan</td></tr>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1422
     * <tr><td>zh-xiang</td><td>&nbsp;</td><td>hsn</td></tr>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1423
     * </tbody>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1424
     * </table>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1425
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1426
     * <p>Grandfathered tags with no modern replacement will be
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1427
     * converted as follows:
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1428
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1429
     * <table>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1430
     * <tbody align="center">
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1431
     * <tr><th>grandfathered tag</th><th>&nbsp;</th><th>converts to</th></tr>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1432
     * <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
  1433
     * <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
  1434
     * <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
  1435
     * <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
  1436
     * <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
  1437
     * <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
  1438
     * </tbody>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1439
     * </table>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1440
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1441
     * <p>For a list of all grandfathered tags, see the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1442
     * IANA Language Subtag Registry (search for "Type: grandfathered").
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1443
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1444
     * <p><b>Note</b>: there is no guarantee that <code>toLanguageTag</code>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1445
     * and <code>forLanguageTag</code> will round-trip.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1446
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1447
     * @param languageTag the language tag
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1448
     * @return The locale that best represents the language tag.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1449
     * @throws NullPointerException if <code>languageTag</code> is <code>null</code>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1450
     * @see #toLanguageTag()
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1451
     * @see java.util.Locale.Builder#setLanguageTag(String)
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1452
     * @since 1.7
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1453
     */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1454
    public static Locale forLanguageTag(String languageTag) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1455
        LanguageTag tag = LanguageTag.parse(languageTag, null);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1456
        InternalLocaleBuilder bldr = new InternalLocaleBuilder();
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1457
        bldr.setLanguageTag(tag);
7513
7091fccbc86d 7002320: Locale.forLanguageTag()/toLanguageTag() not working properly with ja_JP_JP locale
naoto
parents: 7508
diff changeset
  1458
        BaseLocale base = bldr.getBaseLocale();
7091fccbc86d 7002320: Locale.forLanguageTag()/toLanguageTag() not working properly with ja_JP_JP locale
naoto
parents: 7508
diff changeset
  1459
        LocaleExtensions exts = bldr.getLocaleExtensions();
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1460
        if (exts == null && base.getVariant().length() > 0) {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1461
            exts = getCompatibilityExtensions(base.getLanguage(), base.getScript(),
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1462
                                              base.getRegion(), base.getVariant());
7513
7091fccbc86d 7002320: Locale.forLanguageTag()/toLanguageTag() not working properly with ja_JP_JP locale
naoto
parents: 7508
diff changeset
  1463
        }
7091fccbc86d 7002320: Locale.forLanguageTag()/toLanguageTag() not working properly with ja_JP_JP locale
naoto
parents: 7508
diff changeset
  1464
        return getInstance(base, exts);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1465
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1466
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1467
    /**
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1468
     * Returns a three-letter abbreviation of this locale's language.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1469
     * If the language matches an ISO 639-1 two-letter code, the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1470
     * corresponding ISO 639-2/T three-letter lowercase code is
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1471
     * returned.  The ISO 639-2 language codes can be found on-line,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1472
     * see "Codes for the Representation of Names of Languages Part 2:
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1473
     * Alpha-3 Code".  If the locale specifies a three-letter
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1474
     * language, the language is returned as is.  If the locale does
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1475
     * not specify a language the empty string is returned.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1476
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1477
     * @return A three-letter abbreviation of this locale's language.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1478
     * @exception MissingResourceException Throws MissingResourceException if
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
     * three-letter language abbreviation is not available for this locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
    public String getISO3Language() throws MissingResourceException {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1482
        String lang = baseLocale.getLanguage();
7783
8eb31199b5ed 4940539: Constructor of java.util.Locale should handle ISO 639-2 Language Codes
naoto
parents: 7513
diff changeset
  1483
        if (lang.length() == 3) {
8eb31199b5ed 4940539: Constructor of java.util.Locale should handle ISO 639-2 Language Codes
naoto
parents: 7513
diff changeset
  1484
            return lang;
8eb31199b5ed 4940539: Constructor of java.util.Locale should handle ISO 639-2 Language Codes
naoto
parents: 7513
diff changeset
  1485
        }
8eb31199b5ed 4940539: Constructor of java.util.Locale should handle ISO 639-2 Language Codes
naoto
parents: 7513
diff changeset
  1486
8eb31199b5ed 4940539: Constructor of java.util.Locale should handle ISO 639-2 Language Codes
naoto
parents: 7513
diff changeset
  1487
        String language3 = getISO3Code(lang, LocaleISOData.isoLanguageTable);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
        if (language3 == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
            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
  1490
                    + lang, "FormatData_" + toString(), "ShortLanguage");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
        return language3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
    /**
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1496
     * Returns a three-letter abbreviation for this locale's country.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1497
     * If the country matches an ISO 3166-1 alpha-2 code, the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1498
     * corresponding ISO 3166-1 alpha-3 uppercase code is returned.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1499
     * If the locale doesn't specify a country, this will be the empty
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1500
     * string.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1501
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1502
     * <p>The ISO 3166-1 codes can be found on-line.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1503
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1504
     * @return A three-letter abbreviation of this locale's country.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
     * @exception MissingResourceException Throws MissingResourceException if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
     * three-letter country abbreviation is not available for this locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
    public String getISO3Country() throws MissingResourceException {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1509
        String country3 = getISO3Code(baseLocale.getRegion(), LocaleISOData.isoCountryTable);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
        if (country3 == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
            throw new MissingResourceException("Couldn't find 3-letter country code for "
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1512
                    + baseLocale.getRegion(), "FormatData_" + toString(), "ShortCountry");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
        return country3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
    private static final String getISO3Code(String iso2Code, String table) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
        int codeLength = iso2Code.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
        if (codeLength == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
            return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
        int tableLength = table.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
        int index = tableLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
        if (codeLength == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
            char c1 = iso2Code.charAt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
            char c2 = iso2Code.charAt(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
            for (index = 0; index < tableLength; index += 5) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
                if (table.charAt(index) == c1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
                    && table.charAt(index + 1) == c2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
        return index < tableLength ? table.substring(index + 2, index + 5) : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
     * Returns a name for the locale's language that is appropriate for display to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
     * user.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
     * If possible, the name returned will be localized for the default locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
     * For example, if the locale is fr_FR and the default locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
     * is en_US, getDisplayLanguage() will return "French"; if the locale is en_US and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
     * the default locale is fr_FR, getDisplayLanguage() will return "anglais".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
     * If the name returned cannot be localized for the default locale,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
     * (say, we don't have a Japanese name for Croatian),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
     * this function falls back on the English name, and uses the ISO code as a last-resort
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
     * value.  If the locale doesn't specify a language, this function returns the empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
    public final String getDisplayLanguage() {
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  1551
        return getDisplayLanguage(getDefault(Category.DISPLAY));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
     * Returns a name for the locale's language that is appropriate for display to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
     * user.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
     * If possible, the name returned will be localized according to inLocale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
     * For example, if the locale is fr_FR and inLocale
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
     * is en_US, getDisplayLanguage() will return "French"; if the locale is en_US and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
     * inLocale is fr_FR, getDisplayLanguage() will return "anglais".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
     * If the name returned cannot be localized according to inLocale,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
     * (say, we don't have a Japanese name for Croatian),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
     * this function falls back on the English name, and finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
     * on the ISO code as a last-resort value.  If the locale doesn't specify a language,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
     * this function returns the empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
     * @exception NullPointerException if <code>inLocale</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
    public String getDisplayLanguage(Locale inLocale) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1570
        return getDisplayString(baseLocale.getLanguage(), inLocale, DISPLAY_LANGUAGE);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1571
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1572
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1573
    /**
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1574
     * 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
  1575
     * the user. If possible, the name will be localized for the default locale.  Returns
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1576
     * the empty string if this locale doesn't specify a script code.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1577
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1578
     * @return the display name of the script code for the current default locale
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1579
     * @since 1.7
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1580
     */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1581
    public String getDisplayScript() {
10339
80d9f4bc094b 7079486: Locale.getDisplayScript() (no args) does not honor default DISPLAY locale
naoto
parents: 9232
diff changeset
  1582
        return getDisplayScript(getDefault(Category.DISPLAY));
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1583
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1584
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1585
    /**
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1586
     * Returns a name for the locale's script that is appropriate
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1587
     * for display to the user. If possible, the name will be
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1588
     * localized for the given locale. Returns the empty string if
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1589
     * this locale doesn't specify a script code.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1590
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1591
     * @return the display name of the script code for the current default locale
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1592
     * @throws NullPointerException if <code>inLocale</code> is <code>null</code>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1593
     * @since 1.7
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1594
     */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1595
    public String getDisplayScript(Locale inLocale) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1596
        return getDisplayString(baseLocale.getScript(), inLocale, DISPLAY_SCRIPT);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
     * Returns a name for the locale's country that is appropriate for display to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
     * user.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
     * If possible, the name returned will be localized for the default locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
     * For example, if the locale is fr_FR and the default locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
     * is en_US, getDisplayCountry() will return "France"; if the locale is en_US and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
     * the default locale is fr_FR, getDisplayCountry() will return "Etats-Unis".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
     * If the name returned cannot be localized for the default locale,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
     * (say, we don't have a Japanese name for Croatia),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
     * this function falls back on the English name, and uses the ISO code as a last-resort
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
     * value.  If the locale doesn't specify a country, this function returns the empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
    public final String getDisplayCountry() {
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  1612
        return getDisplayCountry(getDefault(Category.DISPLAY));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
     * Returns a name for the locale's country that is appropriate for display to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
     * user.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
     * If possible, the name returned will be localized according to inLocale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
     * For example, if the locale is fr_FR and inLocale
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
     * is en_US, getDisplayCountry() will return "France"; if the locale is en_US and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
     * inLocale is fr_FR, getDisplayCountry() will return "Etats-Unis".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
     * If the name returned cannot be localized according to inLocale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
     * (say, we don't have a Japanese name for Croatia),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
     * this function falls back on the English name, and finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
     * on the ISO code as a last-resort value.  If the locale doesn't specify a country,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
     * this function returns the empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
     * @exception NullPointerException if <code>inLocale</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
    public String getDisplayCountry(Locale inLocale) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1631
        return getDisplayString(baseLocale.getRegion(), inLocale, DISPLAY_COUNTRY);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
    private String getDisplayString(String code, Locale inLocale, int type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
        if (code.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
            return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
        if (inLocale == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
            OpenListResourceBundle bundle = LocaleData.getLocaleNames(inLocale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
            String key = (type == DISPLAY_VARIANT ? "%%"+code : code);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
            String result = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
            // Check whether a provider can provide an implementation that's closer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
            // to the requested locale than what the Java runtime itself can provide.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
            LocaleServiceProviderPool pool =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
                LocaleServiceProviderPool.getPool(LocaleNameProvider.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
            if (pool.hasProviders()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
                result = pool.getLocalizedObject(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
                                    LocaleNameGetter.INSTANCE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
                                    inLocale, bundle, key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
                                    type, code);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
            if (result == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
                result = bundle.getString(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
            if (result != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
                return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
        catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
            // just fall through
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
        return code;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
     * Returns a name for the locale's variant code that is appropriate for display to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
     * user.  If possible, the name will be localized for the default locale.  If the locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
     * doesn't specify a variant code, this function returns the empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
    public final String getDisplayVariant() {
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  1679
        return getDisplayVariant(getDefault(Category.DISPLAY));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
     * Returns a name for the locale's variant code that is appropriate for display to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
     * user.  If possible, the name will be localized for inLocale.  If the locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
     * doesn't specify a variant code, this function returns the empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
     * @exception NullPointerException if <code>inLocale</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
    public String getDisplayVariant(Locale inLocale) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1690
        if (baseLocale.getVariant().length() == 0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
            return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
        OpenListResourceBundle bundle = LocaleData.getLocaleNames(inLocale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
        String names[] = getDisplayVariantArray(bundle, inLocale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
        // Get the localized patterns for formatting a list, and use
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
        // them to format the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
        String listPattern = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
        String listCompositionPattern = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
            listPattern = bundle.getString("ListPattern");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
            listCompositionPattern = bundle.getString("ListCompositionPattern");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
        } catch (MissingResourceException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
        return formatList(names, listPattern, listCompositionPattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
     * Returns a name for the locale that is appropriate for display to the
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1711
     * user. This will be the values returned by getDisplayLanguage(),
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1712
     * getDisplayScript(), getDisplayCountry(), and getDisplayVariant() assembled
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1713
     * into a single string. The the non-empty values are used in order,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1714
     * with the second and subsequent names in parentheses.  For example:
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1715
     * <blockquote>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1716
     * language (script, country, variant)<br>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1717
     * language (country)<br>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1718
     * language (variant)<br>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1719
     * script (country)<br>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1720
     * country<br>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1721
     * </blockquote>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1722
     * depending on which fields are specified in the locale.  If the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1723
     * language, sacript, country, and variant fields are all empty,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1724
     * this function returns the empty string.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
    public final String getDisplayName() {
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  1727
        return getDisplayName(getDefault(Category.DISPLAY));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
    /**
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1731
     * Returns a name for the locale that is appropriate for display
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1732
     * to the user.  This will be the values returned by
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1733
     * getDisplayLanguage(), getDisplayScript(),getDisplayCountry(),
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1734
     * and getDisplayVariant() assembled into a single string.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1735
     * The non-empty values are used in order,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1736
     * with the second and subsequent names in parentheses.  For example:
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1737
     * <blockquote>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1738
     * language (script, country, variant)<br>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1739
     * language (country)<br>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1740
     * language (variant)<br>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1741
     * script (country)<br>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1742
     * country<br>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1743
     * </blockquote>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1744
     * depending on which fields are specified in the locale.  If the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1745
     * language, script, country, and variant fields are all empty,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1746
     * this function returns the empty string.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
     *
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1748
     * @throws NullPointerException if <code>inLocale</code> is <code>null</code>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
    public String getDisplayName(Locale inLocale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
        OpenListResourceBundle bundle = LocaleData.getLocaleNames(inLocale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
        String languageName = getDisplayLanguage(inLocale);
6843
6ab7e78c51eb 6992272: I18N: Locale.getDisplayName() and toString() return empty if just script is set
naoto
parents: 6840
diff changeset
  1754
        String scriptName = getDisplayScript(inLocale);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
        String countryName = getDisplayCountry(inLocale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
        String[] variantNames = getDisplayVariantArray(bundle, inLocale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
        // Get the localized patterns for formatting a display name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
        String displayNamePattern = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
        String listPattern = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
        String listCompositionPattern = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
            displayNamePattern = bundle.getString("DisplayNamePattern");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
            listPattern = bundle.getString("ListPattern");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
            listCompositionPattern = bundle.getString("ListCompositionPattern");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
        } catch (MissingResourceException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
        // The display name consists of a main name, followed by qualifiers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
        // Typically, the format is "MainName (Qualifier, Qualifier)" but this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
        // depends on what pattern is stored in the display locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
        String   mainName       = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
        String[] qualifierNames = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
6843
6ab7e78c51eb 6992272: I18N: Locale.getDisplayName() and toString() return empty if just script is set
naoto
parents: 6840
diff changeset
  1775
        // 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
  1776
        // 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
  1777
        // (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
  1778
        // display name.
6ab7e78c51eb 6992272: I18N: Locale.getDisplayName() and toString() return empty if just script is set
naoto
parents: 6840
diff changeset
  1779
        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
  1780
            if (variantNames.length == 0) {
6ab7e78c51eb 6992272: I18N: Locale.getDisplayName() and toString() return empty if just script is set
naoto
parents: 6840
diff changeset
  1781
                return "";
6ab7e78c51eb 6992272: I18N: Locale.getDisplayName() and toString() return empty if just script is set
naoto
parents: 6840
diff changeset
  1782
            } else {
6ab7e78c51eb 6992272: I18N: Locale.getDisplayName() and toString() return empty if just script is set
naoto
parents: 6840
diff changeset
  1783
                return formatList(variantNames, listPattern, listCompositionPattern);
2
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
        }
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1786
        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
  1787
        if (languageName.length() != 0) {
6ab7e78c51eb 6992272: I18N: Locale.getDisplayName() and toString() return empty if just script is set
naoto
parents: 6840
diff changeset
  1788
            names.add(languageName);
6ab7e78c51eb 6992272: I18N: Locale.getDisplayName() and toString() return empty if just script is set
naoto
parents: 6840
diff changeset
  1789
        }
6ab7e78c51eb 6992272: I18N: Locale.getDisplayName() and toString() return empty if just script is set
naoto
parents: 6840
diff changeset
  1790
        if (scriptName.length() != 0) {
6ab7e78c51eb 6992272: I18N: Locale.getDisplayName() and toString() return empty if just script is set
naoto
parents: 6840
diff changeset
  1791
            names.add(scriptName);
6ab7e78c51eb 6992272: I18N: Locale.getDisplayName() and toString() return empty if just script is set
naoto
parents: 6840
diff changeset
  1792
        }
6ab7e78c51eb 6992272: I18N: Locale.getDisplayName() and toString() return empty if just script is set
naoto
parents: 6840
diff changeset
  1793
        if (countryName.length() != 0) {
6ab7e78c51eb 6992272: I18N: Locale.getDisplayName() and toString() return empty if just script is set
naoto
parents: 6840
diff changeset
  1794
            names.add(countryName);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
        }
6843
6ab7e78c51eb 6992272: I18N: Locale.getDisplayName() and toString() return empty if just script is set
naoto
parents: 6840
diff changeset
  1796
        if (variantNames.length != 0) {
6ab7e78c51eb 6992272: I18N: Locale.getDisplayName() and toString() return empty if just script is set
naoto
parents: 6840
diff changeset
  1797
            for (String var : variantNames) {
6ab7e78c51eb 6992272: I18N: Locale.getDisplayName() and toString() return empty if just script is set
naoto
parents: 6840
diff changeset
  1798
                names.add(var);
6ab7e78c51eb 6992272: I18N: Locale.getDisplayName() and toString() return empty if just script is set
naoto
parents: 6840
diff changeset
  1799
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
        }
6843
6ab7e78c51eb 6992272: I18N: Locale.getDisplayName() and toString() return empty if just script is set
naoto
parents: 6840
diff changeset
  1801
6ab7e78c51eb 6992272: I18N: Locale.getDisplayName() and toString() return empty if just script is set
naoto
parents: 6840
diff changeset
  1802
        // 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
  1803
        mainName = names.get(0);
6ab7e78c51eb 6992272: I18N: Locale.getDisplayName() and toString() return empty if just script is set
naoto
parents: 6840
diff changeset
  1804
6ab7e78c51eb 6992272: I18N: Locale.getDisplayName() and toString() return empty if just script is set
naoto
parents: 6840
diff changeset
  1805
        // Others are qualifiers
6ab7e78c51eb 6992272: I18N: Locale.getDisplayName() and toString() return empty if just script is set
naoto
parents: 6840
diff changeset
  1806
        int numNames = names.size();
6ab7e78c51eb 6992272: I18N: Locale.getDisplayName() and toString() return empty if just script is set
naoto
parents: 6840
diff changeset
  1807
        qualifierNames = (numNames > 1) ?
6ab7e78c51eb 6992272: I18N: Locale.getDisplayName() and toString() return empty if just script is set
naoto
parents: 6840
diff changeset
  1808
                names.subList(1, numNames).toArray(new String[numNames - 1]) : new String[0];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
        // Create an array whose first element is the number of remaining
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
        // elements.  This serves as a selector into a ChoiceFormat pattern from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
        // the resource.  The second and third elements are the main name and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
        // the qualifier; if there are no qualifiers, the third element is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
        // unused by the format pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
        Object[] displayNames = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
            new Integer(qualifierNames.length != 0 ? 2 : 1),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
            mainName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
            // We could also just call formatList() and have it handle the empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
            // list case, but this is more efficient, and we want it to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
            // efficient since all the language-only locales will not have any
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
            // qualifiers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
            qualifierNames.length != 0 ? formatList(qualifierNames, listPattern, listCompositionPattern) : null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
        if (displayNamePattern != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
            return new MessageFormat(displayNamePattern).format(displayNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
            // If we cannot get the message format pattern, then we use a simple
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
            // hard-coded pattern.  This should not occur in practice unless the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
            // installation is missing some core files (FormatData etc.).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
            StringBuilder result = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
            result.append((String)displayNames[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
            if (displayNames.length > 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
                result.append(" (");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
                result.append((String)displayNames[2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
                result.append(')');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
            return result.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
    /**
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1844
     * Overrides Cloneable.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
    public Object clone()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
            Locale that = (Locale)super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
            return that;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
        } catch (CloneNotSupportedException e) {
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 10339
diff changeset
  1852
            throw new InternalError(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
     * Override hashCode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
     * Since Locales are often used in hashtables, caches the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
     * for speed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
     */
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1861
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
        int hc = hashCodeValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
        if (hc == 0) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1865
            hc = baseLocale.hashCode();
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1866
            if (localeExtensions != null) {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1867
                hc ^= localeExtensions.hashCode();
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1868
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
            hashCodeValue = hc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
        return hc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
    // Overrides
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
     * Returns true if this Locale is equal to another object.  A Locale is
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1878
     * deemed equal to another Locale with identical language, script, country,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1879
     * variant and extensions, and unequal to all other objects.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
     * @return true if this Locale is equal to the specified object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
     */
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1883
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
        if (this == obj)                      // quick check
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
        if (!(obj instanceof Locale))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
            return false;
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1889
        BaseLocale otherBase = ((Locale)obj).baseLocale;
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1890
        if (!baseLocale.equals(otherBase)) {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1891
            return false;
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1892
        }
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1893
        if (localeExtensions == null) {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1894
            return ((Locale)obj).localeExtensions == null;
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1895
        }
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1896
        return localeExtensions.equals(((Locale)obj).localeExtensions);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
    // ================= privates =====================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1901
    private transient BaseLocale baseLocale;
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1902
    private transient LocaleExtensions localeExtensions;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
    /**
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1905
     * Calculated hashcode
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
    private transient volatile int hashCodeValue = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
11282
2c8538c9daa6 4808233: "Locale" not thread-safe
naoto
parents: 10419
diff changeset
  1909
    private volatile static Locale defaultLocale = initDefault();
2c8538c9daa6 4808233: "Locale" not thread-safe
naoto
parents: 10419
diff changeset
  1910
    private volatile static Locale defaultDisplayLocale = null;
2c8538c9daa6 4808233: "Locale" not thread-safe
naoto
parents: 10419
diff changeset
  1911
    private volatile static Locale defaultFormatLocale = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
     * Return an array of the display names of the variant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
     * @param bundle the ResourceBundle to use to get the display names
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
     * @return an array of display names, possible of zero length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
    private String[] getDisplayVariantArray(OpenListResourceBundle bundle, Locale inLocale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
        // Split the variant name into tokens separated by '_'.
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  1920
        StringTokenizer tokenizer = new StringTokenizer(baseLocale.getVariant(), "_");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
        String[] names = new String[tokenizer.countTokens()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
        // For each variant token, lookup the display name.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
        // not found, use the variant name itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
        for (int i=0; i<names.length; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
            names[i] = getDisplayString(tokenizer.nextToken(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
                                inLocale, DISPLAY_VARIANT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
        return names;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
     * Format a list using given pattern strings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
     * If either of the patterns is null, then a the list is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
     * formatted by concatenation with the delimiter ','.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
     * @param stringList the list of strings to be formatted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
     * @param listPattern should create a MessageFormat taking 0-3 arguments
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
     * and formatting them into a list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
     * @param listCompositionPattern should take 2 arguments
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
     * and is used by composeList.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
     * @return a string representing the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
    private static String formatList(String[] stringList, String listPattern, String listCompositionPattern) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
        // If we have no list patterns, compose the list in a simple,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
        // non-localized way.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
        if (listPattern == null || listCompositionPattern == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
            StringBuffer result = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
            for (int i=0; i<stringList.length; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
                if (i>0) result.append(',');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
                result.append(stringList[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
            return result.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
        // Compose the list down to three elements if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
        if (stringList.length > 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
            MessageFormat format = new MessageFormat(listCompositionPattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
            stringList = composeList(format, stringList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
        // Rebuild the argument list with the list length as the first element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
        Object[] args = new Object[stringList.length + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
        System.arraycopy(stringList, 0, args, 1, stringList.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
        args[0] = new Integer(stringList.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
        // Format it using the pattern in the resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
        MessageFormat format = new MessageFormat(listPattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
        return format.format(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
     * Given a list of strings, return a list shortened to three elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
     * Shorten it by applying the given format to the first two elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
     * recursively.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
     * @param format a format which takes two arguments
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
     * @param list a list of strings
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
     * @return if the list is three elements or shorter, the same list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
     * otherwise, a new list of three elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
    private static String[] composeList(MessageFormat format, String[] list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
        if (list.length <= 3) return list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
        // Use the given format to compose the first two elements into one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
        String[] listItems = { list[0], list[1] };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
        String newItem = format.format(listItems);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
        // Form a new list one element shorter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
        String[] newList = new String[list.length-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
        System.arraycopy(list, 2, newList, 1, newList.length-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
        newList[0] = newItem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
        // Recurse
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
        return composeList(format, newList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
    /**
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1998
     * @serialField language    String
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  1999
     *      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
  2000
     * @serialField country     String
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2001
     *      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
  2002
     * @serialField variant     String
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2003
     *      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
  2004
     * @serialField hashcode    int
6840
51af1a50aec6 6991013: Serialized form for java.util.Locale contains typos
naoto
parents: 6655
diff changeset
  2005
     *      deprecated, for forward compatibility only
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2006
     * @serialField script      String
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2007
     *      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
  2008
     * @serialField extensions  String
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2009
     *      canonical representation of extensions, that is,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2010
     *      BCP47 extensions in alphabetical order followed by
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2011
     *      BCP47 private use subtags, all in lower case letters
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2012
     *      separated by HYPHEN-MINUS characters.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2013
     *      (See <a href="java/util/Locale.html#getExtensionKeys()">getExtensionKeys()</a>,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2014
     *      <a href="java/util/Locale.html#getExtension(char)">getExtension(char)</a>)
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2015
     */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2016
    private static final ObjectStreamField[] serialPersistentFields = {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2017
        new ObjectStreamField("language", String.class),
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2018
        new ObjectStreamField("country", String.class),
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2019
        new ObjectStreamField("variant", String.class),
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2020
        new ObjectStreamField("hashcode", int.class),
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2021
        new ObjectStreamField("script", String.class),
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2022
        new ObjectStreamField("extensions", String.class),
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2023
    };
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2024
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2025
    /**
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2026
     * Serializes this <code>Locale</code> to the specified <code>ObjectOutputStream</code>.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2027
     * @param out the <code>ObjectOutputStream</code> to write
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2028
     * @throws IOException
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2029
     * @since 1.7
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2030
     */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2031
    private void writeObject(ObjectOutputStream out) throws IOException {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2032
        ObjectOutputStream.PutField fields = out.putFields();
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2033
        fields.put("language", baseLocale.getLanguage());
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2034
        fields.put("script", baseLocale.getScript());
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2035
        fields.put("country", baseLocale.getRegion());
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2036
        fields.put("variant", baseLocale.getVariant());
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2037
        fields.put("extensions", localeExtensions == null ? "" : localeExtensions.getID());
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2038
        fields.put("hashcode", -1); // place holder just for backward support
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2039
        out.writeFields();
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2040
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2041
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2042
    /**
6840
51af1a50aec6 6991013: Serialized form for java.util.Locale contains typos
naoto
parents: 6655
diff changeset
  2043
     * Deserializes this <code>Locale</code>.
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2044
     * @param in the <code>ObjectInputStream</code> to read
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2045
     * @throws IOException
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2046
     * @throws ClassNotFoundException
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2047
     * @throws IllformdLocaleException
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2048
     * @since 1.7
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2049
     */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2050
    private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2051
        ObjectInputStream.GetField fields = in.readFields();
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2052
        String language = (String)fields.get("language", "");
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2053
        String script = (String)fields.get("script", "");
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2054
        String country = (String)fields.get("country", "");
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2055
        String variant = (String)fields.get("variant", "");
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2056
        String extStr = (String)fields.get("extensions", "");
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2057
        baseLocale = BaseLocale.getInstance(convertOldISOCodes(language), script, country, variant);
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2058
        if (extStr.length() > 0) {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2059
            try {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2060
                InternalLocaleBuilder bldr = new InternalLocaleBuilder();
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2061
                bldr.setExtensions(extStr);
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2062
                localeExtensions = bldr.getLocaleExtensions();
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2063
            } catch (LocaleSyntaxException e) {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2064
                throw new IllformedLocaleException(e.getMessage());
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2065
            }
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2066
        } else {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2067
            localeExtensions = null;
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2068
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2069
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2070
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2071
    /**
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2072
     * Returns a cached <code>Locale</code> instance equivalent to
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2073
     * the deserialized <code>Locale</code>. When serialized
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2074
     * language, country and variant fields read from the object data stream
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2075
     * are exactly "ja", "JP", "JP" or "th", "TH", "TH" and script/extensions
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2076
     * fields are empty, this method supplies <code>UNICODE_LOCALE_EXTENSION</code>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2077
     * "ca"/"japanese" (calendar type is "japanese") or "nu"/"thai" (number script
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2078
     * type is "thai"). See <a href="Locale.html#special_cases_constructor"/>Special Cases</a>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2079
     * for more information.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2080
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2081
     * @return an instance of <code>Locale</code> equivalent to
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2082
     * the deserialized <code>Locale</code>.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2083
     * @throws java.io.ObjectStreamException
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
    private Object readResolve() throws java.io.ObjectStreamException {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2086
        return getInstance(baseLocale.getLanguage(), baseLocale.getScript(),
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2087
                baseLocale.getRegion(), baseLocale.getVariant(), localeExtensions);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
    private static volatile String[] isoLanguages = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
    private static volatile String[] isoCountries = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2094
    private static String convertOldISOCodes(String language) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
        // we accept both the old and the new ISO codes for the languages whose ISO
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
        // 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
  2097
        language = LocaleUtils.toLowerString(language).intern();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
        if (language == "he") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
            return "iw";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
        } else if (language == "yi") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
            return "ji";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
        } else if (language == "id") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
            return "in";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
            return language;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2109
    private static LocaleExtensions getCompatibilityExtensions(String language,
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2110
                                                               String script,
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2111
                                                               String country,
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2112
                                                               String variant) {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2113
        LocaleExtensions extensions = null;
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2114
        // Special cases for backward compatibility support
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2115
        if (LocaleUtils.caseIgnoreMatch(language, "ja")
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2116
                && script.length() == 0
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2117
                && LocaleUtils.caseIgnoreMatch(country, "jp")
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2118
                && "JP".equals(variant)) {
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2119
            // ja_JP_JP -> u-ca-japanese (calendar = japanese)
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2120
            extensions = LocaleExtensions.CALENDAR_JAPANESE;
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2121
        } else if (LocaleUtils.caseIgnoreMatch(language, "th")
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2122
                && script.length() == 0
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2123
                && LocaleUtils.caseIgnoreMatch(country, "th")
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2124
                && "TH".equals(variant)) {
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2125
            // th_TH_TH -> u-nu-thai (numbersystem = thai)
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2126
            extensions = LocaleExtensions.NUMBER_THAI;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2127
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2128
        return extensions;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2129
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2130
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
     * Obtains a localized locale names from a LocaleNameProvider
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
     * implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
    private static class LocaleNameGetter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
        implements LocaleServiceProviderPool.LocalizedObjectGetter<LocaleNameProvider, String> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
        private static final LocaleNameGetter INSTANCE = new LocaleNameGetter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
        public String getObject(LocaleNameProvider localeNameProvider,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
                                Locale locale,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
                                String key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
                                Object... params) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
            assert params.length == 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
            int type = (Integer)params[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
            String code = (String)params[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
            switch(type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
            case DISPLAY_LANGUAGE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
                return localeNameProvider.getDisplayLanguage(code, locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
            case DISPLAY_COUNTRY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
                return localeNameProvider.getDisplayCountry(code, locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
            case DISPLAY_VARIANT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
                return localeNameProvider.getDisplayVariant(code, locale);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2154
            case DISPLAY_SCRIPT:
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2155
                return localeNameProvider.getDisplayScript(code, locale);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
                assert false; // shouldn't happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
    }
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  2163
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  2164
    /**
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  2165
     * 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
  2166
     * 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
  2167
     * category.
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  2168
     *
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  2169
     * @see #getDefault(Locale.Category)
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  2170
     * @see #setDefault(Locale.Category, Locale)
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  2171
     * @since 1.7
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  2172
     */
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  2173
    public enum Category {
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
        /**
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  2176
         * Category used to represent the default locale for
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  2177
         * displaying user interfaces.
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  2178
         */
7017
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
  2179
        DISPLAY("user.language.display",
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
  2180
                "user.script.display",
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
  2181
                "user.country.display",
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
  2182
                "user.variant.display"),
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  2183
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
         * Category used to represent the default locale for
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  2186
         * formatting dates, numbers, and/or currencies.
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  2187
         */
7017
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
  2188
        FORMAT("user.language.format",
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
  2189
               "user.script.format",
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
  2190
               "user.country.format",
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
  2191
               "user.variant.format");
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
  2192
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
  2193
        Category(String languageKey, String scriptKey, String countryKey, String variantKey) {
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
  2194
            this.languageKey = languageKey;
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
  2195
            this.scriptKey = scriptKey;
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
  2196
            this.countryKey = countryKey;
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
  2197
            this.variantKey = variantKey;
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
  2198
        }
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
  2199
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
  2200
        final String languageKey;
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
  2201
        final String scriptKey;
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
  2202
        final String countryKey;
f3bfa15db005 6989111: Incorrect default locale for New Zealand
naoto
parents: 6843
diff changeset
  2203
        final String variantKey;
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
  2204
    }
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2205
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2206
    /**
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2207
     * <code>Builder</code> is used to build instances of <code>Locale</code>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2208
     * from values configured by the setters.  Unlike the <code>Locale</code>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2209
     * constructors, the <code>Builder</code> checks if a value configured by a
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2210
     * setter satisfies the syntax requirements defined by the <code>Locale</code>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2211
     * class.  A <code>Locale</code> object created by a <code>Builder</code> is
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2212
     * 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
  2213
     * without losing information.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2214
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2215
     * <p><b>Note:</b> The <code>Locale</code> class does not provide any
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2216
     * syntactic restrictions on variant, while BCP 47 requires each variant
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2217
     * subtag to be 5 to 8 alphanumerics or a single numeric followed by 3
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2218
     * alphanumerics.  The method <code>setVariant</code> throws
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2219
     * <code>IllformedLocaleException</code> for a variant that does not satisfy
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2220
     * this restriction. If it is necessary to support such a variant, use a
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2221
     * Locale constructor.  However, keep in mind that a <code>Locale</code>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2222
     * object created this way might lose the variant information when
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2223
     * transformed to a BCP 47 language tag.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2224
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2225
     * <p>The following example shows how to create a <code>Locale</code> object
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2226
     * with the <code>Builder</code>.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2227
     * <blockquote>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2228
     * <pre>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2229
     *     Locale aLocale = new Builder().setLanguage("sr").setScript("Latn").setRegion("RS").build();
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2230
     * </pre>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2231
     * </blockquote>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2232
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2233
     * <p>Builders can be reused; <code>clear()</code> resets all
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2234
     * fields to their default values.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2235
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2236
     * @see Locale#forLanguageTag
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2237
     * @since 1.7
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2238
     */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2239
    public static final class Builder {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2240
        private final InternalLocaleBuilder localeBuilder;
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2241
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2242
        /**
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2243
         * Constructs an empty Builder. The default value of all
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2244
         * fields, extensions, and private use information is the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2245
         * empty string.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2246
         */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2247
        public Builder() {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2248
            localeBuilder = new InternalLocaleBuilder();
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2249
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2250
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2251
        /**
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2252
         * Resets the <code>Builder</code> to match the provided
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2253
         * <code>locale</code>.  Existing state is discarded.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2254
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2255
         * <p>All fields of the locale must be well-formed, see {@link Locale}.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2256
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2257
         * <p>Locales with any ill-formed fields cause
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2258
         * <code>IllformedLocaleException</code> to be thrown, except for the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2259
         * following three cases which are accepted for compatibility
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2260
         * reasons:<ul>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2261
         * <li>Locale("ja", "JP", "JP") is treated as "ja-JP-u-ca-japanese"
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2262
         * <li>Locale("th", "TH", "TH") is treated as "th-TH-u-nu-thai"
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2263
         * <li>Locale("no", "NO", "NY") is treated as "nn-NO"</ul>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2264
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2265
         * @param locale the locale
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2266
         * @return This builder.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2267
         * @throws IllformedLocaleException if <code>locale</code> has
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2268
         * any ill-formed fields.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2269
         * @throws NullPointerException if <code>locale</code> is null.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2270
         */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2271
        public Builder setLocale(Locale locale) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2272
            try {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2273
                localeBuilder.setLocale(locale.baseLocale, locale.localeExtensions);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2274
            } catch (LocaleSyntaxException e) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2275
                throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex());
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2276
            }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2277
            return this;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2278
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2279
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2280
        /**
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2281
         * Resets the Builder to match the provided IETF BCP 47
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2282
         * language tag.  Discards the existing state.  Null and the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2283
         * empty string cause the builder to be reset, like {@link
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2284
         * #clear}.  Grandfathered tags (see {@link
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2285
         * Locale#forLanguageTag}) are converted to their canonical
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2286
         * form before being processed.  Otherwise, the language tag
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2287
         * must be well-formed (see {@link Locale}) or an exception is
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2288
         * thrown (unlike <code>Locale.forLanguageTag</code>, which
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2289
         * just discards ill-formed and following portions of the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2290
         * tag).
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2291
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2292
         * @param languageTag the language tag
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2293
         * @return This builder.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2294
         * @throws IllformedLocaleException if <code>languageTag</code> is ill-formed
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2295
         * @see Locale#forLanguageTag(String)
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2296
         */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2297
        public Builder setLanguageTag(String languageTag) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2298
            ParseStatus sts = new ParseStatus();
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2299
            LanguageTag tag = LanguageTag.parse(languageTag, sts);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2300
            if (sts.isError()) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2301
                throw new IllformedLocaleException(sts.getErrorMessage(), sts.getErrorIndex());
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2302
            }
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2303
            localeBuilder.setLanguageTag(tag);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2304
            return this;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2305
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2306
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2307
        /**
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2308
         * Sets the language.  If <code>language</code> is the empty string or
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2309
         * null, the language in this <code>Builder</code> is removed.  Otherwise,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2310
         * the language must be <a href="./Locale.html#def_language">well-formed</a>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2311
         * or an exception is thrown.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2312
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2313
         * <p>The typical language value is a two or three-letter language
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2314
         * code as defined in ISO639.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2315
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2316
         * @param language the language
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2317
         * @return This builder.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2318
         * @throws IllformedLocaleException if <code>language</code> is ill-formed
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2319
         */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2320
        public Builder setLanguage(String language) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2321
            try {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2322
                localeBuilder.setLanguage(language);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2323
            } catch (LocaleSyntaxException e) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2324
                throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex());
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2325
            }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2326
            return this;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2327
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2328
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2329
        /**
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2330
         * Sets the script. If <code>script</code> is null or the empty string,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2331
         * the script in this <code>Builder</code> is removed.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2332
         * 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
  2333
         * exception is thrown.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2334
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2335
         * <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
  2336
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2337
         * @param script the script
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2338
         * @return This builder.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2339
         * @throws IllformedLocaleException if <code>script</code> is ill-formed
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2340
         */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2341
        public Builder setScript(String script) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2342
            try {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2343
                localeBuilder.setScript(script);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2344
            } catch (LocaleSyntaxException e) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2345
                throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex());
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2346
            }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2347
            return this;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2348
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2349
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2350
        /**
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2351
         * Sets the region.  If region is null or the empty string, the region
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2352
         * in this <code>Builder</code> is removed.  Otherwise,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2353
         * 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
  2354
         * exception is thrown.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2355
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2356
         * <p>The typical region value is a two-letter ISO 3166 code or a
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2357
         * three-digit UN M.49 area code.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2358
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2359
         * <p>The country value in the <code>Locale</code> created by the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2360
         * <code>Builder</code> is always normalized to upper case.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2361
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2362
         * @param region the region
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2363
         * @return This builder.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2364
         * @throws IllformedLocaleException if <code>region</code> is ill-formed
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2365
         */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2366
        public Builder setRegion(String region) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2367
            try {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2368
                localeBuilder.setRegion(region);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2369
            } catch (LocaleSyntaxException e) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2370
                throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex());
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2371
            }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2372
            return this;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2373
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2374
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2375
        /**
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2376
         * Sets the variant.  If variant is null or the empty string, the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2377
         * variant in this <code>Builder</code> is removed.  Otherwise, it
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2378
         * 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
  2379
         * subtags, or an exception is thrown.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2380
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2381
         * <p><b>Note:</b> This method checks if <code>variant</code>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2382
         * satisfies the IETF BCP 47 variant subtag's syntax requirements,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2383
         * and normalizes the value to lowercase letters.  However,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2384
         * the <code>Locale</code> class does not impose any syntactic
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2385
         * restriction on variant, and the variant value in
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2386
         * <code>Locale</code> is case sensitive.  To set such a variant,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2387
         * use a Locale constructor.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2388
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2389
         * @param variant the variant
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2390
         * @return This builder.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2391
         * @throws IllformedLocaleException if <code>variant</code> is ill-formed
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2392
         */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2393
        public Builder setVariant(String variant) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2394
            try {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2395
                localeBuilder.setVariant(variant);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2396
            } catch (LocaleSyntaxException e) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2397
                throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex());
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2398
            }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2399
            return this;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2400
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2401
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2402
        /**
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2403
         * Sets the extension for the given key. If the value is null or the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2404
         * empty string, the extension is removed.  Otherwise, the extension
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2405
         * must be <a href="./Locale.html#def_extensions">well-formed</a> or an exception
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2406
         * is thrown.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2407
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2408
         * <p><b>Note:</b> The key {@link Locale#UNICODE_LOCALE_EXTENSION
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2409
         * UNICODE_LOCALE_EXTENSION} ('u') is used for the Unicode locale extension.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2410
         * Setting a value for this key replaces any existing Unicode locale key/type
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2411
         * pairs with those defined in the extension.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2412
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2413
         * <p><b>Note:</b> The key {@link Locale#PRIVATE_USE_EXTENSION
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2414
         * PRIVATE_USE_EXTENSION} ('x') is used for the private use code. To be
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2415
         * 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
  2416
         * eight alphanumeric characters, not two to eight as in the general case.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2417
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2418
         * @param key the extension key
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2419
         * @param value the extension value
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2420
         * @return This builder.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2421
         * @throws IllformedLocaleException if <code>key</code> is illegal
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2422
         * or <code>value</code> is ill-formed
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2423
         * @see #setUnicodeLocaleKeyword(String, String)
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2424
         */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2425
        public Builder setExtension(char key, String value) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2426
            try {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2427
                localeBuilder.setExtension(key, value);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2428
            } catch (LocaleSyntaxException e) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2429
                throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex());
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2430
            }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2431
            return this;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2432
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2433
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2434
        /**
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2435
         * Sets the Unicode locale keyword type for the given key.  If the type
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2436
         * is null, the Unicode keyword is removed.  Otherwise, the key must be
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2437
         * non-null and both key and type must be <a
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2438
         * href="./Locale.html#def_locale_extension">well-formed</a> or an exception
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2439
         * is thrown.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2440
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2441
         * <p>Keys and types are converted to lower case.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2442
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2443
         * <p><b>Note</b>:Setting the 'u' extension via {@link #setExtension}
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2444
         * replaces all Unicode locale keywords with those defined in the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2445
         * extension.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2446
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2447
         * @param key the Unicode locale key
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2448
         * @param type the Unicode locale type
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2449
         * @return This builder.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2450
         * @throws IllformedLocaleException if <code>key</code> or <code>type</code>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2451
         * is ill-formed
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2452
         * @throws NullPointerException if <code>key</code> is null
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2453
         * @see #setExtension(char, String)
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2454
         */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2455
        public Builder setUnicodeLocaleKeyword(String key, String type) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2456
            try {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2457
                localeBuilder.setUnicodeLocaleKeyword(key, type);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2458
            } catch (LocaleSyntaxException e) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2459
                throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex());
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2460
            }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2461
            return this;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2462
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2463
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2464
        /**
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2465
         * Adds a unicode locale attribute, if not already present, otherwise
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2466
         * has no effect.  The attribute must not be null and must be <a
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2467
         * href="./Locale.html#def_locale_extension">well-formed</a> or an exception
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2468
         * is thrown.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2469
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2470
         * @param attribute the attribute
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2471
         * @return This builder.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2472
         * @throws NullPointerException if <code>attribute</code> is null
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2473
         * @throws IllformedLocaleException if <code>attribute</code> is ill-formed
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2474
         * @see #setExtension(char, String)
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2475
         */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2476
        public Builder addUnicodeLocaleAttribute(String attribute) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2477
            try {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2478
                localeBuilder.addUnicodeLocaleAttribute(attribute);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2479
            } catch (LocaleSyntaxException e) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2480
                throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex());
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2481
            }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2482
            return this;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2483
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2484
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2485
        /**
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2486
         * Removes a unicode locale attribute, if present, otherwise has no
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2487
         * effect.  The attribute must not be null and must be <a
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2488
         * href="./Locale.html#def_locale_extension">well-formed</a> or an exception
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2489
         * is thrown.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2490
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2491
         * <p>Attribute comparision for removal is case-insensitive.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2492
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2493
         * @param attribute the attribute
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2494
         * @return This builder.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2495
         * @throws NullPointerException if <code>attribute</code> is null
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2496
         * @throws IllformedLocaleException if <code>attribute</code> is ill-formed
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2497
         * @see #setExtension(char, String)
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2498
         */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2499
        public Builder removeUnicodeLocaleAttribute(String attribute) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2500
            try {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2501
                localeBuilder.removeUnicodeLocaleAttribute(attribute);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2502
            } catch (LocaleSyntaxException e) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2503
                throw new IllformedLocaleException(e.getMessage(), e.getErrorIndex());
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2504
            }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2505
            return this;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2506
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2507
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2508
        /**
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2509
         * Resets the builder to its initial, empty state.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2510
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2511
         * @return This builder.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2512
         */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2513
        public Builder clear() {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2514
            localeBuilder.clear();
6501
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 extensions to their initial, empty state.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2520
         * Language, script, region and variant are unchanged.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2521
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2522
         * @return This builder.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2523
         * @see #setExtension(char, String)
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2524
         */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2525
        public Builder clearExtensions() {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2526
            localeBuilder.clearExtensions();
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2527
            return this;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2528
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2529
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2530
        /**
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2531
         * Returns an instance of <code>Locale</code> created from the fields set
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2532
         * on this builder.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2533
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2534
         * <p>This applies the conversions listed in {@link Locale#forLanguageTag}
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2535
         * when constructing a Locale. (Grandfathered tags are handled in
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2536
         * {@link #setLanguageTag}.)
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2537
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2538
         * @return A Locale.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2539
         */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2540
        public Locale build() {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2541
            BaseLocale baseloc = localeBuilder.getBaseLocale();
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2542
            LocaleExtensions extensions = localeBuilder.getLocaleExtensions();
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 8780
diff changeset
  2543
            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
  2544
                extensions = getCompatibilityExtensions(baseloc.getLanguage(), baseloc.getScript(),
7091fccbc86d 7002320: Locale.forLanguageTag()/toLanguageTag() not working properly with ja_JP_JP locale
naoto
parents: 7508
diff changeset
  2545
                        baseloc.getRegion(), baseloc.getVariant());
7091fccbc86d 7002320: Locale.forLanguageTag()/toLanguageTag() not working properly with ja_JP_JP locale
naoto
parents: 7508
diff changeset
  2546
            }
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2547
            return Locale.getInstance(baseloc, extensions);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2548
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 6489
diff changeset
  2549
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2550
}