currency.java

来自「This is a resource based on j2me embedde」· Java 代码 · 共 384 行 · 第 1/2 页

JAVA
384
字号
/* * @(#)Currency.java	1.7 06/10/10 * * Copyright  1990-2008 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER *  * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation.  *  * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt).  *  * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA  *  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions.  */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.7, 10/10/06 * @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.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?