org.jscience.economics.money
Class Currency

Object
  extended by Unit<Q>
      extended by DerivedUnit<Money>
          extended by Currency
All Implemented Interfaces:
Serializable

public class Currency
extends DerivedUnit<Money>

This class represents a currency Unit. Currencies are a special form of DerivedUnit, conversions between currencies is possible if their respective exchange rates have been set. The conversion factor can be changed dynamically.

Measurements stated in a Currency are usually of Money type. For example:

         Measure<Money> tenDollars = Measure.valueOf(10, Currency.USD);
     

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). Although, local mapping of currency symbol is possible. For example:

         EUR.setLabel("€");
         GBP.setLabel("£");
         JPY.setLabel("¥");
         USD.setLabel("$");
     

Version:
5.0, January 2, 2010
Author:
Jean-Marie Dautelle
See Also:
Serialized Form

Field Summary
static Currency AUD
          The Australian Dollar currency unit.
static Currency CAD
          The Canadian Dollar currency unit.
static Currency CNY
          The China Yan currency.
static Currency EUR
          The Euro currency.
static Currency GBP
          The British Pound currency.
static Currency JPY
          The Japanese Yen currency.
static Currency KRW
          The Korean Republic Won currency.
static Currency TWD
          The Taiwanese dollar currency.
static Currency USD
          The United State dollar currency.
 
Fields inherited from class Unit
ONE
 
Constructor Summary
Currency(String code)
          Creates the currency unit for the given currency code.
 
Method Summary
 boolean equals(Object obj)
           
 String getCode()
          Returns the currency code for this currency.
 UnitConverter getConverterToSI()
           
 Dimension getDimension()
           
 UnitConverter getDimensionalTransform()
           
 Number getExchangeRate(Currency target)
          Returns the exchange rate from this Currency to the specified currency.
 int getFractionDigits()
          Returns the number of fraction digits used with this currency unit.
static Currency getInstance(Locale locale)
          Returns the currency for the country of the given locale.
 String getLabel()
          Returns the local label for this currency (default {@link #getCode()).
 int hashCode()
           
 void setExchangeRate(Number factor, Currency target)
          Sets the exchange rate of this Currency to the specified target currency.
 void setLabel(String label)
          Sets the local label for this currency.
 Currency toSI()
           
 String toString()
           
 
Methods inherited from class Unit
alternate, asType, compound, divide, divide, divide, getConverterTo, getConverterToAny, inverse, isCompatible, isSI, plus, pow, root, times, times, times, toStringLocale, transform, valueOf
 
Methods inherited from class Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

AUD

public static final Currency AUD
The Australian Dollar currency unit.


CAD

public static final Currency CAD
The Canadian Dollar currency unit.


CNY

public static final Currency CNY
The China Yan currency.


EUR

public static final Currency EUR
The Euro currency.


GBP

public static final Currency GBP
The British Pound currency.


JPY

public static final Currency JPY
The Japanese Yen currency.


KRW

public static final Currency KRW
The Korean Republic Won currency.


TWD

public static final Currency TWD
The Taiwanese dollar currency.


USD

public static final Currency USD
The United State dollar currency.

Constructor Detail

Currency

public Currency(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. The currency code is added to the standard (UCUM) symbol map.

Parameters:
code - the ISO-4217 code of the currency (e.g. "EUR", "USD", "JPY").
Throws:
IllegalArgumentException - if the specified code is not an ISO-4217 code.
See Also:
UnitFormat.getSymbolMap()
Method Detail

getInstance

public static Currency getInstance(Locale locale)
Returns the currency for the country of the given locale.

Parameters:
locale - the locale for whose country a Currency instance is returned.
Returns:
the currency instance for the country of the given locale.

getCode

public String getCode()
Returns the currency code for this currency.

Returns:
the ISO-4217 code of the currency (e.g. "EUR", "USD", "JPY").

getLabel

public String getLabel()
Returns the local label for this currency (default {@link #getCode()).

Returns:
the label used by toString()

setLabel

public void setLabel(String label)
Sets the local label for this currency.

Parameters:
label - the new label.

getFractionDigits

public int getFractionDigits()
Returns the number of fraction digits used with this currency unit. For example, the number of fraction digits for the EUR is 2, while for the JPY (Yen) it's 0. This method can be overriden for custom currencies returning values different from 2.

Returns:
the default number of fraction digits for this currency.

setExchangeRate

public void setExchangeRate(Number factor,
                            Currency target)
Sets the exchange rate of this Currency to the specified target currency. Setting the exchange rate allows for conversion between money quantities stated in this currency to money quantities setting in the specified currency. For example:
     // Sets the exchange rate from EUR to USD and from USD to EUR.
     EUR.setExchangeRate(1.45, USD);
     USD.setExchangeRate(0.62, EUR);
 

Parameters:
factor - the amount of the target currency equals to this currency.
target - the target currency.
See Also:
CurrencyConverter#setInstance(org.jscience.economics.money.CurrencyConverter)

getExchangeRate

public Number getExchangeRate(Currency target)
Returns the exchange rate from this Currency to the specified currency.

Parameters:
target - the target currency.
Returns:
the amount of the target currency equals to this currency or BigDecimal.ONE if the specified target is equals to this or null if the exchange rate has not be set.

equals

public boolean equals(Object obj)
Specified by:
equals in class Unit<Money>

hashCode

public int hashCode()
Specified by:
hashCode in class Unit<Money>

toSI

public Currency toSI()
Specified by:
toSI in class Unit<Money>

toString

public String toString()
Overrides:
toString in class Unit<Money>

getConverterToSI

public UnitConverter getConverterToSI()
Specified by:
getConverterToSI in class Unit<Money>

getDimension

public Dimension getDimension()
Overrides:
getDimension in class Unit<Money>

getDimensionalTransform

public UnitConverter getDimensionalTransform()
Overrides:
getDimensionalTransform in class Unit<Money>


Copyright © 2005-2010 JScience. All Rights Reserved.