jdk/src/share/classes/sun/util/locale/UnicodeLocaleExtension.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:
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
     1
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
     2
/*
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
     3
 * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
     4
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
     5
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
     6
 * This code is free software; you can redistribute it and/or modify it
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
     7
 * under the terms of the GNU General Public License version 2 only, as
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
     8
 * published by the Free Software Foundation.  Oracle designates this
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
     9
 * particular file as subject to the "Classpath" exception as provided
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    10
 * by Oracle in the LICENSE file that accompanied this code.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    11
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    12
 * This code is distributed in the hope that it will be useful, but WITHOUT
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    14
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    15
 * version 2 for more details (a copy is included in the LICENSE file that
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    16
 * accompanied this code).
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    17
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    18
 * You should have received a copy of the GNU General Public License version
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    19
 * 2 along with this work; if not, write to the Free Software Foundation,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    20
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    21
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    22
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    23
 * or visit www.oracle.com if you need additional information or have any
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    24
 * questions.
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
 *******************************************************************************
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    29
 * Copyright (C) 2009-2010, International Business Machines Corporation and    *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    30
 * others. All Rights Reserved.                                                *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    31
 *******************************************************************************
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    32
 */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    33
package sun.util.locale;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    34
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    35
import java.util.Collections;
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    36
import java.util.Map;
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    37
import java.util.Map.Entry;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    38
import java.util.Set;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    39
import java.util.SortedMap;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    40
import java.util.SortedSet;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    41
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    42
public class UnicodeLocaleExtension extends Extension {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    43
    public static final char SINGLETON = 'u';
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    44
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    45
    private final Set<String> attributes;
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    46
    private final Map<String, String> keywords;
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    47
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    48
    public static final UnicodeLocaleExtension CA_JAPANESE
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    49
        = new UnicodeLocaleExtension("ca", "japanese");
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    50
    public static final UnicodeLocaleExtension NU_THAI
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    51
        = new UnicodeLocaleExtension("nu", "thai");
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    52
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    53
    private UnicodeLocaleExtension(String key, String value) {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    54
        super(SINGLETON, key + "-" + value);
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    55
        attributes = Collections.emptySet();
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    56
        keywords = Collections.singletonMap(key, value);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    57
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    58
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    59
    UnicodeLocaleExtension(SortedSet<String> attributes, SortedMap<String, String> keywords) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    60
        super(SINGLETON);
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    61
        if (attributes != null) {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    62
            this.attributes = attributes;
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    63
        } else {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    64
            this.attributes = Collections.emptySet();
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    65
        }
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    66
        if (keywords != null) {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    67
            this.keywords = keywords;
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    68
        } else {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    69
            this.keywords = Collections.emptyMap();
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    70
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    71
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    72
        if (!this.attributes.isEmpty() || !this.keywords.isEmpty()) {
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    73
            StringBuilder sb = new StringBuilder();
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    74
            for (String attribute : this.attributes) {
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    75
                sb.append(LanguageTag.SEP).append(attribute);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    76
            }
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    77
            for (Entry<String, String> keyword : this.keywords.entrySet()) {
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    78
                String key = keyword.getKey();
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    79
                String value = keyword.getValue();
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    80
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    81
                sb.append(LanguageTag.SEP).append(key);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    82
                if (value.length() > 0) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    83
                    sb.append(LanguageTag.SEP).append(value);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    84
                }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    85
            }
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    86
            setValue(sb.substring(1));   // skip leading '-'
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    87
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    88
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    89
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    90
    public Set<String> getUnicodeLocaleAttributes() {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    91
        if (attributes == Collections.EMPTY_SET) {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    92
            return attributes;
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    93
        }
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    94
        return Collections.unmodifiableSet(attributes);
6501
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
    public Set<String> getUnicodeLocaleKeys() {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    98
        if (keywords == Collections.EMPTY_MAP) {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    99
            return Collections.emptySet();
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
   100
        }
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
   101
        return Collections.unmodifiableSet(keywords.keySet());
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   102
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   103
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   104
    public String getUnicodeLocaleType(String unicodeLocaleKey) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
   105
        return keywords.get(unicodeLocaleKey);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   106
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   107
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   108
    public static boolean isSingletonChar(char c) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
   109
        return (SINGLETON == LocaleUtils.toLower(c));
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
    public static boolean isAttribute(String s) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   113
        // 3*8alphanum
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
   114
        int len = s.length();
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
   115
        return (len >= 3) && (len <= 8) && LocaleUtils.isAlphaNumericString(s);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   116
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   117
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   118
    public static boolean isKey(String s) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   119
        // 2alphanum
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
   120
        return (s.length() == 2) && LocaleUtils.isAlphaNumericString(s);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   121
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   122
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   123
    public static boolean isTypeSubtag(String s) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   124
        // 3*8alphanum
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
   125
        int len = s.length();
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
   126
        return (len >= 3) && (len <= 8) && LocaleUtils.isAlphaNumericString(s);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   127
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   128
}