java.io.Serializable, java.lang.Comparable<CurrencyUnit>public final class CurrencyUnit extends java.lang.Object implements java.lang.Comparable<CurrencyUnit>, java.io.Serializable
This class represents a unit of currency such as the British Pound, Euro or US Dollar.
The set of loaded currencies is provided by an instance of CurrencyUnitDataProvider.
The provider used is determined by the system property org.joda.money.CurrencyUnitDataProvider
which should be the fully qualified class name of the provider. The default provider loads the first
resource named /org/joda/money/MoneyData.csv on the classpath.
This class is immutable and thread-safe.
| Modifier and Type | Field | Description |
|---|---|---|
static CurrencyUnit |
AUD |
The currency 'AUD' - Australian Dollar.
|
static CurrencyUnit |
CAD |
The currency 'CAD' - Canadian Dollar.
|
static CurrencyUnit |
CHF |
The currency 'CHF' - Swiss Franc.
|
static CurrencyUnit |
EUR |
The currency 'EUR' - Euro.
|
static CurrencyUnit |
GBP |
The currency 'GBP' - British pound.
|
static CurrencyUnit |
JPY |
The currency 'JPY' - Japanese Yen.
|
static CurrencyUnit |
USD |
The currency 'USD' - United States Dollar.
|
| Modifier and Type | Method | Description |
|---|---|---|
int |
compareTo(CurrencyUnit other) |
Compares this currency to another by alphabetical comparison of the code.
|
boolean |
equals(java.lang.Object obj) |
Checks if this currency equals another currency.
|
java.lang.String |
getCode() |
Gets the ISO-4217 three-letter currency code.
|
java.util.Set<java.lang.String> |
getCountryCodes() |
Gets the country codes applicable to this currency.
|
int |
getDecimalPlaces() |
Gets the number of decimal places typically used by this currency.
|
java.lang.String |
getNumeric3Code() |
Gets the ISO-4217 numeric currency code as a three digit string.
|
int |
getNumericCode() |
Gets the ISO-4217 numeric currency code.
|
java.lang.String |
getSymbol() |
Gets the symbol for this locale from the JDK.
|
java.lang.String |
getSymbol(java.util.Locale locale) |
Gets the symbol for this locale from the JDK.
|
int |
hashCode() |
Returns a suitable hash code for the currency.
|
boolean |
isPseudoCurrency() |
Checks if this is a pseudo-currency.
|
static CurrencyUnit |
of(java.lang.String currencyCode) |
Obtains an instance of
CurrencyUnit for the specified three letter currency code. |
static CurrencyUnit |
of(java.util.Currency currency) |
Obtains an instance of
CurrencyUnit matching the specified JDK currency. |
static CurrencyUnit |
of(java.util.Locale locale) |
Obtains an instance of
CurrencyUnit for the specified locale. |
static CurrencyUnit |
ofCountry(java.lang.String countryCode) |
Obtains an instance of
CurrencyUnit for the specified ISO-3166 country code. |
static CurrencyUnit |
ofNumericCode(int numericCurrencyCode) |
Obtains an instance of
CurrencyUnit for the specified ISO-4217 numeric currency code. |
static CurrencyUnit |
ofNumericCode(java.lang.String numericCurrencyCode) |
Obtains an instance of
CurrencyUnit for the specified ISO-4217 numeric currency code. |
static void |
registerCountry(java.lang.String countryCode,
CurrencyUnit currency) |
Registers a country code, typically ISO 3166-1-alpha-2.
|
static CurrencyUnit |
registerCurrency(java.lang.String currencyCode,
int numericCurrencyCode,
int decimalPlaces,
boolean force) |
Registers a currency allowing it to be used, allowing replacement.
|
static CurrencyUnit |
registerCurrency(java.lang.String currencyCode,
int numericCurrencyCode,
int decimalPlaces,
java.util.List<java.lang.String> countryCodes) |
Registers a currency and associated countries allowing it to be used.
|
static CurrencyUnit |
registerCurrency(java.lang.String currencyCode,
int numericCurrencyCode,
int decimalPlaces,
java.util.List<java.lang.String> countryCodes,
boolean force) |
Registers a currency and associated countries allowing it to be used, allowing replacement.
|
static java.util.List<java.lang.String> |
registeredCountries() |
Gets the list of all registered countries.
|
static java.util.List<CurrencyUnit> |
registeredCurrencies() |
Gets the list of all registered currencies.
|
java.util.Currency |
toCurrency() |
Gets the JDK currency instance equivalent to this currency.
|
java.lang.String |
toString() |
Gets the currency code as a string.
|
public static final CurrencyUnit USD
public static final CurrencyUnit EUR
public static final CurrencyUnit JPY
public static final CurrencyUnit GBP
public static final CurrencyUnit CHF
public static final CurrencyUnit AUD
public static final CurrencyUnit CAD
public static CurrencyUnit registerCurrency(java.lang.String currencyCode, int numericCurrencyCode, int decimalPlaces, java.util.List<java.lang.String> countryCodes)
This class only permits known currencies to be returned. To achieve this, all currencies have to be registered in advance.
Since this method is public, it is possible to add currencies in application code. It is recommended to do this only at startup, however it is safe to do so later as the internal implementation is thread-safe.
The currency code must be three upper-case ASCII letters, based on ISO-4217. The numeric code must be from 0 to 999, or -1 if not applicable.
currencyCode - the three-letter upper-case currency code, not nullnumericCurrencyCode - the numeric currency code, from 0 to 999, -1 if nonedecimalPlaces - the number of decimal places that the currency
normally has, from 0 to 30 (normally 0, 2 or 3), or -1 for a pseudo-currencycountryCodes - the country codes to register the currency under, not nulljava.lang.IllegalArgumentException - if the code is already registered, or the
specified data is invalidpublic static CurrencyUnit registerCurrency(java.lang.String currencyCode, int numericCurrencyCode, int decimalPlaces, java.util.List<java.lang.String> countryCodes, boolean force)
This class only permits known currencies to be returned. To achieve this, all currencies have to be registered in advance.
Since this method is public, it is possible to add currencies in application code. It is recommended to do this only at startup, however it is safe to do so later as the internal implementation is thread-safe.
This method uses a flag to determine whether the registered currency must be new, or can replace an existing currency.
The currency code must be three upper-case ASCII letters, based on ISO-4217. The numeric code must be from 0 to 999, or -1 if not applicable.
currencyCode - the three-letter upper-case currency code, not nullnumericCurrencyCode - the numeric currency code, from 0 to 999, -1 if nonedecimalPlaces - the number of decimal places that the currency
normally has, from 0 to 30 (normally 0, 2 or 3), or -1 for a pseudo-currencycountryCodes - the country codes to register the currency under,
use of ISO-3166 is recommended, not nullforce - true to register forcefully, replacing any existing matching currency,
false to validate that there is no existing matching currencyjava.lang.IllegalArgumentException - if the code is already registered and force is false;
or if the specified data is invalidpublic static CurrencyUnit registerCurrency(java.lang.String currencyCode, int numericCurrencyCode, int decimalPlaces, boolean force)
This class only permits known currencies to be returned. To achieve this, all currencies have to be registered in advance.
Since this method is public, it is possible to add currencies in application code. It is recommended to do this only at startup, however it is safe to do so later as the internal implementation is thread-safe.
This method uses a flag to determine whether the registered currency must be new, or can replace an existing currency.
The currency code must be three upper-case ASCII letters, based on ISO-4217. The numeric code must be from 0 to 999, or -1 if not applicable.
currencyCode - the three-letter upper-case currency code, not nullnumericCurrencyCode - the numeric currency code, from 0 to 999, -1 if nonedecimalPlaces - the number of decimal places that the currency
normally has, from 0 to 30 (normally 0, 2 or 3), or -1 for a pseudo-currency
use of ISO-3166 is recommended, not nullforce - true to register forcefully, replacing any existing matching currency,
false to validate that there is no existing matching currencyjava.lang.IllegalArgumentException - if the code is already registered and force is false;
or if the specified data is invalidpublic static void registerCountry(java.lang.String countryCode,
CurrencyUnit currency)
This registers a country code and the associated currency.
The country code is typically from ISO 3166-1-alpha-2, and is therefore two upper-case ASCII letters.
If the country code already exists, the data is replaced.
countryCode - the country code, two upper case letters if following ISO 3166-1-alpha-2, not nullcurrency - the associated currencyjava.lang.IllegalArgumentException - if the code is already registered and force is false;
or if the specified data is invalidpublic static java.util.List<CurrencyUnit> registeredCurrencies()
This class only permits known currencies to be returned, thus this list is the complete list of valid singleton currencies. The list may change after application startup, however this isn't recommended.
public static java.util.List<java.lang.String> registeredCountries()
This returns the list of known countries. The list may change after application startup, however this isn't recommended.
public static CurrencyUnit of(java.util.Currency currency)
CurrencyUnit matching the specified JDK currency.
This converts the JDK currency instance to a currency unit using the code.
currency - the currency, not nullIllegalCurrencyException - if the currency is unknownpublic static CurrencyUnit of(java.lang.String currencyCode)
CurrencyUnit for the specified three letter currency code.
A currency is uniquely identified by a three letter code, based on ISO-4217. Valid currency codes are three upper-case ASCII letters.
currencyCode - the three-letter currency code, not nullIllegalCurrencyException - if the currency is unknownpublic static CurrencyUnit ofNumericCode(java.lang.String numericCurrencyCode)
CurrencyUnit for the specified ISO-4217 numeric currency code.
The numeric code is an alternative to the three letter code. This method is lenient and does not require the string to be left padded with zeroes.
numericCurrencyCode - the currency code, not nullIllegalCurrencyException - if the currency is unknownpublic static CurrencyUnit ofNumericCode(int numericCurrencyCode)
CurrencyUnit for the specified ISO-4217 numeric currency code.
The numeric code is an alternative to the three letter code.
numericCurrencyCode - the numeric currency code, not nullIllegalCurrencyException - if the currency is unknownpublic static CurrencyUnit of(java.util.Locale locale)
CurrencyUnit for the specified locale.
Only the country is used from the locale.
locale - the locale, not nullIllegalCurrencyException - if the currency is unknownpublic static CurrencyUnit ofCountry(java.lang.String countryCode)
CurrencyUnit for the specified ISO-3166 country code.
Country codes should generally be in upper case. This method is case sensitive.
countryCode - the country code, typically ISO-3166, not nullIllegalCurrencyException - if the currency is unknownpublic java.lang.String getCode()
Each currency is uniquely identified by a three-letter upper-case code, based on ISO-4217.
public int getNumericCode()
The numeric code is an alternative to the standard string-based code.
public java.lang.String getNumeric3Code()
This formats the numeric code as a three digit string prefixed by zeroes if necessary. If there is no valid code, then an empty string is returned.
public java.util.Set<java.lang.String> getCountryCodes()
A currency is typically valid in one or more countries. The codes are typically defined by ISO-3166. An empty set indicates that no the currency is not associated with a country code.
public int getDecimalPlaces()
Different currencies have different numbers of decimal places by default. For example, 'GBP' has 2 decimal places, but 'JPY' has zero. Pseudo-currencies will return zero.
public boolean isPseudoCurrency()
public java.lang.String getSymbol()
If this currency doesn't have a JDK equivalent, then the currency code is returned.
This method matches the API of Currency.
public java.lang.String getSymbol(java.util.Locale locale)
If this currency doesn't have a JDK equivalent, then the currency code is returned.
This method matches the API of Currency.
locale - the locale to get the symbol for, not nullpublic java.util.Currency toCurrency()
This attempts to convert a CurrencyUnit to a JDK Currency.
java.lang.IllegalArgumentException - if no matching currency exists in the JDKpublic int compareTo(CurrencyUnit other)
compareTo in interface java.lang.Comparable<CurrencyUnit>other - the other currency, not nullpublic boolean equals(java.lang.Object obj)
The comparison checks the 3 letter currency code.
equals in class java.lang.Objectobj - the other currency, null returns falsepublic int hashCode()
hashCode in class java.lang.Objectpublic java.lang.String toString()
toString in class java.lang.ObjectCopyright © 2009–2018 Joda.org. All rights reserved.