japod@55
|
1 |
package org.apidesign.apifest08.currency;
|
japod@55
|
2 |
|
japod@55
|
3 |
|
japod@55
|
4 |
import java.math.BigDecimal;
|
japod@55
|
5 |
import java.util.Currency;
|
japod@55
|
6 |
import java.util.Set;
|
japod@55
|
7 |
|
japod@55
|
8 |
|
japod@55
|
9 |
/**
|
japod@55
|
10 |
* A Convertor that looks up the exchange rate with each call to "convert".
|
japod@55
|
11 |
*
|
japod@55
|
12 |
* @author D'Arcy Smith
|
japod@55
|
13 |
* @version 1.0
|
japod@55
|
14 |
*/
|
japod@55
|
15 |
public class OnlineConvertor
|
japod@55
|
16 |
implements Convertor
|
japod@55
|
17 |
{
|
japod@55
|
18 |
/**
|
japod@55
|
19 |
* The currency to convert from.
|
japod@55
|
20 |
*/
|
japod@55
|
21 |
private final Currency currencyA;
|
japod@55
|
22 |
|
japod@55
|
23 |
/**
|
japod@55
|
24 |
* The currency to convert to.
|
japod@55
|
25 |
*/
|
japod@55
|
26 |
private final Currency currencyB;
|
japod@55
|
27 |
|
japod@55
|
28 |
/**
|
japod@55
|
29 |
* Used to find the current exchange rate.
|
japod@55
|
30 |
*/
|
japod@55
|
31 |
private final ExchangeRateFinder finder;
|
japod@55
|
32 |
|
japod@55
|
33 |
/**
|
japod@55
|
34 |
* The convertor to perform the conversion.
|
japod@55
|
35 |
*/
|
japod@55
|
36 |
private Convertor realConvertor;
|
japod@55
|
37 |
|
japod@55
|
38 |
/**
|
japod@55
|
39 |
* Constructs an OnlinConvertor with the specified currencies.
|
japod@55
|
40 |
*
|
japod@55
|
41 |
* @param a the currency to convert from.
|
japod@55
|
42 |
* @param b the currency to convert to.
|
japod@55
|
43 |
* @param f the finder used to obtanin the current exchange rate.
|
japod@55
|
44 |
* @throws IllegalArgumentException if either a or b are null.
|
japod@55
|
45 |
*/
|
japod@55
|
46 |
public OnlineConvertor(final Currency a,
|
japod@55
|
47 |
final Currency b,
|
japod@55
|
48 |
final ExchangeRateFinder f)
|
japod@55
|
49 |
{
|
japod@55
|
50 |
if(a == null)
|
japod@55
|
51 |
{
|
japod@55
|
52 |
throw new IllegalArgumentException("a cannot be null");
|
japod@55
|
53 |
}
|
japod@55
|
54 |
|
japod@55
|
55 |
if(b == null)
|
japod@55
|
56 |
{
|
japod@55
|
57 |
throw new IllegalArgumentException("b cannot be null");
|
japod@55
|
58 |
}
|
japod@55
|
59 |
|
japod@55
|
60 |
if(f == null)
|
japod@55
|
61 |
{
|
japod@55
|
62 |
throw new IllegalArgumentException("f cannot be null");
|
japod@55
|
63 |
}
|
japod@55
|
64 |
|
japod@55
|
65 |
currencyA = a;
|
japod@55
|
66 |
currencyB = b;
|
japod@55
|
67 |
finder = f;
|
japod@55
|
68 |
realConvertor = lookupRate();
|
japod@55
|
69 |
}
|
japod@55
|
70 |
|
japod@55
|
71 |
/**
|
japod@55
|
72 |
* Convert an amount from one currency to another. Before the conversion takes place
|
japod@55
|
73 |
* the current exchange rate is looked up.
|
japod@55
|
74 |
*
|
japod@55
|
75 |
* @param from the currency to convert from.
|
japod@55
|
76 |
* @param to the currency to convert to.
|
japod@55
|
77 |
* @param amount the amount to convert.
|
japod@55
|
78 |
* @return the converted amount.
|
japod@55
|
79 |
* @throws IllegalArgumentException if any of the arguments are null.
|
japod@55
|
80 |
* @throws InvalidConversionException if either from or to are not equal to the currencies passed to the constructor.
|
japod@55
|
81 |
*/
|
japod@55
|
82 |
public BigDecimal convert(final Currency from,
|
japod@55
|
83 |
final Currency to,
|
japod@55
|
84 |
final BigDecimal amount)
|
japod@55
|
85 |
throws InvalidConversionException
|
japod@55
|
86 |
{
|
japod@55
|
87 |
final BigDecimal value;
|
japod@55
|
88 |
|
japod@55
|
89 |
synchronized(this)
|
japod@55
|
90 |
{
|
japod@55
|
91 |
realConvertor = lookupRate();
|
japod@55
|
92 |
value = realConvertor.convert(from, to, amount);
|
japod@55
|
93 |
}
|
japod@55
|
94 |
|
japod@55
|
95 |
return (value);
|
japod@55
|
96 |
}
|
japod@55
|
97 |
|
japod@55
|
98 |
/**
|
japod@55
|
99 |
* Lookup the current exchange rate.
|
japod@55
|
100 |
*
|
japod@55
|
101 |
* @return
|
japod@55
|
102 |
*/
|
japod@55
|
103 |
private final Convertor lookupRate()
|
japod@55
|
104 |
{
|
japod@55
|
105 |
final Convertor convertor;
|
japod@55
|
106 |
final ExchangeRate rate;
|
japod@55
|
107 |
|
japod@55
|
108 |
rate = finder.findRate(currencyA, currencyB);
|
japod@55
|
109 |
convertor = ConvertorFactory.getConvertor(rate.getCurrencyA(), rate.getRateAtoB(), rate.getCurrencyB(), rate.getRateBtoA());
|
japod@55
|
110 |
|
japod@55
|
111 |
return (convertor);
|
japod@55
|
112 |
}
|
japod@55
|
113 |
|
japod@55
|
114 |
/**
|
japod@55
|
115 |
* Check to see if converting between the two currencies is possible.
|
japod@55
|
116 |
*
|
japod@55
|
117 |
* @param from the currency to convert from.
|
japod@55
|
118 |
* @param to the currency to convert to.
|
japod@55
|
119 |
* @return true if the conversion is possible.
|
japod@55
|
120 |
* @throws IllegalArgumentException if either from or to are null.
|
japod@55
|
121 |
*/
|
japod@55
|
122 |
public boolean canConvert(final Currency from,
|
japod@55
|
123 |
final Currency to)
|
japod@55
|
124 |
{
|
japod@55
|
125 |
if(from == null)
|
japod@55
|
126 |
{
|
japod@55
|
127 |
throw new IllegalArgumentException("from cannot be null");
|
japod@55
|
128 |
}
|
japod@55
|
129 |
|
japod@55
|
130 |
if(to == null)
|
japod@55
|
131 |
{
|
japod@55
|
132 |
throw new IllegalArgumentException("to cannot be null");
|
japod@55
|
133 |
}
|
japod@55
|
134 |
|
japod@55
|
135 |
return (realConvertor.canConvert(from, to));
|
japod@55
|
136 |
}
|
japod@55
|
137 |
|
japod@55
|
138 |
/**
|
japod@55
|
139 |
* Get the currencies that the convertor supports.
|
japod@55
|
140 |
*
|
japod@55
|
141 |
* @return the supported currencies.
|
japod@55
|
142 |
*/
|
japod@55
|
143 |
public Set<Currency> getCurrencies()
|
japod@55
|
144 |
{
|
japod@55
|
145 |
return (realConvertor.getCurrencies());
|
japod@55
|
146 |
}
|
japod@55
|
147 |
|
japod@55
|
148 |
/**
|
japod@55
|
149 |
* Get the conversion rate between two currencies. This does not lookup the current
|
japod@55
|
150 |
* conversion rate (it probably should, but given the way the contest works that might
|
japod@55
|
151 |
* not be a good idea - if it were the real world way it might be a good idea).
|
japod@55
|
152 |
*
|
japod@55
|
153 |
* @param from the currency to convert from.
|
japod@55
|
154 |
* @param to the currency to convert to.
|
japod@55
|
155 |
* @return the conversion rate between the two currencies.
|
japod@55
|
156 |
* @throws InvalidConversionException if canConvert would return false.
|
japod@55
|
157 |
* @throws IllegalArgumentException if either from or to are null.
|
japod@55
|
158 |
*/
|
japod@55
|
159 |
public BigDecimal getConversionRate(final Currency from,
|
japod@55
|
160 |
final Currency to)
|
japod@55
|
161 |
throws InvalidConversionException
|
japod@55
|
162 |
{
|
japod@55
|
163 |
if(from == null)
|
japod@55
|
164 |
{
|
japod@55
|
165 |
throw new IllegalArgumentException("from cannot be null");
|
japod@55
|
166 |
}
|
japod@55
|
167 |
|
japod@55
|
168 |
if(to == null)
|
japod@55
|
169 |
{
|
japod@55
|
170 |
throw new IllegalArgumentException("to cannot be null");
|
japod@55
|
171 |
}
|
japod@55
|
172 |
|
japod@55
|
173 |
return (realConvertor.getConversionRate(from, to));
|
japod@55
|
174 |
}
|
japod@55
|
175 |
}
|