task1/solution11/src/org/apidesign/apifest08/currency/Convertor.java
author japod@localhost
Sun, 28 Sep 2008 14:12:38 +0200
changeset 6 97662396c0fd
child 19 45e6a2d1ffd1
permissions -rw-r--r--
Adding solutions received for task1
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@6
    25
        this.computer = computer;
japod@6
    26
        this.firstCurrencyExchangeRate = firstCurrencyExchangeRate;
japod@6
    27
        this.secondCurrencyExchangeRate = secondCurrencyExchangeRate;
japod@6
    28
    }
japod@6
    29
japod@6
    30
    /**
japod@6
    31
     * Convert an amount of the one currency to an amount of the another one currency
japod@6
    32
     * with respect to previously specified exchange rate.
japod@6
    33
     * 
japod@6
    34
     * @param currencyValue an amount of the one currency
japod@6
    35
     * @return an amount of the another one currency
japod@6
    36
     */
japod@6
    37
    public CurrencyValue<AmountType, IdentifierType> convert(CurrencyValue<AmountType, IdentifierType> currencyValue) {
japod@6
    38
        if (firstCurrencyExchangeRate.getIdentifier().equals(currencyValue.getIdentifier())) {
japod@6
    39
            ComputerRequest<AmountType> computerRequest = new ComputerRequest<AmountType>();
japod@6
    40
            computerRequest.setInput(currencyValue.getAmount());
japod@6
    41
            computerRequest.setInputCurrencyRatio(firstCurrencyExchangeRate.getAmount());
japod@6
    42
            computerRequest.setOutputCurrencyRatio(secondCurrencyExchangeRate.getAmount());
japod@6
    43
            ComputerResponse<AmountType> computerResponse = computer.compute(computerRequest);
japod@6
    44
japod@6
    45
            return CurrencyValue.getCurrencyValue(
japod@6
    46
                    computerResponse.getResult(),
japod@6
    47
                    secondCurrencyExchangeRate.getIdentifier());
japod@6
    48
        } else if (secondCurrencyExchangeRate.getIdentifier().equals(currencyValue.getIdentifier())) {
japod@6
    49
            ComputerRequest<AmountType> computerRequest = new ComputerRequest<AmountType>();
japod@6
    50
            computerRequest.setInput(currencyValue.getAmount());
japod@6
    51
            computerRequest.setInputCurrencyRatio(secondCurrencyExchangeRate.getAmount());
japod@6
    52
            computerRequest.setOutputCurrencyRatio(firstCurrencyExchangeRate.getAmount());
japod@6
    53
            ComputerResponse<AmountType> computerResponse = computer.compute(computerRequest);
japod@6
    54
japod@6
    55
            return CurrencyValue.getCurrencyValue(
japod@6
    56
                    computerResponse.getResult(),
japod@6
    57
                    firstCurrencyExchangeRate.getIdentifier());
japod@6
    58
        } else {
japod@6
    59
            throw new IllegalArgumentException("Inappropriate currency to convert!");
japod@6
    60
        }
japod@6
    61
    }
japod@6
    62
japod@6
    63
    static <AmountType, IdentifierType> Convertor<AmountType, IdentifierType> getConvertor(
japod@6
    64
            Computer<AmountType> computer,
japod@6
    65
            CurrencyValue<AmountType, IdentifierType> firstCurrencyExchangeRate,
japod@6
    66
            CurrencyValue<AmountType, IdentifierType> secondCurrencyExchangeRate) {
japod@6
    67
        return new Convertor<AmountType, IdentifierType>(
japod@6
    68
                computer,
japod@6
    69
                firstCurrencyExchangeRate,
japod@6
    70
                secondCurrencyExchangeRate);
japod@6
    71
    }
japod@6
    72
    
japod@6
    73
    static final Computer<Double> DoubleComputer = new Computer<Double>() {
japod@6
    74
japod@6
    75
        public ComputerResponse<Double> compute(ComputerRequest<Double> request) {
japod@6
    76
            ComputerResponse<Double> response = new ComputerResponse<Double>();
japod@6
    77
            response.setResult(request.getInput() * request.getOutputCurrencyRatio() / request.getInputCurrencyRatio());
japod@6
    78
            return response;
japod@6
    79
        }
japod@6
    80
    };
japod@6
    81
japod@6
    82
    /**
japod@6
    83
     * Creates convertor for Double|String values with specified exchange rate
japod@6
    84
     * between two currencies.
japod@6
    85
     * 
japod@6
    86
     * @param firstCurrencyExchangeRate first currency
japod@6
    87
     * @param secondCurrencyExchangeRate second currency
japod@6
    88
     * @return convertor
japod@6
    89
     */
japod@6
    90
japod@6
    91
    public static Convertor<Double, String> getConvertorDoubleString(
japod@6
    92
            CurrencyValue<Double, String> firstCurrencyExchangeRate,
japod@6
    93
            CurrencyValue<Double, String> secondCurrencyExchangeRate) {
japod@6
    94
        return getConvertor(DoubleComputer, firstCurrencyExchangeRate, secondCurrencyExchangeRate);
japod@6
    95
    }
japod@6
    96
    
japod@6
    97
    static final Computer<Integer> IntegerComputer = new Computer<Integer>() {
japod@6
    98
japod@6
    99
        public ComputerResponse<Integer> compute(ComputerRequest<Integer> request) {
japod@6
   100
            ComputerResponse<Integer> response = new ComputerResponse<Integer>();
japod@6
   101
            response.setResult(request.getInput() * request.getOutputCurrencyRatio() / request.getInputCurrencyRatio());
japod@6
   102
            return response;
japod@6
   103
        }
japod@6
   104
    };
japod@6
   105
japod@6
   106
    /**
japod@6
   107
     * Creates convertor for Integer|String values with specified exchange rate
japod@6
   108
     * between two currencies.
japod@6
   109
     * 
japod@6
   110
     * @param firstCurrencyExchangeRate first currency
japod@6
   111
     * @param secondCurrencyExchangeRate second currency
japod@6
   112
     * @return convertor
japod@6
   113
     */
japod@6
   114
    public static Convertor<Integer, String> getConvertorIntegerString(
japod@6
   115
            CurrencyValue<Integer, String> firstCurrencyExchangeRate,
japod@6
   116
            CurrencyValue<Integer, String> secondCurrencyExchangeRate) {
japod@6
   117
        return getConvertor(IntegerComputer, firstCurrencyExchangeRate, secondCurrencyExchangeRate);
japod@6
   118
    }
japod@6
   119
}