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