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