author | okutsu |
Thu, 14 Apr 2011 15:59:47 +0900 | |
changeset 9224 | 75c0420badef |
parent 6655 | b4130d85e450 |
permissions | -rw-r--r-- |
6501 | 1 |
/* |
9224 | 2 |
* Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved. |
6501 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
/* |
|
27 |
******************************************************************************* |
|
28 |
* Copyright (C) 2009-2010, International Business Machines Corporation and * |
|
29 |
* others. All Rights Reserved. * |
|
30 |
******************************************************************************* |
|
31 |
*/ |
|
32 |
||
33 |
package sun.util.locale; |
|
34 |
||
35 |
||
36 |
public final class BaseLocale { |
|
37 |
||
38 |
public static final String SEP = "_"; |
|
39 |
||
40 |
private static final Cache CACHE = new Cache(); |
|
9224 | 41 |
|
42 |
private final String language; |
|
43 |
private final String script; |
|
44 |
private final String region; |
|
45 |
private final String variant; |
|
46 |
||
47 |
private volatile int hash = 0; |
|
6501 | 48 |
|
9224 | 49 |
// This method must be called only when creating the Locale.* constants. |
50 |
private BaseLocale(String language, String region) { |
|
51 |
this.language = language; |
|
52 |
this.script = ""; |
|
53 |
this.region = region; |
|
54 |
this.variant = ""; |
|
55 |
} |
|
6501 | 56 |
|
57 |
private BaseLocale(String language, String script, String region, String variant) { |
|
9224 | 58 |
this.language = (language != null) ? LocaleUtils.toLowerString(language).intern() : ""; |
59 |
this.script = (script != null) ? LocaleUtils.toTitleString(script).intern() : ""; |
|
60 |
this.region = (region != null) ? LocaleUtils.toUpperString(region).intern() : ""; |
|
61 |
this.variant = (variant != null) ? variant.intern() : ""; |
|
6501 | 62 |
} |
63 |
||
9224 | 64 |
// Called for creating the Locale.* constants. No argument |
65 |
// validation is performed. |
|
66 |
public static BaseLocale createInstance(String language, String region) { |
|
67 |
BaseLocale base = new BaseLocale(language, region); |
|
68 |
CACHE.put(new Key(language, region), base); |
|
69 |
return base; |
|
70 |
} |
|
71 |
||
72 |
public static BaseLocale getInstance(String language, String script, |
|
73 |
String region, String variant) { |
|
6501 | 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 | 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 | 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 | 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 | 83 |
} |
84 |
||
85 |
Key key = new Key(language, script, region, variant); |
|
86 |
BaseLocale baseLocale = CACHE.get(key); |
|
87 |
return baseLocale; |
|
88 |
} |
|
89 |
||
90 |
public String getLanguage() { |
|
9224 | 91 |
return language; |
6501 | 92 |
} |
93 |
||
94 |
public String getScript() { |
|
9224 | 95 |
return script; |
6501 | 96 |
} |
97 |
||
98 |
public String getRegion() { |
|
9224 | 99 |
return region; |
6501 | 100 |
} |
101 |
||
102 |
public String getVariant() { |
|
9224 | 103 |
return variant; |
6501 | 104 |
} |
105 |
||
9224 | 106 |
@Override |
6501 | 107 |
public boolean equals(Object obj) { |
108 |
if (this == obj) { |
|
109 |
return true; |
|
110 |
} |
|
111 |
if (!(obj instanceof BaseLocale)) { |
|
112 |
return false; |
|
113 |
} |
|
114 |
BaseLocale other = (BaseLocale)obj; |
|
9224 | 115 |
return language == other.language |
116 |
&& script == other.script |
|
117 |
&& region == other.region |
|
118 |
&& variant == other.variant; |
|
6501 | 119 |
} |
120 |
||
9224 | 121 |
@Override |
6501 | 122 |
public String toString() { |
123 |
StringBuilder buf = new StringBuilder(); |
|
9224 | 124 |
if (language.length() > 0) { |
6501 | 125 |
buf.append("language="); |
9224 | 126 |
buf.append(language); |
6501 | 127 |
} |
9224 | 128 |
if (script.length() > 0) { |
6501 | 129 |
if (buf.length() > 0) { |
130 |
buf.append(", "); |
|
131 |
} |
|
132 |
buf.append("script="); |
|
9224 | 133 |
buf.append(script); |
6501 | 134 |
} |
9224 | 135 |
if (region.length() > 0) { |
6501 | 136 |
if (buf.length() > 0) { |
137 |
buf.append(", "); |
|
138 |
} |
|
139 |
buf.append("region="); |
|
9224 | 140 |
buf.append(region); |
6501 | 141 |
} |
9224 | 142 |
if (variant.length() > 0) { |
6501 | 143 |
if (buf.length() > 0) { |
144 |
buf.append(", "); |
|
145 |
} |
|
146 |
buf.append("variant="); |
|
9224 | 147 |
buf.append(variant); |
6501 | 148 |
} |
149 |
return buf.toString(); |
|
150 |
} |
|
151 |
||
9224 | 152 |
@Override |
6501 | 153 |
public int hashCode() { |
9224 | 154 |
int h = hash; |
6501 | 155 |
if (h == 0) { |
156 |
// Generating a hash value from language, script, region and variant |
|
9224 | 157 |
h = language.hashCode(); |
158 |
h = 31 * h + script.hashCode(); |
|
159 |
h = 31 * h + region.hashCode(); |
|
160 |
h = 31 * h + variant.hashCode(); |
|
161 |
hash = h; |
|
6501 | 162 |
} |
163 |
return h; |
|
164 |
} |
|
165 |
||
9224 | 166 |
private static final class Key implements Comparable<Key> { |
167 |
private final String lang; |
|
168 |
private final String scrt; |
|
169 |
private final String regn; |
|
170 |
private final String vart; |
|
171 |
private final boolean normalized; |
|
172 |
private final int hash; |
|
173 |
||
174 |
/** |
|
175 |
* Creates a Key. language and region must be normalized |
|
176 |
* (intern'ed in the proper case). |
|
177 |
*/ |
|
178 |
private Key(String language, String region) { |
|
179 |
assert language.intern() == language |
|
180 |
&& region.intern() == region; |
|
6501 | 181 |
|
9224 | 182 |
lang = language; |
183 |
scrt = ""; |
|
184 |
regn = region; |
|
185 |
vart = ""; |
|
186 |
this.normalized = true; |
|
187 |
||
188 |
int h = language.hashCode(); |
|
189 |
if (region != "") { |
|
190 |
int len = region.length(); |
|
191 |
for (int i = 0; i < len; i++) { |
|
192 |
h = 31 * h + LocaleUtils.toLower(region.charAt(i)); |
|
193 |
} |
|
194 |
} |
|
195 |
hash = h; |
|
196 |
} |
|
6501 | 197 |
|
198 |
public Key(String language, String script, String region, String variant) { |
|
9224 | 199 |
this(language, script, region, variant, false); |
200 |
} |
|
201 |
||
202 |
private Key(String language, String script, String region, |
|
203 |
String variant, boolean normalized) { |
|
204 |
int h = 0; |
|
6501 | 205 |
if (language != null) { |
9224 | 206 |
lang = language; |
207 |
int len = language.length(); |
|
208 |
for (int i = 0; i < len; i++) { |
|
209 |
h = 31*h + LocaleUtils.toLower(language.charAt(i)); |
|
210 |
} |
|
211 |
} else { |
|
212 |
lang = ""; |
|
6501 | 213 |
} |
214 |
if (script != null) { |
|
9224 | 215 |
scrt = script; |
216 |
int len = script.length(); |
|
217 |
for (int i = 0; i < len; i++) { |
|
218 |
h = 31*h + LocaleUtils.toLower(script.charAt(i)); |
|
219 |
} |
|
220 |
} else { |
|
221 |
scrt = ""; |
|
6501 | 222 |
} |
223 |
if (region != null) { |
|
9224 | 224 |
regn = region; |
225 |
int len = region.length(); |
|
226 |
for (int i = 0; i < len; i++) { |
|
227 |
h = 31*h + LocaleUtils.toLower(region.charAt(i)); |
|
228 |
} |
|
229 |
} else { |
|
230 |
regn = ""; |
|
6501 | 231 |
} |
232 |
if (variant != null) { |
|
9224 | 233 |
vart = variant; |
234 |
int len = variant.length(); |
|
235 |
for (int i = 0; i < len; i++) { |
|
236 |
h = 31*h + variant.charAt(i); |
|
237 |
} |
|
238 |
} else { |
|
239 |
vart = ""; |
|
6501 | 240 |
} |
9224 | 241 |
hash = h; |
242 |
this.normalized = normalized; |
|
6501 | 243 |
} |
244 |
||
9224 | 245 |
@Override |
6501 | 246 |
public boolean equals(Object obj) { |
247 |
return (this == obj) || |
|
248 |
(obj instanceof Key) |
|
9224 | 249 |
&& this.hash == ((Key)obj).hash |
250 |
&& LocaleUtils.caseIgnoreMatch(((Key)obj).lang, this.lang) |
|
251 |
&& LocaleUtils.caseIgnoreMatch(((Key)obj).scrt, this.scrt) |
|
252 |
&& LocaleUtils.caseIgnoreMatch(((Key)obj).regn, this.regn) |
|
253 |
&& ((Key)obj).vart.equals(vart); // variant is case sensitive in JDK! |
|
6501 | 254 |
} |
255 |
||
9224 | 256 |
@Override |
6501 | 257 |
public int compareTo(Key other) { |
9224 | 258 |
int res = LocaleUtils.caseIgnoreCompare(this.lang, other.lang); |
6501 | 259 |
if (res == 0) { |
9224 | 260 |
res = LocaleUtils.caseIgnoreCompare(this.scrt, other.scrt); |
6501 | 261 |
if (res == 0) { |
9224 | 262 |
res = LocaleUtils.caseIgnoreCompare(this.regn, other.regn); |
6501 | 263 |
if (res == 0) { |
9224 | 264 |
res = this.vart.compareTo(other.vart); |
6501 | 265 |
} |
266 |
} |
|
267 |
} |
|
268 |
return res; |
|
269 |
} |
|
270 |
||
9224 | 271 |
@Override |
6501 | 272 |
public int hashCode() { |
9224 | 273 |
return hash; |
6501 | 274 |
} |
275 |
||
276 |
public static Key normalize(Key key) { |
|
9224 | 277 |
if (key.normalized) { |
278 |
return key; |
|
279 |
} |
|
6501 | 280 |
|
9224 | 281 |
String lang = LocaleUtils.toLowerString(key.lang).intern(); |
282 |
String scrt = LocaleUtils.toTitleString(key.scrt).intern(); |
|
283 |
String regn = LocaleUtils.toUpperString(key.regn).intern(); |
|
284 |
String vart = key.vart.intern(); // preserve upper/lower cases |
|
285 |
||
286 |
return new Key(lang, scrt, regn, vart, true); |
|
6501 | 287 |
} |
288 |
} |
|
289 |
||
290 |
private static class Cache extends LocaleObjectCache<Key, BaseLocale> { |
|
291 |
||
292 |
public Cache() { |
|
293 |
} |
|
294 |
||
9224 | 295 |
@Override |
6501 | 296 |
protected Key normalizeKey(Key key) { |
297 |
return Key.normalize(key); |
|
298 |
} |
|
299 |
||
9224 | 300 |
@Override |
6501 | 301 |
protected BaseLocale createObject(Key key) { |
9224 | 302 |
return new BaseLocale(key.lang, key.scrt, key.regn, key.vart); |
6501 | 303 |
} |
304 |
} |
|
305 |
} |