Path: blob/master/src/java.base/share/classes/sun/util/locale/provider/TimeZoneNameProviderImpl.java
41161 views
/*1* Copyright (c) 2012, 2018, 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.util.Locale;28import java.util.Objects;29import java.util.Set;30import java.util.TimeZone;31import java.util.spi.TimeZoneNameProvider;3233/**34* Concrete implementation of the35* {@link java.util.spi.TimeZoneNameProvider TimeZoneNameProvider} class36* for the JRE LocaleProviderAdapter.37*38* @author Naoto Sato39* @author Masayoshi Okutsu40*/41public class TimeZoneNameProviderImpl extends TimeZoneNameProvider {42private final LocaleProviderAdapter.Type type;43private final Set<String> langtags;4445protected TimeZoneNameProviderImpl(LocaleProviderAdapter.Type type, Set<String> langtags) {46this.type = type;47this.langtags = langtags;48}4950/**51* Returns an array of all locales for which this locale service provider52* can provide localized objects or names.53*54* @return An array of all locales for which this locale service provider55* can provide localized objects or names.56*/57@Override58public Locale[] getAvailableLocales() {59return LocaleProviderAdapter.toLocaleArray(langtags);60}6162@Override63public boolean isSupportedLocale(Locale locale) {64return LocaleProviderAdapter.forType(type).isSupportedProviderLocale(locale, langtags);65}6667/**68* Returns a name for the given time zone ID that's suitable for69* presentation to the user in the specified locale. The given time70* zone ID is "GMT" or one of the names defined using "Zone" entries71* in the "tz database", a public domain time zone database at72* <a href="ftp://elsie.nci.nih.gov/pub/">ftp://elsie.nci.nih.gov/pub/</a>.73* The data of this database is contained in a file whose name starts with74* "tzdata", and the specification of the data format is part of the zic.875* man page, which is contained in a file whose name starts with "tzcode".76* <p>77* If <code>daylight</code> is true, the method should return a name78* appropriate for daylight saving time even if the specified time zone79* has not observed daylight saving time in the past.80*81* @param id a time zone ID string82* @param daylight if true, return the daylight saving name.83* @param style either {@link java.util.TimeZone#LONG TimeZone.LONG} or84* {@link java.util.TimeZone#SHORT TimeZone.SHORT}85* @param locale the desired locale86* @return the human-readable name of the given time zone in the87* given locale, or null if it's not available.88* @exception IllegalArgumentException if <code>style</code> is invalid,89* or <code>locale</code> isn't one of the locales returned from90* {@link java.util.spi.LocaleServiceProvider#getAvailableLocales()91* getAvailableLocales()}.92* @exception NullPointerException if <code>ID</code> or <code>locale</code>93* is null94* @see java.util.TimeZone#getDisplayName(boolean, int, java.util.Locale)95*/96@Override97public String getDisplayName(String id, boolean daylight, int style, Locale locale) {98String[] names = getDisplayNameArray(id, locale);99if (Objects.nonNull(names)) {100assert names.length >= 7;101int index = daylight ? 3 : 1;102if (style == TimeZone.SHORT) {103index++;104}105return names[index];106}107return null;108}109110@Override111public String getGenericDisplayName(String id, int style, Locale locale) {112String[] names = getDisplayNameArray(id, locale);113if (Objects.nonNull(names)) {114assert names.length >= 7;115return names[(style == TimeZone.LONG) ? 5 : 6];116}117return null;118}119120protected String[] getDisplayNameArray(String id, Locale locale) {121Objects.requireNonNull(id);122Objects.requireNonNull(locale);123124return (String []) LocaleProviderAdapter.forType(type)125.getLocaleResources(locale)126.getTimeZoneNames(id);127}128129/**130* Returns a String[][] as the DateFormatSymbols.getZoneStrings() value for131* the given locale.132*133* @param locale a Locale for time zone names134* @return an array of time zone names arrays135*/136protected String[][] getZoneStrings(Locale locale) {137return LocaleProviderAdapter.forType(type).getLocaleResources(locale).getZoneStrings();138}139}140141142