1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved. 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * This code is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License version 2 only, as 8 * published by the Free Software Foundation. Oracle designates this 9 * particular file as subject to the "Classpath" exception as provided 10 * by Oracle in the LICENSE file that accompanied this code. 11 * 12 * This code is distributed in the hope that it will be useful, but WITHOUT 13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 15 * version 2 for more details (a copy is included in the LICENSE file that 16 * accompanied this code). 17 * 18 * You should have received a copy of the GNU General Public License version 19 * 2 along with this work; if not, write to the Free Software Foundation, 20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 21 * 22 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 23 * or visit www.oracle.com if you need additional information or have any 24 * questions. 25 */ 26 27 package java.util; 28 29 import java.io.Serializable; 30 import java.util.concurrent.ConcurrentHashMap; 31 import java.util.concurrent.ConcurrentMap; 32 33 import libcore.icu.ICU; 34 35 // BEGIN Android-changed: Removed docs about superseding runtime currency data. 36 // Doing so via a properties file is not supported on Android. 37 /** 38 * Represents a currency. Currencies are identified by their ISO 4217 currency 39 * codes. Visit the <a href="http://www.iso.org/iso/home/standards/currency_codes.htm"> 40 * ISO web site</a> for more information. 41 * <p> 42 * The class is designed so that there's never more than one 43 * <code>Currency</code> instance for any given currency. Therefore, there's 44 * no public constructor. You obtain a <code>Currency</code> instance using 45 * the <code>getInstance</code> methods. 46 * 47 * @since 1.4 48 */ 49 // END Android-changed: Removed docs about superseding runtime currency data. 50 public final class Currency implements Serializable { 51 52 private static final long serialVersionUID = -158308464356906721L; 53 54 /** 55 * ISO 4217 currency code for this currency. 56 * 57 * @serial 58 */ 59 private final String currencyCode; 60 61 // class data: instance map 62 63 private static ConcurrentMap<String, Currency> instances = new ConcurrentHashMap<>(7); 64 private static HashSet<Currency> available; 65 66 // Android-changed: Implement Currency on top of ICU throughout. 67 // We do not keep track of defaultFractionDigits and numericCode separately here. 68 private transient final android.icu.util.Currency icuCurrency; 69 70 /** 71 * Constructs a <code>Currency</code> instance. The constructor is private 72 * so that we can insure that there's never more than one instance for a 73 * given currency. 74 */ Currency(android.icu.util.Currency icuCurrency)75 private Currency(android.icu.util.Currency icuCurrency) { 76 this.icuCurrency = icuCurrency; 77 this.currencyCode = icuCurrency.getCurrencyCode(); 78 } 79 80 /** 81 * Returns the <code>Currency</code> instance for the given currency code. 82 * 83 * @param currencyCode the ISO 4217 code of the currency 84 * @return the <code>Currency</code> instance for the given currency code 85 * @exception NullPointerException if <code>currencyCode</code> is null 86 * @exception IllegalArgumentException if <code>currencyCode</code> is not 87 * a supported ISO 4217 code. 88 */ getInstance(String currencyCode)89 public static Currency getInstance(String currencyCode) { 90 // BEGIN Android-changed: use ICU 91 Currency instance = instances.get(currencyCode); 92 if (instance != null) { 93 return instance; 94 } 95 android.icu.util.Currency icuInstance = 96 android.icu.util.Currency.getInstance(currencyCode); 97 if (icuInstance == null) { 98 return null; 99 } 100 Currency currencyVal = new Currency(icuInstance); 101 // END Android-changed 102 instance = instances.putIfAbsent(currencyCode, currencyVal); 103 return (instance != null ? instance : currencyVal); 104 } 105 106 /** 107 * Returns the <code>Currency</code> instance for the country of the 108 * given locale. The language and variant components of the locale 109 * are ignored. The result may vary over time, as countries change their 110 * currencies. For example, for the original member countries of the 111 * European Monetary Union, the method returns the old national currencies 112 * until December 31, 2001, and the Euro from January 1, 2002, local time 113 * of the respective countries. 114 * <p> 115 * The method returns <code>null</code> for territories that don't 116 * have a currency, such as Antarctica. 117 * 118 * @param locale the locale for whose country a <code>Currency</code> 119 * instance is needed 120 * @return the <code>Currency</code> instance for the country of the given 121 * locale, or {@code null} 122 * @exception NullPointerException if <code>locale</code> or its country 123 * code is {@code null} 124 * @exception IllegalArgumentException if the country of the given {@code locale} 125 * is not a supported ISO 3166 country code. 126 */ getInstance(Locale locale)127 public static Currency getInstance(Locale locale) { 128 // BEGIN Android-changed: use ICU 129 android.icu.util.Currency icuInstance = 130 android.icu.util.Currency.getInstance(locale); 131 String variant = locale.getVariant(); 132 String country = locale.getCountry(); 133 if (!variant.isEmpty() && (variant.equals("EURO") || variant.equals("HK") || 134 variant.equals("PREEURO"))) { 135 country = country + "_" + variant; 136 } 137 String currencyCode = ICU.getCurrencyCode(country); 138 if (currencyCode == null) { 139 throw new IllegalArgumentException("Unsupported ISO 3166 country: " + locale); 140 } 141 if (icuInstance == null || icuInstance.getCurrencyCode().equals("XXX")) { 142 return null; 143 } 144 return getInstance(currencyCode); 145 // END Android-changed 146 } 147 148 /** 149 * Gets the set of available currencies. The returned set of currencies 150 * contains all of the available currencies, which may include currencies 151 * that represent obsolete ISO 4217 codes. The set can be modified 152 * without affecting the available currencies in the runtime. 153 * 154 * @return the set of available currencies. If there is no currency 155 * available in the runtime, the returned set is empty. 156 * @since 1.7 157 */ getAvailableCurrencies()158 public static Set<Currency> getAvailableCurrencies() { 159 synchronized(Currency.class) { 160 if (available == null) { 161 // BEGIN Android-changed: use ICU 162 Set<android.icu.util.Currency> icuAvailableCurrencies 163 = android.icu.util.Currency.getAvailableCurrencies(); 164 available = new HashSet<>(); 165 for (android.icu.util.Currency icuCurrency : icuAvailableCurrencies) { 166 Currency currency = getInstance(icuCurrency.getCurrencyCode()); 167 if (currency == null) { 168 currency = new Currency(icuCurrency); 169 instances.put(currency.currencyCode, currency); 170 } 171 available.add(currency); 172 } 173 // END Android-changed 174 } 175 } 176 177 @SuppressWarnings("unchecked") 178 Set<Currency> result = (Set<Currency>) available.clone(); 179 return result; 180 } 181 182 /** 183 * Gets the ISO 4217 currency code of this currency. 184 * 185 * @return the ISO 4217 currency code of this currency. 186 */ getCurrencyCode()187 public String getCurrencyCode() { 188 return currencyCode; 189 } 190 191 /** 192 * Gets the symbol of this currency for the default 193 * {@link Locale.Category#DISPLAY DISPLAY} locale. 194 * For example, for the US Dollar, the symbol is "$" if the default 195 * locale is the US, while for other locales it may be "US$". If no 196 * symbol can be determined, the ISO 4217 currency code is returned. 197 * <p> 198 * This is equivalent to calling 199 * {@link #getSymbol(Locale) 200 * getSymbol(Locale.getDefault(Locale.Category.DISPLAY))}. 201 * 202 * @return the symbol of this currency for the default 203 * {@link Locale.Category#DISPLAY DISPLAY} locale 204 */ getSymbol()205 public String getSymbol() { 206 return getSymbol(Locale.getDefault(Locale.Category.DISPLAY)); 207 } 208 209 /** 210 * Gets the symbol of this currency for the specified locale. 211 * For example, for the US Dollar, the symbol is "$" if the specified 212 * locale is the US, while for other locales it may be "US$". If no 213 * symbol can be determined, the ISO 4217 currency code is returned. 214 * 215 * @param locale the locale for which a display name for this currency is 216 * needed 217 * @return the symbol of this currency for the specified locale 218 * @exception NullPointerException if <code>locale</code> is null 219 */ getSymbol(Locale locale)220 public String getSymbol(Locale locale) { 221 // BEGIN Android-changed: use ICU 222 if (locale == null) { 223 throw new NullPointerException("locale == null"); 224 } 225 return icuCurrency.getSymbol(locale); 226 // END Android-changed 227 } 228 229 /** 230 * Gets the default number of fraction digits used with this currency. 231 * For example, the default number of fraction digits for the Euro is 2, 232 * while for the Japanese Yen it's 0. 233 * In the case of pseudo-currencies, such as IMF Special Drawing Rights, 234 * -1 is returned. 235 * 236 * @return the default number of fraction digits used with this currency 237 */ getDefaultFractionDigits()238 public int getDefaultFractionDigits() { 239 // BEGIN Android-changed: use ICU 240 if (icuCurrency.getCurrencyCode().equals("XXX")) { 241 return -1; 242 } 243 return icuCurrency.getDefaultFractionDigits(); 244 // END Android-changed 245 } 246 247 /** 248 * Returns the ISO 4217 numeric code of this currency. 249 * 250 * @return the ISO 4217 numeric code of this currency 251 * @since 1.7 252 */ getNumericCode()253 public int getNumericCode() { 254 // Android-changed: use ICU 255 // was: return numericCode; 256 return icuCurrency.getNumericCode(); 257 } 258 259 /** 260 * Gets the name that is suitable for displaying this currency for 261 * the default {@link Locale.Category#DISPLAY DISPLAY} locale. 262 * If there is no suitable display name found 263 * for the default locale, the ISO 4217 currency code is returned. 264 * <p> 265 * This is equivalent to calling 266 * {@link #getDisplayName(Locale) 267 * getDisplayName(Locale.getDefault(Locale.Category.DISPLAY))}. 268 * 269 * @return the display name of this currency for the default 270 * {@link Locale.Category#DISPLAY DISPLAY} locale 271 * @since 1.7 272 */ getDisplayName()273 public String getDisplayName() { 274 return getDisplayName(Locale.getDefault(Locale.Category.DISPLAY)); 275 } 276 277 /** 278 * Gets the name that is suitable for displaying this currency for 279 * the specified locale. If there is no suitable display name found 280 * for the specified locale, the ISO 4217 currency code is returned. 281 * 282 * @param locale the locale for which a display name for this currency is 283 * needed 284 * @return the display name of this currency for the specified locale 285 * @exception NullPointerException if <code>locale</code> is null 286 * @since 1.7 287 */ getDisplayName(Locale locale)288 public String getDisplayName(Locale locale) { 289 // Android-changed: use ICU 290 return icuCurrency.getDisplayName(Objects.requireNonNull(locale)); 291 } 292 293 /** 294 * Returns the ISO 4217 currency code of this currency. 295 * 296 * @return the ISO 4217 currency code of this currency 297 */ 298 @Override toString()299 public String toString() { 300 // Android-changed: use ICU 301 return icuCurrency.toString(); 302 } 303 304 /** 305 * Resolves instances being deserialized to a single instance per currency. 306 */ readResolve()307 private Object readResolve() { 308 return getInstance(currencyCode); 309 } 310 } 311