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