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