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 this.computer = computer;
26 this.firstCurrencyExchangeRate = firstCurrencyExchangeRate;
27 this.secondCurrencyExchangeRate = secondCurrencyExchangeRate;
31 * Convert an amount of the one currency to an amount of the another one currency
32 * with respect to previously specified exchange rate.
34 * @param currencyValue an amount of the one currency
35 * @return an amount of the another one currency
37 public CurrencyValue<AmountType, IdentifierType> convert(CurrencyValue<AmountType, IdentifierType> currencyValue) {
38 if (firstCurrencyExchangeRate.getIdentifier().equals(currencyValue.getIdentifier())) {
39 ComputerRequest<AmountType> computerRequest = new ComputerRequest<AmountType>();
40 computerRequest.setInput(currencyValue.getAmount());
41 computerRequest.setInputCurrencyRatio(firstCurrencyExchangeRate.getAmount());
42 computerRequest.setOutputCurrencyRatio(secondCurrencyExchangeRate.getAmount());
43 ComputerResponse<AmountType> computerResponse = computer.compute(computerRequest);
45 return CurrencyValue.getCurrencyValue(
46 computerResponse.getResult(),
47 secondCurrencyExchangeRate.getIdentifier());
48 } else if (secondCurrencyExchangeRate.getIdentifier().equals(currencyValue.getIdentifier())) {
49 ComputerRequest<AmountType> computerRequest = new ComputerRequest<AmountType>();
50 computerRequest.setInput(currencyValue.getAmount());
51 computerRequest.setInputCurrencyRatio(secondCurrencyExchangeRate.getAmount());
52 computerRequest.setOutputCurrencyRatio(firstCurrencyExchangeRate.getAmount());
53 ComputerResponse<AmountType> computerResponse = computer.compute(computerRequest);
55 return CurrencyValue.getCurrencyValue(
56 computerResponse.getResult(),
57 firstCurrencyExchangeRate.getIdentifier());
59 throw new IllegalArgumentException("Inappropriate currency to convert!");
63 static <AmountType, IdentifierType> Convertor<AmountType, IdentifierType> getConvertor(
64 Computer<AmountType> computer,
65 CurrencyValue<AmountType, IdentifierType> firstCurrencyExchangeRate,
66 CurrencyValue<AmountType, IdentifierType> secondCurrencyExchangeRate) {
67 return new Convertor<AmountType, IdentifierType>(
69 firstCurrencyExchangeRate,
70 secondCurrencyExchangeRate);
73 static final Computer<Double> DoubleComputer = new Computer<Double>() {
75 public ComputerResponse<Double> compute(ComputerRequest<Double> request) {
76 ComputerResponse<Double> response = new ComputerResponse<Double>();
77 response.setResult(request.getInput() * request.getOutputCurrencyRatio() / request.getInputCurrencyRatio());
83 * Creates convertor for Double|String values with specified exchange rate
84 * between two currencies.
86 * @param firstCurrencyExchangeRate first currency
87 * @param secondCurrencyExchangeRate second currency
91 public static Convertor<Double, String> getConvertorDoubleString(
92 CurrencyValue<Double, String> firstCurrencyExchangeRate,
93 CurrencyValue<Double, String> secondCurrencyExchangeRate) {
94 return getConvertor(DoubleComputer, firstCurrencyExchangeRate, secondCurrencyExchangeRate);
97 static final Computer<Integer> IntegerComputer = new Computer<Integer>() {
99 public ComputerResponse<Integer> compute(ComputerRequest<Integer> request) {
100 ComputerResponse<Integer> response = new ComputerResponse<Integer>();
101 response.setResult(request.getInput() * request.getOutputCurrencyRatio() / request.getInputCurrencyRatio());
107 * Creates convertor for Integer|String values with specified exchange rate
108 * between two currencies.
110 * @param firstCurrencyExchangeRate first currency
111 * @param secondCurrencyExchangeRate second currency
114 public static Convertor<Integer, String> getConvertorIntegerString(
115 CurrencyValue<Integer, String> firstCurrencyExchangeRate,
116 CurrencyValue<Integer, String> secondCurrencyExchangeRate) {
117 return getConvertor(IntegerComputer, firstCurrencyExchangeRate, secondCurrencyExchangeRate);