task4/solution02/src/org/apidesign/apifest08/currency/CompositeConvertor.java
changeset 61 58ec6da75f6f
parent 45 251d0ed461fb
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/task4/solution02/src/org/apidesign/apifest08/currency/CompositeConvertor.java	Sat Oct 11 23:38:46 2008 +0200
     1.3 @@ -0,0 +1,75 @@
     1.4 +package org.apidesign.apifest08.currency;
     1.5 +
     1.6 +import java.io.Serializable;
     1.7 +import java.util.Arrays;
     1.8 +import java.util.Currency;
     1.9 +
    1.10 +/**
    1.11 + * Convertor that is composed from other convertors. 
    1.12 + * @author lukas
    1.13 + *
    1.14 + */
    1.15 +class CompositeConvertor implements ExtendedConvertor, Serializable {
    1.16 +
    1.17 +	private static final long serialVersionUID = -2702026568249130055L;
    1.18 +	
    1.19 +	private final ExtendedConvertor[] convertors;
    1.20 +
    1.21 +
    1.22 +	public CompositeConvertor(ExtendedConvertor... convertors) {
    1.23 +		this.convertors = convertors.clone();
    1.24 +		for (ExtendedConvertor convertor: this.convertors)
    1.25 +		{
    1.26 +			if (convertor==null)
    1.27 +			{
    1.28 +				throw new NullPointerException("One of the convertors to be merged is null");
    1.29 +			}
    1.30 +		}
    1.31 +	}
    1.32 +
    1.33 +	/**
    1.34 +	 * Returns true if the composite contains convertor that supports given conversion.
    1.35 +	 */
    1.36 +	public boolean isConversionSupported(Currency from, Currency to) {
    1.37 +		return findConvertorFor(from, to)!=null;
    1.38 +	}
    1.39 +
    1.40 +
    1.41 +	/**
    1.42 +	 * If the composite contains convertor that supports given conversion, delegates to its convert method.
    1.43 +	 * Throws {@link IllegalArgumentException} convertor supporting given conversion is not found. 
    1.44 +	 */
    1.45 +	public Money convert(Money amount, Currency destinationCurrency) throws IllegalArgumentException {
    1.46 +		ExtendedConvertor convertor = findConvertorFor(amount.getCurrency(), destinationCurrency);
    1.47 +		if (convertor!=null)
    1.48 +		{
    1.49 +			return convertor.convert(amount, destinationCurrency);
    1.50 +		}
    1.51 +		throw new IllegalArgumentException("Conversion not supported. Can not convert to "+destinationCurrency+".");
    1.52 +	}
    1.53 +	
    1.54 +	/**
    1.55 +	 * Finds convertor for given currencies. If not found, returns null. 
    1.56 +	 * @param from
    1.57 +	 * @param to
    1.58 +	 * @return
    1.59 +	 */
    1.60 +	ExtendedConvertor findConvertorFor(Currency from, Currency to) {
    1.61 +		//does linear search, in the future can cache the results.
    1.62 +		for (ExtendedConvertor convertor:convertors)
    1.63 +		{
    1.64 +			if (convertor.isConversionSupported(from, to))
    1.65 +			{
    1.66 +				return convertor;
    1.67 +			}
    1.68 +		}
    1.69 +		return null;
    1.70 +	}
    1.71 +	
    1.72 +
    1.73 +	@Override
    1.74 +	public String toString() {
    1.75 +		return getClass().getName()+" converts "+Arrays.toString(convertors);
    1.76 +	}
    1.77 +
    1.78 +}