Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/util/locale/provider/TimeZoneNameUtility.java
38918 views
/*1* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.util.locale.provider;2627import java.lang.ref.SoftReference;28import java.util.LinkedList;29import java.util.List;30import java.util.Locale;31import java.util.Map;32import java.util.Objects;33import java.util.Set;34import java.util.concurrent.ConcurrentHashMap;35import java.util.spi.TimeZoneNameProvider;36import sun.util.calendar.ZoneInfo;3738/**39* Utility class that deals with the localized time zone names40*41* @author Naoto Sato42* @author Masayoshi Okutsu43*/44public final class TimeZoneNameUtility {4546/**47* cache to hold time zone localized strings. Keyed by Locale48*/49private static ConcurrentHashMap<Locale, SoftReference<String[][]>> cachedZoneData =50new ConcurrentHashMap<>();5152/**53* Cache for managing display names per timezone per locale54* The structure is:55* Map(key=id, value=SoftReference(Map(key=locale, value=displaynames)))56*/57private static final Map<String, SoftReference<Map<Locale, String[]>>> cachedDisplayNames =58new ConcurrentHashMap<>();5960/**61* get time zone localized strings. Enumerate all keys.62*/63public static String[][] getZoneStrings(Locale locale) {64String[][] zones;65SoftReference<String[][]> data = cachedZoneData.get(locale);6667if (data == null || ((zones = data.get()) == null)) {68zones = loadZoneStrings(locale);69data = new SoftReference<>(zones);70cachedZoneData.put(locale, data);71}7273return zones;74}7576private static String[][] loadZoneStrings(Locale locale) {77// If the provider is a TimeZoneNameProviderImpl, call its getZoneStrings78// in order to avoid per-ID retrieval.79LocaleProviderAdapter adapter = LocaleProviderAdapter.getAdapter(TimeZoneNameProvider.class, locale);80TimeZoneNameProvider provider = adapter.getTimeZoneNameProvider();81if (provider instanceof TimeZoneNameProviderImpl) {82return ((TimeZoneNameProviderImpl)provider).getZoneStrings(locale);83}8485// Performs per-ID retrieval.86Set<String> zoneIDs = LocaleProviderAdapter.forJRE().getLocaleResources(locale).getZoneIDs();87List<String[]> zones = new LinkedList<>();88for (String key : zoneIDs) {89String[] names = retrieveDisplayNamesImpl(key, locale);90if (names != null) {91zones.add(names);92}93}9495String[][] zonesArray = new String[zones.size()][];96return zones.toArray(zonesArray);97}9899/**100* Retrieve display names for a time zone ID.101*/102public static String[] retrieveDisplayNames(String id, Locale locale) {103Objects.requireNonNull(id);104Objects.requireNonNull(locale);105106return retrieveDisplayNamesImpl(id, locale);107}108109/**110* Retrieves a generic time zone display name for a time zone ID.111*112* @param id time zone ID113* @param style TimeZone.LONG or TimeZone.SHORT114* @param locale desired Locale115* @return the requested generic time zone display name, or null if not found.116*/117public static String retrieveGenericDisplayName(String id, int style, Locale locale) {118String[] names = retrieveDisplayNamesImpl(id, locale);119if (Objects.nonNull(names)) {120return names[6 - style];121} else {122return null;123}124}125126/**127* Retrieves a standard or daylight-saving time name for the given time zone ID.128*129* @param id time zone ID130* @param daylight true for a daylight saving time name, or false for a standard time name131* @param style TimeZone.LONG or TimeZone.SHORT132* @param locale desired Locale133* @return the requested time zone name, or null if not found.134*/135public static String retrieveDisplayName(String id, boolean daylight, int style, Locale locale) {136String[] names = retrieveDisplayNamesImpl(id, locale);137if (Objects.nonNull(names)) {138return names[(daylight ? 4 : 2) - style];139} else {140return null;141}142}143144private static String[] retrieveDisplayNamesImpl(String id, Locale locale) {145LocaleServiceProviderPool pool =146LocaleServiceProviderPool.getPool(TimeZoneNameProvider.class);147String[] names;148Map<Locale, String[]> perLocale = null;149150SoftReference<Map<Locale, String[]>> ref = cachedDisplayNames.get(id);151if (Objects.nonNull(ref)) {152perLocale = ref.get();153if (Objects.nonNull(perLocale)) {154names = perLocale.get(locale);155if (Objects.nonNull(names)) {156return names;157}158}159}160161// build names array162names = new String[7];163names[0] = id;164for (int i = 1; i <= 6; i ++) {165names[i] = pool.getLocalizedObject(TimeZoneNameGetter.INSTANCE, locale,166i<5 ? (i<3 ? "std" : "dst") : "generic", i%2, id);167}168169if (Objects.isNull(perLocale)) {170perLocale = new ConcurrentHashMap<>();171}172perLocale.put(locale, names);173ref = new SoftReference<>(perLocale);174cachedDisplayNames.put(id, ref);175return names;176}177178179/**180* Obtains a localized time zone strings from a TimeZoneNameProvider181* implementation.182*/183private static class TimeZoneNameGetter184implements LocaleServiceProviderPool.LocalizedObjectGetter<TimeZoneNameProvider,185String> {186private static final TimeZoneNameGetter INSTANCE =187new TimeZoneNameGetter();188189@Override190public String getObject(TimeZoneNameProvider timeZoneNameProvider,191Locale locale,192String requestID,193Object... params) {194assert params.length == 2;195int style = (int) params[0];196String tzid = (String) params[1];197String value = getName(timeZoneNameProvider, locale, requestID, style, tzid);198if (value == null) {199Map<String, String> aliases = ZoneInfo.getAliasTable();200if (aliases != null) {201String canonicalID = aliases.get(tzid);202if (canonicalID != null) {203value = getName(timeZoneNameProvider, locale, requestID, style, canonicalID);204}205if (value == null) {206value = examineAliases(timeZoneNameProvider, locale, requestID,207canonicalID != null ? canonicalID : tzid, style, aliases);208}209}210}211212return value;213}214215private static String examineAliases(TimeZoneNameProvider tznp, Locale locale,216String requestID, String tzid, int style,217Map<String, String> aliases) {218for (Map.Entry<String, String> entry : aliases.entrySet()) {219if (entry.getValue().equals(tzid)) {220String alias = entry.getKey();221String name = getName(tznp, locale, requestID, style, alias);222if (name != null) {223return name;224}225name = examineAliases(tznp, locale, requestID, alias, style, aliases);226if (name != null) {227return name;228}229}230}231return null;232}233234private static String getName(TimeZoneNameProvider timeZoneNameProvider,235Locale locale, String requestID, int style, String tzid) {236String value = null;237switch (requestID) {238case "std":239value = timeZoneNameProvider.getDisplayName(tzid, false, style, locale);240break;241case "dst":242value = timeZoneNameProvider.getDisplayName(tzid, true, style, locale);243break;244case "generic":245value = timeZoneNameProvider.getGenericDisplayName(tzid, style, locale);246break;247}248return value;249}250}251252// No instantiation253private TimeZoneNameUtility() {254}255}256257258