< prev index next >

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

Print this page
rev 47480 : [mq]: 8176841


  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.cldr;
  27 
  28 import java.security.AccessController;
  29 import java.security.AccessControlException;
  30 import java.security.PrivilegedActionException;
  31 import java.security.PrivilegedExceptionAction;
  32 import java.text.spi.BreakIteratorProvider;
  33 import java.text.spi.CollatorProvider;
  34 import java.util.Arrays;
  35 import java.util.Collections;
  36 import java.util.HashSet;
  37 import java.util.List;
  38 import java.util.Locale;
  39 import java.util.Map;
  40 import java.util.Objects;
  41 import java.util.ServiceLoader;
  42 import java.util.ServiceConfigurationError;
  43 import java.util.Set;
  44 import java.util.StringTokenizer;
  45 import java.util.concurrent.ConcurrentHashMap;
  46 import sun.util.locale.provider.JRELocaleProviderAdapter;
  47 import sun.util.locale.provider.LocaleProviderAdapter;
  48 import sun.util.locale.provider.LocaleDataMetaInfo;

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


 106     }
 107 
 108     @Override
 109     public CollatorProvider getCollatorProvider() {
 110         return null;
 111     }
 112 
 113     @Override
 114     public Locale[] getAvailableLocales() {
 115         Set<String> all = createLanguageTagSet("AvailableLocales");
 116         Locale[] locs = new Locale[all.size()];
 117         int index = 0;
 118         for (String tag : all) {
 119             locs[index++] = Locale.forLanguageTag(tag);
 120         }
 121         return locs;
 122     }
 123 
 124     @Override
 125     protected Set<String> createLanguageTagSet(String category) {




 126         // Directly call Base tags, as we know it's in the base module.
 127         String supportedLocaleString = baseMetaInfo.availableLanguageTags(category);
 128         String nonBaseTags = null;
 129 
 130         if (nonBaseMetaInfo != null) {
 131             nonBaseTags = nonBaseMetaInfo.availableLanguageTags(category);
 132         }
 133         if (nonBaseTags != null) {
 134             if (supportedLocaleString != null) {
 135                 supportedLocaleString += " " + nonBaseTags;
 136             } else {
 137                 supportedLocaleString = nonBaseTags;
 138             }
 139         }
 140         if (supportedLocaleString == null) {
 141             return Collections.emptySet();
 142         }
 143         Set<String> tagset = new HashSet<>();
 144         StringTokenizer tokens = new StringTokenizer(supportedLocaleString);
 145         while (tokens.hasMoreTokens()) {


 203      * no, no-NO locales so that COMPAT locales do not precede
 204      * those locales during ResourceBundle search path.
 205      */
 206     private static Locale getEquivalentLoc(Locale locale) {
 207         switch (locale.toString()) {
 208             case "zh_HK":
 209                 return Locale.forLanguageTag("zh-Hant-HK");
 210             case "no":
 211             case "no_NO":
 212                 return Locale.forLanguageTag("nb");
 213         }
 214         return locale;
 215     }
 216 
 217     @Override
 218     public boolean isSupportedProviderLocale(Locale locale, Set<String> langtags) {
 219         return Locale.ROOT.equals(locale)
 220                 || langtags.contains(locale.stripExtensions().toLanguageTag())
 221                 || langtags.contains(getEquivalentLoc(locale).toLanguageTag());
 222     }







 223 }


  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.cldr;
  27 
  28 import java.security.AccessController;
  29 import java.security.AccessControlException;
  30 import java.security.PrivilegedActionException;
  31 import java.security.PrivilegedExceptionAction;
  32 import java.text.spi.BreakIteratorProvider;
  33 import java.text.spi.CollatorProvider;
  34 import java.util.Arrays;
  35 import java.util.Collections;
  36 import java.util.HashSet;
  37 import java.util.List;
  38 import java.util.Locale;
  39 import java.util.Map;
  40 import java.util.Optional;
  41 import java.util.ServiceLoader;
  42 import java.util.ServiceConfigurationError;
  43 import java.util.Set;
  44 import java.util.StringTokenizer;
  45 import java.util.concurrent.ConcurrentHashMap;
  46 import sun.util.locale.provider.JRELocaleProviderAdapter;

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


 106     }
 107 
 108     @Override
 109     public CollatorProvider getCollatorProvider() {
 110         return null;
 111     }
 112 
 113     @Override
 114     public Locale[] getAvailableLocales() {
 115         Set<String> all = createLanguageTagSet("AvailableLocales");
 116         Locale[] locs = new Locale[all.size()];
 117         int index = 0;
 118         for (String tag : all) {
 119             locs[index++] = Locale.forLanguageTag(tag);
 120         }
 121         return locs;
 122     }
 123 
 124     @Override
 125     protected Set<String> createLanguageTagSet(String category) {
 126         // Assume all categories support the same set as AvailableLocales
 127         // in CLDR adapter.
 128         category = "AvailableLocales";
 129 
 130         // Directly call Base tags, as we know it's in the base module.
 131         String supportedLocaleString = baseMetaInfo.availableLanguageTags(category);
 132         String nonBaseTags = null;
 133 
 134         if (nonBaseMetaInfo != null) {
 135             nonBaseTags = nonBaseMetaInfo.availableLanguageTags(category);
 136         }
 137         if (nonBaseTags != null) {
 138             if (supportedLocaleString != null) {
 139                 supportedLocaleString += " " + nonBaseTags;
 140             } else {
 141                 supportedLocaleString = nonBaseTags;
 142             }
 143         }
 144         if (supportedLocaleString == null) {
 145             return Collections.emptySet();
 146         }
 147         Set<String> tagset = new HashSet<>();
 148         StringTokenizer tokens = new StringTokenizer(supportedLocaleString);
 149         while (tokens.hasMoreTokens()) {


 207      * no, no-NO locales so that COMPAT locales do not precede
 208      * those locales during ResourceBundle search path.
 209      */
 210     private static Locale getEquivalentLoc(Locale locale) {
 211         switch (locale.toString()) {
 212             case "zh_HK":
 213                 return Locale.forLanguageTag("zh-Hant-HK");
 214             case "no":
 215             case "no_NO":
 216                 return Locale.forLanguageTag("nb");
 217         }
 218         return locale;
 219     }
 220 
 221     @Override
 222     public boolean isSupportedProviderLocale(Locale locale, Set<String> langtags) {
 223         return Locale.ROOT.equals(locale)
 224                 || langtags.contains(locale.stripExtensions().toLanguageTag())
 225                 || langtags.contains(getEquivalentLoc(locale).toLanguageTag());
 226     }
 227 
 228     /**
 229      * Returns the time zone ID from an LDML's short ID
 230      */
 231     public Optional<String> getTimeZoneID(String shortID) {
 232         return Optional.ofNullable(baseMetaInfo.tzShortIDs().get(shortID));
 233     }
 234 }
< prev index next >