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