author | michaelm |
Thu, 14 Nov 2019 15:01:49 +0000 | |
branch | unixdomainchannels |
changeset 59082 | 5e250ee9259e |
parent 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 |
|
54412 | 35 |
import jdk.internal.misc.VM; |
36 |
import jdk.internal.vm.annotation.Stable; |
|
37 |
||
27067 | 38 |
import java.lang.ref.SoftReference; |
26219
1a19360ff122
8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents:
25859
diff
changeset
|
39 |
import java.util.StringJoiner; |
6501 | 40 |
|
41 |
public final class BaseLocale { |
|
42 |
||
54412 | 43 |
public static @Stable BaseLocale[] constantBaseLocales; |
44 |
public static final byte ENGLISH = 0, |
|
45 |
FRENCH = 1, |
|
46 |
GERMAN = 2, |
|
47 |
ITALIAN = 3, |
|
48 |
JAPANESE = 4, |
|
49 |
KOREAN = 5, |
|
50 |
CHINESE = 6, |
|
51 |
SIMPLIFIED_CHINESE = 7, |
|
52 |
TRADITIONAL_CHINESE = 8, |
|
53 |
FRANCE = 9, |
|
54 |
GERMANY = 10, |
|
55 |
ITALY = 11, |
|
56 |
JAPAN = 12, |
|
57 |
KOREA = 13, |
|
58 |
UK = 14, |
|
59 |
US = 15, |
|
60 |
CANADA = 16, |
|
61 |
CANADA_FRENCH = 17, |
|
62 |
ROOT = 18, |
|
63 |
NUM_CONSTANTS = 19; |
|
64 |
static { |
|
65 |
VM.initializeFromArchive(BaseLocale.class); |
|
66 |
BaseLocale[] baseLocales = constantBaseLocales; |
|
67 |
if (baseLocales == null) { |
|
68 |
baseLocales = new BaseLocale[NUM_CONSTANTS]; |
|
69 |
baseLocales[ENGLISH] = createInstance("en", ""); |
|
70 |
baseLocales[FRENCH] = createInstance("fr", ""); |
|
71 |
baseLocales[GERMAN] = createInstance("de", ""); |
|
72 |
baseLocales[ITALIAN] = createInstance("it", ""); |
|
73 |
baseLocales[JAPANESE] = createInstance("ja", ""); |
|
74 |
baseLocales[KOREAN] = createInstance("ko", ""); |
|
75 |
baseLocales[CHINESE] = createInstance("zh", ""); |
|
76 |
baseLocales[SIMPLIFIED_CHINESE] = createInstance("zh", "CN"); |
|
77 |
baseLocales[TRADITIONAL_CHINESE] = createInstance("zh", "TW"); |
|
78 |
baseLocales[FRANCE] = createInstance("fr", "FR"); |
|
79 |
baseLocales[GERMANY] = createInstance("de", "DE"); |
|
80 |
baseLocales[ITALY] = createInstance("it", "IT"); |
|
81 |
baseLocales[JAPAN] = createInstance("ja", "JP"); |
|
82 |
baseLocales[KOREA] = createInstance("ko", "KR"); |
|
83 |
baseLocales[UK] = createInstance("en", "GB"); |
|
84 |
baseLocales[US] = createInstance("en", "US"); |
|
85 |
baseLocales[CANADA] = createInstance("en", "CA"); |
|
86 |
baseLocales[CANADA_FRENCH] = createInstance("fr", "CA"); |
|
87 |
baseLocales[ROOT] = createInstance("", ""); |
|
88 |
constantBaseLocales = baseLocales; |
|
89 |
} |
|
90 |
} |
|
6501 | 91 |
|
54412 | 92 |
public static final String SEP = "_"; |
9224 | 93 |
|
94 |
private final String language; |
|
95 |
private final String script; |
|
96 |
private final String region; |
|
97 |
private final String variant; |
|
98 |
||
34774
03b4e6dc367b
8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents:
27067
diff
changeset
|
99 |
private volatile int hash; |
6501 | 100 |
|
48763 | 101 |
// This method must be called with normalize = false only when creating the |
102 |
// Locale.* constants and non-normalized BaseLocale$Keys used for lookup. |
|
103 |
private BaseLocale(String language, String script, String region, String variant, |
|
104 |
boolean normalize) { |
|
105 |
if (normalize) { |
|
106 |
this.language = LocaleUtils.toLowerString(language).intern(); |
|
107 |
this.script = LocaleUtils.toTitleString(script).intern(); |
|
108 |
this.region = LocaleUtils.toUpperString(region).intern(); |
|
109 |
this.variant = variant.intern(); |
|
110 |
} else { |
|
111 |
this.language = language; |
|
112 |
this.script = script; |
|
113 |
this.region = region; |
|
114 |
this.variant = variant; |
|
115 |
} |
|
6501 | 116 |
} |
117 |
||
9224 | 118 |
// Called for creating the Locale.* constants. No argument |
119 |
// validation is performed. |
|
54412 | 120 |
private static BaseLocale createInstance(String language, String region) { |
121 |
return new BaseLocale(language, "", region, "", false); |
|
9224 | 122 |
} |
123 |
||
124 |
public static BaseLocale getInstance(String language, String script, |
|
125 |
String region, String variant) { |
|
54412 | 126 |
|
127 |
if (script == null) { |
|
128 |
script = ""; |
|
129 |
} |
|
130 |
if (region == null) { |
|
131 |
region = ""; |
|
132 |
} |
|
133 |
if (language == null) { |
|
134 |
language = null; |
|
135 |
} |
|
136 |
if (variant == null) { |
|
137 |
variant = ""; |
|
138 |
} |
|
139 |
||
140 |
// Non-allocating for most uses |
|
141 |
language = LocaleUtils.toLowerString(language); |
|
142 |
region = LocaleUtils.toUpperString(region); |
|
143 |
||
144 |
// Check for constant base locales first |
|
145 |
if (script.isEmpty() && variant.isEmpty()) { |
|
146 |
for (BaseLocale baseLocale : constantBaseLocales) { |
|
147 |
if (baseLocale.getLanguage().equals(language) |
|
148 |
&& baseLocale.getRegion().equals(region)) { |
|
149 |
return baseLocale; |
|
150 |
} |
|
151 |
} |
|
152 |
} |
|
153 |
||
6501 | 154 |
// JDK uses deprecated ISO639.1 language codes for he, yi and id |
54412 | 155 |
if (!language.isEmpty()) { |
156 |
if (language.equals("he")) { |
|
6655
b4130d85e450
6986612: pit jdk7 b112: java.util.Locale getDisplayVariant() sqe test getDisplayVariantTests.java fails
naoto
parents:
6501
diff
changeset
|
157 |
language = "iw"; |
54412 | 158 |
} else if (language.equals("yi")) { |
6655
b4130d85e450
6986612: pit jdk7 b112: java.util.Locale getDisplayVariant() sqe test getDisplayVariantTests.java fails
naoto
parents:
6501
diff
changeset
|
159 |
language = "ji"; |
54412 | 160 |
} else if (language.equals("id")) { |
6655
b4130d85e450
6986612: pit jdk7 b112: java.util.Locale getDisplayVariant() sqe test getDisplayVariantTests.java fails
naoto
parents:
6501
diff
changeset
|
161 |
language = "in"; |
b4130d85e450
6986612: pit jdk7 b112: java.util.Locale getDisplayVariant() sqe test getDisplayVariantTests.java fails
naoto
parents:
6501
diff
changeset
|
162 |
} |
6501 | 163 |
} |
164 |
||
48763 | 165 |
Key key = new Key(language, script, region, variant, false); |
54412 | 166 |
return Cache.CACHE.get(key); |
6501 | 167 |
} |
168 |
||
169 |
public String getLanguage() { |
|
9224 | 170 |
return language; |
6501 | 171 |
} |
172 |
||
173 |
public String getScript() { |
|
9224 | 174 |
return script; |
6501 | 175 |
} |
176 |
||
177 |
public String getRegion() { |
|
9224 | 178 |
return region; |
6501 | 179 |
} |
180 |
||
181 |
public String getVariant() { |
|
9224 | 182 |
return variant; |
6501 | 183 |
} |
184 |
||
9224 | 185 |
@Override |
6501 | 186 |
public boolean equals(Object obj) { |
187 |
if (this == obj) { |
|
188 |
return true; |
|
189 |
} |
|
190 |
if (!(obj instanceof BaseLocale)) { |
|
191 |
return false; |
|
192 |
} |
|
193 |
BaseLocale other = (BaseLocale)obj; |
|
9224 | 194 |
return language == other.language |
195 |
&& script == other.script |
|
196 |
&& region == other.region |
|
197 |
&& variant == other.variant; |
|
6501 | 198 |
} |
199 |
||
9224 | 200 |
@Override |
6501 | 201 |
public String toString() { |
26219
1a19360ff122
8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents:
25859
diff
changeset
|
202 |
StringJoiner sj = new StringJoiner(", "); |
48763 | 203 |
if (!language.isEmpty()) { |
26219
1a19360ff122
8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents:
25859
diff
changeset
|
204 |
sj.add("language=" + language); |
6501 | 205 |
} |
48763 | 206 |
if (!script.isEmpty()) { |
26219
1a19360ff122
8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents:
25859
diff
changeset
|
207 |
sj.add("script=" + script); |
6501 | 208 |
} |
48763 | 209 |
if (!region.isEmpty()) { |
26219
1a19360ff122
8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents:
25859
diff
changeset
|
210 |
sj.add("region=" + region); |
6501 | 211 |
} |
48763 | 212 |
if (!variant.isEmpty()) { |
26219
1a19360ff122
8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents:
25859
diff
changeset
|
213 |
sj.add("variant=" + variant); |
6501 | 214 |
} |
26219
1a19360ff122
8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents:
25859
diff
changeset
|
215 |
return sj.toString(); |
6501 | 216 |
} |
217 |
||
9224 | 218 |
@Override |
6501 | 219 |
public int hashCode() { |
9224 | 220 |
int h = hash; |
6501 | 221 |
if (h == 0) { |
222 |
// Generating a hash value from language, script, region and variant |
|
9224 | 223 |
h = language.hashCode(); |
224 |
h = 31 * h + script.hashCode(); |
|
225 |
h = 31 * h + region.hashCode(); |
|
226 |
h = 31 * h + variant.hashCode(); |
|
34774
03b4e6dc367b
8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents:
27067
diff
changeset
|
227 |
if (h != 0) { |
03b4e6dc367b
8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents:
27067
diff
changeset
|
228 |
hash = h; |
03b4e6dc367b
8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents:
27067
diff
changeset
|
229 |
} |
6501 | 230 |
} |
231 |
return h; |
|
232 |
} |
|
233 |
||
27067 | 234 |
private static final class Key { |
48763 | 235 |
/** |
236 |
* Keep a SoftReference to the Key data if normalized (actually used |
|
237 |
* as a cache key) and not initialized via the constant creation path. |
|
238 |
* |
|
239 |
* This allows us to avoid creating SoftReferences on lookup Keys |
|
240 |
* (which are short-lived) and for Locales created via |
|
241 |
* Locale#createConstant. |
|
242 |
*/ |
|
243 |
private final SoftReference<BaseLocale> holderRef; |
|
244 |
private final BaseLocale holder; |
|
245 |
||
9224 | 246 |
private final boolean normalized; |
247 |
private final int hash; |
|
248 |
||
48763 | 249 |
private Key(String language, String script, String region, |
250 |
String variant, boolean normalize) { |
|
251 |
BaseLocale locale = new BaseLocale(language, script, region, variant, normalize); |
|
252 |
this.normalized = normalize; |
|
253 |
if (normalized) { |
|
254 |
this.holderRef = new SoftReference<>(locale); |
|
255 |
this.holder = null; |
|
256 |
} else { |
|
257 |
this.holderRef = null; |
|
258 |
this.holder = locale; |
|
259 |
} |
|
260 |
this.hash = hashCode(locale); |
|
9224 | 261 |
} |
262 |
||
48763 | 263 |
public int hashCode() { |
264 |
return hash; |
|
265 |
} |
|
266 |
||
267 |
private int hashCode(BaseLocale locale) { |
|
9224 | 268 |
int h = 0; |
48763 | 269 |
String lang = locale.getLanguage(); |
270 |
int len = lang.length(); |
|
271 |
for (int i = 0; i < len; i++) { |
|
272 |
h = 31*h + LocaleUtils.toLower(lang.charAt(i)); |
|
6501 | 273 |
} |
48763 | 274 |
String scrt = locale.getScript(); |
275 |
len = scrt.length(); |
|
276 |
for (int i = 0; i < len; i++) { |
|
277 |
h = 31*h + LocaleUtils.toLower(scrt.charAt(i)); |
|
6501 | 278 |
} |
48763 | 279 |
String regn = locale.getRegion(); |
280 |
len = regn.length(); |
|
281 |
for (int i = 0; i < len; i++) { |
|
282 |
h = 31*h + LocaleUtils.toLower(regn.charAt(i)); |
|
6501 | 283 |
} |
48763 | 284 |
String vart = locale.getVariant(); |
285 |
len = vart.length(); |
|
286 |
for (int i = 0; i < len; i++) { |
|
287 |
h = 31*h + vart.charAt(i); |
|
6501 | 288 |
} |
48763 | 289 |
return h; |
290 |
} |
|
291 |
||
292 |
private BaseLocale getBaseLocale() { |
|
293 |
return (holder == null) ? holderRef.get() : holder; |
|
6501 | 294 |
} |
295 |
||
9224 | 296 |
@Override |
6501 | 297 |
public boolean equals(Object obj) { |
27067 | 298 |
if (this == obj) { |
299 |
return true; |
|
300 |
} |
|
301 |
if (obj instanceof Key && this.hash == ((Key)obj).hash) { |
|
48763 | 302 |
BaseLocale other = ((Key) obj).getBaseLocale(); |
303 |
BaseLocale locale = this.getBaseLocale(); |
|
304 |
if (other != null && locale != null |
|
305 |
&& LocaleUtils.caseIgnoreMatch(other.getLanguage(), locale.getLanguage()) |
|
306 |
&& LocaleUtils.caseIgnoreMatch(other.getScript(), locale.getScript()) |
|
307 |
&& LocaleUtils.caseIgnoreMatch(other.getRegion(), locale.getRegion()) |
|
308 |
// variant is case sensitive in JDK! |
|
309 |
&& other.getVariant().equals(locale.getVariant())) { |
|
310 |
return true; |
|
6501 | 311 |
} |
312 |
} |
|
27067 | 313 |
return false; |
6501 | 314 |
} |
315 |
||
316 |
public static Key normalize(Key key) { |
|
9224 | 317 |
if (key.normalized) { |
318 |
return key; |
|
319 |
} |
|
6501 | 320 |
|
48763 | 321 |
// Only normalized keys may be softly referencing the data holder |
322 |
assert (key.holder != null && key.holderRef == null); |
|
323 |
BaseLocale locale = key.holder; |
|
324 |
return new Key(locale.getLanguage(), locale.getScript(), |
|
325 |
locale.getRegion(), locale.getVariant(), true); |
|
6501 | 326 |
} |
327 |
} |
|
328 |
||
329 |
private static class Cache extends LocaleObjectCache<Key, BaseLocale> { |
|
330 |
||
54412 | 331 |
private static final Cache CACHE = new Cache(); |
332 |
||
6501 | 333 |
public Cache() { |
334 |
} |
|
335 |
||
9224 | 336 |
@Override |
6501 | 337 |
protected Key normalizeKey(Key key) { |
338 |
return Key.normalize(key); |
|
339 |
} |
|
340 |
||
9224 | 341 |
@Override |
6501 | 342 |
protected BaseLocale createObject(Key key) { |
48763 | 343 |
return Key.normalize(key).getBaseLocale(); |
6501 | 344 |
} |
345 |
} |
|
346 |
} |