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