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 // BEGIN Android-changed: Use ICU. 62 // We do not keep track of defaultFractionDigits and numericCode separately. 63 /* 64 /** 65 * Default fraction digits for this currency. 66 * Set from currency data tables. 67 * 68 transient private final int defaultFractionDigits; 69 70 /** 71 * ISO 4217 numeric code for this currency. 72 * Set from currency data tables. 73 * 74 transient private final int numericCode; 75 */ 76 private transient final android.icu.util.Currency icuCurrency; 77 // END Android-changed: Use ICU. 78 79 80 // class data: instance map 81 82 private static ConcurrentMap<String, Currency> instances = new ConcurrentHashMap<>(7); 83 private static HashSet<Currency> available; 84 85 // BEGIN Android-removed: Use ICU. 86 // We don't need any of these static fields nor the static initializer. 87 /* 88 // Class data: currency data obtained from currency.data file. 89 // Purpose: 90 // - determine valid country codes 91 // - determine valid currency codes 92 // - map country codes to currency codes 93 // - obtain default fraction digits for currency codes 94 // 95 // sc = special case; dfd = default fraction digits 96 // Simple countries are those where the country code is a prefix of the 97 // currency code, and there are no known plans to change the currency. 98 // 99 // table formats: 100 // - mainTable: 101 // - maps country code to 32-bit int 102 // - 26*26 entries, corresponding to [A-Z]*[A-Z] 103 // - \u007F -> not valid country 104 // - bits 20-31: unused 105 // - bits 10-19: numeric code (0 to 1023) 106 // - bit 9: 1 - special case, bits 0-4 indicate which one 107 // 0 - simple country, bits 0-4 indicate final char of currency code 108 // - bits 5-8: fraction digits for simple countries, 0 for special cases 109 // - bits 0-4: final char for currency code for simple country, or ID of special case 110 // - special case IDs: 111 // - 0: country has no currency 112 // - other: index into sc* arrays + 1 113 // - scCutOverTimes: cut-over time in millis as returned by 114 // System.currentTimeMillis for special case countries that are changing 115 // currencies; Long.MAX_VALUE for countries that are not changing currencies 116 // - scOldCurrencies: old currencies for special case countries 117 // - scNewCurrencies: new currencies for special case countries that are 118 // changing currencies; null for others 119 // - scOldCurrenciesDFD: default fraction digits for old currencies 120 // - scNewCurrenciesDFD: default fraction digits for new currencies, 0 for 121 // countries that are not changing currencies 122 // - otherCurrencies: concatenation of all currency codes that are not the 123 // main currency of a simple country, separated by "-" 124 // - otherCurrenciesDFD: decimal format digits for currencies in otherCurrencies, same order 125 126 static int formatVersion; 127 static int dataVersion; 128 static int[] mainTable; 129 static long[] scCutOverTimes; 130 static String[] scOldCurrencies; 131 static String[] scNewCurrencies; 132 static int[] scOldCurrenciesDFD; 133 static int[] scNewCurrenciesDFD; 134 static int[] scOldCurrenciesNumericCode; 135 static int[] scNewCurrenciesNumericCode; 136 static String otherCurrencies; 137 static int[] otherCurrenciesDFD; 138 static int[] otherCurrenciesNumericCode; 139 140 // handy constants - must match definitions in GenerateCurrencyData 141 // magic number 142 private static final int MAGIC_NUMBER = 0x43757244; 143 // number of characters from A to Z 144 private static final int A_TO_Z = ('Z' - 'A') + 1; 145 // entry for invalid country codes 146 private static final int INVALID_COUNTRY_ENTRY = 0x0000007F; 147 // entry for countries without currency 148 private static final int COUNTRY_WITHOUT_CURRENCY_ENTRY = 0x00000200; 149 // mask for simple case country entries 150 private static final int SIMPLE_CASE_COUNTRY_MASK = 0x00000000; 151 // mask for simple case country entry final character 152 private static final int SIMPLE_CASE_COUNTRY_FINAL_CHAR_MASK = 0x0000001F; 153 // mask for simple case country entry default currency digits 154 private static final int SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_MASK = 0x000001E0; 155 // shift count for simple case country entry default currency digits 156 private static final int SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_SHIFT = 5; 157 // maximum number for simple case country entry default currency digits 158 private static final int SIMPLE_CASE_COUNTRY_MAX_DEFAULT_DIGITS = 9; 159 // mask for special case country entries 160 private static final int SPECIAL_CASE_COUNTRY_MASK = 0x00000200; 161 // mask for special case country index 162 private static final int SPECIAL_CASE_COUNTRY_INDEX_MASK = 0x0000001F; 163 // delta from entry index component in main table to index into special case tables 164 private static final int SPECIAL_CASE_COUNTRY_INDEX_DELTA = 1; 165 // mask for distinguishing simple and special case countries 166 private static final int COUNTRY_TYPE_MASK = SIMPLE_CASE_COUNTRY_MASK | SPECIAL_CASE_COUNTRY_MASK; 167 // mask for the numeric code of the currency 168 private static final int NUMERIC_CODE_MASK = 0x000FFC00; 169 // shift count for the numeric code of the currency 170 private static final int NUMERIC_CODE_SHIFT = 10; 171 172 // Currency data format version 173 private static final int VALID_FORMAT_VERSION = 2; 174 175 static { 176 AccessController.doPrivileged(new PrivilegedAction<Void>() { 177 @Override 178 public Void run() { 179 String homeDir = System.getProperty("java.home"); 180 try { 181 String dataFile = homeDir + File.separator + 182 "lib" + File.separator + "currency.data"; 183 try (DataInputStream dis = new DataInputStream( 184 new BufferedInputStream( 185 new FileInputStream(dataFile)))) { 186 if (dis.readInt() != MAGIC_NUMBER) { 187 throw new InternalError("Currency data is possibly corrupted"); 188 } 189 formatVersion = dis.readInt(); 190 if (formatVersion != VALID_FORMAT_VERSION) { 191 throw new InternalError("Currency data format is incorrect"); 192 } 193 dataVersion = dis.readInt(); 194 mainTable = readIntArray(dis, A_TO_Z * A_TO_Z); 195 int scCount = dis.readInt(); 196 scCutOverTimes = readLongArray(dis, scCount); 197 scOldCurrencies = readStringArray(dis, scCount); 198 scNewCurrencies = readStringArray(dis, scCount); 199 scOldCurrenciesDFD = readIntArray(dis, scCount); 200 scNewCurrenciesDFD = readIntArray(dis, scCount); 201 scOldCurrenciesNumericCode = readIntArray(dis, scCount); 202 scNewCurrenciesNumericCode = readIntArray(dis, scCount); 203 int ocCount = dis.readInt(); 204 otherCurrencies = dis.readUTF(); 205 otherCurrenciesDFD = readIntArray(dis, ocCount); 206 otherCurrenciesNumericCode = readIntArray(dis, ocCount); 207 } 208 } catch (IOException e) { 209 throw new InternalError(e); 210 } 211 212 // look for the properties file for overrides 213 String propsFile = System.getProperty("java.util.currency.data"); 214 if (propsFile == null) { 215 propsFile = homeDir + File.separator + "lib" + 216 File.separator + "currency.properties"; 217 } 218 try { 219 File propFile = new File(propsFile); 220 if (propFile.exists()) { 221 Properties props = new Properties(); 222 try (FileReader fr = new FileReader(propFile)) { 223 props.load(fr); 224 } 225 Set<String> keys = props.stringPropertyNames(); 226 Pattern propertiesPattern = 227 Pattern.compile("([A-Z]{3})\\s*,\\s*(\\d{3})\\s*,\\s*" + 228 "(\\d+)\\s*,?\\s*(\\d{4}-\\d{2}-\\d{2}T\\d{2}:" + 229 "\\d{2}:\\d{2})?"); 230 for (String key : keys) { 231 replaceCurrencyData(propertiesPattern, 232 key.toUpperCase(Locale.ROOT), 233 props.getProperty(key).toUpperCase(Locale.ROOT)); 234 } 235 } 236 } catch (IOException e) { 237 info("currency.properties is ignored because of an IOException", e); 238 } 239 return null; 240 } 241 }); 242 } 243 244 /** 245 * Constants for retrieving localized names from the name providers. 246 * 247 private static final int SYMBOL = 0; 248 private static final int DISPLAYNAME = 1; 249 */ 250 // END Android-removed: Use ICU. 251 252 /** 253 * Constructs a <code>Currency</code> instance. The constructor is private 254 * so that we can insure that there's never more than one instance for a 255 * given currency. 256 */ 257 // BEGIN Android-changed: Use ICU. 258 // We do not keep track of defaultFractionDigits and numericCode separately. 259 /* 260 private Currency(String currencyCode, int defaultFractionDigits, int numericCode) { 261 this.currencyCode = currencyCode; 262 this.defaultFractionDigits = defaultFractionDigits; 263 this.numericCode = numericCode; 264 } 265 */ Currency(android.icu.util.Currency icuCurrency)266 private Currency(android.icu.util.Currency icuCurrency) { 267 this.icuCurrency = icuCurrency; 268 this.currencyCode = icuCurrency.getCurrencyCode(); 269 } 270 // END Android-changed: Use ICU. 271 272 /** 273 * Returns the <code>Currency</code> instance for the given currency code. 274 * 275 * @param currencyCode the ISO 4217 code of the currency 276 * @return the <code>Currency</code> instance for the given currency code 277 * @exception NullPointerException if <code>currencyCode</code> is null 278 * @exception IllegalArgumentException if <code>currencyCode</code> is not 279 * a supported ISO 4217 code. 280 */ getInstance(String currencyCode)281 public static Currency getInstance(String currencyCode) { 282 // BEGIN Android-changed: Use ICU. 283 // Upstream uses a private static helper method, implemented differently. 284 Currency instance = instances.get(currencyCode); 285 if (instance != null) { 286 return instance; 287 } 288 android.icu.util.Currency icuInstance = 289 android.icu.util.Currency.getInstance(currencyCode); 290 if (icuInstance == null) { 291 return null; 292 } 293 Currency currencyVal = new Currency(icuInstance); 294 // END Android-changed: Use ICU. 295 instance = instances.putIfAbsent(currencyCode, currencyVal); 296 return (instance != null ? instance : currencyVal); 297 } 298 299 /** 300 * Returns the <code>Currency</code> instance for the country of the 301 * given locale. The language and variant components of the locale 302 * are ignored. The result may vary over time, as countries change their 303 * currencies. For example, for the original member countries of the 304 * European Monetary Union, the method returns the old national currencies 305 * until December 31, 2001, and the Euro from January 1, 2002, local time 306 * of the respective countries. 307 * <p> 308 * The method returns <code>null</code> for territories that don't 309 * have a currency, such as Antarctica. 310 * 311 * @param locale the locale for whose country a <code>Currency</code> 312 * instance is needed 313 * @return the <code>Currency</code> instance for the country of the given 314 * locale, or {@code null} 315 * @exception NullPointerException if <code>locale</code> or its country 316 * code is {@code null} 317 * @exception IllegalArgumentException if the country of the given {@code locale} 318 * is not a supported ISO 3166 country code. 319 */ getInstance(Locale locale)320 public static Currency getInstance(Locale locale) { 321 String country = locale.getCountry(); 322 if (country == null) { 323 throw new NullPointerException(); 324 } 325 326 // BEGIN Android-changed: Use ICU. 327 /* 328 if (country.length() != 2) { 329 throw new IllegalArgumentException(); 330 } 331 332 char char1 = country.charAt(0); 333 char char2 = country.charAt(1); 334 int tableEntry = getMainTableEntry(char1, char2); 335 if ((tableEntry & COUNTRY_TYPE_MASK) == SIMPLE_CASE_COUNTRY_MASK 336 && tableEntry != INVALID_COUNTRY_ENTRY) { 337 char finalChar = (char) ((tableEntry & SIMPLE_CASE_COUNTRY_FINAL_CHAR_MASK) + 'A'); 338 int defaultFractionDigits = (tableEntry & SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_MASK) >> SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_SHIFT; 339 int numericCode = (tableEntry & NUMERIC_CODE_MASK) >> NUMERIC_CODE_SHIFT; 340 StringBuilder sb = new StringBuilder(country); 341 sb.append(finalChar); 342 return getInstance(sb.toString(), defaultFractionDigits, numericCode); 343 } else { 344 // special cases 345 if (tableEntry == INVALID_COUNTRY_ENTRY) { 346 throw new IllegalArgumentException(); 347 } 348 if (tableEntry == COUNTRY_WITHOUT_CURRENCY_ENTRY) { 349 return null; 350 } else { 351 int index = (tableEntry & SPECIAL_CASE_COUNTRY_INDEX_MASK) - SPECIAL_CASE_COUNTRY_INDEX_DELTA; 352 if (scCutOverTimes[index] == Long.MAX_VALUE || System.currentTimeMillis() < scCutOverTimes[index]) { 353 return getInstance(scOldCurrencies[index], scOldCurrenciesDFD[index], 354 scOldCurrenciesNumericCode[index]); 355 } else { 356 return getInstance(scNewCurrencies[index], scNewCurrenciesDFD[index], 357 scNewCurrenciesNumericCode[index]); 358 } 359 } 360 } 361 */ 362 android.icu.util.Currency icuInstance = 363 android.icu.util.Currency.getInstance(locale); 364 String variant = locale.getVariant(); 365 if (!variant.isEmpty() && (variant.equals("EURO") || variant.equals("HK") || 366 variant.equals("PREEURO"))) { 367 country = country + "_" + variant; 368 } 369 String currencyCode = ICU.getCurrencyCode(country); 370 if (currencyCode == null) { 371 throw new IllegalArgumentException("Unsupported ISO 3166 country: " + locale); 372 } 373 if (icuInstance == null || icuInstance.getCurrencyCode().equals("XXX")) { 374 return null; 375 } 376 return getInstance(currencyCode); 377 // END Android-changed: Use ICU. 378 } 379 380 /** 381 * Gets the set of available currencies. The returned set of currencies 382 * contains all of the available currencies, which may include currencies 383 * that represent obsolete ISO 4217 codes. The set can be modified 384 * without affecting the available currencies in the runtime. 385 * 386 * @return the set of available currencies. If there is no currency 387 * available in the runtime, the returned set is empty. 388 * @since 1.7 389 */ getAvailableCurrencies()390 public static Set<Currency> getAvailableCurrencies() { 391 synchronized(Currency.class) { 392 if (available == null) { 393 // BEGIN Android-changed: Use ICU. 394 /* 395 available = new HashSet<>(256); 396 397 // Add simple currencies first 398 for (char c1 = 'A'; c1 <= 'Z'; c1 ++) { 399 for (char c2 = 'A'; c2 <= 'Z'; c2 ++) { 400 int tableEntry = getMainTableEntry(c1, c2); 401 if ((tableEntry & COUNTRY_TYPE_MASK) == SIMPLE_CASE_COUNTRY_MASK 402 && tableEntry != INVALID_COUNTRY_ENTRY) { 403 char finalChar = (char) ((tableEntry & SIMPLE_CASE_COUNTRY_FINAL_CHAR_MASK) + 'A'); 404 int defaultFractionDigits = (tableEntry & SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_MASK) >> SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_SHIFT; 405 int numericCode = (tableEntry & NUMERIC_CODE_MASK) >> NUMERIC_CODE_SHIFT; 406 StringBuilder sb = new StringBuilder(); 407 sb.append(c1); 408 sb.append(c2); 409 sb.append(finalChar); 410 available.add(getInstance(sb.toString(), defaultFractionDigits, numericCode)); 411 } 412 } 413 } 414 415 // Now add other currencies 416 StringTokenizer st = new StringTokenizer(otherCurrencies, "-"); 417 while (st.hasMoreElements()) { 418 available.add(getInstance((String)st.nextElement())); 419 } 420 */ 421 available = new HashSet<>(); 422 Set<android.icu.util.Currency> icuAvailableCurrencies 423 = android.icu.util.Currency.getAvailableCurrencies(); 424 for (android.icu.util.Currency icuCurrency : icuAvailableCurrencies) { 425 Currency currency = getInstance(icuCurrency.getCurrencyCode()); 426 if (currency == null) { 427 currency = new Currency(icuCurrency); 428 instances.put(currency.currencyCode, currency); 429 } 430 available.add(currency); 431 } 432 // END Android-changed: Use ICU. 433 } 434 } 435 436 @SuppressWarnings("unchecked") 437 Set<Currency> result = (Set<Currency>) available.clone(); 438 return result; 439 } 440 441 /** 442 * Gets the ISO 4217 currency code of this currency. 443 * 444 * @return the ISO 4217 currency code of this currency. 445 */ getCurrencyCode()446 public String getCurrencyCode() { 447 return currencyCode; 448 } 449 450 /** 451 * Gets the symbol of this currency for the default 452 * {@link Locale.Category#DISPLAY DISPLAY} locale. 453 * For example, for the US Dollar, the symbol is "$" if the default 454 * locale is the US, while for other locales it may be "US$". If no 455 * symbol can be determined, the ISO 4217 currency code is returned. 456 * <p> 457 * This is equivalent to calling 458 * {@link #getSymbol(Locale) 459 * getSymbol(Locale.getDefault(Locale.Category.DISPLAY))}. 460 * 461 * @return the symbol of this currency for the default 462 * {@link Locale.Category#DISPLAY DISPLAY} locale 463 */ getSymbol()464 public String getSymbol() { 465 return getSymbol(Locale.getDefault(Locale.Category.DISPLAY)); 466 } 467 468 /** 469 * Gets the symbol of this currency for the specified locale. 470 * For example, for the US Dollar, the symbol is "$" if the specified 471 * locale is the US, while for other locales it may be "US$". If no 472 * symbol can be determined, the ISO 4217 currency code is returned. 473 * 474 * @param locale the locale for which a display name for this currency is 475 * needed 476 * @return the symbol of this currency for the specified locale 477 * @exception NullPointerException if <code>locale</code> is null 478 */ getSymbol(Locale locale)479 public String getSymbol(Locale locale) { 480 // BEGIN Android-changed: Use ICU. 481 /* 482 LocaleServiceProviderPool pool = 483 LocaleServiceProviderPool.getPool(CurrencyNameProvider.class); 484 String symbol = pool.getLocalizedObject( 485 CurrencyNameGetter.INSTANCE, 486 locale, currencyCode, SYMBOL); 487 if (symbol != null) { 488 return symbol; 489 } 490 491 // use currency code as symbol of last resort 492 return currencyCode; 493 */ 494 if (locale == null) { 495 throw new NullPointerException("locale == null"); 496 } 497 return icuCurrency.getSymbol(locale); 498 // END Android-changed: Use ICU. 499 } 500 501 /** 502 * Gets the default number of fraction digits used with this currency. 503 * For example, the default number of fraction digits for the Euro is 2, 504 * while for the Japanese Yen it's 0. 505 * In the case of pseudo-currencies, such as IMF Special Drawing Rights, 506 * -1 is returned. 507 * 508 * @return the default number of fraction digits used with this currency 509 */ getDefaultFractionDigits()510 public int getDefaultFractionDigits() { 511 // BEGIN Android-changed: Use ICU. 512 // return defaultFractionDigits; 513 if (icuCurrency.getCurrencyCode().equals("XXX")) { 514 return -1; 515 } 516 return icuCurrency.getDefaultFractionDigits(); 517 // END Android-changed: Use ICU. 518 } 519 520 /** 521 * Returns the ISO 4217 numeric code of this currency. 522 * 523 * @return the ISO 4217 numeric code of this currency 524 * @since 1.7 525 */ getNumericCode()526 public int getNumericCode() { 527 // Android-changed: Use ICU. 528 // return numericCode; 529 return icuCurrency.getNumericCode(); 530 } 531 532 /** 533 * Gets the name that is suitable for displaying this currency for 534 * the default {@link Locale.Category#DISPLAY DISPLAY} locale. 535 * If there is no suitable display name found 536 * for the default locale, the ISO 4217 currency code is returned. 537 * <p> 538 * This is equivalent to calling 539 * {@link #getDisplayName(Locale) 540 * getDisplayName(Locale.getDefault(Locale.Category.DISPLAY))}. 541 * 542 * @return the display name of this currency for the default 543 * {@link Locale.Category#DISPLAY DISPLAY} locale 544 * @since 1.7 545 */ getDisplayName()546 public String getDisplayName() { 547 return getDisplayName(Locale.getDefault(Locale.Category.DISPLAY)); 548 } 549 550 /** 551 * Gets the name that is suitable for displaying this currency for 552 * the specified locale. If there is no suitable display name found 553 * for the specified locale, the ISO 4217 currency code is returned. 554 * 555 * @param locale the locale for which a display name for this currency is 556 * needed 557 * @return the display name of this currency for the specified locale 558 * @exception NullPointerException if <code>locale</code> is null 559 * @since 1.7 560 */ getDisplayName(Locale locale)561 public String getDisplayName(Locale locale) { 562 // Android-changed: Use ICU. 563 /* 564 LocaleServiceProviderPool pool = 565 LocaleServiceProviderPool.getPool(CurrencyNameProvider.class); 566 String result = pool.getLocalizedObject( 567 CurrencyNameGetter.INSTANCE, 568 locale, currencyCode, DISPLAYNAME); 569 if (result != null) { 570 return result; 571 } 572 573 // use currency code as symbol of last resort 574 return currencyCode; 575 */ 576 return icuCurrency.getDisplayName(Objects.requireNonNull(locale)); 577 } 578 579 /** 580 * Returns the ISO 4217 currency code of this currency. 581 * 582 * @return the ISO 4217 currency code of this currency 583 */ 584 @Override toString()585 public String toString() { 586 // Android-changed: Use ICU. 587 // return currencyCode; 588 return icuCurrency.toString(); 589 } 590 591 /** 592 * Resolves instances being deserialized to a single instance per currency. 593 */ readResolve()594 private Object readResolve() { 595 return getInstance(currencyCode); 596 } 597 598 // Android-removed: Use ICU. 599 // Removed a bunch of private helper methods that are unused on Android. 600 } 601