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.util.Locale;
  29 import java.util.MissingResourceException;
  30 import java.util.ResourceBundle;
  31 import java.util.Set;
  32 import java.util.spi.CurrencyNameProvider;
  33 
  34 /**
  35  * Concrete implementation of the
  36  * {@link java.util.spi.CurrencyNameProvider CurrencyNameProvider} class
  37  * for the JRE LocaleProviderAdapter.
  38  *
  39  * @author Naoto Sato
  40  * @author Masayoshi Okutsu
  41  */
  42 public class CurrencyNameProviderImpl extends CurrencyNameProvider
  43                                       implements AvailableLanguageTags {
  44     private final LocaleProviderAdapter.Type type;
  45     private final Set<String> langtags;
  46 
  47     public CurrencyNameProviderImpl(LocaleProviderAdapter.Type type, Set<String> langtags) {
  48         this.type = type;
  49         this.langtags = langtags;
  50     }
  51 
  52     @Override
  53     public Set<String> getAvailableLanguageTags() {
  54         return langtags;
  55     }
  56 
  57     /**
  58      * Returns an array of all locales for which this locale service provider
  59      * can provide localized objects or names.
  60      *
  61      * @return An array of all locales for which this locale service provider
  62      * can provide localized objects or names.
  63      */
  64     @Override
  65     public Locale[] getAvailableLocales() {
  66         return LocaleProviderAdapter.toLocaleArray(langtags);
  67     }
  68 
  69     /**
  70      * Gets the symbol of the given currency code for the specified locale.
  71      * For example, for "USD" (US Dollar), the symbol is "$" if the specified
  72      * locale is the US, while for other locales it may be "US$". If no
  73      * symbol can be determined, null should be returned.
  74      *
  75      * @param currencyCode the ISO 4217 currency code, which
  76      *     consists of three upper-case letters between 'A' (U+0041) and
  77      *     'Z' (U+005A)
  78      * @param locale the desired locale
  79      * @return the symbol of the given currency code for the specified locale, or null if
  80      *     the symbol is not available for the locale
  81      * @exception NullPointerException if <code>currencyCode</code> or
  82      *     <code>locale</code> is null
  83      * @exception IllegalArgumentException if <code>currencyCode</code> is not in
  84      *     the form of three upper-case letters, or <code>locale</code> isn't
  85      *     one of the locales returned from
  86      *     {@link java.util.spi.LocaleServiceProvider#getAvailableLocales()
  87      *     getAvailableLocales()}.
  88      * @see java.util.Currency#getSymbol(java.util.Locale)
  89      */
  90     @Override
  91     public String getSymbol(String currencyCode, Locale locale) {
  92         return getString(currencyCode.toUpperCase(Locale.ROOT), locale);
  93     }
  94 
  95     /**
  96      * Returns a name for the currency that is appropriate for display to the
  97      * user.  The default implementation returns null.
  98      *
  99      * @param currencyCode the ISO 4217 currency code, which
 100      *     consists of three upper-case letters between 'A' (U+0041) and
 101      *     'Z' (U+005A)
 102      * @param locale the desired locale
 103      * @return the name for the currency that is appropriate for display to the
 104      *     user, or null if the name is not available for the locale
 105      * @exception IllegalArgumentException if <code>currencyCode</code> is not in
 106      *     the form of three upper-case letters, or <code>locale</code> isn't
 107      *     one of the locales returned from
 108      *     {@link java.util.spi.LocaleServiceProvider#getAvailableLocales()
 109      *     getAvailableLocales()}.
 110      * @exception NullPointerException if <code>currencyCode</code> or
 111      *     <code>locale</code> is <code>null</code>
 112      * @since 1.7
 113      */
 114     @Override
 115     public String getDisplayName(String currencyCode, Locale locale) {
 116         return getString(currencyCode.toLowerCase(Locale.ROOT), locale);
 117     }
 118 
 119     private String getString(String key, Locale locale) {
 120         if (locale == null) {
 121             throw new NullPointerException();
 122         }
 123 
 124         ResourceBundle bundle = LocaleProviderAdapter.forType(type).getLocaleData().getCurrencyNames(locale);
 125         try {
 126             if (bundle.containsKey(key)) {
 127                 return bundle.getString(key);
 128             }
 129         } catch (MissingResourceException mre) {}
 130 
 131         return null;
 132     }
 133 }