1 /*
   2  * Copyright (c) 2012, 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
  23  * questions.
  24  */
  25 
  26 package sun.util.locale.provider;
  27 
  28 import java.text.spi.BreakIteratorProvider;
  29 import java.text.spi.CollatorProvider;
  30 import java.text.spi.DateFormatProvider;
  31 import java.text.spi.DateFormatSymbolsProvider;
  32 import java.text.spi.DecimalFormatSymbolsProvider;
  33 import java.text.spi.NumberFormatProvider;
  34 import java.util.Arrays;
  35 import java.util.HashSet;
  36 import java.util.Locale;
  37 import java.util.Set;
  38 import java.util.concurrent.ConcurrentHashMap;
  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 
  47 /**
  48  * An abstract parent class for the
  49  * HostLocaleProviderAdapter/SPILocaleProviderAdapter.
  50  *
  51  * @author Naoto Sato
  52  * @author Masayoshi Okutsu
  53  */
  54 public abstract class AuxLocaleProviderAdapter extends LocaleProviderAdapter {
  55     /**
  56      * SPI implementations map
  57      */
  58     private ConcurrentMap<Class<? extends LocaleServiceProvider>, LocaleServiceProvider> providersMap =
  59             new ConcurrentHashMap<>();
  60 
  61     /**
  62      * Getter method for Locale Service Providers
  63      */
  64     @Override
  65     public <P extends LocaleServiceProvider> P getLocaleServiceProvider(Class<P> c) {
  66         @SuppressWarnings("unchecked")
  67         P lsp = (P) providersMap.get(c);
  68         if (lsp == null) {
  69             lsp = findInstalledProvider(c);
  70             providersMap.putIfAbsent(c, lsp == null ? NULL_PROVIDER : lsp);
  71         }
  72 
  73         return lsp;
  74     }
  75 
  76     /**
  77      * Real body to find an implementation for each SPI.
  78      *
  79      * @param <P>
  80      * @param c
  81      * @return
  82      */
  83     protected abstract <P extends LocaleServiceProvider> P findInstalledProvider(final Class<P> c);
  84 
  85     @Override
  86     public BreakIteratorProvider getBreakIteratorProvider() {
  87         return getLocaleServiceProvider(BreakIteratorProvider.class);
  88     }
  89 
  90     @Override
  91     public CollatorProvider getCollatorProvider() {
  92         return getLocaleServiceProvider(CollatorProvider.class);
  93     }
  94 
  95     @Override
  96     public DateFormatProvider getDateFormatProvider() {
  97         return getLocaleServiceProvider(DateFormatProvider.class);
  98     }
  99 
 100     @Override
 101     public DateFormatSymbolsProvider getDateFormatSymbolsProvider() {
 102         return getLocaleServiceProvider(DateFormatSymbolsProvider.class);
 103     }
 104 
 105     @Override
 106     public DecimalFormatSymbolsProvider getDecimalFormatSymbolsProvider() {
 107         return getLocaleServiceProvider(DecimalFormatSymbolsProvider.class);
 108     }
 109 
 110     @Override
 111     public NumberFormatProvider getNumberFormatProvider() {
 112         return getLocaleServiceProvider(NumberFormatProvider.class);
 113     }
 114 
 115     /**
 116      * Getter methods for java.util.spi.* providers
 117      */
 118     @Override
 119     public CurrencyNameProvider getCurrencyNameProvider() {
 120         return getLocaleServiceProvider(CurrencyNameProvider.class);
 121     }
 122 
 123     @Override
 124     public LocaleNameProvider getLocaleNameProvider() {
 125         return getLocaleServiceProvider(LocaleNameProvider.class);
 126     }
 127 
 128     @Override
 129     public TimeZoneNameProvider getTimeZoneNameProvider() {
 130         return getLocaleServiceProvider(TimeZoneNameProvider.class);
 131     }
 132 
 133     @Override
 134     public CalendarDataProvider getCalendarDataProvider() {
 135         return getLocaleServiceProvider(CalendarDataProvider.class);
 136     }
 137 
 138     @Override
 139     public CalendarNameProvider getCalendarNameProvider() {
 140         return getLocaleServiceProvider(CalendarNameProvider.class);
 141     }
 142 
 143     @Override
 144     public LocaleResources getLocaleResources(Locale locale) {
 145         return null;
 146     }
 147 
 148     private static Locale[] availableLocales = null;
 149 
 150     @Override
 151     public Locale[] getAvailableLocales() {
 152         if (availableLocales == null) {
 153             Set<Locale> avail = new HashSet<>();
 154             for (Class<? extends LocaleServiceProvider> c :
 155                     LocaleServiceProviderPool.spiClasses) {
 156                 LocaleServiceProvider lsp = getLocaleServiceProvider(c);
 157                 if (lsp != null) {
 158                     avail.addAll(Arrays.asList(lsp.getAvailableLocales()));
 159                 }
 160             }
 161             availableLocales = avail.toArray(new Locale[0]);
 162         }
 163 
 164         // assuming caller won't mutate the array.
 165         return availableLocales;
 166     }
 167 
 168     /**
 169      * A dummy locale service provider that indicates there is no
 170      * provider available
 171      */
 172     private static NullProvider NULL_PROVIDER = new NullProvider();
 173     private static class NullProvider extends LocaleServiceProvider {
 174         @Override
 175         public Locale[] getAvailableLocales() {
 176             return new Locale[0];
 177         }
 178     }
 179 }