1 /*
2 * Copyright (c) 2005, 2015, 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 final 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 */
135 }
136
137 /**
138 * Retrieves a standard or daylight-saving time name for the given time zone ID.
139 *
140 * @param id time zone ID
141 * @param daylight true for a daylight saving time name, or false for a standard time name
142 * @param style TimeZone.LONG or TimeZone.SHORT
143 * @param locale desired Locale
144 * @return the requested time zone name, or null if not found.
145 */
146 public static String retrieveDisplayName(String id, boolean daylight, int style, Locale locale) {
147 String[] names = retrieveDisplayNamesImpl(id, locale);
148 if (Objects.nonNull(names)) {
149 return names[(daylight ? 4 : 2) - style];
150 } else {
151 return null;
152 }
153 }
154
155 private static String[] retrieveDisplayNamesImpl(String id, Locale locale) {
156 LocaleServiceProviderPool pool =
157 LocaleServiceProviderPool.getPool(TimeZoneNameProvider.class);
158 String[] names;
159 Map<Locale, String[]> perLocale = null;
160
161 SoftReference<Map<Locale, String[]>> ref = cachedDisplayNames.get(id);
162 if (Objects.nonNull(ref)) {
163 perLocale = ref.get();
164 if (Objects.nonNull(perLocale)) {
165 names = perLocale.get(locale);
166 if (Objects.nonNull(names)) {
167 return names;
168 }
169 }
170 }
171
172 // build names array
173 names = new String[7];
174 names[0] = id;
|
1 /*
2 * Copyright (c) 2005, 2017, 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.Optional;
35 import java.util.Set;
36 import java.util.concurrent.ConcurrentHashMap;
37 import java.util.spi.TimeZoneNameProvider;
38 import sun.util.calendar.ZoneInfo;
39 import sun.util.cldr.CLDRLocaleProviderAdapter;
40 import static sun.util.locale.provider.LocaleProviderAdapter.Type;
41
42 /**
43 * Utility class that deals with the localized time zone names
44 *
45 * @author Naoto Sato
46 * @author Masayoshi Okutsu
47 */
48 public final class TimeZoneNameUtility {
49
50 /**
51 * cache to hold time zone localized strings. Keyed by Locale
52 */
53 private static final ConcurrentHashMap<Locale, SoftReference<String[][]>> cachedZoneData =
54 new ConcurrentHashMap<>();
55
56 /**
57 * Cache for managing display names per timezone per locale
58 * The structure is:
59 * Map(key=id, value=SoftReference(Map(key=locale, value=displaynames)))
60 */
138 }
139
140 /**
141 * Retrieves a standard or daylight-saving time name for the given time zone ID.
142 *
143 * @param id time zone ID
144 * @param daylight true for a daylight saving time name, or false for a standard time name
145 * @param style TimeZone.LONG or TimeZone.SHORT
146 * @param locale desired Locale
147 * @return the requested time zone name, or null if not found.
148 */
149 public static String retrieveDisplayName(String id, boolean daylight, int style, Locale locale) {
150 String[] names = retrieveDisplayNamesImpl(id, locale);
151 if (Objects.nonNull(names)) {
152 return names[(daylight ? 4 : 2) - style];
153 } else {
154 return null;
155 }
156 }
157
158 /**
159 * Converts the time zone id from LDML's 5-letter id to tzdb's id
160 *
161 * @param shortID time zone short ID defined in LDML
162 * @return the tzdb's time zone ID
163 */
164 public static Optional<String> convertLDMLShortID(String shortID) {
165 return ((CLDRLocaleProviderAdapter)LocaleProviderAdapter.forType(Type.CLDR))
166 .getTimeZoneID(shortID)
167 .map(id -> id.replaceAll("\\s.*", ""));
168 }
169
170 private static String[] retrieveDisplayNamesImpl(String id, Locale locale) {
171 LocaleServiceProviderPool pool =
172 LocaleServiceProviderPool.getPool(TimeZoneNameProvider.class);
173 String[] names;
174 Map<Locale, String[]> perLocale = null;
175
176 SoftReference<Map<Locale, String[]>> ref = cachedDisplayNames.get(id);
177 if (Objects.nonNull(ref)) {
178 perLocale = ref.get();
179 if (Objects.nonNull(perLocale)) {
180 names = perLocale.get(locale);
181 if (Objects.nonNull(names)) {
182 return names;
183 }
184 }
185 }
186
187 // build names array
188 names = new String[7];
189 names[0] = id;
|