src/java.base/share/classes/java/util/Currency.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 58288 48e480e56aad
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 53120
diff changeset
     2
 * Copyright (c) 2000, 2019, 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.FileReader;
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    32
import java.io.InputStream;
2
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;
48929
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
    44
import java.util.stream.Collectors;
50817
fa1e04811ff6 8066709: Make some JDK system properties read only
rriggs
parents: 48929
diff changeset
    45
fa1e04811ff6 8066709: Make some JDK system properties read only
rriggs
parents: 48929
diff changeset
    46
import jdk.internal.util.StaticProperty;
48251
57148c79bd75 8176841: Additional Unicode Language-Tag Extensions
naoto
parents: 47216
diff changeset
    47
import sun.util.locale.provider.CalendarDataUtility;
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12448
diff changeset
    48
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
    49
import sun.util.logging.PlatformLogger;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * Represents a currency. Currencies are identified by their ISO 4217 currency
17156
c42df24102c1 8011946: java.util.Currency javadoc has broken link to iso.org
dxu
parents: 16010
diff changeset
    54
 * codes. Visit the <a href="http://www.iso.org/iso/home/standards/currency_codes.htm">
c42df24102c1 8011946: java.util.Currency javadoc has broken link to iso.org
dxu
parents: 16010
diff changeset
    55
 * ISO web site</a> for more information.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * The class is designed so that there's never more than one
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    58
 * {@code Currency} instance for any given currency. Therefore, there's
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    59
 * no public constructor. You obtain a {@code Currency} instance using
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    60
 * the {@code getInstance} methods.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <p>
14697
6ed46ffc2d33 8003846: Override mechanism for currency data should not require creating currency.properties in java.home
alanb
parents: 14014
diff changeset
    62
 * Users can supersede the Java runtime currency data by means of the system
53120
de9fd809bb47 8214567: Use {@systemProperty} for definitions of system properties
rpatil
parents: 50817
diff changeset
    63
 * property {@systemProperty java.util.currency.data}. If this system property is
14697
6ed46ffc2d33 8003846: Override mechanism for currency data should not require creating currency.properties in java.home
alanb
parents: 14014
diff changeset
    64
 * defined then its value is the location of a properties file, the contents of
6ed46ffc2d33 8003846: Override mechanism for currency data should not require creating currency.properties in java.home
alanb
parents: 14014
diff changeset
    65
 * which are key/value pairs of the ISO 3166 country codes and the ISO 4217
6ed46ffc2d33 8003846: Override mechanism for currency data should not require creating currency.properties in java.home
alanb
parents: 14014
diff changeset
    66
 * currency data respectively.  The value part consists of three ISO 4217 values
6ed46ffc2d33 8003846: Override mechanism for currency data should not require creating currency.properties in java.home
alanb
parents: 14014
diff changeset
    67
 * of a currency, i.e., an alphabetic code, a numeric code, and a minor unit.
6ed46ffc2d33 8003846: Override mechanism for currency data should not require creating currency.properties in java.home
alanb
parents: 14014
diff changeset
    68
 * 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
    69
 * 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
    70
 * 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
    71
 * 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
    72
 * 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
    73
 * 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
    74
 * 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
    75
 * 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
    76
 * {@code 'yyyy-MM-dd'T'HH:mm:ss'}. For example,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * <code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * #Sample currency properties<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * JP=JPZ,999,0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * </code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * <p>
48929
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
    83
 * will supersede the currency data for Japan. If JPZ is one of the existing
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
    84
 * ISO 4217 currency code referred by other countries, the existing
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
    85
 * JPZ currency data is updated with the given numeric code and minor
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
    86
 * unit value.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *
13790
5b29e3921008 7180362: RFE: Implement date cutover functionality for currency.properties file
coffeys
parents: 13583
diff changeset
    88
 * <p>
5b29e3921008 7180362: RFE: Implement date cutover functionality for currency.properties file
coffeys
parents: 13583
diff changeset
    89
 * <code>
5b29e3921008 7180362: RFE: Implement date cutover functionality for currency.properties file
coffeys
parents: 13583
diff changeset
    90
 * #Sample currency properties with cutover date<br>
5b29e3921008 7180362: RFE: Implement date cutover functionality for currency.properties file
coffeys
parents: 13583
diff changeset
    91
 * 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
    92
 * </code>
5b29e3921008 7180362: RFE: Implement date cutover functionality for currency.properties file
coffeys
parents: 13583
diff changeset
    93
 * <p>
5b29e3921008 7180362: RFE: Implement date cutover functionality for currency.properties file
coffeys
parents: 13583
diff changeset
    94
 * 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
    95
 * 1st January 2014 00:00:00 GMT.
5b29e3921008 7180362: RFE: Implement date cutover functionality for currency.properties file
coffeys
parents: 13583
diff changeset
    96
 * <p>
5b29e3921008 7180362: RFE: Implement date cutover functionality for currency.properties file
coffeys
parents: 13583
diff changeset
    97
 * 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
    98
 * 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
    99
 * 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
   100
 * {@code Currency} is undefined and the remainder of entries in file are processed.
36113
cd5af2894aee 8074411: Describe "minor unit" and/or "default fraction digits" in Currency class' javadoc clearly
peytoia
parents: 32930
diff changeset
   101
 * <p>
48929
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   102
 * If multiple property entries with same currency code but different numeric code
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   103
 * and/or minor unit are encountered, those entries are ignored and the remainder
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   104
 * of entries in file are processed.
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   105
 *
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   106
 * <p>
36113
cd5af2894aee 8074411: Describe "minor unit" and/or "default fraction digits" in Currency class' javadoc clearly
peytoia
parents: 32930
diff changeset
   107
 * It is recommended to use {@link java.math.BigDecimal} class while dealing
cd5af2894aee 8074411: Describe "minor unit" and/or "default fraction digits" in Currency class' javadoc clearly
peytoia
parents: 32930
diff changeset
   108
 * with {@code Currency} or monetary values as it provides better handling of floating
cd5af2894aee 8074411: Describe "minor unit" and/or "default fraction digits" in Currency class' javadoc clearly
peytoia
parents: 32930
diff changeset
   109
 * point numbers and their operations.
13790
5b29e3921008 7180362: RFE: Implement date cutover functionality for currency.properties file
coffeys
parents: 13583
diff changeset
   110
 *
36113
cd5af2894aee 8074411: Describe "minor unit" and/or "default fraction digits" in Currency class' javadoc clearly
peytoia
parents: 32930
diff changeset
   111
 * @see java.math.BigDecimal
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
public final class Currency implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 53120
diff changeset
   116
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    private static final long serialVersionUID = -158308464356906721L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * ISO 4217 currency code for this currency.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    private final String currencyCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * Default fraction digits for this currency.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * Set from currency data tables.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 29986
diff changeset
   130
    private final transient int defaultFractionDigits;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * ISO 4217 numeric code for this currency.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * Set from currency data tables.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 29986
diff changeset
   136
    private final transient int numericCode;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    // class data: instance map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
12202
2a63f5718d33 7145454: JVM wide monitor lock in Currency.getInstance(String)
naoto
parents: 11138
diff changeset
   141
    private static ConcurrentMap<String, Currency> instances = new ConcurrentHashMap<>(7);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    private static HashSet<Currency> available;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    // Class data: currency data obtained from currency.data file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    // Purpose:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    // - determine valid country codes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    // - determine valid currency codes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    // - map country codes to currency codes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    // - obtain default fraction digits for currency codes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    // sc = special case; dfd = default fraction digits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    // Simple countries are those where the country code is a prefix of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    // currency code, and there are no known plans to change the currency.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    // table formats:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    // - mainTable:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    //   - maps country code to 32-bit int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    //   - 26*26 entries, corresponding to [A-Z]*[A-Z]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    //   - \u007F -> not valid country
29524
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 27565
diff changeset
   160
    //   - bits 20-31: unused
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 27565
diff changeset
   161
    //   - bits 10-19: numeric code (0 to 1023)
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 27565
diff changeset
   162
    //   - bit 9: 1 - special case, bits 0-4 indicate which one
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    //            0 - simple country, bits 0-4 indicate final char of currency code
29524
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 27565
diff changeset
   164
    //   - bits 5-8: fraction digits for simple countries, 0 for special cases
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    //   - bits 0-4: final char for currency code for simple country, or ID of special case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    // - special case IDs:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    //   - 0: country has no currency
38440
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   168
    //   - other: index into specialCasesList
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    static int formatVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    static int dataVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    static int[] mainTable;
38440
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   173
    static List<SpecialCaseEntry> specialCasesList;
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   174
    static List<OtherCurrencyEntry> otherCurrenciesList;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    // handy constants - must match definitions in GenerateCurrencyData
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    // magic number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    private static final int MAGIC_NUMBER = 0x43757244;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    // number of characters from A to Z
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    private static final int A_TO_Z = ('Z' - 'A') + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    // entry for invalid country codes
29524
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 27565
diff changeset
   182
    private static final int INVALID_COUNTRY_ENTRY = 0x0000007F;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    // entry for countries without currency
29524
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 27565
diff changeset
   184
    private static final int COUNTRY_WITHOUT_CURRENCY_ENTRY = 0x00000200;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    // mask for simple case country entries
29524
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 27565
diff changeset
   186
    private static final int SIMPLE_CASE_COUNTRY_MASK = 0x00000000;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    // mask for simple case country entry final character
29524
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 27565
diff changeset
   188
    private static final int SIMPLE_CASE_COUNTRY_FINAL_CHAR_MASK = 0x0000001F;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    // mask for simple case country entry default currency digits
29524
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 27565
diff changeset
   190
    private static final int SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_MASK = 0x000001E0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    // shift count 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_SHIFT = 5;
29524
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 27565
diff changeset
   193
    // maximum number for simple case country entry default currency digits
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 27565
diff changeset
   194
    private static final int SIMPLE_CASE_COUNTRY_MAX_DEFAULT_DIGITS = 9;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    // mask for special case country entries
29524
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 27565
diff changeset
   196
    private static final int SPECIAL_CASE_COUNTRY_MASK = 0x00000200;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    // mask for special case country index
29524
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 27565
diff changeset
   198
    private static final int SPECIAL_CASE_COUNTRY_INDEX_MASK = 0x0000001F;
2
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
29524
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 27565
diff changeset
   204
    private static final int NUMERIC_CODE_MASK = 0x000FFC00;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    // shift count for the numeric code of the currency
29524
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 27565
diff changeset
   206
    private static final int NUMERIC_CODE_SHIFT = 10;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    // Currency data format version
38440
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   209
    private static final int VALID_FORMAT_VERSION = 3;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    static {
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 29524
diff changeset
   212
        AccessController.doPrivileged(new PrivilegedAction<>() {
13583
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
                try {
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   216
                    try (InputStream in = getClass().getResourceAsStream("/java/util/currency.data")) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   217
                        if (in == null) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   218
                            throw new InternalError("Currency data not found");
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   219
                        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   220
                        DataInputStream dis = new DataInputStream(new BufferedInputStream(in));
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12448
diff changeset
   221
                        if (dis.readInt() != MAGIC_NUMBER) {
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12448
diff changeset
   222
                            throw new InternalError("Currency data is possibly corrupted");
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12448
diff changeset
   223
                        }
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12448
diff changeset
   224
                        formatVersion = dis.readInt();
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12448
diff changeset
   225
                        if (formatVersion != VALID_FORMAT_VERSION) {
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12448
diff changeset
   226
                            throw new InternalError("Currency data format is incorrect");
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12448
diff changeset
   227
                        }
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12448
diff changeset
   228
                        dataVersion = dis.readInt();
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12448
diff changeset
   229
                        mainTable = readIntArray(dis, A_TO_Z * A_TO_Z);
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12448
diff changeset
   230
                        int scCount = dis.readInt();
38440
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   231
                        specialCasesList = readSpecialCases(dis, scCount);
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12448
diff changeset
   232
                        int ocCount = dis.readInt();
38440
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   233
                        otherCurrenciesList = readOtherCurrencies(dis, ocCount);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                } catch (IOException e) {
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 9035
diff changeset
   236
                    throw new InternalError(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                // look for the properties file for overrides
14697
6ed46ffc2d33 8003846: Override mechanism for currency data should not require creating currency.properties in java.home
alanb
parents: 14014
diff changeset
   240
                String propsFile = System.getProperty("java.util.currency.data");
6ed46ffc2d33 8003846: Override mechanism for currency data should not require creating currency.properties in java.home
alanb
parents: 14014
diff changeset
   241
                if (propsFile == null) {
50817
fa1e04811ff6 8066709: Make some JDK system properties read only
rriggs
parents: 48929
diff changeset
   242
                    propsFile = StaticProperty.javaHome() + File.separator + "lib" +
14697
6ed46ffc2d33 8003846: Override mechanism for currency data should not require creating currency.properties in java.home
alanb
parents: 14014
diff changeset
   243
                        File.separator + "currency.properties";
6ed46ffc2d33 8003846: Override mechanism for currency data should not require creating currency.properties in java.home
alanb
parents: 14014
diff changeset
   244
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                try {
14697
6ed46ffc2d33 8003846: Override mechanism for currency data should not require creating currency.properties in java.home
alanb
parents: 14014
diff changeset
   246
                    File propFile = new File(propsFile);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                    if (propFile.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                        Properties props = new Properties();
8543
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 7668
diff changeset
   249
                        try (FileReader fr = new FileReader(propFile)) {
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 7668
diff changeset
   250
                            props.load(fr);
e5ec12a932da 7021209: convert lang, math, util to use try-with-resources
smarks
parents: 7668
diff changeset
   251
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                        Pattern propertiesPattern =
48929
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   253
                                Pattern.compile("([A-Z]{3})\\s*,\\s*(\\d{3})\\s*,\\s*" +
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   254
                                        "(\\d+)\\s*,?\\s*(\\d{4}-\\d{2}-\\d{2}T\\d{2}:" +
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   255
                                        "\\d{2}:\\d{2})?");
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   256
                        List<CurrencyProperty> currencyEntries
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   257
                                = getValidCurrencyData(props, propertiesPattern);
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   258
                        currencyEntries.forEach(Currency::replaceCurrencyData);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                } catch (IOException e) {
48929
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   261
                    CurrencyProperty.info("currency.properties is ignored"
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   262
                            + " because of an IOException", e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * Constants for retrieving localized names from the name providers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    private static final int SYMBOL = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    private static final int DISPLAYNAME = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   277
     * Constructs a {@code Currency} instance. The constructor is private
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * so that we can insure that there's never more than one instance for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * given currency.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    private Currency(String currencyCode, int defaultFractionDigits, int numericCode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        this.currencyCode = currencyCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        this.defaultFractionDigits = defaultFractionDigits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        this.numericCode = numericCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   288
     * Returns the {@code Currency} instance for the given currency code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * @param currencyCode the ISO 4217 code of the currency
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   291
     * @return the {@code Currency} instance for the given currency code
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   292
     * @throws    NullPointerException if {@code currencyCode} is null
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   293
     * @throws    IllegalArgumentException if {@code currencyCode} is not
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * a supported ISO 4217 code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    public static Currency getInstance(String currencyCode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        return getInstance(currencyCode, Integer.MIN_VALUE, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    private static Currency getInstance(String currencyCode, int defaultFractionDigits,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        int numericCode) {
12297
d12a3f2860eb 7156459: Remove unnecessary get() from Currency.getInstance()
naoto
parents: 12202
diff changeset
   302
        // Try to look up the currency code in the instances table.
d12a3f2860eb 7156459: Remove unnecessary get() from Currency.getInstance()
naoto
parents: 12202
diff changeset
   303
        // This does the null pointer check as a side effect.
d12a3f2860eb 7156459: Remove unnecessary get() from Currency.getInstance()
naoto
parents: 12202
diff changeset
   304
        // 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
   305
        Currency instance = instances.get(currencyCode);
d12a3f2860eb 7156459: Remove unnecessary get() from Currency.getInstance()
naoto
parents: 12202
diff changeset
   306
        if (instance != null) {
d12a3f2860eb 7156459: Remove unnecessary get() from Currency.getInstance()
naoto
parents: 12202
diff changeset
   307
            return instance;
d12a3f2860eb 7156459: Remove unnecessary get() from Currency.getInstance()
naoto
parents: 12202
diff changeset
   308
        }
d12a3f2860eb 7156459: Remove unnecessary get() from Currency.getInstance()
naoto
parents: 12202
diff changeset
   309
d12a3f2860eb 7156459: Remove unnecessary get() from Currency.getInstance()
naoto
parents: 12202
diff changeset
   310
        if (defaultFractionDigits == Integer.MIN_VALUE) {
d12a3f2860eb 7156459: Remove unnecessary get() from Currency.getInstance()
naoto
parents: 12202
diff changeset
   311
            // Currency code not internally generated, need to verify first
d12a3f2860eb 7156459: Remove unnecessary get() from Currency.getInstance()
naoto
parents: 12202
diff changeset
   312
            // 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
   313
            // or in the list of other currencies.
38440
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   314
            boolean found = false;
12297
d12a3f2860eb 7156459: Remove unnecessary get() from Currency.getInstance()
naoto
parents: 12202
diff changeset
   315
            if (currencyCode.length() != 3) {
d12a3f2860eb 7156459: Remove unnecessary get() from Currency.getInstance()
naoto
parents: 12202
diff changeset
   316
                throw new IllegalArgumentException();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            }
12297
d12a3f2860eb 7156459: Remove unnecessary get() from Currency.getInstance()
naoto
parents: 12202
diff changeset
   318
            char char1 = currencyCode.charAt(0);
d12a3f2860eb 7156459: Remove unnecessary get() from Currency.getInstance()
naoto
parents: 12202
diff changeset
   319
            char char2 = currencyCode.charAt(1);
d12a3f2860eb 7156459: Remove unnecessary get() from Currency.getInstance()
naoto
parents: 12202
diff changeset
   320
            int tableEntry = getMainTableEntry(char1, char2);
d12a3f2860eb 7156459: Remove unnecessary get() from Currency.getInstance()
naoto
parents: 12202
diff changeset
   321
            if ((tableEntry & COUNTRY_TYPE_MASK) == SIMPLE_CASE_COUNTRY_MASK
d12a3f2860eb 7156459: Remove unnecessary get() from Currency.getInstance()
naoto
parents: 12202
diff changeset
   322
                    && tableEntry != INVALID_COUNTRY_ENTRY
d12a3f2860eb 7156459: Remove unnecessary get() from Currency.getInstance()
naoto
parents: 12202
diff changeset
   323
                    && currencyCode.charAt(2) - 'A' == (tableEntry & SIMPLE_CASE_COUNTRY_FINAL_CHAR_MASK)) {
d12a3f2860eb 7156459: Remove unnecessary get() from Currency.getInstance()
naoto
parents: 12202
diff changeset
   324
                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
   325
                numericCode = (tableEntry & NUMERIC_CODE_MASK) >> NUMERIC_CODE_SHIFT;
38440
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   326
                found = true;
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   327
            } else { //special case
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   328
                int[] fractionAndNumericCode = SpecialCaseEntry.findEntry(currencyCode);
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   329
                if (fractionAndNumericCode != null) {
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   330
                    defaultFractionDigits = fractionAndNumericCode[0];
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   331
                    numericCode = fractionAndNumericCode[1];
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   332
                    found = true;
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   333
                }
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   334
            }
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   335
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   336
            if (!found) {
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   337
                OtherCurrencyEntry ocEntry = OtherCurrencyEntry.findEntry(currencyCode);
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   338
                if (ocEntry == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                    throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                }
38440
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   341
                defaultFractionDigits = ocEntry.fraction;
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   342
                numericCode = ocEntry.numericCode;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            }
12297
d12a3f2860eb 7156459: Remove unnecessary get() from Currency.getInstance()
naoto
parents: 12202
diff changeset
   344
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
12297
d12a3f2860eb 7156459: Remove unnecessary get() from Currency.getInstance()
naoto
parents: 12202
diff changeset
   346
        Currency currencyVal =
d12a3f2860eb 7156459: Remove unnecessary get() from Currency.getInstance()
naoto
parents: 12202
diff changeset
   347
            new Currency(currencyCode, defaultFractionDigits, numericCode);
d12a3f2860eb 7156459: Remove unnecessary get() from Currency.getInstance()
naoto
parents: 12202
diff changeset
   348
        instance = instances.putIfAbsent(currencyCode, currencyVal);
d12a3f2860eb 7156459: Remove unnecessary get() from Currency.getInstance()
naoto
parents: 12202
diff changeset
   349
        return (instance != null ? instance : currencyVal);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   353
     * Returns the {@code Currency} instance for the country of the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * given locale. The language and variant components of the locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * are ignored. The result may vary over time, as countries change their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * currencies. For example, for the original member countries of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * European Monetary Union, the method returns the old national currencies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * until December 31, 2001, and the Euro from January 1, 2002, local time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * of the respective countries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * <p>
48251
57148c79bd75 8176841: Additional Unicode Language-Tag Extensions
naoto
parents: 47216
diff changeset
   361
     * If the specified {@code locale} contains "cu" and/or "rg"
57148c79bd75 8176841: Additional Unicode Language-Tag Extensions
naoto
parents: 47216
diff changeset
   362
     * <a href="./Locale.html#def_locale_extension">Unicode extensions</a>,
57148c79bd75 8176841: Additional Unicode Language-Tag Extensions
naoto
parents: 47216
diff changeset
   363
     * the instance returned from this method reflects
57148c79bd75 8176841: Additional Unicode Language-Tag Extensions
naoto
parents: 47216
diff changeset
   364
     * the values specified with those extensions. If both "cu" and "rg" are
57148c79bd75 8176841: Additional Unicode Language-Tag Extensions
naoto
parents: 47216
diff changeset
   365
     * specified, the currency from the "cu" extension supersedes the implicit one
57148c79bd75 8176841: Additional Unicode Language-Tag Extensions
naoto
parents: 47216
diff changeset
   366
     * from the "rg" extension.
57148c79bd75 8176841: Additional Unicode Language-Tag Extensions
naoto
parents: 47216
diff changeset
   367
     * <p>
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   368
     * The method returns {@code null} for territories that don't
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * have a currency, such as Antarctica.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   371
     * @param locale the locale for whose country a {@code Currency}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * instance is needed
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   373
     * @return the {@code Currency} instance for the country of the given
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12448
diff changeset
   374
     * locale, or {@code null}
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   375
     * @throws    NullPointerException if {@code locale}
32930
7616af8ec145 8075315: API doc of Currency.getInstance(Locale) method is incorrect.
naoto
parents: 32649
diff changeset
   376
     * is {@code null}
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 57956
diff changeset
   377
     * @throws    IllegalArgumentException if the country of the given {@code locale}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * is not a supported ISO 3166 country code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    public static Currency getInstance(Locale locale) {
48251
57148c79bd75 8176841: Additional Unicode Language-Tag Extensions
naoto
parents: 47216
diff changeset
   381
        // check for locale overrides
57148c79bd75 8176841: Additional Unicode Language-Tag Extensions
naoto
parents: 47216
diff changeset
   382
        String override = locale.getUnicodeLocaleType("cu");
57148c79bd75 8176841: Additional Unicode Language-Tag Extensions
naoto
parents: 47216
diff changeset
   383
        if (override != null) {
57148c79bd75 8176841: Additional Unicode Language-Tag Extensions
naoto
parents: 47216
diff changeset
   384
            try {
57148c79bd75 8176841: Additional Unicode Language-Tag Extensions
naoto
parents: 47216
diff changeset
   385
                return getInstance(override.toUpperCase(Locale.ROOT));
57148c79bd75 8176841: Additional Unicode Language-Tag Extensions
naoto
parents: 47216
diff changeset
   386
            } catch (IllegalArgumentException iae) {
57148c79bd75 8176841: Additional Unicode Language-Tag Extensions
naoto
parents: 47216
diff changeset
   387
                // override currency is invalid. Fall through.
57148c79bd75 8176841: Additional Unicode Language-Tag Extensions
naoto
parents: 47216
diff changeset
   388
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
48251
57148c79bd75 8176841: Additional Unicode Language-Tag Extensions
naoto
parents: 47216
diff changeset
   391
        String country = CalendarDataUtility.findRegionOverride(locale).getCountry();
57148c79bd75 8176841: Additional Unicode Language-Tag Extensions
naoto
parents: 47216
diff changeset
   392
57148c79bd75 8176841: Additional Unicode Language-Tag Extensions
naoto
parents: 47216
diff changeset
   393
        if (country == null || !country.matches("^[a-zA-Z]{2}$")) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        char char1 = country.charAt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        char char2 = country.charAt(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        int tableEntry = getMainTableEntry(char1, char2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        if ((tableEntry & COUNTRY_TYPE_MASK) == SIMPLE_CASE_COUNTRY_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                    && tableEntry != INVALID_COUNTRY_ENTRY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            char finalChar = (char) ((tableEntry & SIMPLE_CASE_COUNTRY_FINAL_CHAR_MASK) + 'A');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            int defaultFractionDigits = (tableEntry & SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_MASK) >> SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_SHIFT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            int numericCode = (tableEntry & NUMERIC_CODE_MASK) >> NUMERIC_CODE_SHIFT;
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12448
diff changeset
   405
            StringBuilder sb = new StringBuilder(country);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            sb.append(finalChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            return getInstance(sb.toString(), defaultFractionDigits, numericCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            // special cases
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            if (tableEntry == INVALID_COUNTRY_ENTRY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            if (tableEntry == COUNTRY_WITHOUT_CURRENCY_ENTRY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            } else {
38440
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   416
                int index = SpecialCaseEntry.toIndex(tableEntry);
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   417
                SpecialCaseEntry scEntry = specialCasesList.get(index);
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   418
                if (scEntry.cutOverTime == Long.MAX_VALUE
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   419
                        || System.currentTimeMillis() < scEntry.cutOverTime) {
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   420
                    return getInstance(scEntry.oldCurrency,
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   421
                            scEntry.oldCurrencyFraction,
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   422
                            scEntry.oldCurrencyNumericCode);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                } else {
38440
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   424
                    return getInstance(scEntry.newCurrency,
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   425
                            scEntry.newCurrencyFraction,
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   426
                            scEntry.newCurrencyNumericCode);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * Gets the set of available currencies.  The returned set of currencies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * contains all of the available currencies, which may include currencies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * that represent obsolete ISO 4217 codes.  The set can be modified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * without affecting the available currencies in the runtime.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * @return the set of available currencies.  If there is no currency
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     *    available in the runtime, the returned set is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * @since 1.7
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    public static Set<Currency> getAvailableCurrencies() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        synchronized(Currency.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            if (available == null) {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 12297
diff changeset
   445
                available = new HashSet<>(256);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                // Add simple currencies first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                for (char c1 = 'A'; c1 <= 'Z'; c1 ++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                    for (char c2 = 'A'; c2 <= 'Z'; c2 ++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                        int tableEntry = getMainTableEntry(c1, c2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                        if ((tableEntry & COUNTRY_TYPE_MASK) == SIMPLE_CASE_COUNTRY_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                             && tableEntry != INVALID_COUNTRY_ENTRY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                            char finalChar = (char) ((tableEntry & SIMPLE_CASE_COUNTRY_FINAL_CHAR_MASK) + 'A');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                            int defaultFractionDigits = (tableEntry & SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_MASK) >> SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_SHIFT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                            int numericCode = (tableEntry & NUMERIC_CODE_MASK) >> NUMERIC_CODE_SHIFT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                            StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                            sb.append(c1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                            sb.append(c2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                            sb.append(finalChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                            available.add(getInstance(sb.toString(), defaultFractionDigits, numericCode));
38440
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   461
                        } else if ((tableEntry & COUNTRY_TYPE_MASK) == SPECIAL_CASE_COUNTRY_MASK
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   462
                                && tableEntry != INVALID_COUNTRY_ENTRY
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   463
                                && tableEntry != COUNTRY_WITHOUT_CURRENCY_ENTRY) {
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   464
                            int index = SpecialCaseEntry.toIndex(tableEntry);
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   465
                            SpecialCaseEntry scEntry = specialCasesList.get(index);
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   466
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   467
                            if (scEntry.cutOverTime == Long.MAX_VALUE
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   468
                                    || System.currentTimeMillis() < scEntry.cutOverTime) {
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   469
                                available.add(getInstance(scEntry.oldCurrency,
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   470
                                        scEntry.oldCurrencyFraction,
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   471
                                        scEntry.oldCurrencyNumericCode));
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   472
                            } else {
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   473
                                available.add(getInstance(scEntry.newCurrency,
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   474
                                        scEntry.newCurrencyFraction,
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   475
                                        scEntry.newCurrencyNumericCode));
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   476
                            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                // Now add other currencies
38440
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   482
                for (OtherCurrencyEntry entry : otherCurrenciesList) {
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   483
                    available.add(getInstance(entry.currencyCode));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
11138
9121a1a92512 7117469: Warning cleanup for j.u.Currency and j.u.Locale related classes
naoto
parents: 10419
diff changeset
   488
        @SuppressWarnings("unchecked")
9121a1a92512 7117469: Warning cleanup for j.u.Currency and j.u.Locale related classes
naoto
parents: 10419
diff changeset
   489
        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
   490
        return result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * Gets the ISO 4217 currency code of this currency.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * @return the ISO 4217 currency code of this currency.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    public String getCurrencyCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        return currencyCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    /**
16010
2727163b5df5 7092447: Clarify the default locale used in each locale sensitive operation
naoto
parents: 16006
diff changeset
   503
     * Gets the symbol of this currency for the default
2727163b5df5 7092447: Clarify the default locale used in each locale sensitive operation
naoto
parents: 16006
diff changeset
   504
     * {@link Locale.Category#DISPLAY DISPLAY} locale.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * For example, for the US Dollar, the symbol is "$" if the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * locale is the US, while for other locales it may be "US$". If no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * symbol can be determined, the ISO 4217 currency code is returned.
16010
2727163b5df5 7092447: Clarify the default locale used in each locale sensitive operation
naoto
parents: 16006
diff changeset
   508
     * <p>
48251
57148c79bd75 8176841: Additional Unicode Language-Tag Extensions
naoto
parents: 47216
diff changeset
   509
     * If the default {@link Locale.Category#DISPLAY DISPLAY} locale
57148c79bd75 8176841: Additional Unicode Language-Tag Extensions
naoto
parents: 47216
diff changeset
   510
     * contains "rg" (region override)
57148c79bd75 8176841: Additional Unicode Language-Tag Extensions
naoto
parents: 47216
diff changeset
   511
     * <a href="./Locale.html#def_locale_extension">Unicode extension</a>,
57148c79bd75 8176841: Additional Unicode Language-Tag Extensions
naoto
parents: 47216
diff changeset
   512
     * the symbol returned from this method reflects
57148c79bd75 8176841: Additional Unicode Language-Tag Extensions
naoto
parents: 47216
diff changeset
   513
     * the value specified with that extension.
57148c79bd75 8176841: Additional Unicode Language-Tag Extensions
naoto
parents: 47216
diff changeset
   514
     * <p>
16010
2727163b5df5 7092447: Clarify the default locale used in each locale sensitive operation
naoto
parents: 16006
diff changeset
   515
     * This is equivalent to calling
2727163b5df5 7092447: Clarify the default locale used in each locale sensitive operation
naoto
parents: 16006
diff changeset
   516
     * {@link #getSymbol(Locale)
2727163b5df5 7092447: Clarify the default locale used in each locale sensitive operation
naoto
parents: 16006
diff changeset
   517
     *     getSymbol(Locale.getDefault(Locale.Category.DISPLAY))}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     *
16010
2727163b5df5 7092447: Clarify the default locale used in each locale sensitive operation
naoto
parents: 16006
diff changeset
   519
     * @return the symbol of this currency for the default
2727163b5df5 7092447: Clarify the default locale used in each locale sensitive operation
naoto
parents: 16006
diff changeset
   520
     *     {@link Locale.Category#DISPLAY DISPLAY} locale
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    public String getSymbol() {
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   523
        return getSymbol(Locale.getDefault(Locale.Category.DISPLAY));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * Gets the symbol of this currency for the specified locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * For example, for the US Dollar, the symbol is "$" if the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * locale is the US, while for other locales it may be "US$". If no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * symbol can be determined, the ISO 4217 currency code is returned.
48251
57148c79bd75 8176841: Additional Unicode Language-Tag Extensions
naoto
parents: 47216
diff changeset
   531
     * <p>
57148c79bd75 8176841: Additional Unicode Language-Tag Extensions
naoto
parents: 47216
diff changeset
   532
     * If the specified {@code locale} contains "rg" (region override)
57148c79bd75 8176841: Additional Unicode Language-Tag Extensions
naoto
parents: 47216
diff changeset
   533
     * <a href="./Locale.html#def_locale_extension">Unicode extension</a>,
57148c79bd75 8176841: Additional Unicode Language-Tag Extensions
naoto
parents: 47216
diff changeset
   534
     * the symbol returned from this method reflects
57148c79bd75 8176841: Additional Unicode Language-Tag Extensions
naoto
parents: 47216
diff changeset
   535
     * the value specified with that extension.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * @param locale the locale for which a display name for this currency is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * @return the symbol of this currency for the specified locale
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   540
     * @throws    NullPointerException if {@code locale} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    public String getSymbol(Locale locale) {
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12448
diff changeset
   543
        LocaleServiceProviderPool pool =
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12448
diff changeset
   544
            LocaleServiceProviderPool.getPool(CurrencyNameProvider.class);
48251
57148c79bd75 8176841: Additional Unicode Language-Tag Extensions
naoto
parents: 47216
diff changeset
   545
        locale = CalendarDataUtility.findRegionOverride(locale);
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12448
diff changeset
   546
        String symbol = pool.getLocalizedObject(
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12448
diff changeset
   547
                                CurrencyNameGetter.INSTANCE,
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12448
diff changeset
   548
                                locale, currencyCode, SYMBOL);
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12448
diff changeset
   549
        if (symbol != null) {
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12448
diff changeset
   550
            return symbol;
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12448
diff changeset
   551
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12448
diff changeset
   553
        // use currency code as symbol of last resort
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12448
diff changeset
   554
        return currencyCode;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    /**
38440
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   558
     * Gets the default number of fraction digits used with this currency.
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   559
     * Note that the number of fraction digits is the same as ISO 4217's
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   560
     * minor unit for the currency.
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   561
     * For example, the default number of fraction digits for the Euro is 2,
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   562
     * while for the Japanese Yen it's 0.
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   563
     * In the case of pseudo-currencies, such as IMF Special Drawing Rights,
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   564
     * -1 is returned.
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   565
     *
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   566
     * @return the default number of fraction digits used with this currency
36113
cd5af2894aee 8074411: Describe "minor unit" and/or "default fraction digits" in Currency class' javadoc clearly
peytoia
parents: 32930
diff changeset
   567
    */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    public int getDefaultFractionDigits() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        return defaultFractionDigits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * Returns the ISO 4217 numeric code of this currency.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * @return the ISO 4217 numeric code of this currency
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * @since 1.7
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    public int getNumericCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        return numericCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    /**
37815
0ecfe8f6500e 8154295: Currency iso code is int and hence looses the preceeding zero.
peytoia
parents: 36113
diff changeset
   583
     * Returns the 3 digit ISO 4217 numeric code of this currency as a {@code String}.
50817
fa1e04811ff6 8066709: Make some JDK system properties read only
rriggs
parents: 48929
diff changeset
   584
     * Unlike {@link #getNumericCode()}, which returns the numeric code as {@code int},
37815
0ecfe8f6500e 8154295: Currency iso code is int and hence looses the preceeding zero.
peytoia
parents: 36113
diff changeset
   585
     * this method always returns the numeric code as a 3 digit string.
0ecfe8f6500e 8154295: Currency iso code is int and hence looses the preceeding zero.
peytoia
parents: 36113
diff changeset
   586
     * e.g. a numeric value of 32 would be returned as "032",
0ecfe8f6500e 8154295: Currency iso code is int and hence looses the preceeding zero.
peytoia
parents: 36113
diff changeset
   587
     * and a numeric value of 6 would be returned as "006".
0ecfe8f6500e 8154295: Currency iso code is int and hence looses the preceeding zero.
peytoia
parents: 36113
diff changeset
   588
     *
0ecfe8f6500e 8154295: Currency iso code is int and hence looses the preceeding zero.
peytoia
parents: 36113
diff changeset
   589
     * @return the 3 digit ISO 4217 numeric code of this currency as a {@code String}
0ecfe8f6500e 8154295: Currency iso code is int and hence looses the preceeding zero.
peytoia
parents: 36113
diff changeset
   590
     * @since 9
0ecfe8f6500e 8154295: Currency iso code is int and hence looses the preceeding zero.
peytoia
parents: 36113
diff changeset
   591
     */
0ecfe8f6500e 8154295: Currency iso code is int and hence looses the preceeding zero.
peytoia
parents: 36113
diff changeset
   592
    public String getNumericCodeAsString() {
0ecfe8f6500e 8154295: Currency iso code is int and hence looses the preceeding zero.
peytoia
parents: 36113
diff changeset
   593
        /* numeric code could be returned as a 3 digit string simply by using
0ecfe8f6500e 8154295: Currency iso code is int and hence looses the preceeding zero.
peytoia
parents: 36113
diff changeset
   594
           String.format("%03d",numericCode); which uses regex to parse the format,
0ecfe8f6500e 8154295: Currency iso code is int and hence looses the preceeding zero.
peytoia
parents: 36113
diff changeset
   595
           "%03d" in this case. Parsing a regex gives an extra performance overhead,
0ecfe8f6500e 8154295: Currency iso code is int and hence looses the preceeding zero.
peytoia
parents: 36113
diff changeset
   596
           so String.format() approach is avoided in this scenario.
0ecfe8f6500e 8154295: Currency iso code is int and hence looses the preceeding zero.
peytoia
parents: 36113
diff changeset
   597
        */
0ecfe8f6500e 8154295: Currency iso code is int and hence looses the preceeding zero.
peytoia
parents: 36113
diff changeset
   598
        if (numericCode < 100) {
0ecfe8f6500e 8154295: Currency iso code is int and hence looses the preceeding zero.
peytoia
parents: 36113
diff changeset
   599
            StringBuilder sb = new StringBuilder();
0ecfe8f6500e 8154295: Currency iso code is int and hence looses the preceeding zero.
peytoia
parents: 36113
diff changeset
   600
            sb.append('0');
0ecfe8f6500e 8154295: Currency iso code is int and hence looses the preceeding zero.
peytoia
parents: 36113
diff changeset
   601
            if (numericCode < 10) {
0ecfe8f6500e 8154295: Currency iso code is int and hence looses the preceeding zero.
peytoia
parents: 36113
diff changeset
   602
                sb.append('0');
0ecfe8f6500e 8154295: Currency iso code is int and hence looses the preceeding zero.
peytoia
parents: 36113
diff changeset
   603
            }
0ecfe8f6500e 8154295: Currency iso code is int and hence looses the preceeding zero.
peytoia
parents: 36113
diff changeset
   604
            return sb.append(numericCode).toString();
0ecfe8f6500e 8154295: Currency iso code is int and hence looses the preceeding zero.
peytoia
parents: 36113
diff changeset
   605
        }
0ecfe8f6500e 8154295: Currency iso code is int and hence looses the preceeding zero.
peytoia
parents: 36113
diff changeset
   606
        return String.valueOf(numericCode);
0ecfe8f6500e 8154295: Currency iso code is int and hence looses the preceeding zero.
peytoia
parents: 36113
diff changeset
   607
    }
0ecfe8f6500e 8154295: Currency iso code is int and hence looses the preceeding zero.
peytoia
parents: 36113
diff changeset
   608
0ecfe8f6500e 8154295: Currency iso code is int and hence looses the preceeding zero.
peytoia
parents: 36113
diff changeset
   609
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * Gets the name that is suitable for displaying this currency for
16010
2727163b5df5 7092447: Clarify the default locale used in each locale sensitive operation
naoto
parents: 16006
diff changeset
   611
     * the default {@link Locale.Category#DISPLAY DISPLAY} locale.
2727163b5df5 7092447: Clarify the default locale used in each locale sensitive operation
naoto
parents: 16006
diff changeset
   612
     * If there is no suitable display name found
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * for the default locale, the ISO 4217 currency code is returned.
16010
2727163b5df5 7092447: Clarify the default locale used in each locale sensitive operation
naoto
parents: 16006
diff changeset
   614
     * <p>
2727163b5df5 7092447: Clarify the default locale used in each locale sensitive operation
naoto
parents: 16006
diff changeset
   615
     * This is equivalent to calling
2727163b5df5 7092447: Clarify the default locale used in each locale sensitive operation
naoto
parents: 16006
diff changeset
   616
     * {@link #getDisplayName(Locale)
2727163b5df5 7092447: Clarify the default locale used in each locale sensitive operation
naoto
parents: 16006
diff changeset
   617
     *     getDisplayName(Locale.getDefault(Locale.Category.DISPLAY))}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     *
16010
2727163b5df5 7092447: Clarify the default locale used in each locale sensitive operation
naoto
parents: 16006
diff changeset
   619
     * @return the display name of this currency for the default
2727163b5df5 7092447: Clarify the default locale used in each locale sensitive operation
naoto
parents: 16006
diff changeset
   620
     *     {@link Locale.Category#DISPLAY DISPLAY} locale
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * @since 1.7
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    public String getDisplayName() {
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5506
diff changeset
   624
        return getDisplayName(Locale.getDefault(Locale.Category.DISPLAY));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * Gets the name that is suitable for displaying this currency for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * the specified locale.  If there is no suitable display name found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * for the specified locale, the ISO 4217 currency code is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * @param locale the locale for which a display name for this currency is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * @return the display name of this currency for the specified locale
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   635
     * @throws    NullPointerException if {@code locale} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * @since 1.7
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    public String getDisplayName(Locale locale) {
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12448
diff changeset
   639
        LocaleServiceProviderPool pool =
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12448
diff changeset
   640
            LocaleServiceProviderPool.getPool(CurrencyNameProvider.class);
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12448
diff changeset
   641
        String result = pool.getLocalizedObject(
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12448
diff changeset
   642
                                CurrencyNameGetter.INSTANCE,
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12448
diff changeset
   643
                                locale, currencyCode, DISPLAYNAME);
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12448
diff changeset
   644
        if (result != null) {
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12448
diff changeset
   645
            return result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        // use currency code as symbol of last resort
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        return currencyCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * Returns the ISO 4217 currency code of this currency.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * @return the ISO 4217 currency code of this currency
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     */
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12448
diff changeset
   657
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        return currencyCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * Resolves instances being deserialized to a single instance per currency.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     */
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 53120
diff changeset
   665
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
    private Object readResolve() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        return getInstance(currencyCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * Gets the main table entry for the country whose country code consists
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * of char1 and char2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    private static int getMainTableEntry(char char1, char char2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        if (char1 < 'A' || char1 > 'Z' || char2 < 'A' || char2 > 'Z') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        return mainTable[(char1 - 'A') * A_TO_Z + (char2 - 'A')];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * Sets the main table entry for the country whose country code consists
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * of char1 and char2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    private static void setMainTableEntry(char char1, char char2, int entry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        if (char1 < 'A' || char1 > 'Z' || char2 < 'A' || char2 > 'Z') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        mainTable[(char1 - 'A') * A_TO_Z + (char2 - 'A')] = entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * Obtains a localized currency names from a CurrencyNameProvider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    private static class CurrencyNameGetter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        implements LocaleServiceProviderPool.LocalizedObjectGetter<CurrencyNameProvider,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                                                                   String> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        private static final CurrencyNameGetter INSTANCE = new CurrencyNameGetter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
13583
dc0017b1a452 6336885: RFE: Locale Data Deployment Enhancements
naoto
parents: 12448
diff changeset
   701
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        public String getObject(CurrencyNameProvider currencyNameProvider,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                                Locale locale,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                                String key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                                Object... params) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
            assert params.length == 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            int type = (Integer)params[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            switch(type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            case SYMBOL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                return currencyNameProvider.getSymbol(key, locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            case DISPLAYNAME:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                return currencyNameProvider.getDisplayName(key, locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                assert false; // shouldn't happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
    private static int[] readIntArray(DataInputStream dis, int count) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        int[] ret = new int[count];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        for (int i = 0; i < count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            ret[i] = dis.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
38440
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   731
    private static List<SpecialCaseEntry> readSpecialCases(DataInputStream dis,
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   732
            int count)
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   733
            throws IOException {
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   734
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   735
        List<SpecialCaseEntry> list = new ArrayList<>(count);
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   736
        long cutOverTime;
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   737
        String oldCurrency;
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   738
        String newCurrency;
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   739
        int oldCurrencyFraction;
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   740
        int newCurrencyFraction;
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   741
        int oldCurrencyNumericCode;
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   742
        int newCurrencyNumericCode;
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   743
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        for (int i = 0; i < count; i++) {
38440
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   745
            cutOverTime = dis.readLong();
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   746
            oldCurrency = dis.readUTF();
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   747
            newCurrency = dis.readUTF();
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   748
            oldCurrencyFraction = dis.readInt();
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   749
            newCurrencyFraction = dis.readInt();
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   750
            oldCurrencyNumericCode = dis.readInt();
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   751
            newCurrencyNumericCode = dis.readInt();
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   752
            SpecialCaseEntry sc = new SpecialCaseEntry(cutOverTime,
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   753
                    oldCurrency, newCurrency,
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   754
                    oldCurrencyFraction, newCurrencyFraction,
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   755
                    oldCurrencyNumericCode, newCurrencyNumericCode);
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   756
            list.add(sc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        }
38440
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   758
        return list;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
38440
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   761
    private static List<OtherCurrencyEntry> readOtherCurrencies(DataInputStream dis,
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   762
            int count)
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   763
            throws IOException {
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   764
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   765
        List<OtherCurrencyEntry> list = new ArrayList<>(count);
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   766
        String currencyCode;
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   767
        int fraction;
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   768
        int numericCode;
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   769
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        for (int i = 0; i < count; i++) {
38440
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   771
            currencyCode = dis.readUTF();
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   772
            fraction = dis.readInt();
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   773
            numericCode = dis.readInt();
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   774
            OtherCurrencyEntry oc = new OtherCurrencyEntry(currencyCode,
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   775
                    fraction,
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   776
                    numericCode);
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   777
            list.add(oc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        }
38440
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   779
        return list;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
    /**
48929
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   783
     * Parse currency data found in the properties file (that
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   784
     * java.util.currency.data designates) to a List of CurrencyProperty
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   785
     * instances. Also, remove invalid entries and the multiple currency
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   786
     * code inconsistencies.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     *
48929
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   788
     * @param props properties containing currency data
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   789
     * @param pattern regex pattern for the properties entry
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   790
     * @return list of parsed property entries
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     */
48929
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   792
    private static List<CurrencyProperty> getValidCurrencyData(Properties props,
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   793
            Pattern pattern) {
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   794
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   795
        Set<String> keys = props.stringPropertyNames();
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   796
        List<CurrencyProperty> propertyEntries = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
48929
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   798
        // remove all invalid entries and parse all valid currency properties
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   799
        // entries to a group of CurrencyProperty, classified by currency code
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   800
        Map<String, List<CurrencyProperty>> currencyCodeGroup = keys.stream()
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   801
                .map(k -> CurrencyProperty
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   802
                .getValidEntry(k.toUpperCase(Locale.ROOT),
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   803
                        props.getProperty(k).toUpperCase(Locale.ROOT),
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   804
                        pattern)).flatMap(o -> o.stream())
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   805
                .collect(Collectors.groupingBy(entry -> entry.currencyCode));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
48929
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   807
        // check each group for inconsistencies
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   808
        currencyCodeGroup.forEach((curCode, list) -> {
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   809
            boolean inconsistent = CurrencyProperty
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   810
                    .containsInconsistentInstances(list);
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   811
            if (inconsistent) {
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   812
                list.forEach(prop -> CurrencyProperty.info("The property"
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   813
                        + " entry for " + prop.country + " is inconsistent."
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   814
                        + " Ignored.", null));
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   815
            } else {
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   816
                propertyEntries.addAll(list);
13790
5b29e3921008 7180362: RFE: Implement date cutover functionality for currency.properties file
coffeys
parents: 13583
diff changeset
   817
            }
48929
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   818
        });
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   819
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   820
        return propertyEntries;
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   821
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
48929
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   823
    /**
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   824
     * Replaces currency data found in the properties file that
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   825
     * java.util.currency.data designates. This method is invoked for
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   826
     * each valid currency entry.
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   827
     *
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   828
     * @param prop CurrencyProperty instance of the valid property entry
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   829
     */
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   830
    private static void replaceCurrencyData(CurrencyProperty prop) {
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   831
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   832
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   833
        String ctry = prop.country;
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   834
        String code = prop.currencyCode;
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   835
        int numeric = prop.numericCode;
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   836
        int fraction = prop.fraction;
29524
2dbceeab4b8b 8074350: Support ISO 4217 "Current funds codes" table (A.2)
naoto
parents: 27565
diff changeset
   837
        int entry = numeric << NUMERIC_CODE_SHIFT;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
38440
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   839
        int index = SpecialCaseEntry.indexOf(code, fraction, numeric);
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   840
48929
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   841
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   842
        // If a new entry changes the numeric code/dfd of an existing
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   843
        // currency code, update it in the sc list at the respective
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   844
        // index and also change it in the other currencies list and
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   845
        // main table (if that currency code is also used as a
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   846
        // simple case).
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   847
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   848
        // If all three components do not match with the new entry,
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   849
        // but the currency code exists in the special case list
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   850
        // update the sc entry with the new entry
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   851
        int scCurrencyCodeIndex = -1;
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   852
        if (index == -1) {
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   853
            scCurrencyCodeIndex = SpecialCaseEntry.currencyCodeIndex(code);
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   854
            if (scCurrencyCodeIndex != -1) {
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   855
                //currency code exists in sc list, then update the old entry
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   856
                specialCasesList.set(scCurrencyCodeIndex,
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   857
                        new SpecialCaseEntry(code, fraction, numeric));
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   858
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   859
                // also update the entry in other currencies list
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   860
                OtherCurrencyEntry oe = OtherCurrencyEntry.findEntry(code);
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   861
                if (oe != null) {
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   862
                    int oIndex = otherCurrenciesList.indexOf(oe);
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   863
                    otherCurrenciesList.set(oIndex, new OtherCurrencyEntry(
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   864
                            code, fraction, numeric));
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   865
                }
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   866
            }
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   867
        }
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   868
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   869
        /* If a country switches from simple case to special case or
38440
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   870
         * one special case to other special case which is not present
48929
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   871
         * in the sc arrays then insert the new entry in special case arrays.
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   872
         * If an entry with given currency code exists, update with the new
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   873
         * entry.
38440
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   874
         */
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   875
        if (index == -1 && (ctry.charAt(0) != code.charAt(0)
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   876
                || ctry.charAt(1) != code.charAt(1))) {
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   877
48929
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   878
            if(scCurrencyCodeIndex == -1) {
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   879
                specialCasesList.add(new SpecialCaseEntry(code, fraction,
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   880
                        numeric));
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   881
                index = specialCasesList.size() - 1;
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   882
            } else {
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   883
                index = scCurrencyCodeIndex;
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   884
            }
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   885
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   886
            // update the entry in main table if it exists as a simple case
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   887
            updateMainTableEntry(code, fraction, numeric);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
38440
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   890
        if (index == -1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
            // simple case
38440
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   892
            entry |= (fraction << SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_SHIFT)
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   893
                    | (code.charAt(2) - 'A');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
            // special case
38440
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   896
            entry = SPECIAL_CASE_COUNTRY_MASK
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   897
                    | (index + SPECIAL_CASE_COUNTRY_INDEX_DELTA);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        setMainTableEntry(ctry.charAt(0), ctry.charAt(1), entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
48929
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   902
    // update the entry in maintable for any simple case found, if a new
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   903
    // entry as a special case updates the entry in sc list with
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   904
    // existing currency code
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   905
    private static void updateMainTableEntry(String code, int fraction,
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   906
            int numeric) {
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   907
        // checking the existence of currency code in mainTable
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   908
        int tableEntry = getMainTableEntry(code.charAt(0), code.charAt(1));
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   909
        int entry = numeric << NUMERIC_CODE_SHIFT;
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   910
        if ((tableEntry & COUNTRY_TYPE_MASK) == SIMPLE_CASE_COUNTRY_MASK
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   911
                && tableEntry != INVALID_COUNTRY_ENTRY
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   912
                && code.charAt(2) - 'A' == (tableEntry
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   913
                & SIMPLE_CASE_COUNTRY_FINAL_CHAR_MASK)) {
13790
5b29e3921008 7180362: RFE: Implement date cutover functionality for currency.properties file
coffeys
parents: 13583
diff changeset
   914
48929
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   915
            int numericCode = (tableEntry & NUMERIC_CODE_MASK)
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   916
                    >> NUMERIC_CODE_SHIFT;
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   917
            int defaultFractionDigits = (tableEntry
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   918
                    & SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_MASK)
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   919
                    >> SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_SHIFT;
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   920
            if (numeric != numericCode || fraction != defaultFractionDigits) {
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   921
                // update the entry in main table
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   922
                entry |= (fraction << SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_SHIFT)
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   923
                        | (code.charAt(2) - 'A');
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
   924
                setMainTableEntry(code.charAt(0), code.charAt(1), entry);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
    }
38440
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   928
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   929
    /* Used to represent a special case currency entry
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   930
     * - cutOverTime: cut-over time in millis as returned by
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   931
     *   System.currentTimeMillis for special case countries that are changing
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   932
     *   currencies; Long.MAX_VALUE for countries that are not changing currencies
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   933
     * - oldCurrency: old currencies for special case countries
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   934
     * - newCurrency: new currencies for special case countries that are
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   935
     *   changing currencies; null for others
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   936
     * - oldCurrencyFraction: default fraction digits for old currencies
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   937
     * - newCurrencyFraction: default fraction digits for new currencies, 0 for
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   938
     *   countries that are not changing currencies
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   939
     * - oldCurrencyNumericCode: numeric code for old currencies
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   940
     * - newCurrencyNumericCode: numeric code for new currencies, 0 for countries
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   941
     *   that are not changing currencies
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   942
    */
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   943
    private static class SpecialCaseEntry {
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   944
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   945
        final private long cutOverTime;
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   946
        final private String oldCurrency;
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   947
        final private String newCurrency;
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   948
        final private int oldCurrencyFraction;
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   949
        final private int newCurrencyFraction;
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   950
        final private int oldCurrencyNumericCode;
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   951
        final private int newCurrencyNumericCode;
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   952
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   953
        private SpecialCaseEntry(long cutOverTime, String oldCurrency, String newCurrency,
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   954
                int oldCurrencyFraction, int newCurrencyFraction,
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   955
                int oldCurrencyNumericCode, int newCurrencyNumericCode) {
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   956
            this.cutOverTime = cutOverTime;
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   957
            this.oldCurrency = oldCurrency;
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   958
            this.newCurrency = newCurrency;
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   959
            this.oldCurrencyFraction = oldCurrencyFraction;
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   960
            this.newCurrencyFraction = newCurrencyFraction;
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   961
            this.oldCurrencyNumericCode = oldCurrencyNumericCode;
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   962
            this.newCurrencyNumericCode = newCurrencyNumericCode;
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   963
        }
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   964
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   965
        private SpecialCaseEntry(String currencyCode, int fraction,
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   966
                int numericCode) {
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   967
            this(Long.MAX_VALUE, currencyCode, "", fraction, 0, numericCode, 0);
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   968
        }
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   969
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   970
        //get the index of the special case entry
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   971
        private static int indexOf(String code, int fraction, int numeric) {
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   972
            int size = specialCasesList.size();
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   973
            for (int index = 0; index < size; index++) {
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   974
                SpecialCaseEntry scEntry = specialCasesList.get(index);
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   975
                if (scEntry.oldCurrency.equals(code)
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   976
                        && scEntry.oldCurrencyFraction == fraction
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   977
                        && scEntry.oldCurrencyNumericCode == numeric
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   978
                        && scEntry.cutOverTime == Long.MAX_VALUE) {
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   979
                    return index;
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   980
                }
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   981
            }
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   982
            return -1;
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   983
        }
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   984
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   985
        // get the fraction and numericCode of the sc currencycode
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   986
        private static int[] findEntry(String code) {
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   987
            int[] fractionAndNumericCode = null;
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   988
            int size = specialCasesList.size();
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   989
            for (int index = 0; index < size; index++) {
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   990
                SpecialCaseEntry scEntry = specialCasesList.get(index);
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   991
                if (scEntry.oldCurrency.equals(code) && (scEntry.cutOverTime == Long.MAX_VALUE
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   992
                        || System.currentTimeMillis() < scEntry.cutOverTime)) {
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   993
                    //consider only when there is no new currency or cutover time is not passed
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   994
                    fractionAndNumericCode = new int[2];
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   995
                    fractionAndNumericCode[0] = scEntry.oldCurrencyFraction;
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   996
                    fractionAndNumericCode[1] = scEntry.oldCurrencyNumericCode;
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   997
                    break;
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   998
                } else if (scEntry.newCurrency.equals(code)
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
   999
                        && System.currentTimeMillis() >= scEntry.cutOverTime) {
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
  1000
                    //consider only if the cutover time is passed
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
  1001
                    fractionAndNumericCode = new int[2];
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
  1002
                    fractionAndNumericCode[0] = scEntry.newCurrencyFraction;
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
  1003
                    fractionAndNumericCode[1] = scEntry.newCurrencyNumericCode;
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
  1004
                    break;
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
  1005
                }
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
  1006
            }
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
  1007
            return fractionAndNumericCode;
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
  1008
        }
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
  1009
48929
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1010
        // get the index based on currency code
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1011
        private static int currencyCodeIndex(String code) {
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1012
            int size = specialCasesList.size();
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1013
            for (int index = 0; index < size; index++) {
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1014
                SpecialCaseEntry scEntry = specialCasesList.get(index);
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1015
                if (scEntry.oldCurrency.equals(code) && (scEntry.cutOverTime == Long.MAX_VALUE
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1016
                        || System.currentTimeMillis() < scEntry.cutOverTime)) {
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1017
                    //consider only when there is no new currency or cutover time is not passed
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1018
                    return index;
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1019
                } else if (scEntry.newCurrency.equals(code)
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1020
                        && System.currentTimeMillis() >= scEntry.cutOverTime) {
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1021
                    //consider only if the cutover time is passed
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1022
                    return index;
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1023
                }
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1024
            }
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1025
            return -1;
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1026
        }
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1027
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1028
38440
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
  1029
        // convert the special case entry to sc arrays index
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
  1030
        private static int toIndex(int tableEntry) {
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
  1031
            return (tableEntry & SPECIAL_CASE_COUNTRY_INDEX_MASK) - SPECIAL_CASE_COUNTRY_INDEX_DELTA;
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
  1032
        }
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
  1033
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
  1034
    }
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
  1035
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
  1036
    /* Used to represent Other currencies
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
  1037
     * - currencyCode: currency codes that are not the main currency
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
  1038
     *   of a simple country
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
  1039
     * - otherCurrenciesDFD: decimal format digits for other currencies
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
  1040
     * - otherCurrenciesNumericCode: numeric code for other currencies
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
  1041
     */
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
  1042
    private static class OtherCurrencyEntry {
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
  1043
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
  1044
        final private String currencyCode;
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
  1045
        final private int fraction;
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
  1046
        final private int numericCode;
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
  1047
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
  1048
        private OtherCurrencyEntry(String currencyCode, int fraction,
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
  1049
                int numericCode) {
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
  1050
            this.currencyCode = currencyCode;
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
  1051
            this.fraction = fraction;
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
  1052
            this.numericCode = numericCode;
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
  1053
        }
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
  1054
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
  1055
        //get the instance of the other currency code
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
  1056
        private static OtherCurrencyEntry findEntry(String code) {
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
  1057
            int size = otherCurrenciesList.size();
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
  1058
            for (int index = 0; index < size; index++) {
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
  1059
                OtherCurrencyEntry ocEntry = otherCurrenciesList.get(index);
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
  1060
                if (ocEntry.currencyCode.equalsIgnoreCase(code)) {
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
  1061
                    return ocEntry;
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
  1062
                }
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
  1063
            }
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
  1064
            return null;
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
  1065
        }
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
  1066
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
  1067
    }
9e77c5b81def 7102969: currency.properties supercede not working correctly
nishjain
parents: 37815
diff changeset
  1068
48929
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1069
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1070
    /*
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1071
     * Used to represent an entry of the properties file that
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1072
     * java.util.currency.data designates
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1073
     *
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1074
     * - country: country representing the currency entry
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1075
     * - currencyCode: currency code
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1076
     * - fraction: default fraction digit
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1077
     * - numericCode: numeric code
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1078
     * - date: cutover date
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1079
     */
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1080
    private static class CurrencyProperty {
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1081
        final private String country;
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1082
        final private String currencyCode;
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1083
        final private int fraction;
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1084
        final private int numericCode;
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1085
        final private String date;
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1086
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1087
        private CurrencyProperty(String country, String currencyCode,
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1088
                int fraction, int numericCode, String date) {
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1089
            this.country = country;
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1090
            this.currencyCode = currencyCode;
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1091
            this.fraction = fraction;
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1092
            this.numericCode = numericCode;
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1093
            this.date = date;
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1094
        }
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1095
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1096
        /**
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1097
         * Check the valid currency data and create/return an Optional instance
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1098
         * of CurrencyProperty
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1099
         *
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1100
         * @param ctry    country representing the currency data
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1101
         * @param curData currency data of the given {@code ctry}
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1102
         * @param pattern regex pattern for the properties entry
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1103
         * @return Optional containing CurrencyProperty instance, If valid;
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1104
         *         empty otherwise
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1105
         */
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1106
        private static Optional<CurrencyProperty> getValidEntry(String ctry,
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1107
                String curData,
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1108
                Pattern pattern) {
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1109
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1110
            CurrencyProperty prop = null;
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1111
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1112
            if (ctry.length() != 2) {
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1113
                // Invalid country code. Ignore the entry.
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1114
            } else {
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1115
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1116
                prop = parseProperty(ctry, curData, pattern);
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1117
                // if the property entry failed any of the below checked
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1118
                // criteria it is ignored
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1119
                if (prop == null
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1120
                        || (prop.date == null && curData.chars()
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1121
                                .map(c -> c == ',' ? 1 : 0).sum() >= 3)) {
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1122
                    // format is not recognized.  ignore the data if date
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1123
                    // string is null and we've 4 values, bad date value
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1124
                    prop = null;
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1125
                } else if (prop.fraction
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1126
                        > SIMPLE_CASE_COUNTRY_MAX_DEFAULT_DIGITS) {
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1127
                    prop = null;
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1128
                } else {
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1129
                    try {
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1130
                        if (prop.date != null
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1131
                                && !isPastCutoverDate(prop.date)) {
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1132
                            prop = null;
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1133
                        }
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1134
                    } catch (ParseException ex) {
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1135
                        prop = null;
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1136
                    }
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1137
                }
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1138
            }
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1139
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1140
            if (prop == null) {
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1141
                info("The property entry for " + ctry + " is invalid."
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1142
                        + " Ignored.", null);
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1143
            }
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1144
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1145
            return Optional.ofNullable(prop);
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1146
        }
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1147
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1148
        /*
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1149
         * Parse properties entry and return CurrencyProperty instance
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1150
         */
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1151
        private static CurrencyProperty parseProperty(String ctry,
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1152
                String curData, Pattern pattern) {
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1153
            Matcher m = pattern.matcher(curData);
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1154
            if (!m.find()) {
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1155
                return null;
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1156
            } else {
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1157
                return new CurrencyProperty(ctry, m.group(1),
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1158
                        Integer.parseInt(m.group(3)),
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1159
                        Integer.parseInt(m.group(2)), m.group(4));
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1160
            }
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1161
        }
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1162
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1163
        /**
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1164
         * Checks if the given list contains multiple inconsistent currency instances
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1165
         */
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1166
        private static boolean containsInconsistentInstances(
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1167
                List<CurrencyProperty> list) {
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1168
            int numCode = list.get(0).numericCode;
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1169
            int fractionDigit = list.get(0).fraction;
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1170
            return list.stream().anyMatch(prop -> prop.numericCode != numCode
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1171
                    || prop.fraction != fractionDigit);
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1172
        }
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1173
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1174
        private static boolean isPastCutoverDate(String s)
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1175
                throws ParseException {
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1176
            SimpleDateFormat format = new SimpleDateFormat(
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1177
                    "yyyy-MM-dd'T'HH:mm:ss", Locale.ROOT);
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1178
            format.setTimeZone(TimeZone.getTimeZone("UTC"));
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1179
            format.setLenient(false);
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1180
            long time = format.parse(s.trim()).getTime();
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1181
            return System.currentTimeMillis() > time;
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1182
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1183
        }
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1184
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1185
        private static void info(String message, Throwable t) {
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1186
            PlatformLogger logger = PlatformLogger
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1187
                    .getLogger("java.util.Currency");
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1188
            if (logger.isLoggable(PlatformLogger.Level.INFO)) {
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1189
                if (t != null) {
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1190
                    logger.info(message, t);
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1191
                } else {
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1192
                    logger.info(message);
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1193
                }
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1194
            }
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1195
        }
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1196
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1197
    }
28d8fc8cd3cd 8190904: Incorrect currency instance returned by java.util.Currency.getInstance()
nishjain
parents: 48251
diff changeset
  1198
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
}