diff -r 14e78f48ac2b -r 58ec6da75f6f task4/solution04/src/org/apidesign/apifest08/currency/OnlineConvertor.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/task4/solution04/src/org/apidesign/apifest08/currency/OnlineConvertor.java Sat Oct 11 23:38:46 2008 +0200 @@ -0,0 +1,175 @@ +package org.apidesign.apifest08.currency; + + +import java.math.BigDecimal; +import java.util.Currency; +import java.util.Set; + + +/** + * A Convertor that looks up the exchange rate with each call to "convert". + * + * @author D'Arcy Smith + * @version 1.0 + */ +public class OnlineConvertor + implements Convertor +{ + /** + * The currency to convert from. + */ + private final Currency currencyA; + + /** + * The currency to convert to. + */ + private final Currency currencyB; + + /** + * Used to find the current exchange rate. + */ + private final ExchangeRateFinder finder; + + /** + * The convertor to perform the conversion. + */ + private Convertor realConvertor; + + /** + * Constructs an OnlinConvertor with the specified currencies. + * + * @param a the currency to convert from. + * @param b the currency to convert to. + * @param f the finder used to obtanin the current exchange rate. + * @throws IllegalArgumentException if either a or b are null. + */ + public OnlineConvertor(final Currency a, + final Currency b, + final ExchangeRateFinder f) + { + if(a == null) + { + throw new IllegalArgumentException("a cannot be null"); + } + + if(b == null) + { + throw new IllegalArgumentException("b cannot be null"); + } + + if(f == null) + { + throw new IllegalArgumentException("f cannot be null"); + } + + currencyA = a; + currencyB = b; + finder = f; + realConvertor = lookupRate(); + } + + /** + * Convert an amount from one currency to another. Before the conversion takes place + * the current exchange rate is looked up. + * + * @param from the currency to convert from. + * @param to the currency to convert to. + * @param amount the amount to convert. + * @return the converted amount. + * @throws IllegalArgumentException if any of the arguments are null. + * @throws InvalidConversionException if either from or to are not equal to the currencies passed to the constructor. + */ + public BigDecimal convert(final Currency from, + final Currency to, + final BigDecimal amount) + throws InvalidConversionException + { + final BigDecimal value; + + synchronized(this) + { + realConvertor = lookupRate(); + value = realConvertor.convert(from, to, amount); + } + + return (value); + } + + /** + * Lookup the current exchange rate. + * + * @return + */ + private final Convertor lookupRate() + { + final Convertor convertor; + final ExchangeRate rate; + + rate = finder.findRate(currencyA, currencyB); + convertor = ConvertorFactory.getConvertor(rate.getCurrencyA(), rate.getRateAtoB(), rate.getCurrencyB(), rate.getRateBtoA()); + + return (convertor); + } + + /** + * Check to see if converting between the two currencies is possible. + * + * @param from the currency to convert from. + * @param to the currency to convert to. + * @return true if the conversion is possible. + * @throws IllegalArgumentException if either from or to are null. + */ + public boolean canConvert(final Currency from, + final Currency to) + { + if(from == null) + { + throw new IllegalArgumentException("from cannot be null"); + } + + if(to == null) + { + throw new IllegalArgumentException("to cannot be null"); + } + + return (realConvertor.canConvert(from, to)); + } + + /** + * Get the currencies that the convertor supports. + * + * @return the supported currencies. + */ + public Set getCurrencies() + { + return (realConvertor.getCurrencies()); + } + + /** + * Get the conversion rate between two currencies. This does not lookup the current + * conversion rate (it probably should, but given the way the contest works that might + * not be a good idea - if it were the real world way it might be a good idea). + * + * @param from the currency to convert from. + * @param to the currency to convert to. + * @return the conversion rate between the two currencies. + * @throws InvalidConversionException if canConvert would return false. + * @throws IllegalArgumentException if either from or to are null. + */ + public BigDecimal getConversionRate(final Currency from, + final Currency to) + throws InvalidConversionException + { + if(from == null) + { + throw new IllegalArgumentException("from cannot be null"); + } + + if(to == null) + { + throw new IllegalArgumentException("to cannot be null"); + } + + return (realConvertor.getConversionRate(from, to)); + } +}