1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/task1/solution11/src/org/apidesign/apifest08/currency/Convertor.java Sun Sep 28 14:12:38 2008 +0200
1.3 @@ -0,0 +1,119 @@
1.4 +package org.apidesign.apifest08.currency;
1.5 +
1.6 +import org.apidesign.apifest08.currency.Computer.ComputerRequest;
1.7 +import org.apidesign.apifest08.currency.Computer.ComputerResponse;
1.8 +
1.9 +/**
1.10 + * Convertor.
1.11 + *
1.12 + * In Task 1's version provides conversion between currency values
1.13 + * with amount stored in integer or double, that are identified
1.14 + * with string value. Exchange rates are immutable.
1.15 + *
1.16 + * @author ked
1.17 + */
1.18 +public final class Convertor<AmountType, IdentifierType> {
1.19 +
1.20 + Computer<AmountType> computer;
1.21 + CurrencyValue<AmountType, IdentifierType> firstCurrencyExchangeRate;
1.22 + CurrencyValue<AmountType, IdentifierType> secondCurrencyExchangeRate;
1.23 +
1.24 + Convertor(
1.25 + Computer<AmountType> computer,
1.26 + CurrencyValue<AmountType, IdentifierType> firstCurrencyExchangeRate,
1.27 + CurrencyValue<AmountType, IdentifierType> secondCurrencyExchangeRate) {
1.28 + this.computer = computer;
1.29 + this.firstCurrencyExchangeRate = firstCurrencyExchangeRate;
1.30 + this.secondCurrencyExchangeRate = secondCurrencyExchangeRate;
1.31 + }
1.32 +
1.33 + /**
1.34 + * Convert an amount of the one currency to an amount of the another one currency
1.35 + * with respect to previously specified exchange rate.
1.36 + *
1.37 + * @param currencyValue an amount of the one currency
1.38 + * @return an amount of the another one currency
1.39 + */
1.40 + public CurrencyValue<AmountType, IdentifierType> convert(CurrencyValue<AmountType, IdentifierType> currencyValue) {
1.41 + if (firstCurrencyExchangeRate.getIdentifier().equals(currencyValue.getIdentifier())) {
1.42 + ComputerRequest<AmountType> computerRequest = new ComputerRequest<AmountType>();
1.43 + computerRequest.setInput(currencyValue.getAmount());
1.44 + computerRequest.setInputCurrencyRatio(firstCurrencyExchangeRate.getAmount());
1.45 + computerRequest.setOutputCurrencyRatio(secondCurrencyExchangeRate.getAmount());
1.46 + ComputerResponse<AmountType> computerResponse = computer.compute(computerRequest);
1.47 +
1.48 + return CurrencyValue.getCurrencyValue(
1.49 + computerResponse.getResult(),
1.50 + secondCurrencyExchangeRate.getIdentifier());
1.51 + } else if (secondCurrencyExchangeRate.getIdentifier().equals(currencyValue.getIdentifier())) {
1.52 + ComputerRequest<AmountType> computerRequest = new ComputerRequest<AmountType>();
1.53 + computerRequest.setInput(currencyValue.getAmount());
1.54 + computerRequest.setInputCurrencyRatio(secondCurrencyExchangeRate.getAmount());
1.55 + computerRequest.setOutputCurrencyRatio(firstCurrencyExchangeRate.getAmount());
1.56 + ComputerResponse<AmountType> computerResponse = computer.compute(computerRequest);
1.57 +
1.58 + return CurrencyValue.getCurrencyValue(
1.59 + computerResponse.getResult(),
1.60 + firstCurrencyExchangeRate.getIdentifier());
1.61 + } else {
1.62 + throw new IllegalArgumentException("Inappropriate currency to convert!");
1.63 + }
1.64 + }
1.65 +
1.66 + static <AmountType, IdentifierType> Convertor<AmountType, IdentifierType> getConvertor(
1.67 + Computer<AmountType> computer,
1.68 + CurrencyValue<AmountType, IdentifierType> firstCurrencyExchangeRate,
1.69 + CurrencyValue<AmountType, IdentifierType> secondCurrencyExchangeRate) {
1.70 + return new Convertor<AmountType, IdentifierType>(
1.71 + computer,
1.72 + firstCurrencyExchangeRate,
1.73 + secondCurrencyExchangeRate);
1.74 + }
1.75 +
1.76 + static final Computer<Double> DoubleComputer = new Computer<Double>() {
1.77 +
1.78 + public ComputerResponse<Double> compute(ComputerRequest<Double> request) {
1.79 + ComputerResponse<Double> response = new ComputerResponse<Double>();
1.80 + response.setResult(request.getInput() * request.getOutputCurrencyRatio() / request.getInputCurrencyRatio());
1.81 + return response;
1.82 + }
1.83 + };
1.84 +
1.85 + /**
1.86 + * Creates convertor for Double|String values with specified exchange rate
1.87 + * between two currencies.
1.88 + *
1.89 + * @param firstCurrencyExchangeRate first currency
1.90 + * @param secondCurrencyExchangeRate second currency
1.91 + * @return convertor
1.92 + */
1.93 +
1.94 + public static Convertor<Double, String> getConvertorDoubleString(
1.95 + CurrencyValue<Double, String> firstCurrencyExchangeRate,
1.96 + CurrencyValue<Double, String> secondCurrencyExchangeRate) {
1.97 + return getConvertor(DoubleComputer, firstCurrencyExchangeRate, secondCurrencyExchangeRate);
1.98 + }
1.99 +
1.100 + static final Computer<Integer> IntegerComputer = new Computer<Integer>() {
1.101 +
1.102 + public ComputerResponse<Integer> compute(ComputerRequest<Integer> request) {
1.103 + ComputerResponse<Integer> response = new ComputerResponse<Integer>();
1.104 + response.setResult(request.getInput() * request.getOutputCurrencyRatio() / request.getInputCurrencyRatio());
1.105 + return response;
1.106 + }
1.107 + };
1.108 +
1.109 + /**
1.110 + * Creates convertor for Integer|String values with specified exchange rate
1.111 + * between two currencies.
1.112 + *
1.113 + * @param firstCurrencyExchangeRate first currency
1.114 + * @param secondCurrencyExchangeRate second currency
1.115 + * @return convertor
1.116 + */
1.117 + public static Convertor<Integer, String> getConvertorIntegerString(
1.118 + CurrencyValue<Integer, String> firstCurrencyExchangeRate,
1.119 + CurrencyValue<Integer, String> secondCurrencyExchangeRate) {
1.120 + return getConvertor(IntegerComputer, firstCurrencyExchangeRate, secondCurrencyExchangeRate);
1.121 + }
1.122 +}