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