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