japod@34
|
1 |
package org.apidesign.apifest08.currency;
|
japod@34
|
2 |
|
japod@34
|
3 |
import java.io.Serializable;
|
japod@34
|
4 |
import java.util.Arrays;
|
japod@34
|
5 |
import java.util.Currency;
|
japod@34
|
6 |
|
japod@34
|
7 |
/**
|
japod@34
|
8 |
* Convertor that is composed from other convertors.
|
japod@34
|
9 |
* @author lukas
|
japod@34
|
10 |
*
|
japod@34
|
11 |
*/
|
japod@34
|
12 |
class CompositeConvertor implements ExtendedConvertor, Serializable {
|
japod@34
|
13 |
|
japod@34
|
14 |
private static final long serialVersionUID = -2702026568249130055L;
|
japod@34
|
15 |
|
japod@34
|
16 |
private final ExtendedConvertor[] convertors;
|
japod@34
|
17 |
|
japod@34
|
18 |
|
japod@34
|
19 |
public CompositeConvertor(ExtendedConvertor... convertors) {
|
japod@34
|
20 |
this.convertors = convertors.clone();
|
japod@34
|
21 |
for (ExtendedConvertor convertor: this.convertors)
|
japod@34
|
22 |
{
|
japod@34
|
23 |
if (convertor==null)
|
japod@34
|
24 |
{
|
japod@34
|
25 |
throw new NullPointerException("One of the convertors to be merged is null");
|
japod@34
|
26 |
}
|
japod@34
|
27 |
}
|
japod@34
|
28 |
}
|
japod@34
|
29 |
|
japod@34
|
30 |
/**
|
japod@34
|
31 |
* Returns true if the composite contains convertor that supports given conversion.
|
japod@34
|
32 |
*/
|
japod@34
|
33 |
public boolean isConversionSupported(Currency from, Currency to) {
|
japod@34
|
34 |
return findConvertorFor(from, to)!=null;
|
japod@34
|
35 |
}
|
japod@34
|
36 |
|
japod@34
|
37 |
|
japod@34
|
38 |
/**
|
japod@34
|
39 |
* If the composite contains convertor that supports given conversion, delegates to its convert method.
|
japod@34
|
40 |
* Throws {@link IllegalArgumentException} convertor supporting given conversion is not found.
|
japod@34
|
41 |
*/
|
japod@34
|
42 |
public Money convert(Money amount, Currency destinationCurrency) throws IllegalArgumentException {
|
japod@34
|
43 |
ExtendedConvertor convertor = findConvertorFor(amount.getCurrency(), destinationCurrency);
|
japod@34
|
44 |
if (convertor!=null)
|
japod@34
|
45 |
{
|
japod@34
|
46 |
return convertor.convert(amount, destinationCurrency);
|
japod@34
|
47 |
}
|
japod@34
|
48 |
throw new IllegalArgumentException("Conversion not supported. Can not convert to "+destinationCurrency+".");
|
japod@34
|
49 |
}
|
japod@34
|
50 |
|
japod@34
|
51 |
/**
|
japod@34
|
52 |
* Finds convertor for given currencies. If not found, returns null.
|
japod@34
|
53 |
* @param from
|
japod@34
|
54 |
* @param to
|
japod@34
|
55 |
* @return
|
japod@34
|
56 |
*/
|
japod@34
|
57 |
ExtendedConvertor findConvertorFor(Currency from, Currency to) {
|
japod@34
|
58 |
//does linear search, in the future can cache the results.
|
japod@34
|
59 |
for (ExtendedConvertor convertor:convertors)
|
japod@34
|
60 |
{
|
japod@34
|
61 |
if (convertor.isConversionSupported(from, to))
|
japod@34
|
62 |
{
|
japod@34
|
63 |
return convertor;
|
japod@34
|
64 |
}
|
japod@34
|
65 |
}
|
japod@34
|
66 |
return null;
|
japod@34
|
67 |
}
|
japod@34
|
68 |
|
japod@34
|
69 |
|
japod@34
|
70 |
@Override
|
japod@34
|
71 |
public String toString() {
|
japod@34
|
72 |
return getClass().getName()+" converts "+Arrays.toString(convertors);
|
japod@34
|
73 |
}
|
japod@34
|
74 |
|
japod@34
|
75 |
}
|