1 package org.apidesign.apifest08.currency;
3 import java.math.BigDecimal;
4 import java.math.MathContext;
5 import java.math.RoundingMode;
7 /** Convertor able to convert amount from one currency to other currency.
9 * Conversion method are:
11 * <li>{@link #convert(ConvertorCurrency, ConvertorCurrency, BigDecimal)} - convert using exchange rate specified in exchange rate provide. <em>Do not try use reverted excahnage rate</em>.
12 * <li>{@link #convertWithReversibleRates(ConvertorCurrency, ConvertorCurrency, BigDecimal)} - convert using exchange rate specified in exchange rate provide. <em>Try use reverted excahnage rate</em>.
15 * Exchange rate is provided by {@link ExchangeRateProvider}.
17 public class Convertor {
18 private Convertor[] convertors;
20 /** Create new <code>Convertor</code> as merge of provided convertors. Merged convertor will use
21 * provided convertors to convert between currencies.
23 * Only one should be able to provide conversion between currencies. If more than one convertos
24 * are able to convert currency, one of conversions will be used (it is not defined which).
27 * @param convertors Convertor used to create merge-convertor.
28 * @return Returns new convertor instance.
30 public static Convertor createConvertorAsMerge(Convertor[] convertors) {
31 return new Convertor(convertors);
34 boolean remainderAllowed = true; //if false, remained is not allowed (should be true ideally, but can't handle it now)
35 ExchangeRateProvider exchangeRateProvider;
37 /** Create simle convertor.
40 this.convertors=new Convertor[0];
43 /** Create merge convertor.
45 private Convertor(Convertor[] convertors) {
46 this.convertors = convertors;
50 * Create new <code>Convertor</code> using <code>ExchangeRateProvider</code>.
52 * @param exchangeRateProvider {@link ExchangeRateProvider} used to get exchange rate.
54 * @return Returns <code>Convertor</code> which can be used to convert money.
57 public static Convertor createConvertor(ExchangeRateProvider exchangeRateProvider) {
58 Convertor c = new Convertor();
60 c.exchangeRateProvider = exchangeRateProvider;
65 * Convert <code>amount</code> from <code>fromCurrency</code> to <code>toCurrency</code> as specified
66 * in <code>ExchangeRateProvider</code>.
68 * @param amount Amount which should be converted. Can't be negative value (can be zero or positive).
69 * @return Return <code>ConversionResult</code> which holds conversion result.
71 * @deprecated since version2. Use {@link #convert(ConvertorCurrency, ConvertorCurrency, BigDecimal) } - explicitly specify conversion currencies.
73 public ConversionResult convert(BigDecimal amount) {
74 return convertValue(exchangeRateProvider.getFromCurrency(), exchangeRateProvider.getToCurrency(),amount, false,false);
78 * Convert <code>amount</code> from <code>toCurrency</code> to <code>fromCurrency</code> as specified
79 * in <code>ExchangeRateProvider</code>. This is <em>reverted</em> order than suggested by names of currency fields in <code>ExchangeRate</code>.
81 * @param amount Amount which should be converted. Can't be negative value (can be zero or positive).
82 * @return Return <code>ConversionResult</code> which holds conversion result.
84 * @deprecated since version2. Use {@link #convert(ConvertorCurrency, ConvertorCurrency, BigDecimal) } - explicitly specify conversion currencies.
86 public ConversionResult convertBack(BigDecimal amount) {
87 return convertValue(exchangeRateProvider.getFromCurrency(), exchangeRateProvider.getToCurrency(),amount, true,false);
90 private ConversionResult convertUsingSimpleConvertor(ConvertorCurrency fromCurrency, ConvertorCurrency toCurrency, boolean reversibleExRate, BigDecimal amount, boolean convertBack) throws ConversionNotSupportedException, RuntimeException {
91 ConversionResult result = new ConversionResult();
93 //ExchangeRate rate = exchangeRateProvider.getExchangeRate();
95 if (reversibleExRate) {
96 rate = exchangeRateProvider.getReversibleExchangeRate(fromCurrency, toCurrency);
98 rate = exchangeRateProvider.getExchangeRate(fromCurrency, toCurrency);
104 int fromFranctionDigits = fromCurrency.getDefaultFractionDigits();
105 int toFractionDigits = toCurrency.getDefaultFractionDigits();
107 if (toFractionDigits != 2) {
108 throw new ConvertorException("Can't process currency with defaultFractionDigits!=2, " + exchangeRateProvider.getToCurrency() + " has " + toFractionDigits + " defaultFractionDigits");
110 if (fromFranctionDigits != 2) {
111 throw new ConvertorException("Can't process currency with defaultFractionDigits!=2, " + exchangeRateProvider.getFromCurrency() + " has " + fromFranctionDigits + " defaultFractionDigits");
114 if (amount.signum() == -1) {
115 throw new RuntimeException("Can convert only non-negative value, current value is " + amount);
119 MathContext context = new MathContext(0, RoundingMode.DOWN);
124 //converting in reverted way
125 to = rate.getFromValue();
126 from = rate.getToValue();
128 //converting in normal way
129 from = rate.getFromValue();
130 to = rate.getToValue();
133 BigDecimal amountCent = amount.movePointRight(2);
135 final BigDecimal multiplied = amountCent.multiply(to, context);
136 BigDecimal[] division = multiplied.divideAndRemainder(from, context);
138 if (!remainderAllowed && !(BigDecimal.ZERO.equals(division[1]))) {
139 throw new RuntimeException("Remained is not allowed - remaining amount is " + division[1] + " cents");
141 result.setRemainder(BigDecimal.ZERO);
144 final BigDecimal converted = division[0].movePointLeft(2);
145 result.setConverted(converted);
146 //result.setRemainder(...);
151 private ConversionResult convertValue(ConvertorCurrency fromCurrency, ConvertorCurrency toCurrency,BigDecimal amount, boolean convertBack,boolean reversibleExRate) throws RuntimeException {
152 //result.setRemainder(...);
153 if (convertors.length==0) {
154 return convertUsingSimpleConvertor(fromCurrency, toCurrency, reversibleExRate, amount, convertBack);
156 ConversionResult result = null;
157 for (int i = 0;i<convertors.length;i++) {
158 Convertor subConvertor = convertors[i];
159 result = subConvertor.convertValue(fromCurrency, toCurrency, amount, convertBack, reversibleExRate);
169 * Convert <code>value</code> from <code>fromCurrency</code> to <code>toCurrency</code>.
171 * Exchange rate is provided by exchange rate provider which was specified when Convertor was created.
172 * This method is using only exchange rate from->to and not trying to use reverted excange rate to->from.
174 * @param fromCurrency Source currency to convert from.
175 * @param toCurrency Target currency to convert to.
176 * @param value Value in source currency which should be converted.
177 * @return Return conversion result.
179 * @throws ConversionNotSupportedException If conversion from <code>fromCurrency</code> to <code>toCurrency</code> is not supported.
181 public ConversionResult convert(ConvertorCurrency fromCurrency, ConvertorCurrency toCurrency, BigDecimal value) {
182 ConversionResult result = convertValue(fromCurrency, toCurrency, value, false,false);
184 //throw new ConversionNotSupportedException("Conversion from " + fromCurrency + " to " + toCurrency + " is not supported");
185 throw new ConversionNotSupportedException(fromCurrency.getCurrencyCode(),toCurrency.getCurrencyCode(),false);
191 * Convert <code>value</code> from <code>fromCurrency</code> to <code>toCurrency</code>.
192 * Exchange rate is provided by exchange rate provider which was specified when Convertor was created.
194 * This method is using only exchange rate from->to and if not found, it is trying to use reverted excange rate to->from.
196 * @param fromCurrency Source currency to convert from.
197 * @param toCurrency Target currency to convert to.
198 * @param value Value in source currency which should be converted.
199 * @return Return conversion result.
201 * @throws ConversionNotSupportedException If conversion from <code>fromCurrency</code> to <code>toCurrency</code>
202 * is not supported and neither conversion from <code>toCurrency</code> to <code>fromCurrency</code> is not supported.
204 public ConversionResult convertWithReversibleRates(ConvertorCurrency fromCurrency, ConvertorCurrency toCurrency, BigDecimal value) {
205 ConversionResult result = convertValue(fromCurrency, toCurrency, value, false,true);
207 //throw new ConversionNotSupportedException("Neither onversion nor reverted conversion from " + fromCurrency + " to " + toCurrency + " is not supported,");
208 throw new ConversionNotSupportedException(fromCurrency.getCurrencyCode(),toCurrency.getCurrencyCode(),true);