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