japod@6
|
1 |
package org.apidesign.apifest08.currency;
|
japod@6
|
2 |
|
japod@21
|
3 |
import static org.apidesign.apifest08.currency.Assert.notNull;
|
japod@21
|
4 |
|
japod@6
|
5 |
import java.math.BigDecimal;
|
japod@21
|
6 |
import java.math.RoundingMode;
|
japod@6
|
7 |
import java.util.Currency;
|
japod@6
|
8 |
|
japod@21
|
9 |
public final class Convertor {
|
japod@6
|
10 |
|
japod@21
|
11 |
private final Currency first;
|
japod@21
|
12 |
private final Currency second;
|
japod@21
|
13 |
private final BigDecimal rateValue; // a rate between the first currency and the second currency
|
japod@21
|
14 |
public static final BigDecimal one = new BigDecimal(1);
|
japod@21
|
15 |
|
japod@21
|
16 |
public Convertor(BigDecimal rateValue, Currency currencyFirst, Currency currencySecond) {
|
japod@21
|
17 |
notNull(currencyFirst, "currencyFirst");
|
japod@21
|
18 |
notNull(currencySecond, "currencySecond");
|
japod@21
|
19 |
notNull(rateValue, "rateValue");
|
japod@21
|
20 |
|
japod@21
|
21 |
this.rateValue = rateValue;
|
japod@21
|
22 |
this.first = currencyFirst;
|
japod@21
|
23 |
this.second = currencySecond;
|
japod@21
|
24 |
}
|
japod@21
|
25 |
|
japod@6
|
26 |
/**
|
japod@21
|
27 |
* Converts an amount value between the two currencies of this converter.
|
japod@6
|
28 |
*
|
japod@6
|
29 |
* @param amount an amount
|
japod@6
|
30 |
* @param fromCurrency an amount currency
|
japod@6
|
31 |
* @param toCurrency to a target currency
|
japod@6
|
32 |
* @return a converted amount value
|
japod@6
|
33 |
*
|
japod@6
|
34 |
* @throws ConversionException if the conversion fails
|
japod@6
|
35 |
* @throws UnsupportedConversionException if the conversion between a given currencies is not supported.
|
japod@6
|
36 |
*/
|
japod@21
|
37 |
public Amount convert(BigDecimal amount, Currency fromCurrency, Currency toCurrency) throws ConversionException {
|
japod@21
|
38 |
notNull(amount, "amount");
|
japod@21
|
39 |
notNull(fromCurrency, "fromCurrency");
|
japod@21
|
40 |
notNull(toCurrency, "toCurrency");
|
japod@21
|
41 |
|
japod@21
|
42 |
if((fromCurrency != first && fromCurrency != second) || (toCurrency != first && toCurrency != second)) {
|
japod@21
|
43 |
throw new UnsupportedConversionException(fromCurrency, toCurrency);
|
japod@21
|
44 |
}
|
japod@6
|
45 |
|
japod@21
|
46 |
BigDecimal rateValue = getRateValue(fromCurrency, toCurrency);
|
japod@21
|
47 |
BigDecimal result = rateValue.multiply(amount);
|
japod@21
|
48 |
return new Amount(result, toCurrency);
|
japod@21
|
49 |
}
|
japod@21
|
50 |
|
japod@21
|
51 |
private BigDecimal getRateValue(Currency fromCurrency, Currency toCurrency) {
|
japod@21
|
52 |
|
japod@21
|
53 |
BigDecimal retVal;
|
japod@21
|
54 |
|
japod@21
|
55 |
if(first == fromCurrency) {
|
japod@21
|
56 |
retVal = rateValue;
|
japod@21
|
57 |
} else {
|
japod@21
|
58 |
//reverse rate
|
japod@21
|
59 |
retVal = one.divide(rateValue, 10 ,RoundingMode.HALF_UP);
|
japod@21
|
60 |
}
|
japod@21
|
61 |
|
japod@21
|
62 |
return retVal;
|
japod@21
|
63 |
}
|
japod@6
|
64 |
}
|