Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/util/Currency.java
38829 views
/*1* Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.util;2627import java.io.BufferedInputStream;28import java.io.DataInputStream;29import java.io.File;30import java.io.FileInputStream;31import java.io.FileReader;32import java.io.IOException;33import java.io.Serializable;34import java.security.AccessController;35import java.security.PrivilegedAction;36import java.text.ParseException;37import java.text.SimpleDateFormat;38import java.util.concurrent.ConcurrentHashMap;39import java.util.concurrent.ConcurrentMap;40import java.util.regex.Pattern;41import java.util.regex.Matcher;42import java.util.spi.CurrencyNameProvider;43import sun.util.locale.provider.LocaleServiceProviderPool;44import sun.util.logging.PlatformLogger;454647/**48* Represents a currency. Currencies are identified by their ISO 4217 currency49* codes. Visit the <a href="http://www.iso.org/iso/home/standards/currency_codes.htm">50* ISO web site</a> for more information.51* <p>52* The class is designed so that there's never more than one53* <code>Currency</code> instance for any given currency. Therefore, there's54* no public constructor. You obtain a <code>Currency</code> instance using55* the <code>getInstance</code> methods.56* <p>57* Users can supersede the Java runtime currency data by means of the system58* property {@code java.util.currency.data}. If this system property is59* defined then its value is the location of a properties file, the contents of60* which are key/value pairs of the ISO 3166 country codes and the ISO 421761* currency data respectively. The value part consists of three ISO 4217 values62* of a currency, i.e., an alphabetic code, a numeric code, and a minor unit.63* Those three ISO 4217 values are separated by commas.64* The lines which start with '#'s are considered comment lines. An optional UTC65* timestamp may be specified per currency entry if users need to specify a66* cutover date indicating when the new data comes into effect. The timestamp is67* appended to the end of the currency properties and uses a comma as a separator.68* If a UTC datestamp is present and valid, the JRE will only use the new currency69* properties if the current UTC date is later than the date specified at class70* loading time. The format of the timestamp must be of ISO 8601 format :71* {@code 'yyyy-MM-dd'T'HH:mm:ss'}. For example,72* <p>73* <code>74* #Sample currency properties<br>75* JP=JPZ,999,076* </code>77* <p>78* will supersede the currency data for Japan.79*80* <p>81* <code>82* #Sample currency properties with cutover date<br>83* JP=JPZ,999,0,2014-01-01T00:00:0084* </code>85* <p>86* will supersede the currency data for Japan if {@code Currency} class is loaded after87* 1st January 2014 00:00:00 GMT.88* <p>89* Where syntactically malformed entries are encountered, the entry is ignored90* and the remainder of entries in file are processed. For instances where duplicate91* country code entries exist, the behavior of the Currency information for that92* {@code Currency} is undefined and the remainder of entries in file are processed.93*94* @since 1.495*/96public final class Currency implements Serializable {9798private static final long serialVersionUID = -158308464356906721L;99100/**101* ISO 4217 currency code for this currency.102*103* @serial104*/105private final String currencyCode;106107/**108* Default fraction digits for this currency.109* Set from currency data tables.110*/111transient private final int defaultFractionDigits;112113/**114* ISO 4217 numeric code for this currency.115* Set from currency data tables.116*/117transient private final int numericCode;118119120// class data: instance map121122private static ConcurrentMap<String, Currency> instances = new ConcurrentHashMap<>(7);123private static HashSet<Currency> available;124125// Class data: currency data obtained from currency.data file.126// Purpose:127// - determine valid country codes128// - determine valid currency codes129// - map country codes to currency codes130// - obtain default fraction digits for currency codes131//132// sc = special case; dfd = default fraction digits133// Simple countries are those where the country code is a prefix of the134// currency code, and there are no known plans to change the currency.135//136// table formats:137// - mainTable:138// - maps country code to 32-bit int139// - 26*26 entries, corresponding to [A-Z]*[A-Z]140// - \u007F -> not valid country141// - bits 20-31: unused142// - bits 10-19: numeric code (0 to 1023)143// - bit 9: 1 - special case, bits 0-4 indicate which one144// 0 - simple country, bits 0-4 indicate final char of currency code145// - bits 5-8: fraction digits for simple countries, 0 for special cases146// - bits 0-4: final char for currency code for simple country, or ID of special case147// - special case IDs:148// - 0: country has no currency149// - other: index into sc* arrays + 1150// - scCutOverTimes: cut-over time in millis as returned by151// System.currentTimeMillis for special case countries that are changing152// currencies; Long.MAX_VALUE for countries that are not changing currencies153// - scOldCurrencies: old currencies for special case countries154// - scNewCurrencies: new currencies for special case countries that are155// changing currencies; null for others156// - scOldCurrenciesDFD: default fraction digits for old currencies157// - scNewCurrenciesDFD: default fraction digits for new currencies, 0 for158// countries that are not changing currencies159// - otherCurrencies: concatenation of all currency codes that are not the160// main currency of a simple country, separated by "-"161// - otherCurrenciesDFD: decimal format digits for currencies in otherCurrencies, same order162163static int formatVersion;164static int dataVersion;165static int[] mainTable;166static long[] scCutOverTimes;167static String[] scOldCurrencies;168static String[] scNewCurrencies;169static int[] scOldCurrenciesDFD;170static int[] scNewCurrenciesDFD;171static int[] scOldCurrenciesNumericCode;172static int[] scNewCurrenciesNumericCode;173static String otherCurrencies;174static int[] otherCurrenciesDFD;175static int[] otherCurrenciesNumericCode;176177// handy constants - must match definitions in GenerateCurrencyData178// magic number179private static final int MAGIC_NUMBER = 0x43757244;180// number of characters from A to Z181private static final int A_TO_Z = ('Z' - 'A') + 1;182// entry for invalid country codes183private static final int INVALID_COUNTRY_ENTRY = 0x0000007F;184// entry for countries without currency185private static final int COUNTRY_WITHOUT_CURRENCY_ENTRY = 0x00000200;186// mask for simple case country entries187private static final int SIMPLE_CASE_COUNTRY_MASK = 0x00000000;188// mask for simple case country entry final character189private static final int SIMPLE_CASE_COUNTRY_FINAL_CHAR_MASK = 0x0000001F;190// mask for simple case country entry default currency digits191private static final int SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_MASK = 0x000001E0;192// shift count for simple case country entry default currency digits193private static final int SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_SHIFT = 5;194// maximum number for simple case country entry default currency digits195private static final int SIMPLE_CASE_COUNTRY_MAX_DEFAULT_DIGITS = 9;196// mask for special case country entries197private static final int SPECIAL_CASE_COUNTRY_MASK = 0x00000200;198// mask for special case country index199private static final int SPECIAL_CASE_COUNTRY_INDEX_MASK = 0x0000001F;200// delta from entry index component in main table to index into special case tables201private static final int SPECIAL_CASE_COUNTRY_INDEX_DELTA = 1;202// mask for distinguishing simple and special case countries203private static final int COUNTRY_TYPE_MASK = SIMPLE_CASE_COUNTRY_MASK | SPECIAL_CASE_COUNTRY_MASK;204// mask for the numeric code of the currency205private static final int NUMERIC_CODE_MASK = 0x000FFC00;206// shift count for the numeric code of the currency207private static final int NUMERIC_CODE_SHIFT = 10;208209// Currency data format version210private static final int VALID_FORMAT_VERSION = 2;211212static {213AccessController.doPrivileged(new PrivilegedAction<Void>() {214@Override215public Void run() {216String homeDir = System.getProperty("java.home");217try {218String dataFile = homeDir + File.separator +219"lib" + File.separator + "currency.data";220try (DataInputStream dis = new DataInputStream(221new BufferedInputStream(222new FileInputStream(dataFile)))) {223if (dis.readInt() != MAGIC_NUMBER) {224throw new InternalError("Currency data is possibly corrupted");225}226formatVersion = dis.readInt();227if (formatVersion != VALID_FORMAT_VERSION) {228throw new InternalError("Currency data format is incorrect");229}230dataVersion = dis.readInt();231mainTable = readIntArray(dis, A_TO_Z * A_TO_Z);232int scCount = dis.readInt();233scCutOverTimes = readLongArray(dis, scCount);234scOldCurrencies = readStringArray(dis, scCount);235scNewCurrencies = readStringArray(dis, scCount);236scOldCurrenciesDFD = readIntArray(dis, scCount);237scNewCurrenciesDFD = readIntArray(dis, scCount);238scOldCurrenciesNumericCode = readIntArray(dis, scCount);239scNewCurrenciesNumericCode = readIntArray(dis, scCount);240int ocCount = dis.readInt();241otherCurrencies = dis.readUTF();242otherCurrenciesDFD = readIntArray(dis, ocCount);243otherCurrenciesNumericCode = readIntArray(dis, ocCount);244}245} catch (IOException e) {246throw new InternalError(e);247}248249// look for the properties file for overrides250String propsFile = System.getProperty("java.util.currency.data");251if (propsFile == null) {252propsFile = homeDir + File.separator + "lib" +253File.separator + "currency.properties";254}255try {256File propFile = new File(propsFile);257if (propFile.exists()) {258Properties props = new Properties();259try (FileReader fr = new FileReader(propFile)) {260props.load(fr);261}262Set<String> keys = props.stringPropertyNames();263Pattern propertiesPattern =264Pattern.compile("([A-Z]{3})\\s*,\\s*(\\d{3})\\s*,\\s*" +265"(\\d+)\\s*,?\\s*(\\d{4}-\\d{2}-\\d{2}T\\d{2}:" +266"\\d{2}:\\d{2})?");267for (String key : keys) {268replaceCurrencyData(propertiesPattern,269key.toUpperCase(Locale.ROOT),270props.getProperty(key).toUpperCase(Locale.ROOT));271}272}273} catch (IOException e) {274info("currency.properties is ignored because of an IOException", e);275}276return null;277}278});279}280281/**282* Constants for retrieving localized names from the name providers.283*/284private static final int SYMBOL = 0;285private static final int DISPLAYNAME = 1;286287288/**289* Constructs a <code>Currency</code> instance. The constructor is private290* so that we can insure that there's never more than one instance for a291* given currency.292*/293private Currency(String currencyCode, int defaultFractionDigits, int numericCode) {294this.currencyCode = currencyCode;295this.defaultFractionDigits = defaultFractionDigits;296this.numericCode = numericCode;297}298299/**300* Returns the <code>Currency</code> instance for the given currency code.301*302* @param currencyCode the ISO 4217 code of the currency303* @return the <code>Currency</code> instance for the given currency code304* @exception NullPointerException if <code>currencyCode</code> is null305* @exception IllegalArgumentException if <code>currencyCode</code> is not306* a supported ISO 4217 code.307*/308public static Currency getInstance(String currencyCode) {309return getInstance(currencyCode, Integer.MIN_VALUE, 0);310}311312private static Currency getInstance(String currencyCode, int defaultFractionDigits,313int numericCode) {314// Try to look up the currency code in the instances table.315// This does the null pointer check as a side effect.316// Also, if there already is an entry, the currencyCode must be valid.317Currency instance = instances.get(currencyCode);318if (instance != null) {319return instance;320}321322if (defaultFractionDigits == Integer.MIN_VALUE) {323// Currency code not internally generated, need to verify first324// A currency code must have 3 characters and exist in the main table325// or in the list of other currencies.326if (currencyCode.length() != 3) {327throw new IllegalArgumentException();328}329char char1 = currencyCode.charAt(0);330char char2 = currencyCode.charAt(1);331int tableEntry = getMainTableEntry(char1, char2);332if ((tableEntry & COUNTRY_TYPE_MASK) == SIMPLE_CASE_COUNTRY_MASK333&& tableEntry != INVALID_COUNTRY_ENTRY334&& currencyCode.charAt(2) - 'A' == (tableEntry & SIMPLE_CASE_COUNTRY_FINAL_CHAR_MASK)) {335defaultFractionDigits = (tableEntry & SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_MASK) >> SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_SHIFT;336numericCode = (tableEntry & NUMERIC_CODE_MASK) >> NUMERIC_CODE_SHIFT;337} else {338// Check for '-' separately so we don't get false hits in the table.339if (currencyCode.charAt(2) == '-') {340throw new IllegalArgumentException();341}342int index = otherCurrencies.indexOf(currencyCode);343if (index == -1) {344throw new IllegalArgumentException();345}346defaultFractionDigits = otherCurrenciesDFD[index / 4];347numericCode = otherCurrenciesNumericCode[index / 4];348}349}350351Currency currencyVal =352new Currency(currencyCode, defaultFractionDigits, numericCode);353instance = instances.putIfAbsent(currencyCode, currencyVal);354return (instance != null ? instance : currencyVal);355}356357/**358* Returns the <code>Currency</code> instance for the country of the359* given locale. The language and variant components of the locale360* are ignored. The result may vary over time, as countries change their361* currencies. For example, for the original member countries of the362* European Monetary Union, the method returns the old national currencies363* until December 31, 2001, and the Euro from January 1, 2002, local time364* of the respective countries.365* <p>366* The method returns <code>null</code> for territories that don't367* have a currency, such as Antarctica.368*369* @param locale the locale for whose country a <code>Currency</code>370* instance is needed371* @return the <code>Currency</code> instance for the country of the given372* locale, or {@code null}373* @exception NullPointerException if <code>locale</code> or its country374* code is {@code null}375* @exception IllegalArgumentException if the country of the given {@code locale}376* is not a supported ISO 3166 country code.377*/378public static Currency getInstance(Locale locale) {379String country = locale.getCountry();380if (country == null) {381throw new NullPointerException();382}383384if (country.length() != 2) {385throw new IllegalArgumentException();386}387388char char1 = country.charAt(0);389char char2 = country.charAt(1);390int tableEntry = getMainTableEntry(char1, char2);391if ((tableEntry & COUNTRY_TYPE_MASK) == SIMPLE_CASE_COUNTRY_MASK392&& tableEntry != INVALID_COUNTRY_ENTRY) {393char finalChar = (char) ((tableEntry & SIMPLE_CASE_COUNTRY_FINAL_CHAR_MASK) + 'A');394int defaultFractionDigits = (tableEntry & SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_MASK) >> SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_SHIFT;395int numericCode = (tableEntry & NUMERIC_CODE_MASK) >> NUMERIC_CODE_SHIFT;396StringBuilder sb = new StringBuilder(country);397sb.append(finalChar);398return getInstance(sb.toString(), defaultFractionDigits, numericCode);399} else {400// special cases401if (tableEntry == INVALID_COUNTRY_ENTRY) {402throw new IllegalArgumentException();403}404if (tableEntry == COUNTRY_WITHOUT_CURRENCY_ENTRY) {405return null;406} else {407int index = (tableEntry & SPECIAL_CASE_COUNTRY_INDEX_MASK) - SPECIAL_CASE_COUNTRY_INDEX_DELTA;408if (scCutOverTimes[index] == Long.MAX_VALUE || System.currentTimeMillis() < scCutOverTimes[index]) {409return getInstance(scOldCurrencies[index], scOldCurrenciesDFD[index],410scOldCurrenciesNumericCode[index]);411} else {412return getInstance(scNewCurrencies[index], scNewCurrenciesDFD[index],413scNewCurrenciesNumericCode[index]);414}415}416}417}418419/**420* Gets the set of available currencies. The returned set of currencies421* contains all of the available currencies, which may include currencies422* that represent obsolete ISO 4217 codes. The set can be modified423* without affecting the available currencies in the runtime.424*425* @return the set of available currencies. If there is no currency426* available in the runtime, the returned set is empty.427* @since 1.7428*/429public static Set<Currency> getAvailableCurrencies() {430synchronized(Currency.class) {431if (available == null) {432available = new HashSet<>(256);433434// Add simple currencies first435for (char c1 = 'A'; c1 <= 'Z'; c1 ++) {436for (char c2 = 'A'; c2 <= 'Z'; c2 ++) {437int tableEntry = getMainTableEntry(c1, c2);438if ((tableEntry & COUNTRY_TYPE_MASK) == SIMPLE_CASE_COUNTRY_MASK439&& tableEntry != INVALID_COUNTRY_ENTRY) {440char finalChar = (char) ((tableEntry & SIMPLE_CASE_COUNTRY_FINAL_CHAR_MASK) + 'A');441int defaultFractionDigits = (tableEntry & SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_MASK) >> SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_SHIFT;442int numericCode = (tableEntry & NUMERIC_CODE_MASK) >> NUMERIC_CODE_SHIFT;443StringBuilder sb = new StringBuilder();444sb.append(c1);445sb.append(c2);446sb.append(finalChar);447available.add(getInstance(sb.toString(), defaultFractionDigits, numericCode));448}449}450}451452// Now add other currencies453StringTokenizer st = new StringTokenizer(otherCurrencies, "-");454while (st.hasMoreElements()) {455available.add(getInstance((String)st.nextElement()));456}457}458}459460@SuppressWarnings("unchecked")461Set<Currency> result = (Set<Currency>) available.clone();462return result;463}464465/**466* Gets the ISO 4217 currency code of this currency.467*468* @return the ISO 4217 currency code of this currency.469*/470public String getCurrencyCode() {471return currencyCode;472}473474/**475* Gets the symbol of this currency for the default476* {@link Locale.Category#DISPLAY DISPLAY} locale.477* For example, for the US Dollar, the symbol is "$" if the default478* locale is the US, while for other locales it may be "US$". If no479* symbol can be determined, the ISO 4217 currency code is returned.480* <p>481* This is equivalent to calling482* {@link #getSymbol(Locale)483* getSymbol(Locale.getDefault(Locale.Category.DISPLAY))}.484*485* @return the symbol of this currency for the default486* {@link Locale.Category#DISPLAY DISPLAY} locale487*/488public String getSymbol() {489return getSymbol(Locale.getDefault(Locale.Category.DISPLAY));490}491492/**493* Gets the symbol of this currency for the specified locale.494* For example, for the US Dollar, the symbol is "$" if the specified495* locale is the US, while for other locales it may be "US$". If no496* symbol can be determined, the ISO 4217 currency code is returned.497*498* @param locale the locale for which a display name for this currency is499* needed500* @return the symbol of this currency for the specified locale501* @exception NullPointerException if <code>locale</code> is null502*/503public String getSymbol(Locale locale) {504LocaleServiceProviderPool pool =505LocaleServiceProviderPool.getPool(CurrencyNameProvider.class);506String symbol = pool.getLocalizedObject(507CurrencyNameGetter.INSTANCE,508locale, currencyCode, SYMBOL);509if (symbol != null) {510return symbol;511}512513// use currency code as symbol of last resort514return currencyCode;515}516517/**518* Gets the default number of fraction digits used with this currency.519* For example, the default number of fraction digits for the Euro is 2,520* while for the Japanese Yen it's 0.521* In the case of pseudo-currencies, such as IMF Special Drawing Rights,522* -1 is returned.523*524* @return the default number of fraction digits used with this currency525*/526public int getDefaultFractionDigits() {527return defaultFractionDigits;528}529530/**531* Returns the ISO 4217 numeric code of this currency.532*533* @return the ISO 4217 numeric code of this currency534* @since 1.7535*/536public int getNumericCode() {537return numericCode;538}539540/**541* Gets the name that is suitable for displaying this currency for542* the default {@link Locale.Category#DISPLAY DISPLAY} locale.543* If there is no suitable display name found544* for the default locale, the ISO 4217 currency code is returned.545* <p>546* This is equivalent to calling547* {@link #getDisplayName(Locale)548* getDisplayName(Locale.getDefault(Locale.Category.DISPLAY))}.549*550* @return the display name of this currency for the default551* {@link Locale.Category#DISPLAY DISPLAY} locale552* @since 1.7553*/554public String getDisplayName() {555return getDisplayName(Locale.getDefault(Locale.Category.DISPLAY));556}557558/**559* Gets the name that is suitable for displaying this currency for560* the specified locale. If there is no suitable display name found561* for the specified locale, the ISO 4217 currency code is returned.562*563* @param locale the locale for which a display name for this currency is564* needed565* @return the display name of this currency for the specified locale566* @exception NullPointerException if <code>locale</code> is null567* @since 1.7568*/569public String getDisplayName(Locale locale) {570LocaleServiceProviderPool pool =571LocaleServiceProviderPool.getPool(CurrencyNameProvider.class);572String result = pool.getLocalizedObject(573CurrencyNameGetter.INSTANCE,574locale, currencyCode, DISPLAYNAME);575if (result != null) {576return result;577}578579// use currency code as symbol of last resort580return currencyCode;581}582583/**584* Returns the ISO 4217 currency code of this currency.585*586* @return the ISO 4217 currency code of this currency587*/588@Override589public String toString() {590return currencyCode;591}592593/**594* Resolves instances being deserialized to a single instance per currency.595*/596private Object readResolve() {597return getInstance(currencyCode);598}599600/**601* Gets the main table entry for the country whose country code consists602* of char1 and char2.603*/604private static int getMainTableEntry(char char1, char char2) {605if (char1 < 'A' || char1 > 'Z' || char2 < 'A' || char2 > 'Z') {606throw new IllegalArgumentException();607}608return mainTable[(char1 - 'A') * A_TO_Z + (char2 - 'A')];609}610611/**612* Sets the main table entry for the country whose country code consists613* of char1 and char2.614*/615private static void setMainTableEntry(char char1, char char2, int entry) {616if (char1 < 'A' || char1 > 'Z' || char2 < 'A' || char2 > 'Z') {617throw new IllegalArgumentException();618}619mainTable[(char1 - 'A') * A_TO_Z + (char2 - 'A')] = entry;620}621622/**623* Obtains a localized currency names from a CurrencyNameProvider624* implementation.625*/626private static class CurrencyNameGetter627implements LocaleServiceProviderPool.LocalizedObjectGetter<CurrencyNameProvider,628String> {629private static final CurrencyNameGetter INSTANCE = new CurrencyNameGetter();630631@Override632public String getObject(CurrencyNameProvider currencyNameProvider,633Locale locale,634String key,635Object... params) {636assert params.length == 1;637int type = (Integer)params[0];638639switch(type) {640case SYMBOL:641return currencyNameProvider.getSymbol(key, locale);642case DISPLAYNAME:643return currencyNameProvider.getDisplayName(key, locale);644default:645assert false; // shouldn't happen646}647648return null;649}650}651652private static int[] readIntArray(DataInputStream dis, int count) throws IOException {653int[] ret = new int[count];654for (int i = 0; i < count; i++) {655ret[i] = dis.readInt();656}657658return ret;659}660661private static long[] readLongArray(DataInputStream dis, int count) throws IOException {662long[] ret = new long[count];663for (int i = 0; i < count; i++) {664ret[i] = dis.readLong();665}666667return ret;668}669670private static String[] readStringArray(DataInputStream dis, int count) throws IOException {671String[] ret = new String[count];672for (int i = 0; i < count; i++) {673ret[i] = dis.readUTF();674}675676return ret;677}678679/**680* Replaces currency data found in the currencydata.properties file681*682* @param pattern regex pattern for the properties683* @param ctry country code684* @param curdata currency data. This is a comma separated string that685* consists of "three-letter alphabet code", "three-digit numeric code",686* and "one-digit (0-9) default fraction digit".687* For example, "JPZ,392,0".688* An optional UTC date can be appended to the string (comma separated)689* to allow a currency change take effect after date specified.690* For example, "JP=JPZ,999,0,2014-01-01T00:00:00" has no effect unless691* UTC time is past 1st January 2014 00:00:00 GMT.692*/693private static void replaceCurrencyData(Pattern pattern, String ctry, String curdata) {694695if (ctry.length() != 2) {696// ignore invalid country code697info("currency.properties entry for " + ctry +698" is ignored because of the invalid country code.", null);699return;700}701702Matcher m = pattern.matcher(curdata);703if (!m.find() || (m.group(4) == null && countOccurrences(curdata, ',') >= 3)) {704// format is not recognized. ignore the data705// if group(4) date string is null and we've 4 values, bad date value706info("currency.properties entry for " + ctry +707" ignored because the value format is not recognized.", null);708return;709}710711try {712if (m.group(4) != null && !isPastCutoverDate(m.group(4))) {713info("currency.properties entry for " + ctry +714" ignored since cutover date has not passed :" + curdata, null);715return;716}717} catch (ParseException ex) {718info("currency.properties entry for " + ctry +719" ignored since exception encountered :" + ex.getMessage(), null);720return;721}722723String code = m.group(1);724int numeric = Integer.parseInt(m.group(2));725int entry = numeric << NUMERIC_CODE_SHIFT;726int fraction = Integer.parseInt(m.group(3));727if (fraction > SIMPLE_CASE_COUNTRY_MAX_DEFAULT_DIGITS) {728info("currency.properties entry for " + ctry +729" ignored since the fraction is more than " +730SIMPLE_CASE_COUNTRY_MAX_DEFAULT_DIGITS + ":" + curdata, null);731return;732}733734int index;735for (index = 0; index < scOldCurrencies.length; index++) {736if (scOldCurrencies[index].equals(code)) {737break;738}739}740741if (index == scOldCurrencies.length) {742// simple case743entry |= (fraction << SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_SHIFT) |744(code.charAt(2) - 'A');745} else {746// special case747entry |= SPECIAL_CASE_COUNTRY_MASK |748(index + SPECIAL_CASE_COUNTRY_INDEX_DELTA);749}750setMainTableEntry(ctry.charAt(0), ctry.charAt(1), entry);751}752753private static boolean isPastCutoverDate(String s) throws ParseException {754SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.ROOT);755format.setTimeZone(TimeZone.getTimeZone("UTC"));756format.setLenient(false);757long time = format.parse(s.trim()).getTime();758return System.currentTimeMillis() > time;759760}761762private static int countOccurrences(String value, char match) {763int count = 0;764for (char c : value.toCharArray()) {765if (c == match) {766++count;767}768}769return count;770}771772private static void info(String message, Throwable t) {773PlatformLogger logger = PlatformLogger.getLogger("java.util.Currency");774if (logger.isLoggable(PlatformLogger.Level.INFO)) {775if (t != null) {776logger.info(message, t);777} else {778logger.info(message);779}780}781}782}783784785