< prev index next >

src/java.base/share/classes/sun/util/cldr/CLDRLocaleProviderAdapter.java

Print this page
rev 49854 : 8181157: CLDR Timezone name fallback implementation
Reviewed-by: sherman
   1 /*
   2  * Copyright (c) 2012, 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


  28 import java.security.AccessController;
  29 import java.security.AccessControlException;
  30 import java.security.PrivilegedAction;
  31 import java.security.PrivilegedActionException;
  32 import java.security.PrivilegedExceptionAction;
  33 import java.text.spi.BreakIteratorProvider;
  34 import java.text.spi.CollatorProvider;
  35 import java.util.Arrays;
  36 import java.util.Collections;
  37 import java.util.HashSet;
  38 import java.util.List;
  39 import java.util.Locale;
  40 import java.util.Map;
  41 import java.util.Optional;
  42 import java.util.ServiceLoader;
  43 import java.util.ServiceConfigurationError;
  44 import java.util.Set;
  45 import java.util.StringTokenizer;
  46 import java.util.concurrent.ConcurrentHashMap;
  47 import java.util.spi.CalendarDataProvider;

  48 import sun.util.locale.provider.JRELocaleProviderAdapter;
  49 import sun.util.locale.provider.LocaleDataMetaInfo;
  50 import sun.util.locale.provider.LocaleProviderAdapter;
  51 
  52 /**
  53  * LocaleProviderAdapter implementation for the CLDR locale data.
  54  *
  55  * @author Masayoshi Okutsu
  56  * @author Naoto Sato
  57  */
  58 public class CLDRLocaleProviderAdapter extends JRELocaleProviderAdapter {
  59 
  60     private static final CLDRBaseLocaleDataMetaInfo baseMetaInfo = new CLDRBaseLocaleDataMetaInfo();
  61     // Assumption: CLDR has only one non-Base module.
  62     private final LocaleDataMetaInfo nonBaseMetaInfo;
  63 
  64     // parent locales map
  65     private static volatile Map<Locale, Locale> parentLocalesMap;
  66     static {
  67         parentLocalesMap = new ConcurrentHashMap<>();


 114                 (PrivilegedAction<CalendarDataProvider>) () ->
 115                     new CLDRCalendarDataProviderImpl(
 116                         getAdapterType(),
 117                         getLanguageTagSet("CalendarData")));
 118 
 119             synchronized (this) {
 120                 if (calendarDataProvider == null) {
 121                     calendarDataProvider = provider;
 122                 }
 123             }
 124         }
 125         return calendarDataProvider;
 126     }
 127 
 128     @Override
 129     public CollatorProvider getCollatorProvider() {
 130         return null;
 131     }
 132 
 133     @Override


















 134     public Locale[] getAvailableLocales() {
 135         Set<String> all = createLanguageTagSet("AvailableLocales");
 136         Locale[] locs = new Locale[all.size()];
 137         int index = 0;
 138         for (String tag : all) {
 139             locs[index++] = Locale.forLanguageTag(tag);
 140         }
 141         return locs;
 142     }
 143 
 144     @Override
 145     protected Set<String> createLanguageTagSet(String category) {
 146         // Assume all categories support the same set as AvailableLocales
 147         // in CLDR adapter.
 148         category = "AvailableLocales";
 149 
 150         // Directly call Base tags, as we know it's in the base module.
 151         String supportedLocaleString = baseMetaInfo.availableLanguageTags(category);
 152         String nonBaseTags = null;
 153 


 229      */
 230     private static Locale getEquivalentLoc(Locale locale) {
 231         switch (locale.toString()) {
 232             case "zh_HK":
 233                 return Locale.forLanguageTag("zh-Hant-HK");
 234             case "no":
 235             case "no_NO":
 236                 return Locale.forLanguageTag("nb");
 237         }
 238         return locale;
 239     }
 240 
 241     @Override
 242     public boolean isSupportedProviderLocale(Locale locale, Set<String> langtags) {
 243         return Locale.ROOT.equals(locale)
 244                 || langtags.contains(locale.stripExtensions().toLanguageTag())
 245                 || langtags.contains(getEquivalentLoc(locale).toLanguageTag());
 246     }
 247 
 248     /**
 249      * Returns the time zone ID from an LDML's short ID
 250      */
 251     public Optional<String> getTimeZoneID(String shortID) {
 252         return Optional.ofNullable(baseMetaInfo.tzShortIDs().get(shortID));
 253     }
 254 }
   1 /*
   2  * Copyright (c) 2012, 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


  28 import java.security.AccessController;
  29 import java.security.AccessControlException;
  30 import java.security.PrivilegedAction;
  31 import java.security.PrivilegedActionException;
  32 import java.security.PrivilegedExceptionAction;
  33 import java.text.spi.BreakIteratorProvider;
  34 import java.text.spi.CollatorProvider;
  35 import java.util.Arrays;
  36 import java.util.Collections;
  37 import java.util.HashSet;
  38 import java.util.List;
  39 import java.util.Locale;
  40 import java.util.Map;
  41 import java.util.Optional;
  42 import java.util.ServiceLoader;
  43 import java.util.ServiceConfigurationError;
  44 import java.util.Set;
  45 import java.util.StringTokenizer;
  46 import java.util.concurrent.ConcurrentHashMap;
  47 import java.util.spi.CalendarDataProvider;
  48 import java.util.spi.TimeZoneNameProvider;
  49 import sun.util.locale.provider.JRELocaleProviderAdapter;
  50 import sun.util.locale.provider.LocaleDataMetaInfo;
  51 import sun.util.locale.provider.LocaleProviderAdapter;
  52 
  53 /**
  54  * LocaleProviderAdapter implementation for the CLDR locale data.
  55  *
  56  * @author Masayoshi Okutsu
  57  * @author Naoto Sato
  58  */
  59 public class CLDRLocaleProviderAdapter extends JRELocaleProviderAdapter {
  60 
  61     private static final CLDRBaseLocaleDataMetaInfo baseMetaInfo = new CLDRBaseLocaleDataMetaInfo();
  62     // Assumption: CLDR has only one non-Base module.
  63     private final LocaleDataMetaInfo nonBaseMetaInfo;
  64 
  65     // parent locales map
  66     private static volatile Map<Locale, Locale> parentLocalesMap;
  67     static {
  68         parentLocalesMap = new ConcurrentHashMap<>();


 115                 (PrivilegedAction<CalendarDataProvider>) () ->
 116                     new CLDRCalendarDataProviderImpl(
 117                         getAdapterType(),
 118                         getLanguageTagSet("CalendarData")));
 119 
 120             synchronized (this) {
 121                 if (calendarDataProvider == null) {
 122                     calendarDataProvider = provider;
 123                 }
 124             }
 125         }
 126         return calendarDataProvider;
 127     }
 128 
 129     @Override
 130     public CollatorProvider getCollatorProvider() {
 131         return null;
 132     }
 133 
 134     @Override
 135     public TimeZoneNameProvider getTimeZoneNameProvider() {
 136         if (timeZoneNameProvider == null) {
 137             TimeZoneNameProvider provider = AccessController.doPrivileged(
 138                 (PrivilegedAction<TimeZoneNameProvider>) () ->
 139                     new CLDRTimeZoneNameProviderImpl(
 140                         getAdapterType(),
 141                         getLanguageTagSet("TimeZoneNames")));
 142 
 143             synchronized (this) {
 144                 if (timeZoneNameProvider == null) {
 145                     timeZoneNameProvider = provider;
 146                 }
 147             }
 148         }
 149         return timeZoneNameProvider;
 150     }
 151 
 152     @Override
 153     public Locale[] getAvailableLocales() {
 154         Set<String> all = createLanguageTagSet("AvailableLocales");
 155         Locale[] locs = new Locale[all.size()];
 156         int index = 0;
 157         for (String tag : all) {
 158             locs[index++] = Locale.forLanguageTag(tag);
 159         }
 160         return locs;
 161     }
 162 
 163     @Override
 164     protected Set<String> createLanguageTagSet(String category) {
 165         // Assume all categories support the same set as AvailableLocales
 166         // in CLDR adapter.
 167         category = "AvailableLocales";
 168 
 169         // Directly call Base tags, as we know it's in the base module.
 170         String supportedLocaleString = baseMetaInfo.availableLanguageTags(category);
 171         String nonBaseTags = null;
 172 


 248      */
 249     private static Locale getEquivalentLoc(Locale locale) {
 250         switch (locale.toString()) {
 251             case "zh_HK":
 252                 return Locale.forLanguageTag("zh-Hant-HK");
 253             case "no":
 254             case "no_NO":
 255                 return Locale.forLanguageTag("nb");
 256         }
 257         return locale;
 258     }
 259 
 260     @Override
 261     public boolean isSupportedProviderLocale(Locale locale, Set<String> langtags) {
 262         return Locale.ROOT.equals(locale)
 263                 || langtags.contains(locale.stripExtensions().toLanguageTag())
 264                 || langtags.contains(getEquivalentLoc(locale).toLanguageTag());
 265     }
 266 
 267     /**
 268      * Returns the canonical ID for the given ID
 269      */
 270     public Optional<String> canonicalTZID(String id) {
 271         return Optional.ofNullable(baseMetaInfo.tzCanonicalIDs().get(id));
 272     }
 273 }
< prev index next >