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