Top Description Fields Constructors Methods
org.joda.money

public final Class CurrencyUnit

extends Object
implements Comparable<CurrencyUnit>, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, java.lang.Comparable
Imports
java.io.InvalidObjectException, .ObjectInputStream, .Serializable, java.util.ArrayList, .Collections, .Currency, .HashSet, .List, .Locale, .Map.Entry, .Set, java.util.concurrent.ConcurrentHashMap, .ConcurrentMap, .ConcurrentSkipListMap, java.util.regex.Pattern, org.joda.convert.FromString, .ToString

A unit of currency.

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.

Field Summary

Modifier and TypeField and Description
public static final CurrencyUnit
AUD

The currency 'AUD' - Australian Dollar.

public static final CurrencyUnit
CAD

The currency 'CAD' - Canadian Dollar.

public static final CurrencyUnit
CHF

The currency 'CHF' - Swiss Franc.

private static final Pattern
CODE

The currency code pattern.

private final String
code

The currency code, not null.

private static final ConcurrentMap<String, CurrencyUnit>
currenciesByCode

Map of registered currencies by text code.

private static final ConcurrentMap<String, CurrencyUnit>
currenciesByCountry

Map of registered currencies by country.

private static final ConcurrentMap<Integer, CurrencyUnit>
currenciesByNumericCode

Map of registered currencies by numeric code.

private final short
decimalPlaces

The number of decimal places.

public static final CurrencyUnit
EUR

The currency 'EUR' - Euro.

public static final CurrencyUnit
GBP

The currency 'GBP' - British pound.

public static final CurrencyUnit
JPY

The currency 'JPY' - Japanese Yen.

private final short
numericCode

The numeric currency code.

private static final long
serialVersionUID

The serialisation version.

public static final CurrencyUnit
USD

The currency 'USD' - United States Dollar.

Constructor Summary

AccessConstructor and Description
pack-priv
CurrencyUnit(String
the three-letter currency code, not null
code
,
short
the numeric currency code, from 0 to 999, -1 if none
numericCode
,
short
the decimal places, not null
decimalPlaces
)

Constructor, creating a new currency instance.

Method Summary

Modifier and TypeMethod and Description
public int

Returns:

negative if earlier alphabetically, 0 if equal, positive if greater alphabetically
compareTo
(CurrencyUnit
the other currency, not null
other
)

Implements java.lang.Comparable.compareTo.

Compares this currency to another by alphabetical comparison of the code.

public boolean

Returns:

true if equal
equals
(Object
the other currency, null returns false
obj
)

Overrides java.lang.Object.equals.

Checks if this currency equals another currency.

public String

Returns:

the three-letter upper-case currency code, never null
getCode
()

Gets the ISO-4217 three-letter currency code.

public Set<String>

Returns:

the country codes, may be empty, not null
getCountryCodes
()

Gets the country codes applicable to this currency.

public int

Returns:

the decimal places, from 0 to 9 (normally 0, 2 or 3)
getDecimalPlaces
()

Gets the number of decimal places typically used by this currency.

public String

Returns:

the three digit numeric currency code, empty is no code, never null
getNumeric3Code
()

Gets the ISO-4217 numeric currency code as a three digit string.

public int

Returns:

the numeric currency code, -1 if no numeric code
getNumericCode
()

Gets the ISO-4217 numeric currency code.

public String

Returns:

the JDK currency instance, never null
getSymbol
()

Gets the symbol for this locale from the JDK.

public String

Returns:

the JDK currency instance, never null
getSymbol
(Locale
the locale to get the symbol for, not null
locale
)

Gets the symbol for this locale from the JDK.

public int

Returns:

the hash code
hashCode
()

Overrides java.lang.Object.hashCode.

Returns a suitable hash code for the currency.

public boolean

Returns:

true if this is a pseudo-currency
isPseudoCurrency
()

Checks if this is a pseudo-currency.

public static CurrencyUnit

Returns:

the singleton instance, never null
of
(Currency
the currency, not null
currency
)

Obtains an instance of CurrencyUnit matching the specified JDK currency.

public static CurrencyUnit

Returns:

the singleton instance, never null
of
(String
the three-letter currency code, not null
currencyCode
)

Obtains an instance of CurrencyUnit for the specified three letter currency code.

public static CurrencyUnit

Returns:

the singleton instance, never null
of
(Locale
the locale, not null
locale
)

Obtains an instance of CurrencyUnit for the specified locale.

public static CurrencyUnit

Returns:

the singleton instance, never null
ofCountry
(String
the country code, typically ISO-3166, not null
countryCode
)

Obtains an instance of CurrencyUnit for the specified ISO-3166 country code.

public static CurrencyUnit

Returns:

the singleton instance, never null
ofNumericCode
(String
the currency code, not null
numericCurrencyCode
)

Obtains an instance of CurrencyUnit for the specified ISO-4217 numeric currency code.

public static CurrencyUnit

Returns:

the singleton instance, never null
ofNumericCode
(int
the numeric currency code, not null
numericCurrencyCode
)

Obtains an instance of CurrencyUnit for the specified ISO-4217 numeric currency code.

private void
readObject(ObjectInputStream
the input stream, not null
ois
)

Block malicious data streams.

public static synchronized void
registerCountry(String
the country code, two upper case letters if following ISO 3166-1-alpha-2, not null
countryCode
,
CurrencyUnit
the associated currency
currency
)

Registers a country code, typically ISO 3166-1-alpha-2.

public static synchronized CurrencyUnit

Returns:

the new instance, never null
registerCurrency
(String
the three-letter upper-case currency code, not null
currencyCode
,
int
the numeric currency code, from 0 to 999, -1 if none
numericCurrencyCode
,
int
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
decimalPlaces
,
List<String>
the country codes to register the currency under, not null
countryCodes
)

Registers a currency and associated countries allowing it to be used.

public static synchronized CurrencyUnit

Returns:

the new instance, never null
registerCurrency
(String
the three-letter upper-case currency code, not null
currencyCode
,
int
the numeric currency code, from 0 to 999, -1 if none
numericCurrencyCode
,
int
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
decimalPlaces
,
List<String>
the country codes to register the currency under, use of ISO-3166 is recommended, not null
countryCodes
,
boolean
true to register forcefully, replacing any existing matching currency, false to validate that there is no existing matching currency
force
)

Registers a currency and associated countries allowing it to be used, allowing replacement.

public static synchronized CurrencyUnit

Returns:

the new instance, never null
registerCurrency
(String
the three-letter upper-case currency code, not null
currencyCode
,
int
the numeric currency code, from 0 to 999, -1 if none
numericCurrencyCode
,
int
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 null
decimalPlaces
,
boolean
true to register forcefully, replacing any existing matching currency, false to validate that there is no existing matching currency
force
)

Registers a currency allowing it to be used, allowing replacement.

public static List<String>

Returns:

the sorted, independent, list of all registered countries, never null
registeredCountries
()

Gets the list of all registered countries.

public static List<CurrencyUnit>

Returns:

the sorted, independent, list of all registered currencies, never null
registeredCurrencies
()

Gets the list of all registered currencies.

public Currency

Returns:

the JDK currency instance, never null
toCurrency
()

Gets the JDK currency instance equivalent to this currency.

public String

Returns:

the currency code, never null
toString
()

Overrides java.lang.Object.toString.

Gets the currency code as a string.

private Object

Returns:

the replacing object, never null
writeReplace
()

Uses a serialization delegate.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

AUDback to summary
public static final CurrencyUnit AUD

The currency 'AUD' - Australian Dollar.

CADback to summary
public static final CurrencyUnit CAD

The currency 'CAD' - Canadian Dollar.

CHFback to summary
public static final CurrencyUnit CHF

The currency 'CHF' - Swiss Franc.

CODEback to summary
private static final Pattern CODE

The currency code pattern.

codeback to summary
private final String code

The currency code, not null.

currenciesByCodeback to summary
private static final ConcurrentMap<String, CurrencyUnit> currenciesByCode

Map of registered currencies by text code.

currenciesByCountryback to summary
private static final ConcurrentMap<String, CurrencyUnit> currenciesByCountry

Map of registered currencies by country.

currenciesByNumericCodeback to summary
private static final ConcurrentMap<Integer, CurrencyUnit> currenciesByNumericCode

Map of registered currencies by numeric code.

decimalPlacesback to summary
private final short decimalPlaces

The number of decimal places.

EURback to summary
public static final CurrencyUnit EUR

The currency 'EUR' - Euro.

GBPback to summary
public static final CurrencyUnit GBP

The currency 'GBP' - British pound.

JPYback to summary
public static final CurrencyUnit JPY

The currency 'JPY' - Japanese Yen.

numericCodeback to summary
private final short numericCode

The numeric currency code.

serialVersionUIDback to summary
private static final long serialVersionUID

The serialisation version.

USDback to summary
public static final CurrencyUnit USD

The currency 'USD' - United States Dollar.

Constructor Detail

CurrencyUnitback to summary
pack-priv CurrencyUnit(String code, short numericCode, short decimalPlaces)

Constructor, creating a new currency instance.

Parameters
code:String

the three-letter currency code, not null

numericCode:short

the numeric currency code, from 0 to 999, -1 if none

decimalPlaces:short

the decimal places, not null

Method Detail

compareToback to summary
public int compareTo(CurrencyUnit other)

Implements java.lang.Comparable.compareTo.

Compares this currency to another by alphabetical comparison of the code.

Parameters
other:CurrencyUnit

the other currency, not null

Returns:int

negative if earlier alphabetically, 0 if equal, positive if greater alphabetically

Annotations
@Override
equalsback to summary
public boolean equals(Object obj)

Overrides java.lang.Object.equals.

Checks if this currency equals another currency.

The comparison checks the 3 letter currency code.

Parameters
obj:Object

the other currency, null returns false

Returns:boolean

true if equal

Annotations
@Override
getCodeback to summary
public String getCode()

Gets the ISO-4217 three-letter currency code.

Each currency is uniquely identified by a three-letter upper-case code, based on ISO-4217.

Returns:String

the three-letter upper-case currency code, never null

getCountryCodesback to summary
public Set<String> getCountryCodes()

Gets the country codes applicable to this currency.

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.

Returns:Set<String>

the country codes, may be empty, not null

getDecimalPlacesback to summary
public int getDecimalPlaces()

Gets the number of decimal places typically used by this currency.

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.

Returns:int

the decimal places, from 0 to 9 (normally 0, 2 or 3)

getNumeric3Codeback to summary
public String getNumeric3Code()

Gets the ISO-4217 numeric currency code as a three digit string.

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.

Returns:String

the three digit numeric currency code, empty is no code, never null

getNumericCodeback to summary
public int getNumericCode()

Gets the ISO-4217 numeric currency code.

The numeric code is an alternative to the standard string-based code.

Returns:int

the numeric currency code, -1 if no numeric code

getSymbolback to summary
public String getSymbol()

Gets the symbol for this locale from the JDK.

If this currency doesn't have a JDK equivalent, then the currency code is returned.

This method matches the API of Currency.

Returns:String

the JDK currency instance, never null

getSymbolback to summary
public String getSymbol(Locale locale)

Gets the symbol for this locale from the JDK.

If this currency doesn't have a JDK equivalent, then the currency code is returned.

This method matches the API of Currency.

Parameters
locale:Locale

the locale to get the symbol for, not null

Returns:String

the JDK currency instance, never null

hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Returns a suitable hash code for the currency.

Returns:int

the hash code

Annotations
@Override
isPseudoCurrencyback to summary
public boolean isPseudoCurrency()

Checks if this is a pseudo-currency.

Returns:boolean

true if this is a pseudo-currency

ofback to summary
public static CurrencyUnit of(Currency currency)

Obtains an instance of CurrencyUnit matching the specified JDK currency.

This converts the JDK currency instance to a currency unit using the code.

Parameters
currency:Currency

the currency, not null

Returns:CurrencyUnit

the singleton instance, never null

Exceptions
IllegalCurrencyException:
if the currency is unknown
ofback to summary
public static CurrencyUnit of(String currencyCode)

Obtains an instance of 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.

Parameters
currencyCode:String

the three-letter currency code, not null

Returns:CurrencyUnit

the singleton instance, never null

Annotations
@FromString
Exceptions
IllegalCurrencyException:
if the currency is unknown
ofback to summary
public static CurrencyUnit of(Locale locale)

Obtains an instance of CurrencyUnit for the specified locale.

Only the country is used from the locale.

Parameters
locale:Locale

the locale, not null

Returns:CurrencyUnit

the singleton instance, never null

Exceptions
IllegalCurrencyException:
if the currency is unknown
ofCountryback to summary
public static CurrencyUnit ofCountry(String countryCode)

Obtains an instance of CurrencyUnit for the specified ISO-3166 country code.

Country codes should generally be in upper case. This method is case sensitive.

Parameters
countryCode:String

the country code, typically ISO-3166, not null

Returns:CurrencyUnit

the singleton instance, never null

Exceptions
IllegalCurrencyException:
if the currency is unknown
ofNumericCodeback to summary
public static CurrencyUnit ofNumericCode(String numericCurrencyCode)

Obtains an instance of 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.

Parameters
numericCurrencyCode:String

the currency code, not null

Returns:CurrencyUnit

the singleton instance, never null

Exceptions
IllegalCurrencyException:
if the currency is unknown
ofNumericCodeback to summary
public static CurrencyUnit ofNumericCode(int numericCurrencyCode)

Obtains an instance of CurrencyUnit for the specified ISO-4217 numeric currency code.

The numeric code is an alternative to the three letter code.

Parameters
numericCurrencyCode:int

the numeric currency code, not null

Returns:CurrencyUnit

the singleton instance, never null

Exceptions
IllegalCurrencyException:
if the currency is unknown
readObjectback to summary
private void readObject(ObjectInputStream ois) throws InvalidObjectException

Block malicious data streams.

Parameters
ois:ObjectInputStream

the input stream, not null

Exceptions
InvalidObjectException:
if an error occurs
registerCountryback to summary
public static synchronized void registerCountry(String countryCode, CurrencyUnit currency)

Registers a country code, typically ISO 3166-1-alpha-2.

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.

Parameters
countryCode:String

the country code, two upper case letters if following ISO 3166-1-alpha-2, not null

currency:CurrencyUnit

the associated currency

Exceptions
IllegalArgumentException:
if the code is already registered and force is false; or if the specified data is invalid
registerCurrencyback to summary
public static synchronized CurrencyUnit registerCurrency(String currencyCode, int numericCurrencyCode, int decimalPlaces, List<String> countryCodes)

Registers a currency and associated countries allowing it to be used.

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.

Parameters
currencyCode:String

the three-letter upper-case currency code, not null

numericCurrencyCode:int

the numeric currency code, from 0 to 999, -1 if none

decimalPlaces:int

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

countryCodes:List<String>

the country codes to register the currency under, not null

Returns:CurrencyUnit

the new instance, never null

Exceptions
IllegalArgumentException:
if the code is already registered, or the specified data is invalid
registerCurrencyback to summary
public static synchronized CurrencyUnit registerCurrency(String currencyCode, int numericCurrencyCode, int decimalPlaces, List<String> countryCodes, boolean force)

Registers a currency and associated countries allowing it to be used, allowing replacement.

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.

Parameters
currencyCode:String

the three-letter upper-case currency code, not null

numericCurrencyCode:int

the numeric currency code, from 0 to 999, -1 if none

decimalPlaces:int

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

countryCodes:List<String>

the country codes to register the currency under, use of ISO-3166 is recommended, not null

force:boolean

true to register forcefully, replacing any existing matching currency, false to validate that there is no existing matching currency

Returns:CurrencyUnit

the new instance, never null

Exceptions
IllegalArgumentException:
if the code is already registered and force is false; or if the specified data is invalid
registerCurrencyback to summary
public static synchronized CurrencyUnit registerCurrency(String currencyCode, int numericCurrencyCode, int decimalPlaces, boolean force)

Registers a currency allowing it to be used, allowing replacement.

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.

Parameters
currencyCode:String

the three-letter upper-case currency code, not null

numericCurrencyCode:int

the numeric currency code, from 0 to 999, -1 if none

decimalPlaces:int

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 null

force:boolean

true to register forcefully, replacing any existing matching currency, false to validate that there is no existing matching currency

Returns:CurrencyUnit

the new instance, never null

Exceptions
IllegalArgumentException:
if the code is already registered and force is false; or if the specified data is invalid
registeredCountriesback to summary
public static List<String> registeredCountries()

Gets the list of all registered countries.

This returns the list of known countries. The list may change after application startup, however this isn't recommended.

Returns:List<String>

the sorted, independent, list of all registered countries, never null

registeredCurrenciesback to summary
public static List<CurrencyUnit> registeredCurrencies()

Gets the list of all registered currencies.

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.

Returns:List<CurrencyUnit>

the sorted, independent, list of all registered currencies, never null

toCurrencyback to summary
public Currency toCurrency()

Gets the JDK currency instance equivalent to this currency.

This attempts to convert a CurrencyUnit to a JDK Currency.

Returns:Currency

the JDK currency instance, never null

Exceptions
IllegalArgumentException:
if no matching currency exists in the JDK
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Gets the currency code as a string.

Returns:String

the currency code, never null

Annotations
@Override
@ToString
writeReplaceback to summary
private Object writeReplace()

Uses a serialization delegate.

Returns:Object

the replacing object, never null