task2/solution01/src/org/apidesign/apifest08/currency/CurrencyConvertorImpl.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 01 Oct 2008 10:43:05 +0200
changeset 29 f6073056b9fe
parent 6 task1/solution01/src/org/apidesign/apifest08/currency/CurrencyConvertorImpl.java@97662396c0fd
permissions -rw-r--r--
Getting ready for task2: copying all solutions to new locations
     1 package org.apidesign.apifest08.currency;
     2 
     3 import java.math.BigDecimal;
     4 import java.math.RoundingMode;
     5 import java.util.Currency;
     6 
     7 /**
     8  * Intern implementation of Bidirectional Convertor - this class is only for internal purposes only.
     9  * This implementation uses BigDecimals to count result value
    10  * Scale is set by ISO 4217 as default
    11  *
    12  * @author Ladislav Vitasek
    13  */
    14 class CurrencyConvertorImpl extends AbstractCurrencyConvertor {
    15 
    16     protected RoundingMode roundingMode;
    17     protected int currency1Scale;
    18     protected int currency2Scale;
    19 
    20     /**
    21      * Constructor
    22      * By default it creates convertor with conversion value 1
    23      *
    24      * @param currency1       currency for conversion
    25      * @param currency2       currency for conversion
    26      * @param provider constants provider
    27      * @param roundingMode    math rounding mode
    28      */
    29     CurrencyConvertorImpl(Currency currency1, Currency currency2, ConversionRatioProvider provider, RoundingMode roundingMode) {
    30         this(currency1, currency2, provider, roundingMode, currency1.getDefaultFractionDigits(), currency2.getDefaultFractionDigits());
    31     }
    32 
    33     /**
    34      * Constructor
    35      *
    36      * @param currency1       currency for conversion
    37      * @param currency2       currency for conversion
    38      * @param conversionRatioProvider conversion constants provider
    39      * @param roundingMode    math rounding mode
    40      * @param conversionRatioProvider constants provider
    41      * @param currency2Scale
    42      */
    43     CurrencyConvertorImpl(Currency currency1, Currency currency2, ConversionRatioProvider conversionRatioProvider, RoundingMode roundingMode, int currency1Scale, int currency2Scale) {
    44         super(currency1, currency2, conversionRatioProvider);
    45         this.roundingMode = roundingMode;
    46         this.currency1Scale = currency1Scale;
    47         this.currency2Scale = currency2Scale;
    48     }
    49 
    50 
    51     public BigDecimal convertCurrency1ToCurrency2(BigDecimal amountOfMoney) throws CannotConvertException {
    52         try {
    53             final BigDecimal result = amountOfMoney.multiply(getConversionRatioProvider().getCurrency1ToCurrency2Constant());
    54             return result.setScale(getCurrency2Scale(), getRoundingMode());
    55         } catch (CannotProvideValueException e) {
    56             throw new CannotConvertException(e);            
    57         } catch (ArithmeticException e) {
    58             throw new CannotConvertException(e);
    59         }
    60     }
    61 
    62 
    63     public BigDecimal convertCurrency2ToCurrency1(BigDecimal amountOfMoney) throws CannotConvertException {
    64         try {
    65             final BigDecimal result = amountOfMoney.multiply(getConversionRatioProvider().getCurrency2ToCurrency1Constant());
    66             return result.setScale(getCurrency1Scale(), getRoundingMode());
    67         } catch (CannotProvideValueException e) {
    68             throw new CannotConvertException(e);
    69         } catch (ArithmeticException e) {
    70             throw new CannotConvertException(e);
    71         }
    72     }
    73   
    74     public String toString() {
    75         return "CurrencyConvertorImpl{" +
    76                 "currency1=" + getCurrency1() +
    77                 ",currency2=" + getCurrency2() +
    78                 ", roundingMode=" + roundingMode +
    79                 ", currency1Scale=" + currency1Scale +
    80                 ", currency2Scale=" + currency2Scale +
    81                 '}';
    82     }
    83 
    84     RoundingMode getRoundingMode() {
    85         return roundingMode;
    86     }    
    87 
    88     int getCurrency1Scale() {
    89         return currency1Scale;
    90     }
    91 
    92     int getCurrency2Scale() {
    93         return currency2Scale;
    94     }
    95 
    96     void setRoundingMode(RoundingMode roundingMode) {
    97         this.roundingMode = roundingMode;
    98     }
    99 
   100     void setCurrency1Scale(int currency1Scale) {
   101         this.currency1Scale = currency1Scale;
   102     }
   103 
   104     void setCurrency2Scale(int currency2Scale) {
   105         this.currency2Scale = currency2Scale;
   106     }
   107 
   108 }