1 /* 2 * Copyright (c) 2000, 2011, 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 java.util; 27 28 import java.io.BufferedInputStream; 29 import java.io.DataInputStream; 30 import java.io.File; 31 import java.io.FileInputStream; 32 import java.io.FileReader; 33 import java.io.IOException; 34 import java.io.Serializable; 35 import java.security.AccessController; 36 import java.security.PrivilegedAction; 37 import java.util.concurrent.ConcurrentHashMap; 38 import java.util.concurrent.ConcurrentMap; 39 import java.util.logging.Level; 40 import java.util.regex.Pattern; 41 import java.util.regex.Matcher; 42 import java.util.spi.CurrencyNameProvider; 43 import java.util.spi.LocaleServiceProvider; 44 import sun.util.LocaleServiceProviderPool; 45 import sun.util.logging.PlatformLogger; 46 import sun.util.resources.LocaleData; 47 import sun.util.resources.OpenListResourceBundle; 48 49 50 /** 51 * Represents a currency. Currencies are identified by their ISO 4217 currency 52 * codes. Visit the <a href="http://www.iso.org/iso/en/prods-services/popstds/currencycodes.html"> 53 * ISO web site</a> for more information, including a table of 54 * currency codes. 55 * <p> 56 * The class is designed so that there's never more than one 57 * <code>Currency</code> instance for any given currency. Therefore, there's 58 * no public constructor. You obtain a <code>Currency</code> instance using 59 * the <code>getInstance</code> methods. 60 * <p> 61 * Users can supersede the Java runtime currency data by creating a properties 62 * file named <code><JAVA_HOME>/lib/currency.properties</code>. The contents 63 * of the properties file are key/value pairs of the ISO 3166 country codes 64 * and the ISO 4217 currency data respectively. The value part consists of 65 * three ISO 4217 values of a currency, i.e., an alphabetic code, a numeric 66 * code, and a minor unit. Those three ISO 4217 values are separated by commas. 67 * The lines which start with '#'s are considered comment lines. For example, 68 * <p> 69 * <code> 70 * #Sample currency properties<br> 71 * JP=JPZ,999,0 72 * </code> 73 * <p> 74 * will supersede the currency data for Japan. 75 * 76 * @since 1.4 77 */ 78 public final class Currency implements Serializable { 79 80 private static final long serialVersionUID = -158308464356906721L; 81 82 /** 83 * ISO 4217 currency code for this currency. 84 * 85 * @serial 86 */ 87 private final String currencyCode; 88 89 /** 90 * Default fraction digits for this currency. 91 * Set from currency data tables. 92 */ 93 transient private final int defaultFractionDigits; 94 95 /** 96 * ISO 4217 numeric code for this currency. 97 * Set from currency data tables. 98 */ 99 transient private final int numericCode; 100 101 102 // class data: instance map 103 104 private static ConcurrentMap<String, Currency> instances = new ConcurrentHashMap<>(7); 105 private static HashSet<Currency> available; 106 107 108 // Class data: currency data obtained from currency.data file. 109 // Purpose: 110 // - determine valid country codes 111 // - determine valid currency codes 112 // - map country codes to currency codes 113 // - obtain default fraction digits for currency codes 114 // 115 // sc = special case; dfd = default fraction digits 116 // Simple countries are those where the country code is a prefix of the 117 // currency code, and there are no known plans to change the currency. 118 // 119 // table formats: 120 // - mainTable: 121 // - maps country code to 32-bit int 122 // - 26*26 entries, corresponding to [A-Z]*[A-Z] 123 // - \u007F -> not valid country 124 // - bits 18-31: unused 125 // - bits 8-17: numeric code (0 to 1023) 126 // - bit 7: 1 - special case, bits 0-4 indicate which one 127 // 0 - simple country, bits 0-4 indicate final char of currency code 128 // - bits 5-6: fraction digits for simple countries, 0 for special cases 129 // - bits 0-4: final char for currency code for simple country, or ID of special case 130 // - special case IDs: 131 // - 0: country has no currency 132 // - other: index into sc* arrays + 1 133 // - scCutOverTimes: cut-over time in millis as returned by 134 // System.currentTimeMillis for special case countries that are changing 135 // currencies; Long.MAX_VALUE for countries that are not changing currencies 136 // - scOldCurrencies: old currencies for special case countries 137 // - scNewCurrencies: new currencies for special case countries that are 138 // changing currencies; null for others 139 // - scOldCurrenciesDFD: default fraction digits for old currencies 140 // - scNewCurrenciesDFD: default fraction digits for new currencies, 0 for 141 // countries that are not changing currencies 142 // - otherCurrencies: concatenation of all currency codes that are not the 143 // main currency of a simple country, separated by "-" 144 // - otherCurrenciesDFD: decimal format digits for currencies in otherCurrencies, same order 145 146 static int formatVersion; 147 static int dataVersion; 148 static int[] mainTable; 149 static long[] scCutOverTimes; 150 static String[] scOldCurrencies; 151 static String[] scNewCurrencies; 152 static int[] scOldCurrenciesDFD; 153 static int[] scNewCurrenciesDFD; 154 static int[] scOldCurrenciesNumericCode; 155 static int[] scNewCurrenciesNumericCode; 156 static String otherCurrencies; 157 static int[] otherCurrenciesDFD; 158 static int[] otherCurrenciesNumericCode; 159 160 // handy constants - must match definitions in GenerateCurrencyData 161 // magic number 162 private static final int MAGIC_NUMBER = 0x43757244; 163 // number of characters from A to Z 164 private static final int A_TO_Z = ('Z' - 'A') + 1; 165 // entry for invalid country codes 166 private static final int INVALID_COUNTRY_ENTRY = 0x007F; 167 // entry for countries without currency 168 private static final int COUNTRY_WITHOUT_CURRENCY_ENTRY = 0x0080; 169 // mask for simple case country entries 170 private static final int SIMPLE_CASE_COUNTRY_MASK = 0x0000; 171 // mask for simple case country entry final character 172 private static final int SIMPLE_CASE_COUNTRY_FINAL_CHAR_MASK = 0x001F; 173 // mask for simple case country entry default currency digits 174 private static final int SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_MASK = 0x0060; 175 // shift count for simple case country entry default currency digits 176 private static final int SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_SHIFT = 5; 177 // mask for special case country entries 178 private static final int SPECIAL_CASE_COUNTRY_MASK = 0x0080; 179 // mask for special case country index 180 private static final int SPECIAL_CASE_COUNTRY_INDEX_MASK = 0x001F; 181 // delta from entry index component in main table to index into special case tables 182 private static final int SPECIAL_CASE_COUNTRY_INDEX_DELTA = 1; 183 // mask for distinguishing simple and special case countries 184 private static final int COUNTRY_TYPE_MASK = SIMPLE_CASE_COUNTRY_MASK | SPECIAL_CASE_COUNTRY_MASK; 185 // mask for the numeric code of the currency 186 private static final int NUMERIC_CODE_MASK = 0x0003FF00; 187 // shift count for the numeric code of the currency 188 private static final int NUMERIC_CODE_SHIFT = 8; 189 190 // Currency data format version 191 private static final int VALID_FORMAT_VERSION = 1; 192 193 static { 194 AccessController.doPrivileged(new PrivilegedAction<Object>() { 195 public Object run() { 196 String homeDir = System.getProperty("java.home"); 197 try { 198 String dataFile = homeDir + File.separator + 199 "lib" + File.separator + "currency.data"; 200 DataInputStream dis = new DataInputStream( 201 new BufferedInputStream( 202 new FileInputStream(dataFile))); 203 if (dis.readInt() != MAGIC_NUMBER) { 204 throw new InternalError("Currency data is possibly corrupted"); 205 } 206 formatVersion = dis.readInt(); 207 if (formatVersion != VALID_FORMAT_VERSION) { 208 throw new InternalError("Currency data format is incorrect"); 209 } 210 dataVersion = dis.readInt(); 211 mainTable = readIntArray(dis, A_TO_Z * A_TO_Z); 212 int scCount = dis.readInt(); 213 scCutOverTimes = readLongArray(dis, scCount); 214 scOldCurrencies = readStringArray(dis, scCount); 215 scNewCurrencies = readStringArray(dis, scCount); 216 scOldCurrenciesDFD = readIntArray(dis, scCount); 217 scNewCurrenciesDFD = readIntArray(dis, scCount); 218 scOldCurrenciesNumericCode = readIntArray(dis, scCount); 219 scNewCurrenciesNumericCode = readIntArray(dis, scCount); 220 int ocCount = dis.readInt(); 221 otherCurrencies = dis.readUTF(); 222 otherCurrenciesDFD = readIntArray(dis, ocCount); 223 otherCurrenciesNumericCode = readIntArray(dis, ocCount); 224 dis.close(); 225 } catch (IOException e) { 226 throw new InternalError(e); 227 } 228 229 // look for the properties file for overrides 230 try { 231 File propFile = new File(homeDir + File.separator + 232 "lib" + File.separator + 233 "currency.properties"); 234 if (propFile.exists()) { 235 Properties props = new Properties(); 236 try (FileReader fr = new FileReader(propFile)) { 237 props.load(fr); 238 } 239 Set<String> keys = props.stringPropertyNames(); 240 Pattern propertiesPattern = 241 Pattern.compile("([A-Z]{3})\\s*,\\s*(\\d{3})\\s*,\\s*([0-3])"); 242 for (String key : keys) { 243 replaceCurrencyData(propertiesPattern, 244 key.toUpperCase(Locale.ROOT), 245 props.getProperty(key).toUpperCase(Locale.ROOT)); 246 } 247 } 248 } catch (IOException e) { 249 info("currency.properties is ignored because of an IOException", e); 250 } 251 return null; 252 } 253 }); 254 } 255 256 /** 257 * Constants for retrieving localized names from the name providers. 258 */ 259 private static final int SYMBOL = 0; 260 private static final int DISPLAYNAME = 1; 261 262 263 /** 264 * Constructs a <code>Currency</code> instance. The constructor is private 265 * so that we can insure that there's never more than one instance for a 266 * given currency. 267 */ 268 private Currency(String currencyCode, int defaultFractionDigits, int numericCode) { 269 this.currencyCode = currencyCode; 270 this.defaultFractionDigits = defaultFractionDigits; 271 this.numericCode = numericCode; 272 } 273 274 /** 275 * Returns the <code>Currency</code> instance for the given currency code. 276 * 277 * @param currencyCode the ISO 4217 code of the currency 278 * @return the <code>Currency</code> instance for the given currency code 279 * @exception NullPointerException if <code>currencyCode</code> is null 280 * @exception IllegalArgumentException if <code>currencyCode</code> is not 281 * a supported ISO 4217 code. 282 */ 283 public static Currency getInstance(String currencyCode) { 284 return getInstance(currencyCode, Integer.MIN_VALUE, 0); 285 } 286 287 private static Currency getInstance(String currencyCode, int defaultFractionDigits, 288 int numericCode) { 289 // Try to look up the currency code in the instances table. 290 // This does the null pointer check as a side effect. 291 // Also, if there already is an entry, the currencyCode must be valid. 292 Currency instance = instances.get(currencyCode); 293 if (instance != null) { 294 return instance; 295 } 296 297 if (defaultFractionDigits == Integer.MIN_VALUE) { 298 // Currency code not internally generated, need to verify first 299 // A currency code must have 3 characters and exist in the main table 300 // or in the list of other currencies. 301 if (currencyCode.length() != 3) { 302 throw new IllegalArgumentException(); 303 } 304 char char1 = currencyCode.charAt(0); 305 char char2 = currencyCode.charAt(1); 306 int tableEntry = getMainTableEntry(char1, char2); 307 if ((tableEntry & COUNTRY_TYPE_MASK) == SIMPLE_CASE_COUNTRY_MASK 308 && tableEntry != INVALID_COUNTRY_ENTRY 309 && currencyCode.charAt(2) - 'A' == (tableEntry & SIMPLE_CASE_COUNTRY_FINAL_CHAR_MASK)) { 310 defaultFractionDigits = (tableEntry & SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_MASK) >> SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_SHIFT; 311 numericCode = (tableEntry & NUMERIC_CODE_MASK) >> NUMERIC_CODE_SHIFT; 312 } else { 313 // Check for '-' separately so we don't get false hits in the table. 314 if (currencyCode.charAt(2) == '-') { 315 throw new IllegalArgumentException(); 316 } 317 int index = otherCurrencies.indexOf(currencyCode); 318 if (index == -1) { 319 throw new IllegalArgumentException(); 320 } 321 defaultFractionDigits = otherCurrenciesDFD[index / 4]; 322 numericCode = otherCurrenciesNumericCode[index / 4]; 323 } 324 } 325 326 Currency currencyVal = 327 new Currency(currencyCode, defaultFractionDigits, numericCode); 328 instance = instances.putIfAbsent(currencyCode, currencyVal); 329 return (instance != null ? instance : currencyVal); 330 } 331 332 /** 333 * Returns the <code>Currency</code> instance for the country of the 334 * given locale. The language and variant components of the locale 335 * are ignored. The result may vary over time, as countries change their 336 * currencies. For example, for the original member countries of the 337 * European Monetary Union, the method returns the old national currencies 338 * until December 31, 2001, and the Euro from January 1, 2002, local time 339 * of the respective countries. 340 * <p> 341 * The method returns <code>null</code> for territories that don't 342 * have a currency, such as Antarctica. 343 * 344 * @param locale the locale for whose country a <code>Currency</code> 345 * instance is needed 346 * @return the <code>Currency</code> instance for the country of the given 347 * locale, or null 348 * @exception NullPointerException if <code>locale</code> or its country 349 * code is null 350 * @exception IllegalArgumentException if the country of the given locale 351 * is not a supported ISO 3166 country code. 352 */ 353 public static Currency getInstance(Locale locale) { 354 String country = locale.getCountry(); 355 if (country == null) { 356 throw new NullPointerException(); 357 } 358 359 if (country.length() != 2) { 360 throw new IllegalArgumentException(); 361 } 362 363 char char1 = country.charAt(0); 364 char char2 = country.charAt(1); 365 int tableEntry = getMainTableEntry(char1, char2); 366 if ((tableEntry & COUNTRY_TYPE_MASK) == SIMPLE_CASE_COUNTRY_MASK 367 && tableEntry != INVALID_COUNTRY_ENTRY) { 368 char finalChar = (char) ((tableEntry & SIMPLE_CASE_COUNTRY_FINAL_CHAR_MASK) + 'A'); 369 int defaultFractionDigits = (tableEntry & SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_MASK) >> SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_SHIFT; 370 int numericCode = (tableEntry & NUMERIC_CODE_MASK) >> NUMERIC_CODE_SHIFT; 371 StringBuffer sb = new StringBuffer(country); 372 sb.append(finalChar); 373 return getInstance(sb.toString(), defaultFractionDigits, numericCode); 374 } else { 375 // special cases 376 if (tableEntry == INVALID_COUNTRY_ENTRY) { 377 throw new IllegalArgumentException(); 378 } 379 if (tableEntry == COUNTRY_WITHOUT_CURRENCY_ENTRY) { 380 return null; 381 } else { 382 int index = (tableEntry & SPECIAL_CASE_COUNTRY_INDEX_MASK) - SPECIAL_CASE_COUNTRY_INDEX_DELTA; 383 if (scCutOverTimes[index] == Long.MAX_VALUE || System.currentTimeMillis() < scCutOverTimes[index]) { 384 return getInstance(scOldCurrencies[index], scOldCurrenciesDFD[index], 385 scOldCurrenciesNumericCode[index]); 386 } else { 387 return getInstance(scNewCurrencies[index], scNewCurrenciesDFD[index], 388 scNewCurrenciesNumericCode[index]); 389 } 390 } 391 } 392 } 393 394 /** 395 * Gets the set of available currencies. The returned set of currencies 396 * contains all of the available currencies, which may include currencies 397 * that represent obsolete ISO 4217 codes. The set can be modified 398 * without affecting the available currencies in the runtime. 399 * 400 * @return the set of available currencies. If there is no currency 401 * available in the runtime, the returned set is empty. 402 * @since 1.7 403 */ 404 public static Set<Currency> getAvailableCurrencies() { 405 synchronized(Currency.class) { 406 if (available == null) { 407 available = new HashSet<>(256); 408 409 // Add simple currencies first 410 for (char c1 = 'A'; c1 <= 'Z'; c1 ++) { 411 for (char c2 = 'A'; c2 <= 'Z'; c2 ++) { 412 int tableEntry = getMainTableEntry(c1, c2); 413 if ((tableEntry & COUNTRY_TYPE_MASK) == SIMPLE_CASE_COUNTRY_MASK 414 && tableEntry != INVALID_COUNTRY_ENTRY) { 415 char finalChar = (char) ((tableEntry & SIMPLE_CASE_COUNTRY_FINAL_CHAR_MASK) + 'A'); 416 int defaultFractionDigits = (tableEntry & SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_MASK) >> SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_SHIFT; 417 int numericCode = (tableEntry & NUMERIC_CODE_MASK) >> NUMERIC_CODE_SHIFT; 418 StringBuilder sb = new StringBuilder(); 419 sb.append(c1); 420 sb.append(c2); 421 sb.append(finalChar); 422 available.add(getInstance(sb.toString(), defaultFractionDigits, numericCode)); 423 } 424 } 425 } 426 427 // Now add other currencies 428 StringTokenizer st = new StringTokenizer(otherCurrencies, "-"); 429 while (st.hasMoreElements()) { 430 available.add(getInstance((String)st.nextElement())); 431 } 432 } 433 } 434 435 @SuppressWarnings("unchecked") 436 Set<Currency> result = (Set<Currency>) available.clone(); 437 return result; 438 } 439 440 /** 441 * Gets the ISO 4217 currency code of this currency. 442 * 443 * @return the ISO 4217 currency code of this currency. 444 */ 445 public String getCurrencyCode() { 446 return currencyCode; 447 } 448 449 /** 450 * Gets the symbol of this currency for the default locale. 451 * For example, for the US Dollar, the symbol is "$" if the default 452 * locale is the US, while for other locales it may be "US$". If no 453 * symbol can be determined, the ISO 4217 currency code is returned. 454 * 455 * @return the symbol of this currency for the default locale 456 */ 457 public String getSymbol() { 458 return getSymbol(Locale.getDefault(Locale.Category.DISPLAY)); 459 } 460 461 /** 462 * Gets the symbol of this currency for the specified locale. 463 * For example, for the US Dollar, the symbol is "$" if the specified 464 * locale is the US, while for other locales it may be "US$". If no 465 * symbol can be determined, the ISO 4217 currency code is returned. 466 * 467 * @param locale the locale for which a display name for this currency is 468 * needed 469 * @return the symbol of this currency for the specified locale 470 * @exception NullPointerException if <code>locale</code> is null 471 */ 472 public String getSymbol(Locale locale) { 473 try { 474 // Check whether a provider can provide an implementation that's closer 475 // to the requested locale than what the Java runtime itself can provide. 476 LocaleServiceProviderPool pool = 477 LocaleServiceProviderPool.getPool(CurrencyNameProvider.class); 478 479 if (pool.hasProviders()) { 480 // Assuming that all the country locales include necessary currency 481 // symbols in the Java runtime's resources, so there is no need to 482 // examine whether Java runtime's currency resource bundle is missing 483 // names. Therefore, no resource bundle is provided for calling this 484 // method. 485 String symbol = pool.getLocalizedObject( 486 CurrencyNameGetter.INSTANCE, 487 locale, (OpenListResourceBundle)null, 488 currencyCode, SYMBOL); 489 if (symbol != null) { 490 return symbol; 491 } 492 } 493 494 ResourceBundle bundle = LocaleData.getCurrencyNames(locale); 495 return bundle.getString(currencyCode); 496 } catch (MissingResourceException e) { 497 // use currency code as symbol of last resort 498 return currencyCode; 499 } 500 } 501 502 /** 503 * Gets the default number of fraction digits used with this currency. 504 * For example, the default number of fraction digits for the Euro is 2, 505 * while for the Japanese Yen it's 0. 506 * In the case of pseudo-currencies, such as IMF Special Drawing Rights, 507 * -1 is returned. 508 * 509 * @return the default number of fraction digits used with this currency 510 */ 511 public int getDefaultFractionDigits() { 512 return defaultFractionDigits; 513 } 514 515 /** 516 * Returns the ISO 4217 numeric code of this currency. 517 * 518 * @return the ISO 4217 numeric code of this currency 519 * @since 1.7 520 */ 521 public int getNumericCode() { 522 return numericCode; 523 } 524 525 /** 526 * Gets the name that is suitable for displaying this currency for 527 * the default locale. If there is no suitable display name found 528 * for the default locale, the ISO 4217 currency code is returned. 529 * 530 * @return the display name of this currency for the default locale 531 * @since 1.7 532 */ 533 public String getDisplayName() { 534 return getDisplayName(Locale.getDefault(Locale.Category.DISPLAY)); 535 } 536 537 /** 538 * Gets the name that is suitable for displaying this currency for 539 * the specified locale. If there is no suitable display name found 540 * for the specified locale, the ISO 4217 currency code is returned. 541 * 542 * @param locale the locale for which a display name for this currency is 543 * needed 544 * @return the display name of this currency for the specified locale 545 * @exception NullPointerException if <code>locale</code> is null 546 * @since 1.7 547 */ 548 public String getDisplayName(Locale locale) { 549 try { 550 OpenListResourceBundle bundle = LocaleData.getCurrencyNames(locale); 551 String result = null; 552 String bundleKey = currencyCode.toLowerCase(Locale.ROOT); 553 554 // Check whether a provider can provide an implementation that's closer 555 // to the requested locale than what the Java runtime itself can provide. 556 LocaleServiceProviderPool pool = 557 LocaleServiceProviderPool.getPool(CurrencyNameProvider.class); 558 if (pool.hasProviders()) { 559 result = pool.getLocalizedObject( 560 CurrencyNameGetter.INSTANCE, 561 locale, bundleKey, bundle, currencyCode, DISPLAYNAME); 562 } 563 564 if (result == null) { 565 result = bundle.getString(bundleKey); 566 } 567 568 if (result != null) { 569 return result; 570 } 571 } catch (MissingResourceException e) { 572 // fall through 573 } 574 575 // use currency code as symbol of last resort 576 return currencyCode; 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 public String toString() { 585 return currencyCode; 586 } 587 588 /** 589 * Resolves instances being deserialized to a single instance per currency. 590 */ 591 private Object readResolve() { 592 return getInstance(currencyCode); 593 } 594 595 /** 596 * Gets the main table entry for the country whose country code consists 597 * of char1 and char2. 598 */ 599 private static int getMainTableEntry(char char1, char char2) { 600 if (char1 < 'A' || char1 > 'Z' || char2 < 'A' || char2 > 'Z') { 601 throw new IllegalArgumentException(); 602 } 603 return mainTable[(char1 - 'A') * A_TO_Z + (char2 - 'A')]; 604 } 605 606 /** 607 * Sets the main table entry for the country whose country code consists 608 * of char1 and char2. 609 */ 610 private static void setMainTableEntry(char char1, char char2, int entry) { 611 if (char1 < 'A' || char1 > 'Z' || char2 < 'A' || char2 > 'Z') { 612 throw new IllegalArgumentException(); 613 } 614 mainTable[(char1 - 'A') * A_TO_Z + (char2 - 'A')] = entry; 615 } 616 617 /** 618 * Obtains a localized currency names from a CurrencyNameProvider 619 * implementation. 620 */ 621 private static class CurrencyNameGetter 622 implements LocaleServiceProviderPool.LocalizedObjectGetter<CurrencyNameProvider, 623 String> { 624 private static final CurrencyNameGetter INSTANCE = new CurrencyNameGetter(); 625 626 public String getObject(CurrencyNameProvider currencyNameProvider, 627 Locale locale, 628 String key, 629 Object... params) { 630 assert params.length == 1; 631 int type = (Integer)params[0]; 632 633 switch(type) { 634 case SYMBOL: 635 return currencyNameProvider.getSymbol(key, locale); 636 case DISPLAYNAME: 637 return currencyNameProvider.getDisplayName(key, locale); 638 default: 639 assert false; // shouldn't happen 640 } 641 642 return null; 643 } 644 } 645 646 private static int[] readIntArray(DataInputStream dis, int count) throws IOException { 647 int[] ret = new int[count]; 648 for (int i = 0; i < count; i++) { 649 ret[i] = dis.readInt(); 650 } 651 652 return ret; 653 } 654 655 private static long[] readLongArray(DataInputStream dis, int count) throws IOException { 656 long[] ret = new long[count]; 657 for (int i = 0; i < count; i++) { 658 ret[i] = dis.readLong(); 659 } 660 661 return ret; 662 } 663 664 private static String[] readStringArray(DataInputStream dis, int count) throws IOException { 665 String[] ret = new String[count]; 666 for (int i = 0; i < count; i++) { 667 ret[i] = dis.readUTF(); 668 } 669 670 return ret; 671 } 672 673 /** 674 * Replaces currency data found in the currencydata.properties file 675 * 676 * @param pattern regex pattern for the properties 677 * @param ctry country code 678 * @param data currency data. This is a comma separated string that 679 * consists of "three-letter alphabet code", "three-digit numeric code", 680 * and "one-digit (0,1,2, or 3) default fraction digit". 681 * For example, "JPZ,392,0". 682 * @throws 683 */ 684 private static void replaceCurrencyData(Pattern pattern, String ctry, String curdata) { 685 686 if (ctry.length() != 2) { 687 // ignore invalid country code 688 String message = new StringBuilder() 689 .append("The entry in currency.properties for ") 690 .append(ctry).append(" is ignored because of the invalid country code.") 691 .toString(); 692 info(message, null); 693 return; 694 } 695 696 Matcher m = pattern.matcher(curdata); 697 if (!m.find()) { 698 // format is not recognized. ignore the data 699 String message = new StringBuilder() 700 .append("The entry in currency.properties for ") 701 .append(ctry) 702 .append(" is ignored because the value format is not recognized.") 703 .toString(); 704 info(message, null); 705 return; 706 } 707 708 String code = m.group(1); 709 int numeric = Integer.parseInt(m.group(2)); 710 int fraction = Integer.parseInt(m.group(3)); 711 int entry = numeric << NUMERIC_CODE_SHIFT; 712 713 int index; 714 for (index = 0; index < scOldCurrencies.length; index++) { 715 if (scOldCurrencies[index].equals(code)) { 716 break; 717 } 718 } 719 720 if (index == scOldCurrencies.length) { 721 // simple case 722 entry |= (fraction << SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_SHIFT) | 723 (code.charAt(2) - 'A'); 724 } else { 725 // special case 726 entry |= SPECIAL_CASE_COUNTRY_MASK | 727 (index + SPECIAL_CASE_COUNTRY_INDEX_DELTA); 728 } 729 setMainTableEntry(ctry.charAt(0), ctry.charAt(1), entry); 730 } 731 732 private static void info(String message, Throwable t) { 733 PlatformLogger logger = PlatformLogger.getLogger("java.util.Currency"); 734 if (logger.isLoggable(PlatformLogger.INFO)) { 735 if (t != null) { 736 logger.info(message, t); 737 } else { 738 logger.info(message); 739 } 740 } 741 } 742 }