📄 currency.java
字号:
/* * @(#)Currency.java 1.4 03/01/23 * * Copyright 2003 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package java.util;import java.io.Serializable;import java.security.AccessController;import java.security.PrivilegedAction;import sun.text.resources.LocaleData;/** * Represents a currency. Currencies are identified by their ISO 4217 currency * codes. See the * <a href="http://www.bsi-global.com/iso4217currency"> * ISO 4217 maintenance agency</a> for more information, including a table of * currency codes. * <p> * The class is designed so that there's never more than one * <code>Currency</code> instance for any given currency. Therefore, there's * no public constructor. You obtain a <code>Currency</code> instance using * the <code>getInstance</code> methods. * * @version 1.4, 01/23/03 * @since 1.4 */public final class Currency implements Serializable { /** * ISO 4217 currency code for this currency. * * @serial */ private String currencyCode; /** * Default fraction digits for this currency. * Set from currency data tables. */ transient private int defaultFractionDigits; // class data: instance map private static HashMap instances = new HashMap(7); // Class data: currency data obtained from java.util.CurrencyData. // Purpose: // - determine valid country codes // - determine valid currency codes // - map country codes to currency codes // - obtain default fraction digits for currency codes // // sc = special case; dfd = default fraction digits // Simple countries are those where the country code is a prefix of the // currency code, and there are no known plans to change the currency. // // table formats: // - mainTable: // - maps country code to 8-bit char // - 26*26 entries, corresponding to [A-Z]*[A-Z] // - \u007F -> not valid country // - bit 7 - 1: special case, bits 0-4 indicate which one // 0: simple country, bits 0-4 indicate final char of currency code // - bits 5-6: fraction digits for simple countries, 0 for special cases // - bits 0-4: final char for currency code for simple country, or ID of special case // - special case IDs: // - 0: country has no currency // - other: index into sc* arrays + 1 // - scCutOverTimes: cut-over time in millis as returned by // System.currentTimeMillis for special case countries that are changing // currencies; Long.MAX_VALUE for countries that are not changing currencies // - scOldCurrencies: old currencies for special case countries // - scNewCurrencies: new currencies for special case countries that are // changing currencies; null for others // - scOldCurrenciesDFD: default fraction digits for old currencies // - scNewCurrenciesDFD: default fraction digits for new currencies, 0 for // countries that are not changing currencies // - otherCurrencies: concatenation of all currency codes that are not the // main currency of a simple country, separated by "-" // - otherCurrenciesDFD: decimal format digits for currencies in otherCurrencies, same order static String mainTable; static long[] scCutOverTimes; static String[] scOldCurrencies; static String[] scNewCurrencies; static int[] scOldCurrenciesDFD; static int[] scNewCurrenciesDFD; static String otherCurrencies; static int[] otherCurrenciesDFD; // handy constants - must match definitions in GenerateCurrencyData // number of characters from A to Z private static final int A_TO_Z = ('Z' - 'A') + 1; // entry for invalid country codes private static final int INVALID_COUNTRY_ENTRY = 0x007F; // entry for countries without currency private static final int COUNTRY_WITHOUT_CURRENCY_ENTRY = 0x0080; // mask for simple case country entries private static final int SIMPLE_CASE_COUNTRY_MASK = 0x0000; // mask for simple case country entry final character private static final int SIMPLE_CASE_COUNTRY_FINAL_CHAR_MASK = 0x001F; // mask for simple case country entry default currency digits private static final int SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_MASK = 0x0060; // shift count for simple case country entry default currency digits private static final int SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_SHIFT = 5; // mask for special case country entries private static final int SPECIAL_CASE_COUNTRY_MASK = 0x0080; // mask for special case country index private static final int SPECIAL_CASE_COUNTRY_INDEX_MASK = 0x001F; // delta from entry index component in main table to index into special case tables private static final int SPECIAL_CASE_COUNTRY_INDEX_DELTA = 1; // mask for distinguishing simple and special case countries private static final int COUNTRY_TYPE_MASK = SIMPLE_CASE_COUNTRY_MASK | SPECIAL_CASE_COUNTRY_MASK; static { AccessController.doPrivileged(new PrivilegedAction() { public Object run() { try { Class data = Class.forName("java.util.CurrencyData"); mainTable = (String) data.getDeclaredField("mainTable").get(data); scCutOverTimes = (long[]) data.getDeclaredField("scCutOverTimes").get(data); scOldCurrencies = (String[]) data.getDeclaredField("scOldCurrencies").get(data); scNewCurrencies = (String[]) data.getDeclaredField("scNewCurrencies").get(data); scOldCurrenciesDFD = (int[]) data.getDeclaredField("scOldCurrenciesDFD").get(data); scNewCurrenciesDFD = (int[]) data.getDeclaredField("scNewCurrenciesDFD").get(data); otherCurrencies = (String) data.getDeclaredField("otherCurrencies").get(data); otherCurrenciesDFD = (int[]) data.getDeclaredField("otherCurrenciesDFD").get(data); } catch (ClassNotFoundException e) { throw new InternalError(); } catch (NoSuchFieldException e) { throw new InternalError(); } catch (IllegalAccessException e) { throw new InternalError(); } return null; } }); } /** * Constructs a <code>Currency</code> instance. The constructor is private * so that we can insure that there's never more than one instance for a * given currency. */ private Currency(String currencyCode, int defaultFractionDigits) { this.currencyCode = currencyCode; this.defaultFractionDigits = defaultFractionDigits; } /** * Returns the <code>Currency</code> instance for the given currency code. * * @param currencyCode the ISO 4217 code of the currency * @return the <code>Currency</code> instance for the given currency code * @exception NullPointerException if <code>currencyCode</code> is null * @exception IllegalArgumentException if <code>currencyCode</code> is not * a supported ISO 4217 code. */ public static Currency getInstance(String currencyCode) { return getInstance(currencyCode, Integer.MIN_VALUE); } private static Currency getInstance(String currencyCode, int defaultFractionDigits) { synchronized (instances) { // Try to look up the currency code in the instances table. // This does the null pointer check as a side effect. // Also, if there already is an entry, the currencyCode must be valid. Currency instance = (Currency) instances.get(currencyCode); if (instance != null) { return instance; } if (defaultFractionDigits == Integer.MIN_VALUE) { // Currency code not internally generated, need to verify first // A currency code must have 3 characters and exist in the main table // or in the list of other currencies.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -