japod@23
|
1 |
|
japod@23
|
2 |
package org.apidesign.apifest08.currency;
|
japod@23
|
3 |
|
japod@23
|
4 |
import java.math.BigDecimal;
|
japod@23
|
5 |
import java.util.Date;
|
japod@41
|
6 |
import java.util.HashMap;
|
japod@41
|
7 |
import java.util.Map;
|
japod@23
|
8 |
|
japod@23
|
9 |
/**
|
japod@41
|
10 |
* Exchange rate provider. Provides fixed exchange rate which does not depend
|
japod@41
|
11 |
* on date (method {@link #getExchangeRate()} ).
|
japod@23
|
12 |
*
|
japod@41
|
13 |
* Date dependend exchange rate to be implemented.
|
japod@23
|
14 |
*
|
japod@23
|
15 |
* @author arnostvalicek
|
japod@23
|
16 |
*/
|
japod@23
|
17 |
public class ExchangeRateProvider {
|
japod@23
|
18 |
BigDecimal fromValue, toValue;
|
japod@23
|
19 |
ConvertorCurrency fromCurrency, toCurrency;
|
japod@41
|
20 |
|
japod@41
|
21 |
Map exchangeRateMap = new HashMap();
|
japod@23
|
22 |
|
japod@23
|
23 |
/**
|
japod@23
|
24 |
* Simple constructor for <code>ExchangeRateProviderM</code> which can provide fixed exchange rate.
|
japod@23
|
25 |
*
|
japod@41
|
26 |
* Describes conversion <em>from ONE</em> to <em>to ONE</em> currency.
|
japod@23
|
27 |
*
|
japod@23
|
28 |
* @param fromValue From value. BigDecimal value, precision should be set to currency precision.
|
japod@23
|
29 |
* @param fromCurrency From currency.
|
japod@23
|
30 |
* @param toValue To value. BigDecimal value, precision should be set to currency precision.
|
japod@23
|
31 |
* @param toCurrency To currency.
|
japod@41
|
32 |
* @deprecated deprecated since task2. Use {@link #createExchangeRateProvider() } instead of this constructor.
|
japod@23
|
33 |
*/
|
japod@23
|
34 |
public ExchangeRateProvider(BigDecimal fromValue, ConvertorCurrency fromCurrency, BigDecimal toValue, ConvertorCurrency toCurrency) {
|
japod@23
|
35 |
this.fromValue = fromValue;
|
japod@23
|
36 |
this.toValue = toValue;
|
japod@23
|
37 |
this.fromCurrency = fromCurrency;
|
japod@23
|
38 |
this.toCurrency = toCurrency;
|
japod@23
|
39 |
}
|
japod@23
|
40 |
|
japod@41
|
41 |
private ExchangeRateProvider() {
|
japod@41
|
42 |
|
japod@41
|
43 |
}
|
japod@41
|
44 |
|
japod@41
|
45 |
/**
|
japod@41
|
46 |
* Static method to create new exchange rate provider. This exchange rate provider does not contain
|
japod@41
|
47 |
* any exchange rates (this is difference to public constructor).
|
japod@41
|
48 |
* @return New <code>ExchangeRateProvider</code>
|
japod@41
|
49 |
*/
|
japod@41
|
50 |
public static ExchangeRateProvider createExchangeRateProvider() {
|
japod@41
|
51 |
ExchangeRateProvider provider = new ExchangeRateProvider();
|
japod@41
|
52 |
return provider;
|
japod@41
|
53 |
}
|
japod@41
|
54 |
|
japod@41
|
55 |
/**
|
japod@41
|
56 |
* Add new exchange rate to <code></code> to this exchange rate provider.
|
japod@41
|
57 |
* <p>
|
japod@41
|
58 |
* Example of specifiing conversion rate: 100 SKK == 80 CZK:<br>
|
japod@41
|
59 |
* <code>addFixedCurencyRate(ConvertorCurrency.getInstance("SKK"), new BigDecimal(100), ConvertorCurrency.getInstance("CZK"), new BigDecimal(80));</code>
|
japod@41
|
60 |
* <p>
|
japod@41
|
61 |
*
|
japod@41
|
62 |
* @param fromCurrency Source currency.
|
japod@41
|
63 |
* @param fromValue Valye for from currency.
|
japod@41
|
64 |
* @param toCurrency Target currency.
|
japod@41
|
65 |
* @param toValue Value for target currency.
|
japod@41
|
66 |
*/
|
japod@41
|
67 |
public synchronized void addFixedCurencyRate(ConvertorCurrency fromCurrency, BigDecimal fromValue, ConvertorCurrency toCurrency, BigDecimal toValue) {
|
japod@41
|
68 |
if (fromValue==null) {
|
japod@41
|
69 |
throw new NullPointerException("fromValue can't be null");
|
japod@41
|
70 |
}
|
japod@41
|
71 |
if (toValue==null) {
|
japod@41
|
72 |
throw new NullPointerException("toValue can't be null");
|
japod@41
|
73 |
}
|
japod@41
|
74 |
Map map2 = (Map) exchangeRateMap.get(fromCurrency);
|
japod@41
|
75 |
if (map2==null) {
|
japod@41
|
76 |
map2 = new HashMap();
|
japod@41
|
77 |
exchangeRateMap.put(fromCurrency, map2);
|
japod@41
|
78 |
}
|
japod@41
|
79 |
|
japod@41
|
80 |
ExchangeRate rate = new ExchangeRate(fromValue, toValue);
|
japod@41
|
81 |
map2.put(toCurrency, rate);
|
japod@41
|
82 |
}
|
japod@41
|
83 |
|
japod@23
|
84 |
/**
|
japod@23
|
85 |
* Get fixed exange rate for currencies (from->to).
|
japod@23
|
86 |
* @return Returns exchange rate.
|
japod@41
|
87 |
* @deprecated deprecated since task2. Use {@link #getExchangeRate(ConvertorCurrency, ConvertorCurrency) }
|
japod@23
|
88 |
*/
|
japod@23
|
89 |
public ExchangeRate getExchangeRate() {
|
japod@23
|
90 |
return new ExchangeRate(fromValue, toValue);
|
japod@23
|
91 |
}
|
japod@41
|
92 |
|
japod@41
|
93 |
|
japod@41
|
94 |
/**
|
japod@41
|
95 |
* Get fixed exange rate for currencies (from->to).
|
japod@41
|
96 |
* @return Returns exchange rate or <code>null</code> if exchange rate not found.
|
japod@41
|
97 |
*/
|
japod@41
|
98 |
public ExchangeRate getExchangeRate(ConvertorCurrency fromCurrency, ConvertorCurrency toCurrency) {
|
japod@41
|
99 |
return getExchangeRateImpl(fromCurrency, toCurrency);
|
japod@41
|
100 |
}
|
japod@41
|
101 |
|
japod@41
|
102 |
/**
|
japod@41
|
103 |
* Get fixed exange rate for currencies (from->to) or reversed exchange rate (to->from).
|
japod@41
|
104 |
* @return Returns exchange rate or <code>null</code> if exchange rate not found.
|
japod@41
|
105 |
*/
|
japod@41
|
106 |
public ExchangeRate getReversibleExchangeRate(ConvertorCurrency fromCurrency, ConvertorCurrency toCurrency) {
|
japod@41
|
107 |
ExchangeRate rate = getExchangeRateImpl(fromCurrency, toCurrency);
|
japod@41
|
108 |
if (rate==null) {
|
japod@41
|
109 |
ExchangeRate revertedRate = getExchangeRateImpl(toCurrency, fromCurrency);
|
japod@41
|
110 |
if (revertedRate!=null) {
|
japod@41
|
111 |
rate = ExchangeRate.createRevertedRate(revertedRate);
|
japod@41
|
112 |
}
|
japod@41
|
113 |
}
|
japod@41
|
114 |
return rate;
|
japod@41
|
115 |
}
|
japod@41
|
116 |
|
japod@41
|
117 |
|
japod@23
|
118 |
|
japod@23
|
119 |
/**
|
japod@23
|
120 |
* Get exchange rate for currencies (from->to) based on provided date.
|
japod@23
|
121 |
* @param date Date for which exchange rate should be provided.
|
japod@23
|
122 |
* @return Returns exchange rate
|
japod@41
|
123 |
* @deprecated deprecated since task2. No real implementation in version2.
|
japod@23
|
124 |
*/
|
japod@23
|
125 |
public ExchangeRate getExchangeRate(Date date) {
|
japod@23
|
126 |
return new ExchangeRate(fromValue, toValue);
|
japod@23
|
127 |
}
|
japod@23
|
128 |
|
japod@23
|
129 |
|
japod@23
|
130 |
ConvertorCurrency getFromCurrency() {
|
japod@23
|
131 |
return fromCurrency;
|
japod@23
|
132 |
}
|
japod@23
|
133 |
|
japod@23
|
134 |
ConvertorCurrency getToCurrency() {
|
japod@23
|
135 |
return toCurrency;
|
japod@23
|
136 |
}
|
japod@41
|
137 |
|
japod@41
|
138 |
private ExchangeRate getExchangeRateImpl(ConvertorCurrency fromCurrency, ConvertorCurrency toCurrency) {
|
japod@41
|
139 |
if (fromValue != null && toValue != null && fromCurrency.equals(this.fromCurrency) && toCurrency.equals(this.toCurrency)) {
|
japod@41
|
140 |
return new ExchangeRate(fromValue, toValue);
|
japod@41
|
141 |
}
|
japod@41
|
142 |
//return new ExchangeRate(fromValue, toValue);
|
japod@41
|
143 |
Map map2 = (Map) exchangeRateMap.get(fromCurrency);
|
japod@41
|
144 |
if (map2 == null) {
|
japod@41
|
145 |
return null;
|
japod@41
|
146 |
}
|
japod@41
|
147 |
ExchangeRate result = (ExchangeRate) map2.get(toCurrency);
|
japod@41
|
148 |
return result;
|
japod@41
|
149 |
}
|
japod@23
|
150 |
|
japod@23
|
151 |
|
japod@23
|
152 |
}
|