1 package org.apidesign.apifest08.currency;
3 import org.apidesign.apifest08.currency.Computer.ComputerRequest;
4 import org.apidesign.apifest08.currency.Computer.ComputerResponse;
9 * In Task 1's version provides conversion between currency values
10 * with amount stored in integer or double, that are identified
11 * with string value. Exchange rates are immutable.
15 public final class Convertor<AmountType, IdentifierType> {
17 Computer<AmountType> computer;
18 CurrencyValue<AmountType, IdentifierType> firstCurrencyExchangeRate;
19 CurrencyValue<AmountType, IdentifierType> secondCurrencyExchangeRate;
22 Computer<AmountType> computer,
23 CurrencyValue<AmountType, IdentifierType> firstCurrencyExchangeRate,
24 CurrencyValue<AmountType, IdentifierType> secondCurrencyExchangeRate) {
25 if (firstCurrencyExchangeRate.getIdentifier() == null ||
26 secondCurrencyExchangeRate.getIdentifier() == null ||
27 firstCurrencyExchangeRate.getIdentifier().equals(secondCurrencyExchangeRate.getIdentifier())) {
28 throw new IllegalArgumentException("Inappropriate exchange rates' identifiers!");
30 this.computer = computer;
31 this.firstCurrencyExchangeRate = firstCurrencyExchangeRate;
32 this.secondCurrencyExchangeRate = secondCurrencyExchangeRate;
36 * Convert an amount of the one currency to an amount of the another one currency
37 * with respect to previously specified exchange rate.
39 * @param targetCurrency an identifier of the requested currency
40 * @param currencyValue an amount of the another one currency
41 * @return an amount of the requested currency
43 public CurrencyValue<AmountType, IdentifierType> convert(
44 IdentifierType targetCurrency,
45 CurrencyValue<AmountType, IdentifierType> currencyValue) {
46 if (firstCurrencyExchangeRate.getIdentifier().equals(targetCurrency) &&
47 secondCurrencyExchangeRate.getIdentifier().equals(currencyValue.getIdentifier())) {
48 ComputerRequest<AmountType> computerRequest = new ComputerRequest<AmountType>();
49 computerRequest.setInput(currencyValue.getAmount());
50 computerRequest.setInputCurrencyRatio(secondCurrencyExchangeRate.getAmount());
51 computerRequest.setOutputCurrencyRatio(firstCurrencyExchangeRate.getAmount());
52 ComputerResponse<AmountType> computerResponse = computer.compute(computerRequest);
54 return CurrencyValue.getCurrencyValue(
55 computerResponse.getResult(),
56 firstCurrencyExchangeRate.getIdentifier());
57 } else if (secondCurrencyExchangeRate.getIdentifier().equals(targetCurrency) &&
58 firstCurrencyExchangeRate.getIdentifier().equals(currencyValue.getIdentifier())) {
59 ComputerRequest<AmountType> computerRequest = new ComputerRequest<AmountType>();
60 computerRequest.setInput(currencyValue.getAmount());
61 computerRequest.setInputCurrencyRatio(firstCurrencyExchangeRate.getAmount());
62 computerRequest.setOutputCurrencyRatio(secondCurrencyExchangeRate.getAmount());
63 ComputerResponse<AmountType> computerResponse = computer.compute(computerRequest);
65 return CurrencyValue.getCurrencyValue(
66 computerResponse.getResult(),
67 secondCurrencyExchangeRate.getIdentifier());
69 throw new IllegalArgumentException("Inappropriate currencies to convert!");
73 static <AmountType, IdentifierType> Convertor<AmountType, IdentifierType> getConvertor(
74 Computer<AmountType> computer,
75 CurrencyValue<AmountType, IdentifierType> firstCurrencyExchangeRate,
76 CurrencyValue<AmountType, IdentifierType> secondCurrencyExchangeRate) {
77 return new Convertor<AmountType, IdentifierType>(
79 firstCurrencyExchangeRate,
80 secondCurrencyExchangeRate);
83 static final Computer<Double> DoubleComputer = new Computer<Double>() {
85 public ComputerResponse<Double> compute(ComputerRequest<Double> request) {
86 ComputerResponse<Double> response = new ComputerResponse<Double>();
87 response.setResult(request.getInput() * request.getOutputCurrencyRatio() / request.getInputCurrencyRatio());
93 * Creates convertor for Double|String values with specified exchange rate
94 * between two currencies.
96 * @param firstCurrencyExchangeRate first currency
97 * @param secondCurrencyExchangeRate second currency
101 public static Convertor<Double, String> getConvertorDoubleString(
102 CurrencyValue<Double, String> firstCurrencyExchangeRate,
103 CurrencyValue<Double, String> secondCurrencyExchangeRate) {
104 return getConvertor(DoubleComputer, firstCurrencyExchangeRate, secondCurrencyExchangeRate);
107 static final Computer<Integer> IntegerComputer = new Computer<Integer>() {
109 public ComputerResponse<Integer> compute(ComputerRequest<Integer> request) {
110 ComputerResponse<Integer> response = new ComputerResponse<Integer>();
111 response.setResult(request.getInput() * request.getOutputCurrencyRatio() / request.getInputCurrencyRatio());
117 * Creates convertor for Integer|String values with specified exchange rate
118 * between two currencies.
120 * @param firstCurrencyExchangeRate first currency
121 * @param secondCurrencyExchangeRate second currency
124 public static Convertor<Integer, String> getConvertorIntegerString(
125 CurrencyValue<Integer, String> firstCurrencyExchangeRate,
126 CurrencyValue<Integer, String> secondCurrencyExchangeRate) {
127 return getConvertor(IntegerComputer, firstCurrencyExchangeRate, secondCurrencyExchangeRate);