re-applying Dagi's patch
authorjapod@localhost
Tue, 07 Oct 2008 00:46:19 +0200
changeset 39daec35cd96e8
parent 37 d333e45f6df1
child 40 e6cd39d9b6a2
re-applying Dagi's patch
task2/solution06/src/org/apidesign/apifest08/currency/Convertor.java
task2/solution06/test/org/apidesign/apifest08/test/Task2Test.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:46:19 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  }
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/task2/solution06/test/org/apidesign/apifest08/test/Task2Test.java	Tue Oct 07 00:46:19 2008 +0200
     2.3 @@ -0,0 +1,117 @@
     2.4 +package org.apidesign.apifest08.test;
     2.5 +
     2.6 +import static org.apidesign.apifest08.test.Currencies.CZK;
     2.7 +import static org.apidesign.apifest08.test.Currencies.USD;
     2.8 +import static org.apidesign.apifest08.test.Currencies.SKK;
     2.9 +
    2.10 +import java.math.BigDecimal;
    2.11 +
    2.12 +import junit.framework.TestCase;
    2.13 +import org.apidesign.apifest08.currency.Convertor;
    2.14 +import org.apidesign.apifest08.test.Task1Test;
    2.15 +
    2.16 +/** There are many currencies around the world and many banks manipulate
    2.17 + * with more than one or two at the same time. As banks are usually the
    2.18 + * best paying clients, which is true even in case of your Convertor API,
    2.19 + * it is reasonable to listen to their requests.
    2.20 + * <p>
    2.21 + * The quest for today is to enhance your existing convertor API to hold
    2.22 + * information about many currencies and allow conversions between any of them.
    2.23 + * Also, as conversion rates for diferent currencies usually arise from various
    2.24 + * bank departments, there is another important need. There is a need to
    2.25 + * compose two convertors into one by merging all the information about
    2.26 + * currencies they know about.
    2.27 + */
    2.28 +public class Task2Test extends TestCase {
    2.29 +    public Task2Test(String testName) {
    2.30 +        super(testName);
    2.31 +    }
    2.32 +
    2.33 +    @Override
    2.34 +    protected void setUp() throws Exception {
    2.35 +    }
    2.36 +
    2.37 +    @Override
    2.38 +    protected void tearDown() throws Exception {
    2.39 +    }
    2.40 +
    2.41 +    // As in Task1Test, keep in mind, that there are three parts
    2.42 +    // of the whole system:
    2.43 +    // 1. there is someone who knows the current exchange rate
    2.44 +    // 2. there is someone who wants to do the conversion
    2.45 +    // 3. there is the API between 1. and 2. which allows them to communicate
    2.46 +    // 
    2.47 +    // Please backward compatibly enhance your existing API to support following
    2.48 +    // usecases:
    2.49 +    //
    2.50 +    
    2.51 +    /** Create convertor that understands two currencies, CZK and
    2.52 +     *  SKK. Make 100 SKK == 75 CZK. This is method for the group of users that
    2.53 +     *  knows the exchange rate, and needs to use the API to create objects
    2.54 +     *  with the exchange rate. Anyone shall be ready to call this method without
    2.55 +     *  any other method being called previously. The API itself shall know
    2.56 +     *  nothing about any rates, before this method is called.
    2.57 +     */
    2.58 +    public static Convertor createTripleConvertor() {
    2.59 +        // Rates: 1USD = 15CZK
    2.60 +        // Rates: 1USD = 20SKK
    2.61 +        // Rates: 75CZK = 100SKK
    2.62 +    	Convertor usdCzk = new Convertor(new BigDecimal(15), USD, CZK);
    2.63 +    	Convertor usdSkk = new Convertor(new BigDecimal(20), USD, SKK);
    2.64 +    	Convertor skkCzk = new Convertor(new BigDecimal("0.75"), SKK, CZK);
    2.65 +        return new Convertor(new Convertor[]{usdCzk, usdSkk, skkCzk});
    2.66 +    }
    2.67 +
    2.68 +    /** Define convertor that understands three currencies. Use it.
    2.69 +     */
    2.70 +    public void testConvertorForUSDandCZKandSKK() throws Exception {
    2.71 +        Convertor c = createTripleConvertor();
    2.72 +
    2.73 +        // convert $5 to CZK using c:
    2.74 +        assertEquals("Result is 75 CZK", 75 ,c.convert(new BigDecimal(5), USD, CZK).getValue().intValue());
    2.75 +
    2.76 +        // convert $5 to SKK using c:
    2.77 +        assertEquals("Result is 100 SKK", 100, c.convert(new BigDecimal(5), USD, SKK).getValue().intValue());
    2.78 +
    2.79 +        // convert 200SKK to CZK using c:
    2.80 +        assertEquals("Result is 150 CZK", 150, c.convert(new BigDecimal(200), SKK, CZK).getValue().intValue());
    2.81 +
    2.82 +        // convert 200SKK to USK using c:
    2.83 +        assertEquals("Result is 10 USD", 10, c.convert(new BigDecimal(200), SKK, USD).getValue().intValue());
    2.84 +    }
    2.85 +
    2.86 +    /** Merge all currency rates of convertor 1 with convertor 2.
    2.87 +     * Implement this using your API, preferably this method just delegates
    2.88 +     * into some API method which does the actual work, without requiring
    2.89 +     * API clients to code anything complex.
    2.90 +     */
    2.91 +    public static Convertor merge(Convertor one, Convertor two) {
    2.92 +        return new Convertor(new Convertor[]{one, two});
    2.93 +    }
    2.94 +
    2.95 +    /** Join the convertors from previous task, Task1Test and show that it
    2.96 +     * can be used to do reasonable conversions.
    2.97 +     */
    2.98 +    public void testConvertorComposition() throws Exception {
    2.99 +        Convertor c = merge(
   2.100 +            Task1Test.createCZKtoUSD(),
   2.101 +            Task1Test.createSKKtoCZK()
   2.102 +        );
   2.103 +
   2.104 +        // convert $5 to CZK using c:
   2.105 +        assertEquals("Result is 85 CZK", 85, c.convert(new BigDecimal(5), USD, CZK).getValue().intValue());
   2.106 +
   2.107 +        // convert $8 to CZK using c:
   2.108 +        assertEquals("Result is 136 CZK", 136, c.convert(new BigDecimal(8), USD, CZK).getValue().intValue());
   2.109 +
   2.110 +        // convert 1003CZK to USD using c:
   2.111 +        assertEquals("Result is 59 USD", 59, c.convert(new BigDecimal(1003), CZK, USD).getValue().intValue());
   2.112 +
   2.113 +        // convert 16CZK using c:
   2.114 +        assertEquals("Result is 20 SKK", 20, c.convert(new BigDecimal(16), CZK, SKK).getValue().intValue());
   2.115 +
   2.116 +        // convert 500SKK to CZK using c:
   2.117 +        assertEquals("Result is 400 CZK", 400, c.convert(new BigDecimal(500), SKK, CZK).getValue().intValue());
   2.118 +
   2.119 +    }
   2.120 +}