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;
14 public final class Convertor {
16 private List<ConvertorDelegate> convertorDelegates = new ArrayList<ConvertorDelegate>();
20 * Create new instance of the converter for the given currencies and its rate.
22 * @param rateValue the rate between the first and the second currency
23 * @param currencyFirst the first currency
24 * @param currencySecond the second currency
26 public Convertor(BigDecimal rateValue, Currency currencyFirst, Currency currencySecond) {
27 notNull(currencyFirst, "currencyFirst");
28 notNull(currencySecond, "currencySecond");
29 notNull(rateValue, "rateValue");
30 convertorDelegates.add(new ConvertorDelegate(new StaticRateProvider(rateValue), currencyFirst, currencySecond));
34 * Create new instance of the converter for the given currencies and its rate.
35 * A rate value is provided by {@link RateProvider}.
37 * @param rateProvider the rate provider
38 * @param currencyFirst the first currency
39 * @param currencySecond the second currency
41 public Convertor(RateProvider rateProvider, Currency currencyFirst, Currency currencySecond) {
42 notNull(currencyFirst, "currencyFirst");
43 notNull(currencySecond, "currencySecond");
44 notNull(rateProvider, "rateProvider");
45 convertorDelegates.add(new ConvertorDelegate(rateProvider, currencyFirst, currencySecond));
49 * Create new instance of the convertor from the given convertors.
50 * @param convertors the convertors
52 public Convertor(Convertor... convertors) {
53 notNull(convertors, "convertors");
54 if(convertors.length == 0) {
55 throw new IllegalArgumentException("There must be at least one converter.");
58 for(Convertor convertor: convertors) {
59 if(convertor != null) {
60 convertorDelegates.addAll(convertor.convertorDelegates);
66 * Converts an amount value between the two currencies of this converter.
68 * @param amount an amount
69 * @param fromCurrency an amount currency
70 * @param toCurrency to a target currency
71 * @return a converted amount value
73 * @throws ConversionException if the conversion fails
74 * @throws UnsupportedConversionException if the conversion between a given currencies is not supported.
76 public Amount convert(BigDecimal amount, Currency fromCurrency, Currency toCurrency) throws ConversionException {
77 notNull(amount, "amount");
78 notNull(fromCurrency, "fromCurrency");
79 notNull(toCurrency, "toCurrency");
80 ConvertorDelegate appropriateDelegate = null;
82 //try find an appropriate delegate for conversion
83 for(ConvertorDelegate delegate : convertorDelegates) {
84 if(delegate.isConversionSupported(fromCurrency, toCurrency)) {
85 appropriateDelegate = delegate;
90 if(appropriateDelegate == null) {
91 throw new UnsupportedConversionException(fromCurrency, toCurrency);
94 return appropriateDelegate.convert(amount, fromCurrency, toCurrency);
98 * Internal delegate implements a logic for conversion between two currencies
100 * @see #isConversionSupported(Currency, Currency)
102 private static class ConvertorDelegate {
103 private final Currency first;
104 private final Currency second;
105 private final RateProvider rateProvider;
106 public static final BigDecimal one = new BigDecimal(1);
109 private ConvertorDelegate(RateProvider rateProvider, Currency currencyFirst, Currency currencySecond) {
110 this.rateProvider = rateProvider;
111 this.first = currencyFirst;
112 this.second = currencySecond;
115 private Amount convert(BigDecimal amount, Currency fromCurrency, Currency toCurrency) throws ConversionException {
116 BigDecimal rateValue = getRateValue(fromCurrency, toCurrency);
117 BigDecimal result = rateValue.multiply(amount);
118 return new Amount(result, toCurrency);
121 private BigDecimal getRateValue(Currency fromCurrency, Currency toCurrency) {
125 if(first == fromCurrency) {
126 BigDecimal rateValue = rateProvider.getRate();
127 if(rateValue == null) {
128 throw new NullPointerException("Rate cannot be null!");
132 BigDecimal rateValue = rateProvider.getRate();
133 if(rateValue == null) {
134 throw new NullPointerException("Rate cannot be null!");
137 retVal = one.divide(rateValue, 10 ,RoundingMode.HALF_UP);
144 * @return <code>true</code> if the delegate is able to convert from the given currency
145 * to the given currency and vice versa otherwise <code>false</code>.
147 private boolean isConversionSupported(Currency fromCurrency, Currency toCurrency) {
148 return ((fromCurrency == first || fromCurrency == second) && (toCurrency == first || toCurrency == second));
153 * A rate provider. This class represents a way how could be "static" convertor
154 * extended in order converts according to current rate.
156 public static abstract class RateProvider {
159 * @return a rate between the from currency and the to currency associated with
162 public abstract BigDecimal getRate();
165 private static class StaticRateProvider extends RateProvider{
166 private final BigDecimal rateValue;
168 private StaticRateProvider(BigDecimal rateValue){
169 this.rateValue = rateValue;
172 public BigDecimal getRate() {
173 return this.rateValue;