1 package org.apidesign.apifest08.currency;
4 import java.math.BigDecimal;
5 import java.util.Currency;
10 * A Convertor that looks up the exchange rate with each call to "convert".
12 * @author D'Arcy Smith
15 public class OnlineConvertor
19 * The currency to convert from.
21 private final Currency currencyA;
24 * The currency to convert to.
26 private final Currency currencyB;
29 * Used to find the current exchange rate.
31 private final ExchangeRateFinder finder;
34 * The convertor to perform the conversion.
36 private Convertor realConvertor;
39 * Constructs an OnlinConvertor with the specified currencies.
41 * @param a the currency to convert from.
42 * @param b the currency to convert to.
43 * @param f the finder used to obtanin the current exchange rate.
44 * @throws IllegalArgumentException if either a or b are null.
46 public OnlineConvertor(final Currency a,
48 final ExchangeRateFinder f)
52 throw new IllegalArgumentException("a cannot be null");
57 throw new IllegalArgumentException("b cannot be null");
62 throw new IllegalArgumentException("f cannot be null");
68 realConvertor = lookupRate();
72 * Convert an amount from one currency to another. Before the conversion takes place
73 * the current exchange rate is looked up.
75 * @param from the currency to convert from.
76 * @param to the currency to convert to.
77 * @param amount the amount to convert.
78 * @return the converted amount.
79 * @throws IllegalArgumentException if any of the arguments are null.
80 * @throws InvalidConversionException if either from or to are not equal to the currencies passed to the constructor.
82 public BigDecimal convert(final Currency from,
84 final BigDecimal amount)
85 throws InvalidConversionException
87 final BigDecimal value;
91 realConvertor = lookupRate();
92 value = realConvertor.convert(from, to, amount);
99 * Lookup the current exchange rate.
103 private final Convertor lookupRate()
105 final Convertor convertor;
106 final ExchangeRate rate;
108 rate = finder.findRate(currencyA, currencyB);
109 convertor = ConvertorFactory.getConvertor(rate.getCurrencyA(), rate.getRateAtoB(), rate.getCurrencyB(), rate.getRateBtoA());
115 * Check to see if converting between the two currencies is possible.
117 * @param from the currency to convert from.
118 * @param to the currency to convert to.
119 * @return true if the conversion is possible.
120 * @throws IllegalArgumentException if either from or to are null.
122 public boolean canConvert(final Currency from,
127 throw new IllegalArgumentException("from cannot be null");
132 throw new IllegalArgumentException("to cannot be null");
135 return (realConvertor.canConvert(from, to));
139 * Get the currencies that the convertor supports.
141 * @return the supported currencies.
143 public Set<Currency> getCurrencies()
145 return (realConvertor.getCurrencies());
149 * Get the conversion rate between two currencies. This does not lookup the current
150 * conversion rate (it probably should, but given the way the contest works that might
151 * not be a good idea - if it were the real world way it might be a good idea).
153 * @param from the currency to convert from.
154 * @param to the currency to convert to.
155 * @return the conversion rate between the two currencies.
156 * @throws InvalidConversionException if canConvert would return false.
157 * @throws IllegalArgumentException if either from or to are null.
159 public BigDecimal getConversionRate(final Currency from,
161 throws InvalidConversionException
165 throw new IllegalArgumentException("from cannot be null");
170 throw new IllegalArgumentException("to cannot be null");
173 return (realConvertor.getConversionRate(from, to));