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