task2/solution06/src/org/apidesign/apifest08/currency/Convertor.java
author japod@localhost
Tue, 07 Oct 2008 00:42:57 +0200
changeset 38 5838e0aaa81f
parent 29 f6073056b9fe
permissions -rw-r--r--
adding solution06 for task2
     1 package org.apidesign.apifest08.currency;
     2 
     3 import static org.apidesign.apifest08.currency.Assert.notNull;
     4 
     5 import java.math.BigDecimal;
     6 import java.math.RoundingMode;
     7 import java.util.ArrayList;
     8 import java.util.Currency;
     9 import java.util.List;
    10 
    11 public final class Convertor {
    12 	
    13 	private List<ConvertorDelegate> convertorDelegates = new ArrayList<ConvertorDelegate>();
    14 	
    15 	
    16 	/**
    17 	 * Create new instance of the converter for the given currencies and its rate.
    18 	 * 
    19 	 * @param rateValue the rate between the first and the second currency
    20 	 * @param currencyFirst the first currency
    21 	 * @param currencySecond the second currency
    22 	 */
    23 	public Convertor(BigDecimal rateValue, Currency currencyFirst, Currency currencySecond) {
    24 		notNull(currencyFirst, "currencyFirst");
    25 		notNull(currencySecond, "currencySecond");		
    26 		notNull(rateValue, "rateValue");	
    27 		convertorDelegates.add(new ConvertorDelegate(rateValue, currencyFirst, currencySecond));
    28 	}
    29 	
    30 	/**
    31 	 * Create new instance of the convertor from the given convertors. 
    32 	 * @param convertors the convertors
    33 	 */
    34 	public Convertor(Convertor... convertors) {
    35 		notNull(convertors, "convertors");
    36 		if(convertors.length == 0) {
    37 			throw new IllegalArgumentException("There must be at least one converter.");
    38 		}
    39 		
    40 		for(Convertor convertor: convertors) {
    41 			if(convertor != null) {
    42 				convertorDelegates.addAll(convertor.convertorDelegates);
    43 			}
    44 		}
    45 	}
    46     	
    47 	/**
    48 	 * Converts an amount value between the two currencies of this converter.
    49 	 *  
    50 	 * @param amount an amount
    51 	 * @param fromCurrency an amount currency
    52 	 * @param toCurrency to a target currency
    53 	 * @return a converted amount value
    54 	 * 
    55 	 * @throws ConversionException if the conversion fails
    56 	 * @throws UnsupportedConversionException if the conversion between a given currencies is not supported.
    57 	 */
    58 	public Amount convert(BigDecimal amount, Currency fromCurrency, Currency toCurrency) throws ConversionException {
    59 		notNull(amount, "amount");
    60 		notNull(fromCurrency, "fromCurrency");
    61 		notNull(toCurrency, "toCurrency");
    62 		ConvertorDelegate appropriateDelegate = null;
    63 		
    64 		//try find an appropriate delegate for conversion
    65 		for(ConvertorDelegate delegate : convertorDelegates) {
    66 			if(delegate.isConversionSupported(fromCurrency, toCurrency)) {
    67 				appropriateDelegate = delegate;
    68 				break;
    69 			}
    70 		}
    71 		
    72 		if(appropriateDelegate == null) {
    73 			throw new UnsupportedConversionException(fromCurrency, toCurrency);
    74 		}
    75 		
    76 		return appropriateDelegate.convert(amount, fromCurrency, toCurrency);	
    77 	}
    78 	
    79 	/**
    80 	 * Internal delegate implements a logic for conversion between two currencies
    81 	 * and vice versa.
    82 	 * @see #isConversionSupported(Currency, Currency)
    83 	 */
    84 	private static class ConvertorDelegate {
    85 		private final Currency first;
    86 		private final Currency second;
    87 		private final BigDecimal rateValue; // a rate between the first currency and the second currency
    88 		public static final BigDecimal one = new BigDecimal(1);
    89 		
    90 		private ConvertorDelegate(BigDecimal rateValue, Currency currencyFirst, Currency currencySecond) {
    91 			this.rateValue = rateValue;
    92 			this.first = currencyFirst;
    93 			this.second = currencySecond;
    94 		}
    95 		
    96 		private Amount convert(BigDecimal amount, Currency fromCurrency, Currency toCurrency) throws ConversionException {
    97 			BigDecimal rateValue = getRateValue(fromCurrency, toCurrency);
    98 			BigDecimal result = rateValue.multiply(amount);			
    99 			return new Amount(result, toCurrency);	
   100 		}
   101 		
   102 		private BigDecimal getRateValue(Currency fromCurrency, Currency toCurrency) {		
   103 			
   104 			BigDecimal retVal;
   105 			
   106 			if(first == fromCurrency) {
   107 				retVal = rateValue;
   108 			} else {	
   109 				//reverse rate	
   110 				retVal = one.divide(rateValue, 10 ,RoundingMode.HALF_UP);
   111 			}
   112 			
   113 			return retVal;
   114 		}
   115 		
   116 		/**
   117 		 * @return <code>true</code> if the delegate is able to convert from the given currency
   118 		 * to the given currency and vice versa otherwise <code>false</code>.
   119 		 */
   120 		private boolean isConversionSupported(Currency fromCurrency, Currency toCurrency) {
   121 			return ((fromCurrency == first || fromCurrency == second) && (toCurrency == first || toCurrency == second));
   122 		}
   123 	}
   124 }