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