task1/solution02/src/org/apidesign/apifest08/currency/ConvertorFactory.java
author japod@localhost
Sun, 28 Sep 2008 14:12:38 +0200
changeset 6 97662396c0fd
child 16 2864c6d744c0
permissions -rw-r--r--
Adding solutions received for task1
     1 package org.apidesign.apifest08.currency;
     2 
     3 import java.util.Currency;
     4 
     5 
     6 /**
     7  * Creates default {@link Convertor} implementations.
     8  * @author lukas
     9  *
    10  */
    11 public class ConvertorFactory {
    12 	private static final DefaultConvertorFactory DEFAULT_FACTORY = new DefaultConvertorFactory();
    13 	
    14 	private ConvertorFactory()
    15 	{
    16 		//nothing
    17 	}
    18 		
    19 	/**
    20 	 * Creates {@link Convertor} that converts from sourceCurrency to destinationCurrency with stored exchange rate.
    21 	 * @param sourceCurrency
    22 	 * @param destinationCurrency
    23 	 * @return
    24 	 * @throws UnsupportedConversionException when exchange rate between currencies is not known.
    25 	 */
    26 	/*
    27 	 * Only one of the createConveror methods is needed. The assignment is not explicit where the exchange rate should be set.
    28 	 */
    29 	public static final Convertor createConvertor(Currency sourceCurrency, Currency destinationCurrency) throws UnsupportedConversionException
    30 	{
    31 		return DEFAULT_FACTORY.getConvertor(sourceCurrency, destinationCurrency);
    32 	}
    33 	/**
    34 	 * Creates {@link Convertor} that converts from sourceEquivalent.currency to destinationEquivalent.currency. 
    35 	 * Exchange rate is set as equivalents. It means if you want to create USD to CZK convertor where USD1 = CZK17 
    36      * call createConvertor(new MoneyImpl(1, USD), new MoneyImpl(17, CZK)).
    37 	 * @param sourceEquivalent
    38 	 * @param destinationEquivalent
    39 	 * @return
    40 	 */
    41 	/*
    42 	 * Only one of the createConveror methods is needed. The assignment is not explicit where the exchange rate should be set.
    43 	 */
    44 	public static final Convertor createConvertor(Money sourceEquivalent, Money destinationEquivalent)
    45 	{
    46 		return new DefaultConvertor(sourceEquivalent.getAmount(), destinationEquivalent.getAmount(), sourceEquivalent.getCurrency(), destinationEquivalent.getCurrency());
    47 	}
    48 	
    49 
    50 }