japod@6
|
1 |
package org.apidesign.apifest08.currency;
|
japod@6
|
2 |
|
japod@6
|
3 |
import org.apidesign.apifest08.currency.Computer.ComputerRequest;
|
japod@6
|
4 |
import org.apidesign.apifest08.currency.Computer.ComputerResponse;
|
japod@6
|
5 |
|
japod@6
|
6 |
/**
|
japod@6
|
7 |
* Convertor.
|
japod@6
|
8 |
*
|
japod@6
|
9 |
* In Task 1's version provides conversion between currency values
|
japod@6
|
10 |
* with amount stored in integer or double, that are identified
|
japod@6
|
11 |
* with string value. Exchange rates are immutable.
|
japod@6
|
12 |
*
|
japod@6
|
13 |
* @author ked
|
japod@6
|
14 |
*/
|
japod@6
|
15 |
public final class Convertor<AmountType, IdentifierType> {
|
japod@6
|
16 |
|
japod@6
|
17 |
Computer<AmountType> computer;
|
japod@6
|
18 |
CurrencyValue<AmountType, IdentifierType> firstCurrencyExchangeRate;
|
japod@6
|
19 |
CurrencyValue<AmountType, IdentifierType> secondCurrencyExchangeRate;
|
japod@6
|
20 |
|
japod@6
|
21 |
Convertor(
|
japod@6
|
22 |
Computer<AmountType> computer,
|
japod@6
|
23 |
CurrencyValue<AmountType, IdentifierType> firstCurrencyExchangeRate,
|
japod@6
|
24 |
CurrencyValue<AmountType, IdentifierType> secondCurrencyExchangeRate) {
|
japod@19
|
25 |
if (firstCurrencyExchangeRate.getIdentifier() == null ||
|
japod@19
|
26 |
secondCurrencyExchangeRate.getIdentifier() == null ||
|
japod@19
|
27 |
firstCurrencyExchangeRate.getIdentifier().equals(secondCurrencyExchangeRate.getIdentifier())) {
|
japod@19
|
28 |
throw new IllegalArgumentException("Inappropriate exchange rates' identifiers!");
|
japod@19
|
29 |
}
|
japod@6
|
30 |
this.computer = computer;
|
japod@6
|
31 |
this.firstCurrencyExchangeRate = firstCurrencyExchangeRate;
|
japod@6
|
32 |
this.secondCurrencyExchangeRate = secondCurrencyExchangeRate;
|
japod@6
|
33 |
}
|
japod@6
|
34 |
|
japod@6
|
35 |
/**
|
japod@6
|
36 |
* Convert an amount of the one currency to an amount of the another one currency
|
japod@6
|
37 |
* with respect to previously specified exchange rate.
|
japod@6
|
38 |
*
|
japod@19
|
39 |
* @param targetCurrency an identifier of the requested currency
|
japod@19
|
40 |
* @param currencyValue an amount of the another one currency
|
japod@19
|
41 |
* @return an amount of the requested currency
|
japod@6
|
42 |
*/
|
japod@19
|
43 |
public CurrencyValue<AmountType, IdentifierType> convert(
|
japod@19
|
44 |
IdentifierType targetCurrency,
|
japod@19
|
45 |
CurrencyValue<AmountType, IdentifierType> currencyValue) {
|
japod@19
|
46 |
if (firstCurrencyExchangeRate.getIdentifier().equals(targetCurrency) &&
|
japod@19
|
47 |
secondCurrencyExchangeRate.getIdentifier().equals(currencyValue.getIdentifier())) {
|
japod@19
|
48 |
ComputerRequest<AmountType> computerRequest = new ComputerRequest<AmountType>();
|
japod@19
|
49 |
computerRequest.setInput(currencyValue.getAmount());
|
japod@19
|
50 |
computerRequest.setInputCurrencyRatio(secondCurrencyExchangeRate.getAmount());
|
japod@19
|
51 |
computerRequest.setOutputCurrencyRatio(firstCurrencyExchangeRate.getAmount());
|
japod@19
|
52 |
ComputerResponse<AmountType> computerResponse = computer.compute(computerRequest);
|
japod@19
|
53 |
|
japod@19
|
54 |
return CurrencyValue.getCurrencyValue(
|
japod@19
|
55 |
computerResponse.getResult(),
|
japod@19
|
56 |
firstCurrencyExchangeRate.getIdentifier());
|
japod@19
|
57 |
} else if (secondCurrencyExchangeRate.getIdentifier().equals(targetCurrency) &&
|
japod@19
|
58 |
firstCurrencyExchangeRate.getIdentifier().equals(currencyValue.getIdentifier())) {
|
japod@6
|
59 |
ComputerRequest<AmountType> computerRequest = new ComputerRequest<AmountType>();
|
japod@6
|
60 |
computerRequest.setInput(currencyValue.getAmount());
|
japod@6
|
61 |
computerRequest.setInputCurrencyRatio(firstCurrencyExchangeRate.getAmount());
|
japod@6
|
62 |
computerRequest.setOutputCurrencyRatio(secondCurrencyExchangeRate.getAmount());
|
japod@6
|
63 |
ComputerResponse<AmountType> computerResponse = computer.compute(computerRequest);
|
japod@6
|
64 |
|
japod@6
|
65 |
return CurrencyValue.getCurrencyValue(
|
japod@6
|
66 |
computerResponse.getResult(),
|
japod@6
|
67 |
secondCurrencyExchangeRate.getIdentifier());
|
japod@6
|
68 |
} else {
|
japod@19
|
69 |
throw new IllegalArgumentException("Inappropriate currencies to convert!");
|
japod@6
|
70 |
}
|
japod@6
|
71 |
}
|
japod@6
|
72 |
|
japod@6
|
73 |
static <AmountType, IdentifierType> Convertor<AmountType, IdentifierType> getConvertor(
|
japod@6
|
74 |
Computer<AmountType> computer,
|
japod@6
|
75 |
CurrencyValue<AmountType, IdentifierType> firstCurrencyExchangeRate,
|
japod@6
|
76 |
CurrencyValue<AmountType, IdentifierType> secondCurrencyExchangeRate) {
|
japod@6
|
77 |
return new Convertor<AmountType, IdentifierType>(
|
japod@6
|
78 |
computer,
|
japod@6
|
79 |
firstCurrencyExchangeRate,
|
japod@6
|
80 |
secondCurrencyExchangeRate);
|
japod@6
|
81 |
}
|
japod@6
|
82 |
|
japod@6
|
83 |
static final Computer<Double> DoubleComputer = new Computer<Double>() {
|
japod@6
|
84 |
|
japod@6
|
85 |
public ComputerResponse<Double> compute(ComputerRequest<Double> request) {
|
japod@6
|
86 |
ComputerResponse<Double> response = new ComputerResponse<Double>();
|
japod@6
|
87 |
response.setResult(request.getInput() * request.getOutputCurrencyRatio() / request.getInputCurrencyRatio());
|
japod@6
|
88 |
return response;
|
japod@6
|
89 |
}
|
japod@6
|
90 |
};
|
japod@6
|
91 |
|
japod@6
|
92 |
/**
|
japod@6
|
93 |
* Creates convertor for Double|String values with specified exchange rate
|
japod@6
|
94 |
* between two currencies.
|
japod@6
|
95 |
*
|
japod@6
|
96 |
* @param firstCurrencyExchangeRate first currency
|
japod@6
|
97 |
* @param secondCurrencyExchangeRate second currency
|
japod@6
|
98 |
* @return convertor
|
japod@6
|
99 |
*/
|
japod@6
|
100 |
|
japod@6
|
101 |
public static Convertor<Double, String> getConvertorDoubleString(
|
japod@6
|
102 |
CurrencyValue<Double, String> firstCurrencyExchangeRate,
|
japod@6
|
103 |
CurrencyValue<Double, String> secondCurrencyExchangeRate) {
|
japod@6
|
104 |
return getConvertor(DoubleComputer, firstCurrencyExchangeRate, secondCurrencyExchangeRate);
|
japod@6
|
105 |
}
|
japod@6
|
106 |
|
japod@6
|
107 |
static final Computer<Integer> IntegerComputer = new Computer<Integer>() {
|
japod@6
|
108 |
|
japod@6
|
109 |
public ComputerResponse<Integer> compute(ComputerRequest<Integer> request) {
|
japod@6
|
110 |
ComputerResponse<Integer> response = new ComputerResponse<Integer>();
|
japod@6
|
111 |
response.setResult(request.getInput() * request.getOutputCurrencyRatio() / request.getInputCurrencyRatio());
|
japod@6
|
112 |
return response;
|
japod@6
|
113 |
}
|
japod@6
|
114 |
};
|
japod@6
|
115 |
|
japod@6
|
116 |
/**
|
japod@6
|
117 |
* Creates convertor for Integer|String values with specified exchange rate
|
japod@6
|
118 |
* between two currencies.
|
japod@6
|
119 |
*
|
japod@6
|
120 |
* @param firstCurrencyExchangeRate first currency
|
japod@6
|
121 |
* @param secondCurrencyExchangeRate second currency
|
japod@6
|
122 |
* @return convertor
|
japod@6
|
123 |
*/
|
japod@6
|
124 |
public static Convertor<Integer, String> getConvertorIntegerString(
|
japod@6
|
125 |
CurrencyValue<Integer, String> firstCurrencyExchangeRate,
|
japod@6
|
126 |
CurrencyValue<Integer, String> secondCurrencyExchangeRate) {
|
japod@6
|
127 |
return getConvertor(IntegerComputer, firstCurrencyExchangeRate, secondCurrencyExchangeRate);
|
japod@6
|
128 |
}
|
japod@6
|
129 |
}
|