FileDocCategorySizeDatePackage
Currency.javaAPI DocAndroid 1.5 API7986Wed May 06 22:41:04 BST 2009java.util

Currency

public final class Currency extends Object implements Serializable
This class represents a currency as identified in the ISO 4217 currency codes.
since
Android 1.0

Fields Summary
private static final long
serialVersionUID
private static Hashtable
codesToCurrencies
private String
currencyCode
private static String
currencyVars
private transient int
defaultFractionDigits
Constructors Summary
private Currency(String currencyCode)

param
currencyCode


           
       
        this.currencyCode = currencyCode;
    
Methods Summary
public java.lang.StringgetCurrencyCode()
Returns this {@code Currency}'s ISO 4217 currency code.

return
this {@code Currency}'s ISO 4217 currency code.
since
Android 1.0

        return currencyCode;
    
public intgetDefaultFractionDigits()
Returns the default number of fraction digits for this currency. For instance, the default number of fraction digits for the US dollar is 2. For the Japanese Yen the number is 0. In the case of pseudo-currencies, such as IMF Special Drawing Rights, -1 is returned.

return
the default number of fraction digits for this currency.
since
Android 1.0

        return defaultFractionDigits;
    
public static java.util.CurrencygetInstance(java.lang.String currencyCode)
Returns the {@code Currency} instance for this currency code.

param
currencyCode the currency code.
return
the {@code Currency} instance for this currency code.
throws
IllegalArgumentException if the currency code is not a supported ISO 4217 currency code.
since
Android 1.0

        Currency currency = codesToCurrencies.get(currencyCode);

        if (currency == null) {
            ResourceBundle bundle = Locale.getBundle(
                    "ISO4CurrenciesToDigits", Locale.getDefault()); //$NON-NLS-1$
            currency = new Currency(currencyCode);

            String defaultFractionDigits = null;
            try {
                defaultFractionDigits = bundle.getString(currencyCode);
            } catch (MissingResourceException e) {
                throw new IllegalArgumentException(
                        org.apache.harmony.luni.util.Msg.getString(
                                "K0322", currencyCode)); //$NON-NLS-1$
            }
            currency.defaultFractionDigits = Integer
                    .parseInt(defaultFractionDigits);
            codesToCurrencies.put(currencyCode, currency);
        }

        return currency;
    
public static java.util.CurrencygetInstance(java.util.Locale locale)
Returns the {@code Currency} instance for this {@code Locale}'s country.

param
locale the {@code Locale} of a country.
return
the {@code Currency} used in the country defined by the locale parameter.
throws
IllegalArgumentException if the locale's country is not a supported ISO 3166 Country.
since
Android 1.0

        String country = locale.getCountry();
        String variant = locale.getVariant();
        if (!variant.equals("") && currencyVars.indexOf(variant) > -1) { //$NON-NLS-1$
            country = country + "_" + variant; //$NON-NLS-1$
        }

        ResourceBundle bundle = Locale.getBundle(
                "ISO4Currencies", Locale.getDefault()); //$NON-NLS-1$
        String currencyCode = null;
        try {
            currencyCode = bundle.getString(country);
        } catch (MissingResourceException e) {
            throw new IllegalArgumentException(Msg.getString(
                    "K0323", locale.toString())); //$NON-NLS-1$
        }

        if (currencyCode.equals("None")) { //$NON-NLS-1$
            return null;
        }

        return getInstance(currencyCode);
    
public java.lang.StringgetSymbol()
Returns the symbol for this currency in the default locale. For instance, if the default locale is the US, the symbol of the US dollar is "$". For other locales it may be "US$". If no symbol can be determined, the ISO 4217 currency code of the US dollar is returned.

return
the symbol for this {@code Currency} in the default {@code Locale}.
since
Android 1.0

        return getSymbol(Locale.getDefault());
    
public java.lang.StringgetSymbol(java.util.Locale locale)
Returns the symbol for this currency in the given {@code Locale}.

If the locale doesn't have any countries (e.g. {@code Locale.JAPANESE, new Locale("en","")}), the the ISO 4217 currency code is returned.

First the locale's resource bundle is checked, if the locale has the same currency, the CurrencySymbol in this locale bundle is returned.

Then a currency bundle for this locale is searched.

If a currency bundle for this locale does not exist, or there is no symbol for this currency in this bundle, then the ISO 4217 currency code is returned.

param
locale the locale for which the currency symbol should be returned.
return
the representation of this {@code Currency}'s symbol in the specified locale.
since
Android 1.0

        if (locale.getCountry().equals("")) { //$NON-NLS-1$
            return currencyCode;
        }

        // check in the Locale bundle first, if the local has the same currency
        ResourceBundle bundle = Locale.getBundle("Locale", locale); //$NON-NLS-1$
        if (((String) bundle.getObject("IntCurrencySymbol")) //$NON-NLS-1$
                .equals(currencyCode)) {
            return (String) bundle.getObject("CurrencySymbol"); //$NON-NLS-1$
        }

        // search for a Currency bundle
        bundle = null;
        try {
            bundle = Locale.getBundle("Currency", locale); //$NON-NLS-1$
        } catch (MissingResourceException e) {
            return currencyCode;
        }

        // is the bundle found for a different country? (for instance the
        // default locale's currency bundle)
        if (!bundle.getLocale().getCountry().equals(locale.getCountry())) {
            return currencyCode;
        }

        // check if the currency bundle for this locale
        // has an entry for this currency
        String result = (String) bundle.handleGetObject(currencyCode);
        if (result != null) {
            return result;
        }
        return currencyCode;
    
private java.lang.ObjectreadResolve()

        return getInstance(currencyCode);
    
public java.lang.StringtoString()
Returns this currency's ISO 4217 currency code.

return
this currency's ISO 4217 currency code.
since
Android 1.0

        return currencyCode;