< prev index next >

src/java.base/share/classes/sun/util/locale/provider/TimeZoneNameUtility.java

Print this page
rev 49854 : 8181157: CLDR Timezone name fallback implementation
Reviewed-by: sherman
   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


 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


   1 /*
   2  * Copyright (c) 2005, 2018, 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


 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 canonicalTZID(shortID);
 166     }
 167 
 168     /**
 169      * Returns the canonical ID for the given ID
 170      */
 171     public static Optional<String> canonicalTZID(String id) {
 172         return ((CLDRLocaleProviderAdapter)LocaleProviderAdapter.forType(Type.CLDR))
 173                     .canonicalTZID(id);

 174     }
 175 
 176     private static String[] retrieveDisplayNamesImpl(String id, Locale locale) {
 177         LocaleServiceProviderPool pool =
 178             LocaleServiceProviderPool.getPool(TimeZoneNameProvider.class);
 179         String[] names;
 180         Map<Locale, String[]> perLocale = null;
 181 
 182         SoftReference<Map<Locale, String[]>> ref = cachedDisplayNames.get(id);
 183         if (Objects.nonNull(ref)) {
 184             perLocale = ref.get();
 185             if (Objects.nonNull(perLocale)) {
 186                 names = perLocale.get(locale);
 187                 if (Objects.nonNull(names)) {
 188                     return names;
 189                 }
 190             }
 191         }
 192 
 193         // build names array


< prev index next >