📄 currency.java
字号:
if (currencyCode.length() != 3) { throw new IllegalArgumentException(); } char char1 = currencyCode.charAt(0); char char2 = currencyCode.charAt(1); int tableEntry = getMainTableEntry(char1, char2); if ((tableEntry & COUNTRY_TYPE_MASK) == SIMPLE_CASE_COUNTRY_MASK && tableEntry != INVALID_COUNTRY_ENTRY && currencyCode.charAt(2) - 'A' == (tableEntry & SIMPLE_CASE_COUNTRY_FINAL_CHAR_MASK)) { defaultFractionDigits = (tableEntry & SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_MASK) >> SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_SHIFT; } else { // Check for '-' separately so we don't get false hits in the table. if (currencyCode.charAt(2) == '-') { throw new IllegalArgumentException(); } int index = otherCurrencies.indexOf(currencyCode); if (index == -1) { throw new IllegalArgumentException(); } defaultFractionDigits = otherCurrenciesDFD[index / 4]; } } instance = new Currency(currencyCode, defaultFractionDigits); instances.put(currencyCode, instance); return instance; } } /** * Returns the <code>Currency</code> instance for the country of the * given locale. The language and variant components of the locale * are ignored. The result may vary over time, as countries change their * currencies. For example, for the original member countries of the * European Monetary Union, the method returns the old national currencies * until December 31, 2001, and the Euro from January 1, 2002, local time * of the respective countries. * <p> * The method returns <code>null</code> for territories that don't * have a currency, such as Antarctica. * * @param locale the locale for whose country a <code>Currency</code> * instance is needed * @return the <code>Currency</code> instance for the country of the given * locale, or null * @exception NullPointerException if <code>locale</code> or its country * code is null * @exception IllegalArgumentException if the country of the given locale * is not a supported ISO 3166 country code. */ public static Currency getInstance(Locale locale) { String country = locale.getCountry(); if (country == null) { throw new NullPointerException(); } if (country.length() != 2) { throw new IllegalArgumentException(); } char char1 = country.charAt(0); char char2 = country.charAt(1); int tableEntry = getMainTableEntry(char1, char2); if ((tableEntry & COUNTRY_TYPE_MASK) == SIMPLE_CASE_COUNTRY_MASK && tableEntry != INVALID_COUNTRY_ENTRY) { char finalChar = (char) ((tableEntry & SIMPLE_CASE_COUNTRY_FINAL_CHAR_MASK) + 'A'); int defaultFractionDigits = (tableEntry & SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_MASK) >> SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_SHIFT; StringBuffer sb = new StringBuffer(country); sb.append(finalChar); return getInstance(sb.toString(), defaultFractionDigits); } else { // special cases if (tableEntry == INVALID_COUNTRY_ENTRY) { throw new IllegalArgumentException(); } if (tableEntry == COUNTRY_WITHOUT_CURRENCY_ENTRY) { return null; } else { int index = (tableEntry & SPECIAL_CASE_COUNTRY_INDEX_MASK) - SPECIAL_CASE_COUNTRY_INDEX_DELTA; if (scCutOverTimes[index] == Long.MAX_VALUE || System.currentTimeMillis() < scCutOverTimes[index]) { return getInstance(scOldCurrencies[index], scOldCurrenciesDFD[index]); } else { return getInstance(scNewCurrencies[index], scNewCurrenciesDFD[index]); } } } } /** * Gets the ISO 4217 currency code of this currency. * * @return the ISO 4217 currency code of this currency. */ public String getCurrencyCode() { return currencyCode; } /** * Gets the symbol of this currency for the default locale. * For example, for the US Dollar, the symbol is "$" if the default * locale is the US, while for other locales it may be "US$". If no * symbol can be determined, the ISO 4217 currency code is returned. * * @return the symbol of this currency for the default locale */ public String getSymbol() { return getSymbol(Locale.getDefault()); } /** * Gets the symbol of this currency for the specified locale. * For example, for the US Dollar, the symbol is "$" if the specified * locale is the US, while for other locales it may be "US$". If no * symbol can be determined, the ISO 4217 currency code is returned. * * @param locale the locale for which a display name for this currency is * needed * @return the symbol of this currency for the specified locale * @exception NullPointerException if <code>locale</code> is null */ public String getSymbol(Locale locale) { ResourceBundle bundle; try { bundle = LocaleData.getLocaleElements(locale); } catch (MissingResourceException e) { // use currency code as symbol of last resort return currencyCode; } String[][] symbols = (String[][]) bundle.getObject("CurrencySymbols"); if (symbols != null) { for (int i = 0; i < symbols.length; i++) { if (symbols[i][0].equals(currencyCode)) { return symbols[i][1]; } } } // use currency code as symbol of last resort return currencyCode; } /** * Gets the default number of fraction digits used with this currency. * For example, the default number of fraction digits for the Euro is 2, * while for the Japanese Yen it's 0. * In the case of pseudo-currencies, such as IMF Special Drawing Rights, * -1 is returned. * * @return the default number of fraction digits used with this currency */ public int getDefaultFractionDigits() { return defaultFractionDigits; } /** * Returns the ISO 4217 currency code of this currency. * * @return the ISO 4217 currency code of this currency */ public String toString() { return currencyCode; } /** * Resolves instances being deserialized to a single instance per currency. */ private Object readResolve() { return getInstance(currencyCode); } /** * Gets the main table entry for the country whose country code consists * of char1 and char2. */ private static int getMainTableEntry(char char1, char char2) { if (char1 < 'A' || char1 > 'Z' || char2 < 'A' || char2 > 'Z') { throw new IllegalArgumentException(); } return mainTable.charAt((char1 - 'A') * A_TO_Z + (char2 - 'A')); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -