Fields Summary |
---|
private static final long | serialVersionUID |
private final String | currencyCodeISO 4217 currency code for this currency. |
private final transient int | defaultFractionDigitsDefault fraction digits for this currency.
Set from currency data tables. |
private static HashMap | instances |
static String | mainTable |
static long[] | scCutOverTimes |
static String[] | scOldCurrencies |
static String[] | scNewCurrencies |
static int[] | scOldCurrenciesDFD |
static int[] | scNewCurrenciesDFD |
static String | otherCurrencies |
static int[] | otherCurrenciesDFD |
private static final int | A_TO_Z |
private static final int | INVALID_COUNTRY_ENTRY |
private static final int | COUNTRY_WITHOUT_CURRENCY_ENTRY |
private static final int | SIMPLE_CASE_COUNTRY_MASK |
private static final int | SIMPLE_CASE_COUNTRY_FINAL_CHAR_MASK |
private static final int | SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_MASK |
private static final int | SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_SHIFT |
private static final int | SPECIAL_CASE_COUNTRY_MASK |
private static final int | SPECIAL_CASE_COUNTRY_INDEX_MASK |
private static final int | SPECIAL_CASE_COUNTRY_INDEX_DELTA |
private static final int | COUNTRY_TYPE_MASK |
Methods Summary |
---|
public java.lang.String | getCurrencyCode()Gets the ISO 4217 currency code of this currency.
return currencyCode;
|
public int | getDefaultFractionDigits()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 defaultFractionDigits;
|
public static java.util.Currency | getInstance(java.lang.String currencyCode)Returns the Currency instance for the given currency code.
return getInstance(currencyCode, Integer.MIN_VALUE);
|
private static java.util.Currency | getInstance(java.lang.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.
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;
}
|
public static java.util.Currency | getInstance(java.util.Locale locale)Returns the Currency 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.
The method returns null for territories that don't
have a currency, such as Antarctica.
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]);
}
}
}
|
private static int | getMainTableEntry(char char1, char char2)Gets the main table entry for the country whose country code consists
of char1 and char2.
if (char1 < 'A" || char1 > 'Z" || char2 < 'A" || char2 > 'Z") {
throw new IllegalArgumentException();
}
return mainTable.charAt((char1 - 'A") * A_TO_Z + (char2 - 'A"));
|
public java.lang.String | getSymbol()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 getSymbol(Locale.getDefault());
|
public java.lang.String | getSymbol(java.util.Locale locale)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.
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;
|
private java.lang.Object | readResolve()Resolves instances being deserialized to a single instance per currency.
return getInstance(currencyCode);
|
public java.lang.String | toString()Returns the ISO 4217 currency code of this currency.
return currencyCode;
|