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

Print this page
rev 10560 : 8038436: Re-examine the mechanism to determine available localedata and cldrdata
Reviewed-by:


  39 import java.util.concurrent.ConcurrentMap;
  40 import java.util.spi.CalendarDataProvider;
  41 import java.util.spi.CalendarNameProvider;
  42 import java.util.spi.CurrencyNameProvider;
  43 import java.util.spi.LocaleNameProvider;
  44 import java.util.spi.LocaleServiceProvider;
  45 import java.util.spi.TimeZoneNameProvider;
  46 import sun.util.spi.CalendarProvider;
  47 
  48 /**
  49  * An abstract parent class for the
  50  * HostLocaleProviderAdapter/SPILocaleProviderAdapter.
  51  *
  52  * @author Naoto Sato
  53  * @author Masayoshi Okutsu
  54  */
  55 public abstract class AuxLocaleProviderAdapter extends LocaleProviderAdapter {
  56     /**
  57      * SPI implementations map
  58      */
  59     private ConcurrentMap<Class<? extends LocaleServiceProvider>, LocaleServiceProvider> providersMap =
  60             new ConcurrentHashMap<>();
  61 
  62     /**
  63      * Getter method for Locale Service Providers
  64      */
  65     @Override
  66     public <P extends LocaleServiceProvider> P getLocaleServiceProvider(Class<P> c) {
  67         @SuppressWarnings("unchecked")
  68         P lsp = (P) providersMap.get(c);
  69         if (lsp == null) {
  70             lsp = findInstalledProvider(c);
  71             providersMap.putIfAbsent(c, lsp == null ? NULL_PROVIDER : lsp);
  72         }
  73 
  74         return lsp;
  75     }
  76 
  77     /**
  78      * Real body to find an implementation for each SPI.
  79      *


 150     }
 151 
 152     @Override
 153     public LocaleResources getLocaleResources(Locale locale) {
 154         return null;
 155     }
 156 
 157     private static Locale[] availableLocales = null;
 158 
 159     @Override
 160     public Locale[] getAvailableLocales() {
 161         if (availableLocales == null) {
 162             Set<Locale> avail = new HashSet<>();
 163             for (Class<? extends LocaleServiceProvider> c :
 164                     LocaleServiceProviderPool.spiClasses) {
 165                 LocaleServiceProvider lsp = getLocaleServiceProvider(c);
 166                 if (lsp != null) {
 167                     avail.addAll(Arrays.asList(lsp.getAvailableLocales()));
 168                 }
 169             }
 170             availableLocales = avail.toArray(new Locale[0]);
 171         }
 172 
 173         // assuming caller won't mutate the array.
 174         return availableLocales;
 175     }
 176 
 177     /**
 178      * A dummy locale service provider that indicates there is no
 179      * provider available
 180      */
 181     private static NullProvider NULL_PROVIDER = new NullProvider();
 182     private static class NullProvider extends LocaleServiceProvider {
 183         @Override
 184         public Locale[] getAvailableLocales() {
 185             return new Locale[0];
 186         }
 187     }
 188 }


  39 import java.util.concurrent.ConcurrentMap;
  40 import java.util.spi.CalendarDataProvider;
  41 import java.util.spi.CalendarNameProvider;
  42 import java.util.spi.CurrencyNameProvider;
  43 import java.util.spi.LocaleNameProvider;
  44 import java.util.spi.LocaleServiceProvider;
  45 import java.util.spi.TimeZoneNameProvider;
  46 import sun.util.spi.CalendarProvider;
  47 
  48 /**
  49  * An abstract parent class for the
  50  * HostLocaleProviderAdapter/SPILocaleProviderAdapter.
  51  *
  52  * @author Naoto Sato
  53  * @author Masayoshi Okutsu
  54  */
  55 public abstract class AuxLocaleProviderAdapter extends LocaleProviderAdapter {
  56     /**
  57      * SPI implementations map
  58      */
  59     private final ConcurrentMap<Class<? extends LocaleServiceProvider>, LocaleServiceProvider> providersMap =
  60             new ConcurrentHashMap<>();
  61 
  62     /**
  63      * Getter method for Locale Service Providers
  64      */
  65     @Override
  66     public <P extends LocaleServiceProvider> P getLocaleServiceProvider(Class<P> c) {
  67         @SuppressWarnings("unchecked")
  68         P lsp = (P) providersMap.get(c);
  69         if (lsp == null) {
  70             lsp = findInstalledProvider(c);
  71             providersMap.putIfAbsent(c, lsp == null ? NULL_PROVIDER : lsp);
  72         }
  73 
  74         return lsp;
  75     }
  76 
  77     /**
  78      * Real body to find an implementation for each SPI.
  79      *


 150     }
 151 
 152     @Override
 153     public LocaleResources getLocaleResources(Locale locale) {
 154         return null;
 155     }
 156 
 157     private static Locale[] availableLocales = null;
 158 
 159     @Override
 160     public Locale[] getAvailableLocales() {
 161         if (availableLocales == null) {
 162             Set<Locale> avail = new HashSet<>();
 163             for (Class<? extends LocaleServiceProvider> c :
 164                     LocaleServiceProviderPool.spiClasses) {
 165                 LocaleServiceProvider lsp = getLocaleServiceProvider(c);
 166                 if (lsp != null) {
 167                     avail.addAll(Arrays.asList(lsp.getAvailableLocales()));
 168                 }
 169             }

 170         }
 171 
 172         // assuming caller won't mutate the array.
 173         return availableLocales;
 174     }
 175 
 176     /**
 177      * A dummy locale service provider that indicates there is no
 178      * provider available
 179      */
 180     private static final NullProvider NULL_PROVIDER = new NullProvider();
 181     private static class NullProvider extends LocaleServiceProvider {
 182         @Override
 183         public Locale[] getAvailableLocales() {
 184             return new Locale[0];
 185         }
 186     }
 187 }