diff -r c38391fb9b38 -r 58ec6da75f6f task4/solution06/src/org/apidesign/apifest08/currency/Convertor.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/task4/solution06/src/org/apidesign/apifest08/currency/Convertor.java Sat Oct 11 23:38:46 2008 +0200 @@ -0,0 +1,176 @@ +package org.apidesign.apifest08.currency; + +import static org.apidesign.apifest08.currency.Assert.notNull; + +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.util.ArrayList; +import java.util.Currency; +import java.util.List; + +/** + * Currency covertor. + */ +public final class Convertor { + + private List convertorDelegates = new ArrayList(); + + + /** + * Create new instance of the converter for the given currencies and its rate. + * + * @param rateValue the rate between the first and the second currency + * @param currencyFirst the first currency + * @param currencySecond the second currency + */ + public Convertor(BigDecimal rateValue, Currency currencyFirst, Currency currencySecond) { + notNull(currencyFirst, "currencyFirst"); + notNull(currencySecond, "currencySecond"); + notNull(rateValue, "rateValue"); + convertorDelegates.add(new ConvertorDelegate(new StaticRateProvider(rateValue), currencyFirst, currencySecond)); + } + + /** + * Create new instance of the converter for the given currencies and its rate. + * A rate value is provided by {@link RateProvider}. + * + * @param rateProvider the rate provider + * @param currencyFirst the first currency + * @param currencySecond the second currency + */ + public Convertor(RateProvider rateProvider, Currency currencyFirst, Currency currencySecond) { + notNull(currencyFirst, "currencyFirst"); + notNull(currencySecond, "currencySecond"); + notNull(rateProvider, "rateProvider"); + convertorDelegates.add(new ConvertorDelegate(rateProvider, currencyFirst, currencySecond)); + } + + /** + * Create new instance of the convertor from the given convertors. + * @param convertors the convertors + */ + public Convertor(Convertor... convertors) { + notNull(convertors, "convertors"); + if(convertors.length == 0) { + throw new IllegalArgumentException("There must be at least one converter."); + } + + for(Convertor convertor: convertors) { + if(convertor != null) { + convertorDelegates.addAll(convertor.convertorDelegates); + } + } + } + + /** + * Converts an amount value between the two currencies of this converter. + * + * @param amount an amount + * @param fromCurrency an amount currency + * @param toCurrency to a target currency + * @return a converted amount value + * + * @throws ConversionException if the conversion fails + * @throws UnsupportedConversionException if the conversion between a given currencies is not supported. + */ + public Amount convert(BigDecimal amount, Currency fromCurrency, Currency toCurrency) throws ConversionException { + notNull(amount, "amount"); + notNull(fromCurrency, "fromCurrency"); + notNull(toCurrency, "toCurrency"); + ConvertorDelegate appropriateDelegate = null; + + //try find an appropriate delegate for conversion + for(ConvertorDelegate delegate : convertorDelegates) { + if(delegate.isConversionSupported(fromCurrency, toCurrency)) { + appropriateDelegate = delegate; + break; + } + } + + if(appropriateDelegate == null) { + throw new UnsupportedConversionException(fromCurrency, toCurrency); + } + + return appropriateDelegate.convert(amount, fromCurrency, toCurrency); + } + + /** + * Internal delegate implements a logic for conversion between two currencies + * and vice versa. + * @see #isConversionSupported(Currency, Currency) + */ + private static class ConvertorDelegate { + private final Currency first; + private final Currency second; + private final RateProvider rateProvider; + public static final BigDecimal one = new BigDecimal(1); + + + private ConvertorDelegate(RateProvider rateProvider, Currency currencyFirst, Currency currencySecond) { + this.rateProvider = rateProvider; + this.first = currencyFirst; + this.second = currencySecond; + } + + private Amount convert(BigDecimal amount, Currency fromCurrency, Currency toCurrency) throws ConversionException { + BigDecimal rateValue = getRateValue(fromCurrency, toCurrency); + BigDecimal result = rateValue.multiply(amount); + return new Amount(result, toCurrency); + } + + private BigDecimal getRateValue(Currency fromCurrency, Currency toCurrency) { + + BigDecimal retVal; + + if(first == fromCurrency) { + BigDecimal rateValue = rateProvider.getRate(); + if(rateValue == null) { + throw new NullPointerException("Rate cannot be null!"); + } + retVal = rateValue; + } else { + BigDecimal rateValue = rateProvider.getRate(); + if(rateValue == null) { + throw new NullPointerException("Rate cannot be null!"); + } + //reverse rate + retVal = one.divide(rateValue, 10 ,RoundingMode.HALF_UP); + } + + return retVal; + } + + /** + * @return true if the delegate is able to convert from the given currency + * to the given currency and vice versa otherwise false. + */ + private boolean isConversionSupported(Currency fromCurrency, Currency toCurrency) { + return ((fromCurrency == first || fromCurrency == second) && (toCurrency == first || toCurrency == second)); + } + } + + /** + * A rate provider. This class represents a way how could be "static" convertor + * extended in order converts according to current rate. + */ + public static abstract class RateProvider { + + /** + * @return a rate between the from currency and the to currency associated with + * a given convertor. + */ + public abstract BigDecimal getRate(); + } + + private static class StaticRateProvider extends RateProvider{ + private final BigDecimal rateValue; + + private StaticRateProvider(BigDecimal rateValue){ + this.rateValue = rateValue; + } + + public BigDecimal getRate() { + return this.rateValue; + } + } +}