task4/solution06/src/org/apidesign/apifest08/currency/Convertor.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 11 Oct 2008 23:38:46 +0200
changeset 61 58ec6da75f6f
parent 52 task3/solution06/src/org/apidesign/apifest08/currency/Convertor.java@c38391fb9b38
child 64 51f7b894eba6
permissions -rw-r--r--
Copying structure for task4
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@52
    11
/**
japod@52
    12
 * Currency covertor.
japod@52
    13
 */
japod@21
    14
public final class Convertor {
japod@6
    15
	
japod@38
    16
	private List<ConvertorDelegate> convertorDelegates = new ArrayList<ConvertorDelegate>();
japod@21
    17
	
japod@38
    18
	
japod@38
    19
	/**
japod@38
    20
	 * Create new instance of the converter for the given currencies and its rate.
japod@38
    21
	 * 
japod@38
    22
	 * @param rateValue the rate between the first and the second currency
japod@38
    23
	 * @param currencyFirst the first currency
japod@38
    24
	 * @param currencySecond the second currency
japod@38
    25
	 */
japod@21
    26
	public Convertor(BigDecimal rateValue, Currency currencyFirst, Currency currencySecond) {
japod@21
    27
		notNull(currencyFirst, "currencyFirst");
japod@21
    28
		notNull(currencySecond, "currencySecond");		
japod@38
    29
		notNull(rateValue, "rateValue");	
japod@52
    30
		convertorDelegates.add(new ConvertorDelegate(new StaticRateProvider(rateValue), currencyFirst, currencySecond));
japod@52
    31
	}
japod@52
    32
	
japod@52
    33
	/**
japod@52
    34
	 * Create new instance of the converter for the given currencies and its rate. 
japod@52
    35
	 * A rate value is provided by {@link RateProvider}.
japod@52
    36
	 * 
japod@52
    37
	 * @param rateProvider the rate provider
japod@52
    38
	 * @param currencyFirst the first currency
japod@52
    39
	 * @param currencySecond the second currency
japod@52
    40
	 */
japod@52
    41
	public Convertor(RateProvider rateProvider, Currency currencyFirst, Currency currencySecond) {
japod@52
    42
		notNull(currencyFirst, "currencyFirst");
japod@52
    43
		notNull(currencySecond, "currencySecond");		
japod@52
    44
		notNull(rateProvider, "rateProvider");
japod@52
    45
		convertorDelegates.add(new ConvertorDelegate(rateProvider, currencyFirst, currencySecond));
japod@38
    46
	}
japod@38
    47
	
japod@38
    48
	/**
japod@38
    49
	 * Create new instance of the convertor from the given convertors. 
japod@38
    50
	 * @param convertors the convertors
japod@38
    51
	 */
japod@38
    52
	public Convertor(Convertor... convertors) {
japod@38
    53
		notNull(convertors, "convertors");
japod@38
    54
		if(convertors.length == 0) {
japod@38
    55
			throw new IllegalArgumentException("There must be at least one converter.");
japod@38
    56
		}
japod@21
    57
		
japod@38
    58
		for(Convertor convertor: convertors) {
japod@38
    59
			if(convertor != null) {
japod@38
    60
				convertorDelegates.addAll(convertor.convertorDelegates);
japod@38
    61
			}
japod@38
    62
		}
japod@21
    63
	}
japod@21
    64
    	
japod@6
    65
	/**
japod@21
    66
	 * Converts an amount value between the two currencies of this converter.
japod@6
    67
	 *  
japod@6
    68
	 * @param amount an amount
japod@6
    69
	 * @param fromCurrency an amount currency
japod@6
    70
	 * @param toCurrency to a target currency
japod@6
    71
	 * @return a converted amount value
japod@6
    72
	 * 
japod@6
    73
	 * @throws ConversionException if the conversion fails
japod@6
    74
	 * @throws UnsupportedConversionException if the conversion between a given currencies is not supported.
japod@6
    75
	 */
japod@21
    76
	public Amount convert(BigDecimal amount, Currency fromCurrency, Currency toCurrency) throws ConversionException {
japod@21
    77
		notNull(amount, "amount");
japod@21
    78
		notNull(fromCurrency, "fromCurrency");
japod@21
    79
		notNull(toCurrency, "toCurrency");
japod@38
    80
		ConvertorDelegate appropriateDelegate = null;
japod@21
    81
		
japod@38
    82
		//try find an appropriate delegate for conversion
japod@38
    83
		for(ConvertorDelegate delegate : convertorDelegates) {
japod@38
    84
			if(delegate.isConversionSupported(fromCurrency, toCurrency)) {
japod@38
    85
				appropriateDelegate = delegate;
japod@38
    86
				break;
japod@38
    87
			}
japod@38
    88
		}
japod@38
    89
		
japod@38
    90
		if(appropriateDelegate == null) {
japod@21
    91
			throw new UnsupportedConversionException(fromCurrency, toCurrency);
japod@38
    92
		}
japod@38
    93
		
japod@38
    94
		return appropriateDelegate.convert(amount, fromCurrency, toCurrency);	
japod@21
    95
	}
japod@21
    96
	
japod@38
    97
	/**
japod@38
    98
	 * Internal delegate implements a logic for conversion between two currencies
japod@38
    99
	 * and vice versa.
japod@38
   100
	 * @see #isConversionSupported(Currency, Currency)
japod@38
   101
	 */
japod@38
   102
	private static class ConvertorDelegate {
japod@38
   103
		private final Currency first;
japod@38
   104
		private final Currency second;
japod@52
   105
		private final RateProvider rateProvider;
japod@38
   106
		public static final BigDecimal one = new BigDecimal(1);
japod@21
   107
		
japod@52
   108
		
japod@52
   109
		private ConvertorDelegate(RateProvider rateProvider, Currency currencyFirst, Currency currencySecond) {
japod@52
   110
			this.rateProvider = rateProvider;
japod@38
   111
			this.first = currencyFirst;
japod@38
   112
			this.second = currencySecond;
japod@21
   113
		}
japod@21
   114
		
japod@38
   115
		private Amount convert(BigDecimal amount, Currency fromCurrency, Currency toCurrency) throws ConversionException {
japod@38
   116
			BigDecimal rateValue = getRateValue(fromCurrency, toCurrency);
japod@38
   117
			BigDecimal result = rateValue.multiply(amount);			
japod@38
   118
			return new Amount(result, toCurrency);	
japod@38
   119
		}
japod@38
   120
		
japod@38
   121
		private BigDecimal getRateValue(Currency fromCurrency, Currency toCurrency) {		
japod@38
   122
			
japod@38
   123
			BigDecimal retVal;
japod@38
   124
			
japod@38
   125
			if(first == fromCurrency) {
japod@52
   126
				BigDecimal rateValue = rateProvider.getRate();
japod@52
   127
				if(rateValue == null) {
japod@52
   128
					throw new NullPointerException("Rate cannot be null!");
japod@52
   129
				}
japod@38
   130
				retVal = rateValue;
japod@38
   131
			} else {	
japod@52
   132
				BigDecimal rateValue = rateProvider.getRate();
japod@52
   133
				if(rateValue == null) {
japod@52
   134
					throw new NullPointerException("Rate cannot be null!");
japod@52
   135
				}
japod@38
   136
				//reverse rate	
japod@38
   137
				retVal = one.divide(rateValue, 10 ,RoundingMode.HALF_UP);
japod@38
   138
			}
japod@38
   139
			
japod@38
   140
			return retVal;
japod@38
   141
		}
japod@38
   142
		
japod@38
   143
		/**
japod@38
   144
		 * @return <code>true</code> if the delegate is able to convert from the given currency
japod@38
   145
		 * to the given currency and vice versa otherwise <code>false</code>.
japod@38
   146
		 */
japod@38
   147
		private boolean isConversionSupported(Currency fromCurrency, Currency toCurrency) {
japod@38
   148
			return ((fromCurrency == first || fromCurrency == second) && (toCurrency == first || toCurrency == second));
japod@38
   149
		}
japod@21
   150
	}
japod@52
   151
	
japod@52
   152
	/**
japod@52
   153
	 * A rate provider. This class represents a way how could be "static" convertor
japod@52
   154
	 * extended in order converts according to current rate.
japod@52
   155
	 */
japod@52
   156
	public static abstract class RateProvider { 
japod@52
   157
		
japod@52
   158
		/**
japod@52
   159
		 * @return a rate between the from currency and the to currency associated with
japod@52
   160
		 * a given convertor.
japod@52
   161
		 */
japod@52
   162
		public abstract BigDecimal getRate();
japod@52
   163
	}
japod@52
   164
	
japod@52
   165
	private static class StaticRateProvider extends RateProvider{
japod@52
   166
		private final BigDecimal rateValue;
japod@52
   167
		
japod@52
   168
		private StaticRateProvider(BigDecimal rateValue){
japod@52
   169
			this.rateValue = rateValue;
japod@52
   170
		}
japod@52
   171
		
japod@52
   172
		public BigDecimal getRate() {
japod@52
   173
			return this.rateValue;
japod@52
   174
		}
japod@52
   175
	}
japod@6
   176
}