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
12 * using exchange rate specified in exchange rate provide. This method is not try
13 * use reverted excahnage rate.
14 * <li>{@link #convertWithReversibleRates(ConvertorCurrency, ConvertorCurrency, BigDecimal)} -
15 * convert using exchange rate specified in exchange rate provide. This method
16 * is not trying to use reverted exchange rate.
19 * Exchange rate is provided by {@link ExchangeRateProvider}.
21 public class Convertor {
22 private Convertor[] convertors;
24 /** Create new <code>Convertor</code> as merge of provided convertors. Merged convertor will use
25 * provided convertors to convert between currencies.
27 * Only one should be able to provide conversion between currencies. If more than one convertos
28 * are able to convert currency, one of conversions will be used (it is not defined which).
31 * @param convertors Convertor used to create merge-convertor.
32 * @return Returns new convertor instance.
34 public static Convertor createConvertorAsMerge(Convertor[] convertors) {
35 return new Convertor(convertors);
38 boolean remainderAllowed = true; //if false, remained is not allowed (should be true ideally, but can't handle it now)
39 ExchangeRateProvider exchangeRateProvider;
41 /** Create simle convertor.
44 this.convertors=new Convertor[0];
47 /** Create merge convertor.
49 private Convertor(Convertor[] convertors) {
50 this.convertors = convertors;
54 * Create new <code>Convertor</code> using <code>ExchangeRateProvider</code>.
56 * @param exchangeRateProvider {@link ExchangeRateProvider} used to get exchange rate.
58 * @return Returns <code>Convertor</code> which can be used to convert money.
61 public static Convertor createConvertor(ExchangeRateProvider exchangeRateProvider) {
62 Convertor c = new Convertor();
64 c.exchangeRateProvider = exchangeRateProvider;
69 * Convert <code>amount</code> from <code>fromCurrency</code> to <code>toCurrency</code> as specified
70 * in <code>ExchangeRateProvider</code>.
72 * @param amount Amount which should be converted. Can't be negative value (can be zero or positive).
73 * @return Return <code>ConversionResult</code> which holds conversion result.
75 * @deprecated since version2. Use {@link #convert(ConvertorCurrency, ConvertorCurrency, BigDecimal) } - explicitly specify conversion currencies.
77 public ConversionResult convert(BigDecimal amount) {
78 return convertValue(exchangeRateProvider.getFromCurrency(), exchangeRateProvider.getToCurrency(),amount, false,false);
82 * Convert <code>amount</code> from <code>toCurrency</code> to <code>fromCurrency</code> as specified
83 * in <code>ExchangeRateProvider</code>. This is <em>reverted</em> order than suggested by names of currency fields in <code>ExchangeRate</code>.
85 * @param amount Amount which should be converted. Can't be negative value (can be zero or positive).
86 * @return Return <code>ConversionResult</code> which holds conversion result.
88 * @deprecated since version2. Use {@link #convert(ConvertorCurrency, ConvertorCurrency, BigDecimal) } - explicitly specify conversion currencies.
90 public ConversionResult convertBack(BigDecimal amount) {
91 return convertValue(exchangeRateProvider.getFromCurrency(), exchangeRateProvider.getToCurrency(),amount, true,false);
94 private ConversionResult convertUsingSimpleConvertor(ConvertorCurrency fromCurrency, ConvertorCurrency toCurrency, boolean reversibleExRate, BigDecimal amount, boolean convertBack) throws ConversionNotSupportedException, RuntimeException {
95 ConversionResult result = new ConversionResult();
97 //ExchangeRate rate = exchangeRateProvider.getExchangeRate();
99 if (reversibleExRate) {
100 rate = exchangeRateProvider.getReversibleExchangeRate(fromCurrency, toCurrency);
102 rate = exchangeRateProvider.getExchangeRate(fromCurrency, toCurrency);
108 int fromFranctionDigits = fromCurrency.getDefaultFractionDigits();
109 int toFractionDigits = toCurrency.getDefaultFractionDigits();
111 if (toFractionDigits != 2) {
112 throw new ConvertorException("Can't process currency with defaultFractionDigits!=2, " + exchangeRateProvider.getToCurrency() + " has " + toFractionDigits + " defaultFractionDigits");
114 if (fromFranctionDigits != 2) {
115 throw new ConvertorException("Can't process currency with defaultFractionDigits!=2, " + exchangeRateProvider.getFromCurrency() + " has " + fromFranctionDigits + " defaultFractionDigits");
118 if (amount.signum() == -1) {
119 throw new RuntimeException("Can convert only non-negative value, current value is " + amount);
123 MathContext context = new MathContext(0, RoundingMode.DOWN);
128 //converting in reverted way
129 to = rate.getFromValue();
130 from = rate.getToValue();
132 //converting in normal way
133 from = rate.getFromValue();
134 to = rate.getToValue();
137 BigDecimal amountCent = amount.movePointRight(toFractionDigits);
139 final BigDecimal multiplied = amountCent.multiply(to, context);
140 BigDecimal[] division = multiplied.divideAndRemainder(from, context);
142 if (!remainderAllowed && !(BigDecimal.ZERO.equals(division[1]))) {
143 throw new RuntimeException("Remained is not allowed - remaining amount is " + division[1] + " cents");
145 result.setRemainder(BigDecimal.ZERO);
148 BigDecimal converted = division[0].movePointLeft(toFractionDigits);
149 converted = converted.setScale(toFractionDigits); //XXX ugly
150 result.setConverted(converted);
151 //result.setRemainder(...);
156 private ConversionResult convertValue(ConvertorCurrency fromCurrency, ConvertorCurrency toCurrency,BigDecimal amount, boolean convertBack,boolean reversibleExRate) throws RuntimeException {
157 //result.setRemainder(...);
158 if (convertors.length==0) {
159 return convertUsingSimpleConvertor(fromCurrency, toCurrency, reversibleExRate, amount, convertBack);
161 ConversionResult result = null;
162 for (int i = 0;i<convertors.length;i++) {
163 Convertor subConvertor = convertors[i];
164 result = subConvertor.convertValue(fromCurrency, toCurrency, amount, convertBack, reversibleExRate);
174 * Convert <code>value</code> from <code>fromCurrency</code> to <code>toCurrency</code>.
176 * Exchange rate is provided by exchange rate provider which was specified when Convertor was created.
177 * This method is using only exchange rate from->to and not trying to use reverted excange rate to->from.
179 * @param fromCurrency Source currency to convert from.
180 * @param toCurrency Target currency to convert to.
181 * @param value Value in source currency which should be converted.
182 * @return Return conversion result.
184 * @throws ConversionNotSupportedException If conversion from <code>fromCurrency</code> to <code>toCurrency</code> is not supported.
186 public ConversionResult convert(ConvertorCurrency fromCurrency, ConvertorCurrency toCurrency, BigDecimal value) {
187 ConversionResult result = convertValue(fromCurrency, toCurrency, value, false,false);
189 //throw new ConversionNotSupportedException("Conversion from " + fromCurrency + " to " + toCurrency + " is not supported");
190 throw new ConversionNotSupportedException(fromCurrency.getCurrencyCode(),toCurrency.getCurrencyCode(),false);
196 * Convert <code>value</code> from <code>fromCurrency</code> to <code>toCurrency</code>.
197 * Exchange rate is provided by exchange rate provider which was specified when Convertor was created.
199 * This method is using only exchange rate from->to and if not found, it is trying to use reverted excange rate to->from.
201 * @param fromCurrency Source currency to convert from.
202 * @param toCurrency Target currency to convert to.
203 * @param value Value in source currency which should be converted.
204 * @return Return conversion result.
206 * @throws ConversionNotSupportedException If conversion from <code>fromCurrency</code> to <code>toCurrency</code>
207 * is not supported and neither conversion from <code>toCurrency</code> to <code>fromCurrency</code> is not supported.
209 public ConversionResult convertWithReversibleRates(ConvertorCurrency fromCurrency, ConvertorCurrency toCurrency, BigDecimal value) {
210 ConversionResult result = convertValue(fromCurrency, toCurrency, value, false,true);
212 //throw new ConversionNotSupportedException("Neither onversion nor reverted conversion from " + fromCurrency + " to " + toCurrency + " is not supported,");
213 throw new ConversionNotSupportedException(fromCurrency.getCurrencyCode(),toCurrency.getCurrencyCode(),true);