task2/solution06/src/org/apidesign/apifest08/currency/Convertor.java
changeset 29 f6073056b9fe
parent 21 61e4c4c120fd
child 38 5838e0aaa81f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/task2/solution06/src/org/apidesign/apifest08/currency/Convertor.java	Wed Oct 01 10:43:05 2008 +0200
     1.3 @@ -0,0 +1,64 @@
     1.4 +package org.apidesign.apifest08.currency;
     1.5 +
     1.6 +import static org.apidesign.apifest08.currency.Assert.notNull;
     1.7 +
     1.8 +import java.math.BigDecimal;
     1.9 +import java.math.RoundingMode;
    1.10 +import java.util.Currency;
    1.11 +
    1.12 +public final class Convertor {
    1.13 +	
    1.14 +	private final Currency first;
    1.15 +	private final Currency second;
    1.16 +	private final BigDecimal rateValue; // a rate between the first currency and the second currency
    1.17 +	public static final BigDecimal one = new BigDecimal(1);	
    1.18 +	
    1.19 +	public Convertor(BigDecimal rateValue, Currency currencyFirst, Currency currencySecond) {
    1.20 +		notNull(currencyFirst, "currencyFirst");
    1.21 +		notNull(currencySecond, "currencySecond");		
    1.22 +		notNull(rateValue, "rateValue");
    1.23 +		
    1.24 +		this.rateValue = rateValue;
    1.25 +		this.first = currencyFirst;
    1.26 +		this.second = currencySecond;
    1.27 +	}
    1.28 +    	
    1.29 +	/**
    1.30 +	 * Converts an amount value between the two currencies of this converter.
    1.31 +	 *  
    1.32 +	 * @param amount an amount
    1.33 +	 * @param fromCurrency an amount currency
    1.34 +	 * @param toCurrency to a target currency
    1.35 +	 * @return a converted amount value
    1.36 +	 * 
    1.37 +	 * @throws ConversionException if the conversion fails
    1.38 +	 * @throws UnsupportedConversionException if the conversion between a given currencies is not supported.
    1.39 +	 */
    1.40 +	public Amount convert(BigDecimal amount, Currency fromCurrency, Currency toCurrency) throws ConversionException {
    1.41 +		notNull(amount, "amount");
    1.42 +		notNull(fromCurrency, "fromCurrency");
    1.43 +		notNull(toCurrency, "toCurrency");
    1.44 +		
    1.45 +		if((fromCurrency != first && fromCurrency != second) || (toCurrency != first && toCurrency != second)) {
    1.46 +			throw new UnsupportedConversionException(fromCurrency, toCurrency);
    1.47 +		}		
    1.48 +
    1.49 +		BigDecimal rateValue = getRateValue(fromCurrency, toCurrency);
    1.50 +		BigDecimal result = rateValue.multiply(amount);			
    1.51 +		return new Amount(result, toCurrency);	
    1.52 +	}
    1.53 +	
    1.54 +	private BigDecimal getRateValue(Currency fromCurrency, Currency toCurrency) {		
    1.55 +		
    1.56 +		BigDecimal retVal;
    1.57 +		
    1.58 +		if(first == fromCurrency) {
    1.59 +			retVal = rateValue;
    1.60 +		} else {	
    1.61 +			//reverse rate	
    1.62 +			retVal = one.divide(rateValue, 10 ,RoundingMode.HALF_UP);
    1.63 +		}
    1.64 +		
    1.65 +		return retVal;
    1.66 +	}
    1.67 +}