task1/solution02/src/org/apidesign/apifest08/currency/ConvertorFactory.java
changeset 6 97662396c0fd
child 16 2864c6d744c0
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/task1/solution02/src/org/apidesign/apifest08/currency/ConvertorFactory.java	Sun Sep 28 14:12:38 2008 +0200
     1.3 @@ -0,0 +1,50 @@
     1.4 +package org.apidesign.apifest08.currency;
     1.5 +
     1.6 +import java.util.Currency;
     1.7 +
     1.8 +
     1.9 +/**
    1.10 + * Creates default {@link Convertor} implementations.
    1.11 + * @author lukas
    1.12 + *
    1.13 + */
    1.14 +public class ConvertorFactory {
    1.15 +	private static final DefaultConvertorFactory DEFAULT_FACTORY = new DefaultConvertorFactory();
    1.16 +	
    1.17 +	private ConvertorFactory()
    1.18 +	{
    1.19 +		//nothing
    1.20 +	}
    1.21 +		
    1.22 +	/**
    1.23 +	 * Creates {@link Convertor} that converts from sourceCurrency to destinationCurrency with stored exchange rate.
    1.24 +	 * @param sourceCurrency
    1.25 +	 * @param destinationCurrency
    1.26 +	 * @return
    1.27 +	 * @throws UnsupportedConversionException when exchange rate between currencies is not known.
    1.28 +	 */
    1.29 +	/*
    1.30 +	 * Only one of the createConveror methods is needed. The assignment is not explicit where the exchange rate should be set.
    1.31 +	 */
    1.32 +	public static final Convertor createConvertor(Currency sourceCurrency, Currency destinationCurrency) throws UnsupportedConversionException
    1.33 +	{
    1.34 +		return DEFAULT_FACTORY.getConvertor(sourceCurrency, destinationCurrency);
    1.35 +	}
    1.36 +	/**
    1.37 +	 * Creates {@link Convertor} that converts from sourceEquivalent.currency to destinationEquivalent.currency. 
    1.38 +	 * Exchange rate is set as equivalents. It means if you want to create USD to CZK convertor where USD1 = CZK17 
    1.39 +     * call createConvertor(new MoneyImpl(1, USD), new MoneyImpl(17, CZK)).
    1.40 +	 * @param sourceEquivalent
    1.41 +	 * @param destinationEquivalent
    1.42 +	 * @return
    1.43 +	 */
    1.44 +	/*
    1.45 +	 * Only one of the createConveror methods is needed. The assignment is not explicit where the exchange rate should be set.
    1.46 +	 */
    1.47 +	public static final Convertor createConvertor(Money sourceEquivalent, Money destinationEquivalent)
    1.48 +	{
    1.49 +		return new DefaultConvertor(sourceEquivalent.getAmount(), destinationEquivalent.getAmount(), sourceEquivalent.getCurrency(), destinationEquivalent.getCurrency());
    1.50 +	}
    1.51 +	
    1.52 +
    1.53 +}