task2/solution01/src/org/apidesign/apifest08/currency/CurrencyConvertorImpl.java
changeset 29 f6073056b9fe
parent 6 97662396c0fd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/task2/solution01/src/org/apidesign/apifest08/currency/CurrencyConvertorImpl.java	Wed Oct 01 10:43:05 2008 +0200
     1.3 @@ -0,0 +1,108 @@
     1.4 +package org.apidesign.apifest08.currency;
     1.5 +
     1.6 +import java.math.BigDecimal;
     1.7 +import java.math.RoundingMode;
     1.8 +import java.util.Currency;
     1.9 +
    1.10 +/**
    1.11 + * Intern implementation of Bidirectional Convertor - this class is only for internal purposes only.
    1.12 + * This implementation uses BigDecimals to count result value
    1.13 + * Scale is set by ISO 4217 as default
    1.14 + *
    1.15 + * @author Ladislav Vitasek
    1.16 + */
    1.17 +class CurrencyConvertorImpl extends AbstractCurrencyConvertor {
    1.18 +
    1.19 +    protected RoundingMode roundingMode;
    1.20 +    protected int currency1Scale;
    1.21 +    protected int currency2Scale;
    1.22 +
    1.23 +    /**
    1.24 +     * Constructor
    1.25 +     * By default it creates convertor with conversion value 1
    1.26 +     *
    1.27 +     * @param currency1       currency for conversion
    1.28 +     * @param currency2       currency for conversion
    1.29 +     * @param provider constants provider
    1.30 +     * @param roundingMode    math rounding mode
    1.31 +     */
    1.32 +    CurrencyConvertorImpl(Currency currency1, Currency currency2, ConversionRatioProvider provider, RoundingMode roundingMode) {
    1.33 +        this(currency1, currency2, provider, roundingMode, currency1.getDefaultFractionDigits(), currency2.getDefaultFractionDigits());
    1.34 +    }
    1.35 +
    1.36 +    /**
    1.37 +     * Constructor
    1.38 +     *
    1.39 +     * @param currency1       currency for conversion
    1.40 +     * @param currency2       currency for conversion
    1.41 +     * @param conversionRatioProvider conversion constants provider
    1.42 +     * @param roundingMode    math rounding mode
    1.43 +     * @param conversionRatioProvider constants provider
    1.44 +     * @param currency2Scale
    1.45 +     */
    1.46 +    CurrencyConvertorImpl(Currency currency1, Currency currency2, ConversionRatioProvider conversionRatioProvider, RoundingMode roundingMode, int currency1Scale, int currency2Scale) {
    1.47 +        super(currency1, currency2, conversionRatioProvider);
    1.48 +        this.roundingMode = roundingMode;
    1.49 +        this.currency1Scale = currency1Scale;
    1.50 +        this.currency2Scale = currency2Scale;
    1.51 +    }
    1.52 +
    1.53 +
    1.54 +    public BigDecimal convertCurrency1ToCurrency2(BigDecimal amountOfMoney) throws CannotConvertException {
    1.55 +        try {
    1.56 +            final BigDecimal result = amountOfMoney.multiply(getConversionRatioProvider().getCurrency1ToCurrency2Constant());
    1.57 +            return result.setScale(getCurrency2Scale(), getRoundingMode());
    1.58 +        } catch (CannotProvideValueException e) {
    1.59 +            throw new CannotConvertException(e);            
    1.60 +        } catch (ArithmeticException e) {
    1.61 +            throw new CannotConvertException(e);
    1.62 +        }
    1.63 +    }
    1.64 +
    1.65 +
    1.66 +    public BigDecimal convertCurrency2ToCurrency1(BigDecimal amountOfMoney) throws CannotConvertException {
    1.67 +        try {
    1.68 +            final BigDecimal result = amountOfMoney.multiply(getConversionRatioProvider().getCurrency2ToCurrency1Constant());
    1.69 +            return result.setScale(getCurrency1Scale(), getRoundingMode());
    1.70 +        } catch (CannotProvideValueException e) {
    1.71 +            throw new CannotConvertException(e);
    1.72 +        } catch (ArithmeticException e) {
    1.73 +            throw new CannotConvertException(e);
    1.74 +        }
    1.75 +    }
    1.76 +  
    1.77 +    public String toString() {
    1.78 +        return "CurrencyConvertorImpl{" +
    1.79 +                "currency1=" + getCurrency1() +
    1.80 +                ",currency2=" + getCurrency2() +
    1.81 +                ", roundingMode=" + roundingMode +
    1.82 +                ", currency1Scale=" + currency1Scale +
    1.83 +                ", currency2Scale=" + currency2Scale +
    1.84 +                '}';
    1.85 +    }
    1.86 +
    1.87 +    RoundingMode getRoundingMode() {
    1.88 +        return roundingMode;
    1.89 +    }    
    1.90 +
    1.91 +    int getCurrency1Scale() {
    1.92 +        return currency1Scale;
    1.93 +    }
    1.94 +
    1.95 +    int getCurrency2Scale() {
    1.96 +        return currency2Scale;
    1.97 +    }
    1.98 +
    1.99 +    void setRoundingMode(RoundingMode roundingMode) {
   1.100 +        this.roundingMode = roundingMode;
   1.101 +    }
   1.102 +
   1.103 +    void setCurrency1Scale(int currency1Scale) {
   1.104 +        this.currency1Scale = currency1Scale;
   1.105 +    }
   1.106 +
   1.107 +    void setCurrency2Scale(int currency2Scale) {
   1.108 +        this.currency2Scale = currency2Scale;
   1.109 +    }
   1.110 +
   1.111 +}