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