adding solution06 for task2
authorjapod@localhost
Tue, 07 Oct 2008 00:42:57 +0200
changeset 385838e0aaa81f
parent 37 d333e45f6df1
child 40 e6cd39d9b6a2
adding solution06 for task2
task2/solution06/src/org/apidesign/apifest08/currency/Convertor.java
     1.1 --- a/task2/solution06/src/org/apidesign/apifest08/currency/Convertor.java	Tue Oct 07 00:25:53 2008 +0200
     1.2 +++ b/task2/solution06/src/org/apidesign/apifest08/currency/Convertor.java	Tue Oct 07 00:42:57 2008 +0200
     1.3 @@ -4,23 +4,44 @@
     1.4  
     1.5  import java.math.BigDecimal;
     1.6  import java.math.RoundingMode;
     1.7 +import java.util.ArrayList;
     1.8  import java.util.Currency;
     1.9 +import java.util.List;
    1.10  
    1.11  public final class Convertor {
    1.12  	
    1.13 -	private final Currency first;
    1.14 -	private final Currency second;
    1.15 -	private final BigDecimal rateValue; // a rate between the first currency and the second currency
    1.16 -	public static final BigDecimal one = new BigDecimal(1);	
    1.17 +	private List<ConvertorDelegate> convertorDelegates = new ArrayList<ConvertorDelegate>();
    1.18  	
    1.19 +	
    1.20 +	/**
    1.21 +	 * Create new instance of the converter for the given currencies and its rate.
    1.22 +	 * 
    1.23 +	 * @param rateValue the rate between the first and the second currency
    1.24 +	 * @param currencyFirst the first currency
    1.25 +	 * @param currencySecond the second currency
    1.26 +	 */
    1.27  	public Convertor(BigDecimal rateValue, Currency currencyFirst, Currency currencySecond) {
    1.28  		notNull(currencyFirst, "currencyFirst");
    1.29  		notNull(currencySecond, "currencySecond");		
    1.30 -		notNull(rateValue, "rateValue");
    1.31 +		notNull(rateValue, "rateValue");	
    1.32 +		convertorDelegates.add(new ConvertorDelegate(rateValue, currencyFirst, currencySecond));
    1.33 +	}
    1.34 +	
    1.35 +	/**
    1.36 +	 * Create new instance of the convertor from the given convertors. 
    1.37 +	 * @param convertors the convertors
    1.38 +	 */
    1.39 +	public Convertor(Convertor... convertors) {
    1.40 +		notNull(convertors, "convertors");
    1.41 +		if(convertors.length == 0) {
    1.42 +			throw new IllegalArgumentException("There must be at least one converter.");
    1.43 +		}
    1.44  		
    1.45 -		this.rateValue = rateValue;
    1.46 -		this.first = currencyFirst;
    1.47 -		this.second = currencySecond;
    1.48 +		for(Convertor convertor: convertors) {
    1.49 +			if(convertor != null) {
    1.50 +				convertorDelegates.addAll(convertor.convertorDelegates);
    1.51 +			}
    1.52 +		}
    1.53  	}
    1.54      	
    1.55  	/**
    1.56 @@ -38,27 +59,66 @@
    1.57  		notNull(amount, "amount");
    1.58  		notNull(fromCurrency, "fromCurrency");
    1.59  		notNull(toCurrency, "toCurrency");
    1.60 +		ConvertorDelegate appropriateDelegate = null;
    1.61  		
    1.62 -		if((fromCurrency != first && fromCurrency != second) || (toCurrency != first && toCurrency != second)) {
    1.63 +		//try find an appropriate delegate for conversion
    1.64 +		for(ConvertorDelegate delegate : convertorDelegates) {
    1.65 +			if(delegate.isConversionSupported(fromCurrency, toCurrency)) {
    1.66 +				appropriateDelegate = delegate;
    1.67 +				break;
    1.68 +			}
    1.69 +		}
    1.70 +		
    1.71 +		if(appropriateDelegate == null) {
    1.72  			throw new UnsupportedConversionException(fromCurrency, toCurrency);
    1.73 -		}		
    1.74 -
    1.75 -		BigDecimal rateValue = getRateValue(fromCurrency, toCurrency);
    1.76 -		BigDecimal result = rateValue.multiply(amount);			
    1.77 -		return new Amount(result, toCurrency);	
    1.78 +		}
    1.79 +		
    1.80 +		return appropriateDelegate.convert(amount, fromCurrency, toCurrency);	
    1.81  	}
    1.82  	
    1.83 -	private BigDecimal getRateValue(Currency fromCurrency, Currency toCurrency) {		
    1.84 +	/**
    1.85 +	 * Internal delegate implements a logic for conversion between two currencies
    1.86 +	 * and vice versa.
    1.87 +	 * @see #isConversionSupported(Currency, Currency)
    1.88 +	 */
    1.89 +	private static class ConvertorDelegate {
    1.90 +		private final Currency first;
    1.91 +		private final Currency second;
    1.92 +		private final BigDecimal rateValue; // a rate between the first currency and the second currency
    1.93 +		public static final BigDecimal one = new BigDecimal(1);
    1.94  		
    1.95 -		BigDecimal retVal;
    1.96 -		
    1.97 -		if(first == fromCurrency) {
    1.98 -			retVal = rateValue;
    1.99 -		} else {	
   1.100 -			//reverse rate	
   1.101 -			retVal = one.divide(rateValue, 10 ,RoundingMode.HALF_UP);
   1.102 +		private ConvertorDelegate(BigDecimal rateValue, Currency currencyFirst, Currency currencySecond) {
   1.103 +			this.rateValue = rateValue;
   1.104 +			this.first = currencyFirst;
   1.105 +			this.second = currencySecond;
   1.106  		}
   1.107  		
   1.108 -		return retVal;
   1.109 +		private Amount convert(BigDecimal amount, Currency fromCurrency, Currency toCurrency) throws ConversionException {
   1.110 +			BigDecimal rateValue = getRateValue(fromCurrency, toCurrency);
   1.111 +			BigDecimal result = rateValue.multiply(amount);			
   1.112 +			return new Amount(result, toCurrency);	
   1.113 +		}
   1.114 +		
   1.115 +		private BigDecimal getRateValue(Currency fromCurrency, Currency toCurrency) {		
   1.116 +			
   1.117 +			BigDecimal retVal;
   1.118 +			
   1.119 +			if(first == fromCurrency) {
   1.120 +				retVal = rateValue;
   1.121 +			} else {	
   1.122 +				//reverse rate	
   1.123 +				retVal = one.divide(rateValue, 10 ,RoundingMode.HALF_UP);
   1.124 +			}
   1.125 +			
   1.126 +			return retVal;
   1.127 +		}
   1.128 +		
   1.129 +		/**
   1.130 +		 * @return <code>true</code> if the delegate is able to convert from the given currency
   1.131 +		 * to the given currency and vice versa otherwise <code>false</code>.
   1.132 +		 */
   1.133 +		private boolean isConversionSupported(Currency fromCurrency, Currency toCurrency) {
   1.134 +			return ((fromCurrency == first || fromCurrency == second) && (toCurrency == first || toCurrency == second));
   1.135 +		}
   1.136  	}
   1.137  }