jdk/src/share/classes/sun/util/locale/LocaleExtensions.java
author okutsu
Thu, 14 Apr 2011 15:59:47 +0900
changeset 9224 75c0420badef
parent 6501 684810d882b3
permissions -rw-r--r--
7028818: (lc) Lazily initialize locale extension 7029740: (lc) New Locale class implementation doesn't follow the Java coding conventions 7032820: (lc) sun.util.locale.InternalLocaleBuilder.CaseInsensitiveChar.equals problems 7033503: (lc) Restore optimization code for Locale class initialization 7033504: (lc) incompatible behavior change for ja_JP_JP and th_TH_TH locales Reviewed-by: naoto
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
     1
/*
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
     2
 * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
     4
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    10
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    15
 * accompanied this code).
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    16
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    20
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    23
 * questions.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    24
 */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    25
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    26
/*
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    27
 *******************************************************************************
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    28
 * Copyright (C) 2009-2010, International Business Machines Corporation and    *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    29
 * others. All Rights Reserved.                                                *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    30
 *******************************************************************************
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    31
 */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    32
package sun.util.locale;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    33
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    34
import java.util.Collections;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    35
import java.util.Map;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    36
import java.util.Map.Entry;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    37
import java.util.Set;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    38
import java.util.SortedMap;
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    39
import java.util.SortedSet;
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    40
import java.util.TreeMap;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    41
import java.util.TreeSet;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    42
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    43
import sun.util.locale.InternalLocaleBuilder.CaseInsensitiveChar;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    44
import sun.util.locale.InternalLocaleBuilder.CaseInsensitiveString;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    45
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    46
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    47
public class LocaleExtensions {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    48
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    49
    private final Map<Character, Extension> extensionMap;
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    50
    private final String id;
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    51
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    52
    public static final LocaleExtensions CALENDAR_JAPANESE
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    53
        = new LocaleExtensions("u-ca-japanese",
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    54
                               UnicodeLocaleExtension.SINGLETON,
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    55
                               UnicodeLocaleExtension.CA_JAPANESE);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    56
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    57
    public static final LocaleExtensions NUMBER_THAI
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    58
        = new LocaleExtensions("u-nu-thai",
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    59
                               UnicodeLocaleExtension.SINGLETON,
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    60
                               UnicodeLocaleExtension.NU_THAI);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    61
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    62
    private LocaleExtensions(String id, Character key, Extension value) {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    63
        this.id = id;
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    64
        this.extensionMap = Collections.singletonMap(key, value);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    65
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    66
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    67
    /*
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    68
     * Package private constructor, only used by InternalLocaleBuilder.
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    69
     */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    70
    LocaleExtensions(Map<CaseInsensitiveChar, String> extensions,
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    71
                     Set<CaseInsensitiveString> uattributes,
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    72
                     Map<CaseInsensitiveString, String> ukeywords) {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    73
        boolean hasExtension = !LocaleUtils.isEmpty(extensions);
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    74
        boolean hasUAttributes = !LocaleUtils.isEmpty(uattributes);
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    75
        boolean hasUKeywords = !LocaleUtils.isEmpty(ukeywords);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    76
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    77
        if (!hasExtension && !hasUAttributes && !hasUKeywords) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    78
            id = "";
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    79
            extensionMap = Collections.emptyMap();
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    80
            return;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    81
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    82
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    83
        // Build extension map
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    84
        SortedMap<Character, Extension> map = new TreeMap<>();
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    85
        if (hasExtension) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    86
            for (Entry<CaseInsensitiveChar, String> ext : extensions.entrySet()) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    87
                char key = LocaleUtils.toLower(ext.getKey().value());
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    88
                String value = ext.getValue();
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    89
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    90
                if (LanguageTag.isPrivateusePrefixChar(key)) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    91
                    // we need to exclude special variant in privuateuse, e.g. "x-abc-lvariant-DEF"
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    92
                    value = InternalLocaleBuilder.removePrivateuseVariant(value);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    93
                    if (value == null) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    94
                        continue;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    95
                    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    96
                }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    97
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    98
                map.put(key, new Extension(key, LocaleUtils.toLowerString(value)));
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    99
            }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   100
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   101
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   102
        if (hasUAttributes || hasUKeywords) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
   103
            SortedSet<String> uaset = null;
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
   104
            SortedMap<String, String> ukmap = null;
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   105
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   106
            if (hasUAttributes) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
   107
                uaset = new TreeSet<>();
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   108
                for (CaseInsensitiveString cis : uattributes) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
   109
                    uaset.add(LocaleUtils.toLowerString(cis.value()));
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   110
                }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   111
            }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   112
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   113
            if (hasUKeywords) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
   114
                ukmap = new TreeMap<>();
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   115
                for (Entry<CaseInsensitiveString, String> kwd : ukeywords.entrySet()) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
   116
                    String key = LocaleUtils.toLowerString(kwd.getKey().value());
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
   117
                    String type = LocaleUtils.toLowerString(kwd.getValue());
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   118
                    ukmap.put(key, type);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   119
                }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   120
            }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   121
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   122
            UnicodeLocaleExtension ule = new UnicodeLocaleExtension(uaset, ukmap);
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
   123
            map.put(UnicodeLocaleExtension.SINGLETON, ule);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   124
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   125
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
   126
        if (map.isEmpty()) {
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   127
            // this could happen when only privuateuse with special variant
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
   128
            id = "";
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
   129
            extensionMap = Collections.emptyMap();
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   130
        } else {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
   131
            id = toID(map);
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
   132
            extensionMap = map;
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   133
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   134
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   135
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   136
    public Set<Character> getKeys() {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
   137
        if (extensionMap.isEmpty()) {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
   138
            return Collections.emptySet();
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
   139
        }
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
   140
        return Collections.unmodifiableSet(extensionMap.keySet());
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   141
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   142
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   143
    public Extension getExtension(Character key) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
   144
        return extensionMap.get(LocaleUtils.toLower(key));
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   145
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   146
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   147
    public String getExtensionValue(Character key) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
   148
        Extension ext = extensionMap.get(LocaleUtils.toLower(key));
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   149
        if (ext == null) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   150
            return null;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   151
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   152
        return ext.getValue();
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   153
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   154
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   155
    public Set<String> getUnicodeLocaleAttributes() {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
   156
        Extension ext = extensionMap.get(UnicodeLocaleExtension.SINGLETON);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   157
        if (ext == null) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   158
            return Collections.emptySet();
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   159
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   160
        assert (ext instanceof UnicodeLocaleExtension);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   161
        return ((UnicodeLocaleExtension)ext).getUnicodeLocaleAttributes();
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   162
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   163
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   164
    public Set<String> getUnicodeLocaleKeys() {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
   165
        Extension ext = extensionMap.get(UnicodeLocaleExtension.SINGLETON);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   166
        if (ext == null) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   167
            return Collections.emptySet();
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   168
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   169
        assert (ext instanceof UnicodeLocaleExtension);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   170
        return ((UnicodeLocaleExtension)ext).getUnicodeLocaleKeys();
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   171
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   172
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   173
    public String getUnicodeLocaleType(String unicodeLocaleKey) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
   174
        Extension ext = extensionMap.get(UnicodeLocaleExtension.SINGLETON);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   175
        if (ext == null) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   176
            return null;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   177
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   178
        assert (ext instanceof UnicodeLocaleExtension);
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
   179
        return ((UnicodeLocaleExtension)ext).getUnicodeLocaleType(LocaleUtils.toLowerString(unicodeLocaleKey));
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   180
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   181
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   182
    public boolean isEmpty() {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
   183
        return extensionMap.isEmpty();
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   184
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   185
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   186
    public static boolean isValidKey(char c) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   187
        return LanguageTag.isExtensionSingletonChar(c) || LanguageTag.isPrivateusePrefixChar(c);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   188
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   189
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   190
    public static boolean isValidUnicodeLocaleKey(String ukey) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   191
        return UnicodeLocaleExtension.isKey(ukey);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   192
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   193
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   194
    private static String toID(SortedMap<Character, Extension> map) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   195
        StringBuilder buf = new StringBuilder();
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   196
        Extension privuse = null;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   197
        for (Entry<Character, Extension> entry : map.entrySet()) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
   198
            char singleton = entry.getKey();
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   199
            Extension extension = entry.getValue();
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   200
            if (LanguageTag.isPrivateusePrefixChar(singleton)) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   201
                privuse = extension;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   202
            } else {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   203
                if (buf.length() > 0) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   204
                    buf.append(LanguageTag.SEP);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   205
                }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   206
                buf.append(extension);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   207
            }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   208
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   209
        if (privuse != null) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   210
            if (buf.length() > 0) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   211
                buf.append(LanguageTag.SEP);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   212
            }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   213
            buf.append(privuse);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   214
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   215
        return buf.toString();
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   216
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   217
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
   218
    @Override
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   219
    public String toString() {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
   220
        return id;
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   221
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   222
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   223
    public String getID() {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
   224
        return id;
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   225
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   226
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
   227
    @Override
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   228
    public int hashCode() {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
   229
        return id.hashCode();
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   230
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   231
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
   232
    @Override
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   233
    public boolean equals(Object other) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   234
        if (this == other) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   235
            return true;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   236
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   237
        if (!(other instanceof LocaleExtensions)) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   238
            return false;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   239
        }
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
   240
        return id.equals(((LocaleExtensions)other).id);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   241
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   242
}