Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/util/locale/provider/TimeZoneNameUtility.java
38918 views
1
/*
2
* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
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
package sun.util.locale.provider;
27
28
import java.lang.ref.SoftReference;
29
import java.util.LinkedList;
30
import java.util.List;
31
import java.util.Locale;
32
import java.util.Map;
33
import java.util.Objects;
34
import java.util.Set;
35
import java.util.concurrent.ConcurrentHashMap;
36
import java.util.spi.TimeZoneNameProvider;
37
import sun.util.calendar.ZoneInfo;
38
39
/**
40
* Utility class that deals with the localized time zone names
41
*
42
* @author Naoto Sato
43
* @author Masayoshi Okutsu
44
*/
45
public final class TimeZoneNameUtility {
46
47
/**
48
* cache to hold time zone localized strings. Keyed by Locale
49
*/
50
private static ConcurrentHashMap<Locale, SoftReference<String[][]>> cachedZoneData =
51
new ConcurrentHashMap<>();
52
53
/**
54
* Cache for managing display names per timezone per locale
55
* The structure is:
56
* Map(key=id, value=SoftReference(Map(key=locale, value=displaynames)))
57
*/
58
private static final Map<String, SoftReference<Map<Locale, String[]>>> cachedDisplayNames =
59
new ConcurrentHashMap<>();
60
61
/**
62
* get time zone localized strings. Enumerate all keys.
63
*/
64
public static String[][] getZoneStrings(Locale locale) {
65
String[][] zones;
66
SoftReference<String[][]> data = cachedZoneData.get(locale);
67
68
if (data == null || ((zones = data.get()) == null)) {
69
zones = loadZoneStrings(locale);
70
data = new SoftReference<>(zones);
71
cachedZoneData.put(locale, data);
72
}
73
74
return zones;
75
}
76
77
private static String[][] loadZoneStrings(Locale locale) {
78
// If the provider is a TimeZoneNameProviderImpl, call its getZoneStrings
79
// in order to avoid per-ID retrieval.
80
LocaleProviderAdapter adapter = LocaleProviderAdapter.getAdapter(TimeZoneNameProvider.class, locale);
81
TimeZoneNameProvider provider = adapter.getTimeZoneNameProvider();
82
if (provider instanceof TimeZoneNameProviderImpl) {
83
return ((TimeZoneNameProviderImpl)provider).getZoneStrings(locale);
84
}
85
86
// Performs per-ID retrieval.
87
Set<String> zoneIDs = LocaleProviderAdapter.forJRE().getLocaleResources(locale).getZoneIDs();
88
List<String[]> zones = new LinkedList<>();
89
for (String key : zoneIDs) {
90
String[] names = retrieveDisplayNamesImpl(key, locale);
91
if (names != null) {
92
zones.add(names);
93
}
94
}
95
96
String[][] zonesArray = new String[zones.size()][];
97
return zones.toArray(zonesArray);
98
}
99
100
/**
101
* Retrieve display names for a time zone ID.
102
*/
103
public static String[] retrieveDisplayNames(String id, Locale locale) {
104
Objects.requireNonNull(id);
105
Objects.requireNonNull(locale);
106
107
return retrieveDisplayNamesImpl(id, locale);
108
}
109
110
/**
111
* Retrieves a generic time zone display name for a time zone ID.
112
*
113
* @param id time zone ID
114
* @param style TimeZone.LONG or TimeZone.SHORT
115
* @param locale desired Locale
116
* @return the requested generic time zone display name, or null if not found.
117
*/
118
public static String retrieveGenericDisplayName(String id, int style, Locale locale) {
119
String[] names = retrieveDisplayNamesImpl(id, locale);
120
if (Objects.nonNull(names)) {
121
return names[6 - style];
122
} else {
123
return null;
124
}
125
}
126
127
/**
128
* Retrieves a standard or daylight-saving time name for the given time zone ID.
129
*
130
* @param id time zone ID
131
* @param daylight true for a daylight saving time name, or false for a standard time name
132
* @param style TimeZone.LONG or TimeZone.SHORT
133
* @param locale desired Locale
134
* @return the requested time zone name, or null if not found.
135
*/
136
public static String retrieveDisplayName(String id, boolean daylight, int style, Locale locale) {
137
String[] names = retrieveDisplayNamesImpl(id, locale);
138
if (Objects.nonNull(names)) {
139
return names[(daylight ? 4 : 2) - style];
140
} else {
141
return null;
142
}
143
}
144
145
private static String[] retrieveDisplayNamesImpl(String id, Locale locale) {
146
LocaleServiceProviderPool pool =
147
LocaleServiceProviderPool.getPool(TimeZoneNameProvider.class);
148
String[] names;
149
Map<Locale, String[]> perLocale = null;
150
151
SoftReference<Map<Locale, String[]>> ref = cachedDisplayNames.get(id);
152
if (Objects.nonNull(ref)) {
153
perLocale = ref.get();
154
if (Objects.nonNull(perLocale)) {
155
names = perLocale.get(locale);
156
if (Objects.nonNull(names)) {
157
return names;
158
}
159
}
160
}
161
162
// build names array
163
names = new String[7];
164
names[0] = id;
165
for (int i = 1; i <= 6; i ++) {
166
names[i] = pool.getLocalizedObject(TimeZoneNameGetter.INSTANCE, locale,
167
i<5 ? (i<3 ? "std" : "dst") : "generic", i%2, id);
168
}
169
170
if (Objects.isNull(perLocale)) {
171
perLocale = new ConcurrentHashMap<>();
172
}
173
perLocale.put(locale, names);
174
ref = new SoftReference<>(perLocale);
175
cachedDisplayNames.put(id, ref);
176
return names;
177
}
178
179
180
/**
181
* Obtains a localized time zone strings from a TimeZoneNameProvider
182
* implementation.
183
*/
184
private static class TimeZoneNameGetter
185
implements LocaleServiceProviderPool.LocalizedObjectGetter<TimeZoneNameProvider,
186
String> {
187
private static final TimeZoneNameGetter INSTANCE =
188
new TimeZoneNameGetter();
189
190
@Override
191
public String getObject(TimeZoneNameProvider timeZoneNameProvider,
192
Locale locale,
193
String requestID,
194
Object... params) {
195
assert params.length == 2;
196
int style = (int) params[0];
197
String tzid = (String) params[1];
198
String value = getName(timeZoneNameProvider, locale, requestID, style, tzid);
199
if (value == null) {
200
Map<String, String> aliases = ZoneInfo.getAliasTable();
201
if (aliases != null) {
202
String canonicalID = aliases.get(tzid);
203
if (canonicalID != null) {
204
value = getName(timeZoneNameProvider, locale, requestID, style, canonicalID);
205
}
206
if (value == null) {
207
value = examineAliases(timeZoneNameProvider, locale, requestID,
208
canonicalID != null ? canonicalID : tzid, style, aliases);
209
}
210
}
211
}
212
213
return value;
214
}
215
216
private static String examineAliases(TimeZoneNameProvider tznp, Locale locale,
217
String requestID, String tzid, int style,
218
Map<String, String> aliases) {
219
for (Map.Entry<String, String> entry : aliases.entrySet()) {
220
if (entry.getValue().equals(tzid)) {
221
String alias = entry.getKey();
222
String name = getName(tznp, locale, requestID, style, alias);
223
if (name != null) {
224
return name;
225
}
226
name = examineAliases(tznp, locale, requestID, alias, style, aliases);
227
if (name != null) {
228
return name;
229
}
230
}
231
}
232
return null;
233
}
234
235
private static String getName(TimeZoneNameProvider timeZoneNameProvider,
236
Locale locale, String requestID, int style, String tzid) {
237
String value = null;
238
switch (requestID) {
239
case "std":
240
value = timeZoneNameProvider.getDisplayName(tzid, false, style, locale);
241
break;
242
case "dst":
243
value = timeZoneNameProvider.getDisplayName(tzid, true, style, locale);
244
break;
245
case "generic":
246
value = timeZoneNameProvider.getGenericDisplayName(tzid, style, locale);
247
break;
248
}
249
return value;
250
}
251
}
252
253
// No instantiation
254
private TimeZoneNameUtility() {
255
}
256
}
257
258