1 package org.apidesign.apifest08.currency;
3 import static org.apidesign.apifest08.currency.Assert.notNull;
5 import java.math.BigDecimal;
6 import java.math.RoundingMode;
7 import java.util.ArrayList;
8 import java.util.Currency;
11 public final class Convertor {
13 private List<ConvertorDelegate> convertorDelegates = new ArrayList<ConvertorDelegate>();
17 * Create new instance of the converter for the given currencies and its rate.
19 * @param rateValue the rate between the first and the second currency
20 * @param currencyFirst the first currency
21 * @param currencySecond the second currency
23 public Convertor(BigDecimal rateValue, Currency currencyFirst, Currency currencySecond) {
24 notNull(currencyFirst, "currencyFirst");
25 notNull(currencySecond, "currencySecond");
26 notNull(rateValue, "rateValue");
27 convertorDelegates.add(new ConvertorDelegate(rateValue, currencyFirst, currencySecond));
31 * Create new instance of the convertor from the given convertors.
32 * @param convertors the convertors
34 public Convertor(Convertor... convertors) {
35 notNull(convertors, "convertors");
36 if(convertors.length == 0) {
37 throw new IllegalArgumentException("There must be at least one converter.");
40 for(Convertor convertor: convertors) {
41 if(convertor != null) {
42 convertorDelegates.addAll(convertor.convertorDelegates);
48 * Converts an amount value between the two currencies of this converter.
50 * @param amount an amount
51 * @param fromCurrency an amount currency
52 * @param toCurrency to a target currency
53 * @return a converted amount value
55 * @throws ConversionException if the conversion fails
56 * @throws UnsupportedConversionException if the conversion between a given currencies is not supported.
58 public Amount convert(BigDecimal amount, Currency fromCurrency, Currency toCurrency) throws ConversionException {
59 notNull(amount, "amount");
60 notNull(fromCurrency, "fromCurrency");
61 notNull(toCurrency, "toCurrency");
62 ConvertorDelegate appropriateDelegate = null;
64 //try find an appropriate delegate for conversion
65 for(ConvertorDelegate delegate : convertorDelegates) {
66 if(delegate.isConversionSupported(fromCurrency, toCurrency)) {
67 appropriateDelegate = delegate;
72 if(appropriateDelegate == null) {
73 throw new UnsupportedConversionException(fromCurrency, toCurrency);
76 return appropriateDelegate.convert(amount, fromCurrency, toCurrency);
80 * Internal delegate implements a logic for conversion between two currencies
82 * @see #isConversionSupported(Currency, Currency)
84 private static class ConvertorDelegate {
85 private final Currency first;
86 private final Currency second;
87 private final BigDecimal rateValue; // a rate between the first currency and the second currency
88 public static final BigDecimal one = new BigDecimal(1);
90 private ConvertorDelegate(BigDecimal rateValue, Currency currencyFirst, Currency currencySecond) {
91 this.rateValue = rateValue;
92 this.first = currencyFirst;
93 this.second = currencySecond;
96 private Amount convert(BigDecimal amount, Currency fromCurrency, Currency toCurrency) throws ConversionException {
97 BigDecimal rateValue = getRateValue(fromCurrency, toCurrency);
98 BigDecimal result = rateValue.multiply(amount);
99 return new Amount(result, toCurrency);
102 private BigDecimal getRateValue(Currency fromCurrency, Currency toCurrency) {
106 if(first == fromCurrency) {
110 retVal = one.divide(rateValue, 10 ,RoundingMode.HALF_UP);
117 * @return <code>true</code> if the delegate is able to convert from the given currency
118 * to the given currency and vice versa otherwise <code>false</code>.
120 private boolean isConversionSupported(Currency fromCurrency, Currency toCurrency) {
121 return ((fromCurrency == first || fromCurrency == second) && (toCurrency == first || toCurrency == second));