KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jscience > economics > money > Currency


1 /*
2  * JScience - Java(TM) Tools and Libraries for the Advancement of Sciences.
3  * Copyright (C) 2006 - JScience (http://jscience.org/)
4  * All rights reserved.
5  *
6  * Permission to use, copy, modify, and distribute this software is
7  * freely granted, provided that this notice is preserved.
8  */

9 package org.jscience.economics.money;
10
11 import javolution.context.LocalContext;
12 import javolution.util.LocalMap;
13
14 import javax.measure.converters.ConversionException;
15 import javax.measure.converters.UnitConverter;
16 import javax.measure.units.DerivedUnit;
17 import javax.measure.units.Unit;
18 import javax.measure.units.UnitFormat;
19
20 /**
21  * <p> This class represents a currency {@link javax.measure.units.Unit Unit}.
22  * Currencies are a special form of {@link DerivedUnit}, conversions
23  * between currencies is possible if their respective exchange rates
24  * have been set and the conversion factor can be changed dynamically.</p>
25  *
26  * <p> Quantities stated in {@link Currency} are usually instances of
27  * {@link Money}.</p>
28  *
29  * <p> By default, the label associated to a currency is its ISO-4217 code
30  * (see the <a HREF="http://www.bsi-global.com/iso4217currency"> ISO 4217
31  * maintenance agency</a> for a table of currency codes). An application may
32  * change this default using the {@link javax.measure.units.UnitFormat#label
33  * UnitFormat.label(String)} method.
34  * For example:[code]
35  * UnitFormat.getStandardInstance().label(Currency.EUR, "€");
36  * UnitFormat.getStandardInstance().label(Currency.GBP, "£");
37  * UnitFormat.getStandardInstance().label(Currency.JPY, "¥");
38  * UnitFormat.getStandardInstance().label(Currency.USD, "$");
39  * [/code]</p>
40  *
41  * @author <a HREF="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
42  * @version 3.0, February 13, 2006
43  * @see #setExchangeRate
44  */

45 public class Currency extends DerivedUnit<Money> {
46
47     /**
48      * The Australian Dollar currency unit.
49      */

50     public static final Currency AUD = new Currency("AUD");
51
52     /**
53      * The Canadian Dollar currency unit.
54      */

55     public static final Currency CAD = new Currency("CAD");
56
57     /**
58      * The China Yan currency.
59      */

60     public static final Currency CNY = new Currency("CNY");
61
62     /**
63      * The Euro currency.
64      */

65     public static final Currency EUR = new Currency("EUR");
66
67     /**
68      * The British Pound currency.
69      */

70     public static final Currency GBP = new Currency("GBP");
71
72     /**
73      * The Japanese Yen currency.
74      */

75     public static final Currency JPY = new Currency("JPY");
76
77     /**
78      * The Korean Republic Won currency.
79      */

80     public static final Currency KRW = new Currency("KRW");
81
82     /**
83      * The Taiwanese dollar currency.
84      */

85     public static final Currency TWD = new Currency("TWD");
86
87     /**
88      * The United State dollar currency.
89      */

90     public static final Currency USD = new Currency("USD");
91
92     /**
93      * Holds the reference currency.
94      */

95     private static final LocalContext.Reference<Currency>
96         REFERENCE = new LocalContext.Reference<Currency>();
97     
98     /**
99      * Holds the exchanges rate to the reference currency.
100      */

101     private static final LocalMap<String JavaDoc, Double JavaDoc> TO_REFERENCE =
102         new LocalMap<String JavaDoc, Double JavaDoc>();
103     
104     /**
105      * Holds the converter to the {@link Money#BASE_UNIT money base unit}.
106      */

107     private final Converter _toBaseUnit;
108
109     /**
110      * Creates the currency unit for the given currency code.
111      * See the <a HREF="http://www.bsi-global.com/iso4217currency"> ISO 4217
112      * maintenance agency</a> for more information, including a table of
113      * currency codes.
114      *
115      * @param code the ISO-4217 code of the currency (e.g.
116      * <code>"EUR", "USD", "JPY"</code>).
117      * @throws IllegalArgumentException if the specified code is not an ISO-4217
118      * code.
119      */

120     public Currency(String JavaDoc code) {
121         _toBaseUnit = new Converter(code, false);
122         UnitFormat.getStandardInstance().label(this, code);
123     }
124
125     /**
126      * Returns the currency code for this currency.
127      *
128      * @return the ISO-4217 code of the currency
129      * (e.g. <code>"EUR", "USD", "JPY"</code>).
130      */

131     public String JavaDoc getCode() {
132         return _toBaseUnit._code;
133     }
134     
135     /**
136      * Returns the default number of fraction digits used with this currency
137      * unit. For example, the default number of fraction digits for
138      * the {@link Currency#EUR} is 2, while for the {@link Currency#JPY} (Yen)
139      * it's 0. This method can be overriden for custom currencies returning
140      * values different from <code>2</code>.
141      *
142      * @return the default number of fraction digits for this currency.
143      */

144     public int getDefaultFractionDigits() {
145         return (this.equals(JPY) || (this.equals(KRW))) ?
146                 0 : 2;
147     }
148
149     /**
150      * Sets the reference currency (context-local). Changing the
151      * reference currency clears all the exchange rates previously set.
152      *
153      * @param currency the new reference currency.
154      * @see javolution.context.LocalContext
155      */

156     public static void setReferenceCurrency(Currency currency) {
157         REFERENCE.set(currency);
158         TO_REFERENCE.clear();
159         TO_REFERENCE.put(currency.getCode(), 1.0);
160     }
161     
162     /**
163      * Returns the currency used as reference when setting the exchange rate.
164      * By default, the reference currency is the currency for the default
165      * country locale.
166      *
167      * @return the reference currency.
168      * @see #setExchangeRate
169      */

170     public static Currency getReferenceCurrency() {
171         return REFERENCE.get();
172     }
173     
174     /**
175      * Sets the exchange rate of this {@link Currency} relatively to
176      * the reference currency. Setting the exchange rate allows
177      * for conversion between {@link Money} stated in different currencies.
178      * For example:<pre>
179      * Currency.setReferenceCurrency(Currency.USD);
180      * Currency.EUR.setExchangeRate(1.17); // 1.0 € = 1.17 $
181      * </pre>
182      *
183      * @param refAmount the amount stated in the {@link #getReferenceCurrency}
184      * equals to one unit of this {@link Currency}.
185      * @see #getReferenceCurrency
186      */

187     public void setExchangeRate(double refAmount) {
188         TO_REFERENCE.put(this.getCode(), refAmount);
189     }
190
191     /**
192      * Returns the exchange rate for this {@link Currency}.
193      *
194      * @return the amount stated in the {@link #getReferenceCurrency}
195      * equals to one unit of this {@link Currency}.
196      * @throws ConversionException if the exchange rate has not be set for
197      * this {@link Currency}.
198      */

199     public double getExchangeRate() {
200         Double JavaDoc refAmount = TO_REFERENCE.get(this.getCode());
201         if (refAmount == null)
202               throw new ConversionException("Exchange rate not set for " + this.getCode());
203         return refAmount.doubleValue();
204     }
205
206
207     @Override JavaDoc
208     public boolean equals(Object JavaDoc obj) {
209         if (this == obj) return true;
210         if (!(obj instanceof Currency))
211            return false;
212         Currency that = (Currency) obj;
213         return this._toBaseUnit.equals(that._toBaseUnit);
214     }
215
216     @Override JavaDoc
217     public int hashCode() {
218         return _toBaseUnit.hashCode();
219     }
220     
221     @Override JavaDoc
222     public Unit<? super Money> getSystemUnit() {
223         return Money.BASE_UNIT;
224     }
225
226     @Override JavaDoc
227     public UnitConverter toSystemUnit() {
228         return _toBaseUnit;
229     }
230
231     /**
232      * This class represents the currency converters.
233      */

234     private static class Converter extends UnitConverter {
235         
236         String JavaDoc _code;
237         
238         boolean _invert;
239         
240         private Converter(String JavaDoc code, boolean invert) {
241             _code = code;
242             _invert = invert;
243         }
244
245         @Override JavaDoc
246         public UnitConverter inverse() {
247             return new Converter(_code, !_invert);
248         }
249
250         @Override JavaDoc
251         public double convert(double x) throws ConversionException {
252             Double JavaDoc refAmount = TO_REFERENCE.get(_code);
253             if (refAmount == null)
254                   throw new ConversionException("Exchange rate not set for " + _code);
255             return _invert ? x / refAmount.doubleValue() : x * refAmount.doubleValue();
256         }
257
258         @Override JavaDoc
259         public boolean isLinear() {
260             return true;
261         }
262         
263         @Override JavaDoc
264         public boolean equals(Object JavaDoc obj) {
265             if (this == obj) return true;
266             if (!(obj instanceof Converter))
267                return false;
268             Converter that = (Converter) obj;
269             return this._code.equals(that._code) && (this._invert == that._invert);
270         }
271
272         @Override JavaDoc
273         public int hashCode() {
274             return _invert ? _code.hashCode() : - _code.hashCode();
275         }
276
277         private static final long serialVersionUID = 1L;
278     }
279
280     private static final long serialVersionUID = 1L;
281 }
Popular Tags