1 package org.apidesign.apifest08.currency;
3 import java.math.BigDecimal;
4 import java.math.RoundingMode;
5 import java.util.Currency;
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
12 * @author Ladislav Vitasek
14 class CurrencyConvertorImpl extends AbstractCurrencyConvertor {
16 protected RoundingMode roundingMode;
17 protected int currency1Scale;
18 protected int currency2Scale;
22 * By default it creates convertor with conversion value 1
24 * @param currency1 currency for conversion
25 * @param currency2 currency for conversion
26 * @param provider constants provider
27 * @param roundingMode math rounding mode
29 CurrencyConvertorImpl(Currency currency1, Currency currency2, ConversionRatioProvider provider, RoundingMode roundingMode) {
30 this(currency1, currency2, provider, roundingMode, currency1.getDefaultFractionDigits(), currency2.getDefaultFractionDigits());
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
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;
51 public BigDecimal convertCurrency1ToCurrency2(BigDecimal amountOfMoney) throws CannotConvertException {
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);
63 public BigDecimal convertCurrency2ToCurrency1(BigDecimal amountOfMoney) throws CannotConvertException {
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);
74 public String toString() {
75 return "CurrencyConvertorImpl{" +
76 "currency1=" + getCurrency1() +
77 ",currency2=" + getCurrency2() +
78 ", roundingMode=" + roundingMode +
79 ", currency1Scale=" + currency1Scale +
80 ", currency2Scale=" + currency2Scale +
84 RoundingMode getRoundingMode() {
88 int getCurrency1Scale() {
89 return currency1Scale;
92 int getCurrency2Scale() {
93 return currency2Scale;
96 void setRoundingMode(RoundingMode roundingMode) {
97 this.roundingMode = roundingMode;
100 void setCurrency1Scale(int currency1Scale) {
101 this.currency1Scale = currency1Scale;
104 void setCurrency2Scale(int currency2Scale) {
105 this.currency2Scale = currency2Scale;