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