jdk/src/share/classes/sun/util/locale/BaseLocale.java
author okutsu
Thu, 14 Apr 2011 15:59:47 +0900
changeset 9224 75c0420badef
parent 6655 b4130d85e450
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: 6655
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
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
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    36
public final class BaseLocale {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    37
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    38
    public static final String SEP = "_";
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    39
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    40
    private static final Cache CACHE = new Cache();
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
    41
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
    42
    private final String language;
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
    43
    private final String script;
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
    44
    private final String region;
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
    45
    private final String variant;
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
    46
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
    47
    private volatile int hash = 0;
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    48
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
    49
    // This method must be called only when creating the Locale.* constants.
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
    50
    private BaseLocale(String language, String region) {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
    51
        this.language = language;
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
    52
        this.script = "";
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
    53
        this.region = region;
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
    54
        this.variant = "";
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
    55
    }
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    56
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    57
    private BaseLocale(String language, String script, String region, String variant) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
    58
        this.language = (language != null) ? LocaleUtils.toLowerString(language).intern() : "";
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
    59
        this.script = (script != null) ? LocaleUtils.toTitleString(script).intern() : "";
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
    60
        this.region = (region != null) ? LocaleUtils.toUpperString(region).intern() : "";
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
    61
        this.variant = (variant != null) ? variant.intern() : "";
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    62
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    63
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
    64
    // Called for creating the Locale.* constants. No argument
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
    65
    // validation is performed.
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
    66
    public static BaseLocale createInstance(String language, String region) {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
    67
        BaseLocale base = new BaseLocale(language, region);
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
    68
        CACHE.put(new Key(language, region), base);
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
    69
        return base;
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
    70
    }
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
    71
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
    72
    public static BaseLocale getInstance(String language, String script,
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
    73
                                         String region, String variant) {
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    74
        // JDK uses deprecated ISO639.1 language codes for he, yi and id
6655
b4130d85e450 6986612: pit jdk7 b112: java.util.Locale getDisplayVariant() sqe test getDisplayVariantTests.java fails
naoto
parents: 6501
diff changeset
    75
        if (language != null) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
    76
            if (LocaleUtils.caseIgnoreMatch(language, "he")) {
6655
b4130d85e450 6986612: pit jdk7 b112: java.util.Locale getDisplayVariant() sqe test getDisplayVariantTests.java fails
naoto
parents: 6501
diff changeset
    77
                language = "iw";
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
    78
            } else if (LocaleUtils.caseIgnoreMatch(language, "yi")) {
6655
b4130d85e450 6986612: pit jdk7 b112: java.util.Locale getDisplayVariant() sqe test getDisplayVariantTests.java fails
naoto
parents: 6501
diff changeset
    79
                language = "ji";
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
    80
            } else if (LocaleUtils.caseIgnoreMatch(language, "id")) {
6655
b4130d85e450 6986612: pit jdk7 b112: java.util.Locale getDisplayVariant() sqe test getDisplayVariantTests.java fails
naoto
parents: 6501
diff changeset
    81
                language = "in";
b4130d85e450 6986612: pit jdk7 b112: java.util.Locale getDisplayVariant() sqe test getDisplayVariantTests.java fails
naoto
parents: 6501
diff changeset
    82
            }
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    83
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    84
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    85
        Key key = new Key(language, script, region, variant);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    86
        BaseLocale baseLocale = CACHE.get(key);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    87
        return baseLocale;
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 String getLanguage() {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
    91
        return language;
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    92
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    93
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    94
    public String getScript() {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
    95
        return script;
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    96
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    97
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    98
    public String getRegion() {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
    99
        return region;
6501
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
    public String getVariant() {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   103
        return variant;
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   104
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   105
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   106
    @Override
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   107
    public boolean equals(Object obj) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   108
        if (this == obj) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   109
            return true;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   110
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   111
        if (!(obj instanceof BaseLocale)) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   112
            return false;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   113
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   114
        BaseLocale other = (BaseLocale)obj;
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   115
        return language == other.language
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   116
               && script == other.script
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   117
               && region == other.region
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   118
               && variant == other.variant;
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   119
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   120
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   121
    @Override
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   122
    public String toString() {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   123
        StringBuilder buf = new StringBuilder();
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   124
        if (language.length() > 0) {
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   125
            buf.append("language=");
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   126
            buf.append(language);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   127
        }
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   128
        if (script.length() > 0) {
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   129
            if (buf.length() > 0) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   130
                buf.append(", ");
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   131
            }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   132
            buf.append("script=");
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   133
            buf.append(script);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   134
        }
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   135
        if (region.length() > 0) {
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   136
            if (buf.length() > 0) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   137
                buf.append(", ");
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   138
            }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   139
            buf.append("region=");
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   140
            buf.append(region);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   141
        }
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   142
        if (variant.length() > 0) {
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   143
            if (buf.length() > 0) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   144
                buf.append(", ");
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   145
            }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   146
            buf.append("variant=");
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   147
            buf.append(variant);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   148
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   149
        return buf.toString();
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   150
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   151
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   152
    @Override
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   153
    public int hashCode() {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   154
        int h = hash;
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   155
        if (h == 0) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   156
            // Generating a hash value from language, script, region and variant
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   157
            h = language.hashCode();
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   158
            h = 31 * h + script.hashCode();
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   159
            h = 31 * h + region.hashCode();
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   160
            h = 31 * h + variant.hashCode();
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   161
            hash = h;
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   162
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   163
        return h;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   164
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   165
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   166
    private static final class Key implements Comparable<Key> {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   167
        private final String lang;
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   168
        private final String scrt;
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   169
        private final String regn;
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   170
        private final String vart;
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   171
        private final boolean normalized;
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   172
        private final int hash;
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   173
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   174
        /**
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   175
         * Creates a Key. language and region must be normalized
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   176
         * (intern'ed in the proper case).
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   177
         */
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   178
        private Key(String language, String region) {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   179
            assert language.intern() == language
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   180
                   && region.intern() == region;
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   181
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   182
            lang = language;
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   183
            scrt = "";
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   184
            regn = region;
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   185
            vart = "";
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   186
            this.normalized = true;
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   187
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   188
            int h = language.hashCode();
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   189
            if (region != "") {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   190
                int len = region.length();
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   191
                for (int i = 0; i < len; i++) {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   192
                    h = 31 * h + LocaleUtils.toLower(region.charAt(i));
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   193
                }
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   194
            }
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   195
            hash = h;
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   196
        }
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   197
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   198
        public Key(String language, String script, String region, String variant) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   199
            this(language, script, region, variant, false);
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   200
        }
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   201
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   202
        private Key(String language, String script, String region,
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   203
                    String variant, boolean normalized) {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   204
            int h = 0;
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   205
            if (language != null) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   206
                lang = language;
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   207
                int len = language.length();
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   208
                for (int i = 0; i < len; i++) {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   209
                    h = 31*h + LocaleUtils.toLower(language.charAt(i));
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   210
                }
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   211
            } else {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   212
                lang = "";
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   213
            }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   214
            if (script != null) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   215
                scrt = script;
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   216
                int len = script.length();
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   217
                for (int i = 0; i < len; i++) {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   218
                    h = 31*h + LocaleUtils.toLower(script.charAt(i));
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   219
                }
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   220
            } else {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   221
                scrt = "";
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   222
            }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   223
            if (region != null) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   224
                regn = region;
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   225
                int len = region.length();
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   226
                for (int i = 0; i < len; i++) {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   227
                    h = 31*h + LocaleUtils.toLower(region.charAt(i));
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   228
                }
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   229
            } else {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   230
                regn = "";
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   231
            }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   232
            if (variant != null) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   233
                vart = variant;
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   234
                int len = variant.length();
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   235
                for (int i = 0; i < len; i++) {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   236
                    h = 31*h + variant.charAt(i);
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   237
                }
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   238
            } else {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   239
                vart = "";
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   240
            }
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   241
            hash = h;
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   242
            this.normalized = normalized;
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   243
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   244
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   245
        @Override
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   246
        public boolean equals(Object obj) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   247
            return (this == obj) ||
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   248
                    (obj instanceof Key)
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   249
                    && this.hash == ((Key)obj).hash
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   250
                    && LocaleUtils.caseIgnoreMatch(((Key)obj).lang, this.lang)
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   251
                    && LocaleUtils.caseIgnoreMatch(((Key)obj).scrt, this.scrt)
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   252
                    && LocaleUtils.caseIgnoreMatch(((Key)obj).regn, this.regn)
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   253
                    && ((Key)obj).vart.equals(vart); // variant is case sensitive in JDK!
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   254
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   255
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   256
        @Override
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   257
        public int compareTo(Key other) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   258
            int res = LocaleUtils.caseIgnoreCompare(this.lang, other.lang);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   259
            if (res == 0) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   260
                res = LocaleUtils.caseIgnoreCompare(this.scrt, other.scrt);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   261
                if (res == 0) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   262
                    res = LocaleUtils.caseIgnoreCompare(this.regn, other.regn);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   263
                    if (res == 0) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   264
                        res = this.vart.compareTo(other.vart);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   265
                    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   266
                }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   267
            }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   268
            return res;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   269
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   270
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   271
        @Override
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   272
        public int hashCode() {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   273
            return hash;
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   274
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   275
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   276
        public static Key normalize(Key key) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   277
            if (key.normalized) {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   278
                return key;
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   279
            }
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   280
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   281
            String lang = LocaleUtils.toLowerString(key.lang).intern();
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   282
            String scrt = LocaleUtils.toTitleString(key.scrt).intern();
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   283
            String regn = LocaleUtils.toUpperString(key.regn).intern();
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   284
            String vart = key.vart.intern(); // preserve upper/lower cases
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   285
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   286
            return new Key(lang, scrt, regn, vart, true);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   287
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   288
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   289
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   290
    private static class Cache extends LocaleObjectCache<Key, BaseLocale> {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   291
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   292
        public Cache() {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   293
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   294
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   295
        @Override
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   296
        protected Key normalizeKey(Key key) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   297
            return Key.normalize(key);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   298
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   299
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   300
        @Override
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   301
        protected BaseLocale createObject(Key key) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6655
diff changeset
   302
            return new BaseLocale(key.lang, key.scrt, key.regn, key.vart);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   303
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   304
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   305
}