Class Currency
- java.lang.Object
-
- javax.measure.unit.Unit<Q>
-
- javax.measure.unit.DerivedUnit<Money>
-
- org.jscience.economics.money.Currency
-
- All Implemented Interfaces:
java.io.Serializable
public class Currency extends javax.measure.unit.DerivedUnit<Money>
This class represents a currency
Unit. Currencies are a special form ofDerivedUnit, conversions between currencies is possible if their respective exchange rates have been set and the conversion factor can be changed dynamically.Quantities stated in
Currencyare usually instances ofMoney.By default, the label associated to a currency is its ISO-4217 code (see the ISO 4217 maintenance agency for a table of currency codes). An application may change this default using the
UnitFormat.label(String)method. For example:[code] UnitFormat.getStandardInstance().label(Currency.EUR, "€"); UnitFormat.getStandardInstance().label(Currency.GBP, "£"); UnitFormat.getStandardInstance().label(Currency.JPY, "¥"); UnitFormat.getStandardInstance().label(Currency.USD, "$"); [/code]- Version:
- 3.0, February 13, 2006
- Author:
- Jean-Marie Dautelle
- See Also:
setExchangeRate(double), Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static CurrencyAUDThe Australian Dollar currency unit.static CurrencyCADThe Canadian Dollar currency unit.static CurrencyCNYThe China Yan currency.static CurrencyEURThe Euro currency.static CurrencyGBPThe British Pound currency.static CurrencyJPYThe Japanese Yen currency.static CurrencyKRWThe Korean Republic Won currency.static CurrencyTWDThe Taiwanese dollar currency.static CurrencyUSDThe United State dollar currency.
-
Constructor Summary
Constructors Constructor Description Currency(java.lang.String code)Creates the currency unit for the given currency code.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanequals(java.lang.Object obj)Indicates if the specified unit can be considered equals to the one specified.java.lang.StringgetCode()Returns the currency code for this currency.intgetDefaultFractionDigits()Returns the default number of fraction digits used with this currency unit.doublegetExchangeRate()Returns the exchange rate for thisCurrency.static CurrencygetReferenceCurrency()Returns the currency used as reference when setting the exchange rate.javax.measure.unit.Unit<? super Money>getStandardUnit()Returns thebase unit,alternate unitor product of base units and alternate units this unit is derived from.inthashCode()Returns the hash code for this unit.voidsetExchangeRate(double refAmount)Sets the exchange rate of thisCurrencyrelatively to the reference currency.static voidsetReferenceCurrency(Currency currency)Sets the reference currency (context-local).javax.measure.converter.UnitConvertertoStandardUnit()Returns the converter from this unit to its system unit.
-
-
-
Constructor Detail
-
Currency
public Currency(java.lang.String code)
Creates the currency unit for the given currency code. See the ISO 4217 maintenance agency for more information, including a table of currency codes.- Parameters:
code- the ISO-4217 code of the currency (e.g."EUR", "USD", "JPY").- Throws:
java.lang.IllegalArgumentException- if the specified code is not an ISO-4217 code.
-
-
Method Detail
-
getCode
public java.lang.String getCode()
Returns the currency code for this currency.- Returns:
- the ISO-4217 code of the currency
(e.g.
"EUR", "USD", "JPY").
-
getDefaultFractionDigits
public int getDefaultFractionDigits()
Returns the default number of fraction digits used with this currency unit. For example, the default number of fraction digits for theEURis 2, while for theJPY(Yen) it's 0. This method can be overriden for custom currencies returning values different from2.- Returns:
- the default number of fraction digits for this currency.
-
setReferenceCurrency
public static void setReferenceCurrency(Currency currency)
Sets the reference currency (context-local). Changing the reference currency clears all the exchange rates previously set.- Parameters:
currency- the new reference currency.- See Also:
LocalContext
-
getReferenceCurrency
public static Currency getReferenceCurrency()
Returns the currency used as reference when setting the exchange rate. By default, the reference currency is the currency for the default country locale.- Returns:
- the reference currency.
- See Also:
setExchangeRate(double)
-
setExchangeRate
public void setExchangeRate(double refAmount)
Sets the exchange rate of thisCurrencyrelatively to the reference currency. Setting the exchange rate allows for conversion betweenMoneystated in different currencies. For example:Currency.setReferenceCurrency(Currency.USD); Currency.EUR.setExchangeRate(1.17); // 1.0 € = 1.17 $- Parameters:
refAmount- the amount stated in thegetReferenceCurrency()equals to one unit of thisCurrency.- See Also:
getReferenceCurrency()
-
getExchangeRate
public double getExchangeRate()
Returns the exchange rate for thisCurrency.- Returns:
- the amount stated in the
getReferenceCurrency()equals to one unit of thisCurrency. - Throws:
javax.measure.converter.ConversionException- if the exchange rate has not be set for thisCurrency.
-
equals
public boolean equals(java.lang.Object obj)
Description copied from class:javax.measure.unit.UnitIndicates if the specified unit can be considered equals to the one specified.- Specified by:
equalsin classjavax.measure.unit.Unit<Money>- Parameters:
obj- the object to compare to.- Returns:
trueif this unit is considered equal to that unit;falseotherwise.
-
hashCode
public int hashCode()
Description copied from class:javax.measure.unit.UnitReturns the hash code for this unit.- Specified by:
hashCodein classjavax.measure.unit.Unit<Money>- Returns:
- this unit hashcode value.
-
getStandardUnit
public javax.measure.unit.Unit<? super Money> getStandardUnit()
Description copied from class:javax.measure.unit.UnitReturns thebase unit,alternate unitor product of base units and alternate units this unit is derived from. The standard unit identifies the "type" ofquantityfor which this unit is employed. For example:[code] boolean isAngularVelocity(Unit> u) { return u.getStandardUnit().equals(RADIAN.divide(SECOND)); } assert(REVOLUTION.divide(MINUTE).isAngularVelocity()); [/code]Note: Having the same system unit is not sufficient to ensure that a converter exists between the two units (e.g. °C/m and K/m).
- Specified by:
getStandardUnitin classjavax.measure.unit.Unit<Money>- Returns:
- the system unit this unit is derived from.
-
toStandardUnit
public javax.measure.converter.UnitConverter toStandardUnit()
Description copied from class:javax.measure.unit.UnitReturns the converter from this unit to its system unit.- Specified by:
toStandardUnitin classjavax.measure.unit.Unit<Money>- Returns:
this.getConverterTo(this.getSystemUnit())
-
-