author | redestad |
Thu, 08 Feb 2018 18:45:30 +0100 | |
changeset 48763 | 3d17a524da95 |
parent 47216 | 71c04702a3d5 |
child 54412 | 41356f083e93 |
permissions | -rw-r--r-- |
6501 | 1 |
/* |
48763 | 2 |
* Copyright (c) 2010, 2018, 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; |
|
48763 | 34 |
|
27067 | 35 |
import java.lang.ref.SoftReference; |
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 |
|
48763 | 51 |
// This method must be called with normalize = false only when creating the |
52 |
// Locale.* constants and non-normalized BaseLocale$Keys used for lookup. |
|
53 |
private BaseLocale(String language, String script, String region, String variant, |
|
54 |
boolean normalize) { |
|
55 |
if (normalize) { |
|
56 |
this.language = LocaleUtils.toLowerString(language).intern(); |
|
57 |
this.script = LocaleUtils.toTitleString(script).intern(); |
|
58 |
this.region = LocaleUtils.toUpperString(region).intern(); |
|
59 |
this.variant = variant.intern(); |
|
60 |
} else { |
|
61 |
this.language = language; |
|
62 |
this.script = script; |
|
63 |
this.region = region; |
|
64 |
this.variant = variant; |
|
65 |
} |
|
6501 | 66 |
} |
67 |
||
9224 | 68 |
// Called for creating the Locale.* constants. No argument |
69 |
// validation is performed. |
|
70 |
public static BaseLocale createInstance(String language, String region) { |
|
48763 | 71 |
BaseLocale base = new BaseLocale(language, "", region, "", false); |
72 |
CACHE.put(new Key(base), base); |
|
9224 | 73 |
return base; |
74 |
} |
|
75 |
||
76 |
public static BaseLocale getInstance(String language, String script, |
|
77 |
String region, String variant) { |
|
6501 | 78 |
// 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
|
79 |
if (language != null) { |
9224 | 80 |
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
|
81 |
language = "iw"; |
9224 | 82 |
} 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
|
83 |
language = "ji"; |
9224 | 84 |
} 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
|
85 |
language = "in"; |
b4130d85e450
6986612: pit jdk7 b112: java.util.Locale getDisplayVariant() sqe test getDisplayVariantTests.java fails
naoto
parents:
6501
diff
changeset
|
86 |
} |
6501 | 87 |
} |
88 |
||
48763 | 89 |
Key key = new Key(language, script, region, variant, false); |
6501 | 90 |
BaseLocale baseLocale = CACHE.get(key); |
91 |
return baseLocale; |
|
92 |
} |
|
93 |
||
94 |
public String getLanguage() { |
|
9224 | 95 |
return language; |
6501 | 96 |
} |
97 |
||
98 |
public String getScript() { |
|
9224 | 99 |
return script; |
6501 | 100 |
} |
101 |
||
102 |
public String getRegion() { |
|
9224 | 103 |
return region; |
6501 | 104 |
} |
105 |
||
106 |
public String getVariant() { |
|
9224 | 107 |
return variant; |
6501 | 108 |
} |
109 |
||
9224 | 110 |
@Override |
6501 | 111 |
public boolean equals(Object obj) { |
112 |
if (this == obj) { |
|
113 |
return true; |
|
114 |
} |
|
115 |
if (!(obj instanceof BaseLocale)) { |
|
116 |
return false; |
|
117 |
} |
|
118 |
BaseLocale other = (BaseLocale)obj; |
|
9224 | 119 |
return language == other.language |
120 |
&& script == other.script |
|
121 |
&& region == other.region |
|
122 |
&& variant == other.variant; |
|
6501 | 123 |
} |
124 |
||
9224 | 125 |
@Override |
6501 | 126 |
public String toString() { |
26219
1a19360ff122
8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents:
25859
diff
changeset
|
127 |
StringJoiner sj = new StringJoiner(", "); |
48763 | 128 |
if (!language.isEmpty()) { |
26219
1a19360ff122
8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents:
25859
diff
changeset
|
129 |
sj.add("language=" + language); |
6501 | 130 |
} |
48763 | 131 |
if (!script.isEmpty()) { |
26219
1a19360ff122
8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents:
25859
diff
changeset
|
132 |
sj.add("script=" + script); |
6501 | 133 |
} |
48763 | 134 |
if (!region.isEmpty()) { |
26219
1a19360ff122
8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents:
25859
diff
changeset
|
135 |
sj.add("region=" + region); |
6501 | 136 |
} |
48763 | 137 |
if (!variant.isEmpty()) { |
26219
1a19360ff122
8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents:
25859
diff
changeset
|
138 |
sj.add("variant=" + variant); |
6501 | 139 |
} |
26219
1a19360ff122
8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents:
25859
diff
changeset
|
140 |
return sj.toString(); |
6501 | 141 |
} |
142 |
||
9224 | 143 |
@Override |
6501 | 144 |
public int hashCode() { |
9224 | 145 |
int h = hash; |
6501 | 146 |
if (h == 0) { |
147 |
// Generating a hash value from language, script, region and variant |
|
9224 | 148 |
h = language.hashCode(); |
149 |
h = 31 * h + script.hashCode(); |
|
150 |
h = 31 * h + region.hashCode(); |
|
151 |
h = 31 * h + variant.hashCode(); |
|
34774
03b4e6dc367b
8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents:
27067
diff
changeset
|
152 |
if (h != 0) { |
03b4e6dc367b
8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents:
27067
diff
changeset
|
153 |
hash = h; |
03b4e6dc367b
8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents:
27067
diff
changeset
|
154 |
} |
6501 | 155 |
} |
156 |
return h; |
|
157 |
} |
|
158 |
||
27067 | 159 |
private static final class Key { |
48763 | 160 |
/** |
161 |
* Keep a SoftReference to the Key data if normalized (actually used |
|
162 |
* as a cache key) and not initialized via the constant creation path. |
|
163 |
* |
|
164 |
* This allows us to avoid creating SoftReferences on lookup Keys |
|
165 |
* (which are short-lived) and for Locales created via |
|
166 |
* Locale#createConstant. |
|
167 |
*/ |
|
168 |
private final SoftReference<BaseLocale> holderRef; |
|
169 |
private final BaseLocale holder; |
|
170 |
||
9224 | 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 |
*/ |
|
48763 | 178 |
private Key(BaseLocale locale) { |
179 |
this.holder = locale; |
|
180 |
this.holderRef = null; |
|
9224 | 181 |
this.normalized = true; |
48763 | 182 |
String language = locale.getLanguage(); |
183 |
String region = locale.getRegion(); |
|
184 |
assert LocaleUtils.toLowerString(language).intern() == language |
|
185 |
&& LocaleUtils.toUpperString(region).intern() == region |
|
186 |
&& locale.getVariant() == "" |
|
187 |
&& locale.getScript() == ""; |
|
9224 | 188 |
|
189 |
int h = language.hashCode(); |
|
190 |
if (region != "") { |
|
191 |
int len = region.length(); |
|
192 |
for (int i = 0; i < len; i++) { |
|
193 |
h = 31 * h + LocaleUtils.toLower(region.charAt(i)); |
|
194 |
} |
|
195 |
} |
|
196 |
hash = h; |
|
197 |
} |
|
6501 | 198 |
|
48763 | 199 |
private Key(String language, String script, String region, |
200 |
String variant, boolean normalize) { |
|
201 |
if (language == null) { |
|
202 |
language = ""; |
|
203 |
} |
|
204 |
if (script == null) { |
|
205 |
script = ""; |
|
206 |
} |
|
207 |
if (region == null) { |
|
208 |
region = ""; |
|
209 |
} |
|
210 |
if (variant == null) { |
|
211 |
variant = ""; |
|
212 |
} |
|
213 |
||
214 |
BaseLocale locale = new BaseLocale(language, script, region, variant, normalize); |
|
215 |
this.normalized = normalize; |
|
216 |
if (normalized) { |
|
217 |
this.holderRef = new SoftReference<>(locale); |
|
218 |
this.holder = null; |
|
219 |
} else { |
|
220 |
this.holderRef = null; |
|
221 |
this.holder = locale; |
|
222 |
} |
|
223 |
this.hash = hashCode(locale); |
|
9224 | 224 |
} |
225 |
||
48763 | 226 |
public int hashCode() { |
227 |
return hash; |
|
228 |
} |
|
229 |
||
230 |
private int hashCode(BaseLocale locale) { |
|
9224 | 231 |
int h = 0; |
48763 | 232 |
String lang = locale.getLanguage(); |
233 |
int len = lang.length(); |
|
234 |
for (int i = 0; i < len; i++) { |
|
235 |
h = 31*h + LocaleUtils.toLower(lang.charAt(i)); |
|
6501 | 236 |
} |
48763 | 237 |
String scrt = locale.getScript(); |
238 |
len = scrt.length(); |
|
239 |
for (int i = 0; i < len; i++) { |
|
240 |
h = 31*h + LocaleUtils.toLower(scrt.charAt(i)); |
|
6501 | 241 |
} |
48763 | 242 |
String regn = locale.getRegion(); |
243 |
len = regn.length(); |
|
244 |
for (int i = 0; i < len; i++) { |
|
245 |
h = 31*h + LocaleUtils.toLower(regn.charAt(i)); |
|
6501 | 246 |
} |
48763 | 247 |
String vart = locale.getVariant(); |
248 |
len = vart.length(); |
|
249 |
for (int i = 0; i < len; i++) { |
|
250 |
h = 31*h + vart.charAt(i); |
|
6501 | 251 |
} |
48763 | 252 |
return h; |
253 |
} |
|
254 |
||
255 |
private BaseLocale getBaseLocale() { |
|
256 |
return (holder == null) ? holderRef.get() : holder; |
|
6501 | 257 |
} |
258 |
||
9224 | 259 |
@Override |
6501 | 260 |
public boolean equals(Object obj) { |
27067 | 261 |
if (this == obj) { |
262 |
return true; |
|
263 |
} |
|
264 |
if (obj instanceof Key && this.hash == ((Key)obj).hash) { |
|
48763 | 265 |
BaseLocale other = ((Key) obj).getBaseLocale(); |
266 |
BaseLocale locale = this.getBaseLocale(); |
|
267 |
if (other != null && locale != null |
|
268 |
&& LocaleUtils.caseIgnoreMatch(other.getLanguage(), locale.getLanguage()) |
|
269 |
&& LocaleUtils.caseIgnoreMatch(other.getScript(), locale.getScript()) |
|
270 |
&& LocaleUtils.caseIgnoreMatch(other.getRegion(), locale.getRegion()) |
|
271 |
// variant is case sensitive in JDK! |
|
272 |
&& other.getVariant().equals(locale.getVariant())) { |
|
273 |
return true; |
|
6501 | 274 |
} |
275 |
} |
|
27067 | 276 |
return false; |
6501 | 277 |
} |
278 |
||
279 |
public static Key normalize(Key key) { |
|
9224 | 280 |
if (key.normalized) { |
281 |
return key; |
|
282 |
} |
|
6501 | 283 |
|
48763 | 284 |
// Only normalized keys may be softly referencing the data holder |
285 |
assert (key.holder != null && key.holderRef == null); |
|
286 |
BaseLocale locale = key.holder; |
|
287 |
return new Key(locale.getLanguage(), locale.getScript(), |
|
288 |
locale.getRegion(), locale.getVariant(), true); |
|
6501 | 289 |
} |
290 |
} |
|
291 |
||
292 |
private static class Cache extends LocaleObjectCache<Key, BaseLocale> { |
|
293 |
||
294 |
public Cache() { |
|
295 |
} |
|
296 |
||
9224 | 297 |
@Override |
6501 | 298 |
protected Key normalizeKey(Key key) { |
299 |
return Key.normalize(key); |
|
300 |
} |
|
301 |
||
9224 | 302 |
@Override |
6501 | 303 |
protected BaseLocale createObject(Key key) { |
48763 | 304 |
return Key.normalize(key).getBaseLocale(); |
6501 | 305 |
} |
306 |
} |
|
307 |
} |