2 package org.apidesign.apifest08.currency;
4 import java.math.BigDecimal;
6 import java.util.HashMap;
10 * Exchange rate provider. Provides fixed exchange rate which does not depend
11 * on date (method {@link #getExchangeRate()} ).
13 * Date dependend exchange rate to be implemented.
15 * @author arnostvalicek
17 public class ExchangeRateProvider {
18 BigDecimal fromValue, toValue;
19 ConvertorCurrency fromCurrency, toCurrency;
21 Map exchangeRateMap = new HashMap();
24 * Simple constructor for <code>ExchangeRateProviderM</code> which can provide fixed exchange rate.
26 * Describes conversion <em>from ONE</em> to <em>to ONE</em> currency.
28 * @param fromValue From value. BigDecimal value, precision should be set to currency precision.
29 * @param fromCurrency From currency.
30 * @param toValue To value. BigDecimal value, precision should be set to currency precision.
31 * @param toCurrency To currency.
32 * @deprecated deprecated since task2. Use {@link #createExchangeRateProvider() } instead of this constructor.
34 public ExchangeRateProvider(BigDecimal fromValue, ConvertorCurrency fromCurrency, BigDecimal toValue, ConvertorCurrency toCurrency) {
35 this.fromValue = fromValue;
36 this.toValue = toValue;
37 this.fromCurrency = fromCurrency;
38 this.toCurrency = toCurrency;
41 private ExchangeRateProvider() {
46 * Static method to create new exchange rate provider. This exchange rate provider does not contain
47 * any exchange rates (this is difference to public constructor).
48 * @return New <code>ExchangeRateProvider</code>
50 public static ExchangeRateProvider createExchangeRateProvider() {
51 ExchangeRateProvider provider = new ExchangeRateProvider();
56 * Add new exchange rate to <code></code> to this exchange rate provider.
58 * Example of specifiing conversion rate: 100 SKK == 80 CZK:<br>
59 * <code>addFixedCurencyRate(ConvertorCurrency.getInstance("SKK"), new BigDecimal(100), ConvertorCurrency.getInstance("CZK"), new BigDecimal(80));</code>
62 * @param fromCurrency Source currency.
63 * @param fromValue Valye for from currency.
64 * @param toCurrency Target currency.
65 * @param toValue Value for target currency.
67 public synchronized void addFixedCurencyRate(ConvertorCurrency fromCurrency, BigDecimal fromValue, ConvertorCurrency toCurrency, BigDecimal toValue) {
68 if (fromValue==null) {
69 throw new NullPointerException("fromValue can't be null");
72 throw new NullPointerException("toValue can't be null");
74 Map map2 = (Map) exchangeRateMap.get(fromCurrency);
77 exchangeRateMap.put(fromCurrency, map2);
80 ExchangeRate rate = new ExchangeRate(fromValue, toValue);
81 map2.put(toCurrency, rate);
85 * Get fixed exange rate for currencies (from->to).
86 * @return Returns exchange rate.
87 * @deprecated deprecated since task2. Use {@link #getExchangeRate(ConvertorCurrency, ConvertorCurrency) }
89 public ExchangeRate getExchangeRate() {
90 return new ExchangeRate(fromValue, toValue);
95 * Get fixed exange rate for currencies (from->to).
96 * @return Returns exchange rate or <code>null</code> if exchange rate not found.
98 public ExchangeRate getExchangeRate(ConvertorCurrency fromCurrency, ConvertorCurrency toCurrency) {
99 return getExchangeRateImpl(fromCurrency, toCurrency);
103 * Get fixed exange rate for currencies (from->to) or reversed exchange rate (to->from).
104 * @return Returns exchange rate or <code>null</code> if exchange rate not found.
106 public ExchangeRate getReversibleExchangeRate(ConvertorCurrency fromCurrency, ConvertorCurrency toCurrency) {
107 ExchangeRate rate = getExchangeRateImpl(fromCurrency, toCurrency);
109 ExchangeRate revertedRate = getExchangeRateImpl(toCurrency, fromCurrency);
110 if (revertedRate!=null) {
111 rate = ExchangeRate.createRevertedRate(revertedRate);
120 * Get exchange rate for currencies (from->to) based on provided date.
121 * @param date Date for which exchange rate should be provided.
122 * @return Returns exchange rate
123 * @deprecated deprecated since task2. No real implementation in version2.
125 public ExchangeRate getExchangeRate(Date date) {
126 return new ExchangeRate(fromValue, toValue);
130 ConvertorCurrency getFromCurrency() {
134 ConvertorCurrency getToCurrency() {
138 private ExchangeRate getExchangeRateImpl(ConvertorCurrency fromCurrency, ConvertorCurrency toCurrency) {
139 if (fromValue != null && toValue != null && fromCurrency.equals(this.fromCurrency) && toCurrency.equals(this.toCurrency)) {
140 return new ExchangeRate(fromValue, toValue);
142 //return new ExchangeRate(fromValue, toValue);
143 Map map2 = (Map) exchangeRateMap.get(fromCurrency);
147 ExchangeRate result = (ExchangeRate) map2.get(toCurrency);