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
     1 package org.apidesign.apifest08.currency;
     2 
     3 import org.apidesign.apifest08.currency.Computer.ComputerRequest;
     4 import org.apidesign.apifest08.currency.Computer.ComputerResponse;
     5 
     6 /**
     7  * Convertor.
     8  * 
     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.
    12  * 
    13  * @author ked
    14  */
    15 public final class Convertor<AmountType, IdentifierType> {
    16 
    17     Computer<AmountType> computer;
    18     CurrencyValue<AmountType, IdentifierType> firstCurrencyExchangeRate;
    19     CurrencyValue<AmountType, IdentifierType> secondCurrencyExchangeRate;
    20 
    21     Convertor(
    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;
    28     }
    29 
    30     /**
    31      * Convert an amount of the one currency to an amount of the another one currency
    32      * with respect to previously specified exchange rate.
    33      * 
    34      * @param currencyValue an amount of the one currency
    35      * @return an amount of the another one currency
    36      */
    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);
    44 
    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);
    54 
    55             return CurrencyValue.getCurrencyValue(
    56                     computerResponse.getResult(),
    57                     firstCurrencyExchangeRate.getIdentifier());
    58         } else {
    59             throw new IllegalArgumentException("Inappropriate currency to convert!");
    60         }
    61     }
    62 
    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>(
    68                 computer,
    69                 firstCurrencyExchangeRate,
    70                 secondCurrencyExchangeRate);
    71     }
    72     
    73     static final Computer<Double> DoubleComputer = new Computer<Double>() {
    74 
    75         public ComputerResponse<Double> compute(ComputerRequest<Double> request) {
    76             ComputerResponse<Double> response = new ComputerResponse<Double>();
    77             response.setResult(request.getInput() * request.getOutputCurrencyRatio() / request.getInputCurrencyRatio());
    78             return response;
    79         }
    80     };
    81 
    82     /**
    83      * Creates convertor for Double|String values with specified exchange rate
    84      * between two currencies.
    85      * 
    86      * @param firstCurrencyExchangeRate first currency
    87      * @param secondCurrencyExchangeRate second currency
    88      * @return convertor
    89      */
    90 
    91     public static Convertor<Double, String> getConvertorDoubleString(
    92             CurrencyValue<Double, String> firstCurrencyExchangeRate,
    93             CurrencyValue<Double, String> secondCurrencyExchangeRate) {
    94         return getConvertor(DoubleComputer, firstCurrencyExchangeRate, secondCurrencyExchangeRate);
    95     }
    96     
    97     static final Computer<Integer> IntegerComputer = new Computer<Integer>() {
    98 
    99         public ComputerResponse<Integer> compute(ComputerRequest<Integer> request) {
   100             ComputerResponse<Integer> response = new ComputerResponse<Integer>();
   101             response.setResult(request.getInput() * request.getOutputCurrencyRatio() / request.getInputCurrencyRatio());
   102             return response;
   103         }
   104     };
   105 
   106     /**
   107      * Creates convertor for Integer|String values with specified exchange rate
   108      * between two currencies.
   109      * 
   110      * @param firstCurrencyExchangeRate first currency
   111      * @param secondCurrencyExchangeRate second currency
   112      * @return convertor
   113      */
   114     public static Convertor<Integer, String> getConvertorIntegerString(
   115             CurrencyValue<Integer, String> firstCurrencyExchangeRate,
   116             CurrencyValue<Integer, String> secondCurrencyExchangeRate) {
   117         return getConvertor(IntegerComputer, firstCurrencyExchangeRate, secondCurrencyExchangeRate);
   118     }
   119 }