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