diff -r 07c16ec15a25 -r 58ec6da75f6f task4/solution13/src/org/apidesign/apifest08/currency/Convertor.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/task4/solution13/src/org/apidesign/apifest08/currency/Convertor.java Sat Oct 11 23:38:46 2008 +0200 @@ -0,0 +1,218 @@ +package org.apidesign.apifest08.currency; + +import java.math.BigDecimal; +import java.math.MathContext; +import java.math.RoundingMode; + +/** Convertor able to convert amount from one currency to other currency. + *

+ * Conversion method are: + *

+ * + * Exchange rate is provided by {@link ExchangeRateProvider}. + */ +public class Convertor { + private Convertor[] convertors; + + /** Create new Convertor as merge of provided convertors. Merged convertor will use + * provided convertors to convert between currencies. + *

+ * Only one should be able to provide conversion between currencies. If more than one convertos + * are able to convert currency, one of conversions will be used (it is not defined which). + * + * @since version 2 + * @param convertors Convertor used to create merge-convertor. + * @return Returns new convertor instance. + */ + public static Convertor createConvertorAsMerge(Convertor[] convertors) { + return new Convertor(convertors); + } + + boolean remainderAllowed = true; //if false, remained is not allowed (should be true ideally, but can't handle it now) + ExchangeRateProvider exchangeRateProvider; + + /** Create simle convertor. + */ + private Convertor() { + this.convertors=new Convertor[0]; + } + + /** Create merge convertor. + */ + private Convertor(Convertor[] convertors) { + this.convertors = convertors; + } + + /** + * Create new Convertor using ExchangeRateProvider. + * + * @param exchangeRateProvider {@link ExchangeRateProvider} used to get exchange rate. + * + * @return Returns Convertor which can be used to convert money. + * @since version1 + */ + public static Convertor createConvertor(ExchangeRateProvider exchangeRateProvider) { + Convertor c = new Convertor(); + + c.exchangeRateProvider = exchangeRateProvider; + return c; + } + + /** + * Convert amount from fromCurrency to toCurrency as specified + * in ExchangeRateProvider. + * + * @param amount Amount which should be converted. Can't be negative value (can be zero or positive). + * @return Return ConversionResult which holds conversion result. + * @since version1 + * @deprecated since version2. Use {@link #convert(ConvertorCurrency, ConvertorCurrency, BigDecimal) } - explicitly specify conversion currencies. + */ + public ConversionResult convert(BigDecimal amount) { + return convertValue(exchangeRateProvider.getFromCurrency(), exchangeRateProvider.getToCurrency(),amount, false,false); + } + + /** + * Convert amount from toCurrency to fromCurrency as specified + * in ExchangeRateProvider. This is reverted order than suggested by names of currency fields in ExchangeRate. + * + * @param amount Amount which should be converted. Can't be negative value (can be zero or positive). + * @return Return ConversionResult which holds conversion result. + * @since version1 + * @deprecated since version2. Use {@link #convert(ConvertorCurrency, ConvertorCurrency, BigDecimal) } - explicitly specify conversion currencies. + */ + public ConversionResult convertBack(BigDecimal amount) { + return convertValue(exchangeRateProvider.getFromCurrency(), exchangeRateProvider.getToCurrency(),amount, true,false); + } + + private ConversionResult convertUsingSimpleConvertor(ConvertorCurrency fromCurrency, ConvertorCurrency toCurrency, boolean reversibleExRate, BigDecimal amount, boolean convertBack) throws ConversionNotSupportedException, RuntimeException { + ConversionResult result = new ConversionResult(); + + //ExchangeRate rate = exchangeRateProvider.getExchangeRate(); + ExchangeRate rate; + if (reversibleExRate) { + rate = exchangeRateProvider.getReversibleExchangeRate(fromCurrency, toCurrency); + } else { + rate = exchangeRateProvider.getExchangeRate(fromCurrency, toCurrency); + } + if (rate == null) { + return null; + } + + int fromFranctionDigits = fromCurrency.getDefaultFractionDigits(); + int toFractionDigits = toCurrency.getDefaultFractionDigits(); + + if (toFractionDigits != 2) { + throw new ConvertorException("Can't process currency with defaultFractionDigits!=2, " + exchangeRateProvider.getToCurrency() + " has " + toFractionDigits + " defaultFractionDigits"); + } + if (fromFranctionDigits != 2) { + throw new ConvertorException("Can't process currency with defaultFractionDigits!=2, " + exchangeRateProvider.getFromCurrency() + " has " + fromFranctionDigits + " defaultFractionDigits"); + } + + if (amount.signum() == -1) { + throw new RuntimeException("Can convert only non-negative value, current value is " + amount); + } + + + MathContext context = new MathContext(0, RoundingMode.DOWN); + + BigDecimal from; + BigDecimal to; + if (convertBack) { + //converting in reverted way + to = rate.getFromValue(); + from = rate.getToValue(); + } else { + //converting in normal way + from = rate.getFromValue(); + to = rate.getToValue(); + } + + BigDecimal amountCent = amount.movePointRight(toFractionDigits); + + final BigDecimal multiplied = amountCent.multiply(to, context); + BigDecimal[] division = multiplied.divideAndRemainder(from, context); + + if (!remainderAllowed && !(BigDecimal.ZERO.equals(division[1]))) { + throw new RuntimeException("Remained is not allowed - remaining amount is " + division[1] + " cents"); + } else { + result.setRemainder(BigDecimal.ZERO); + } + + BigDecimal converted = division[0].movePointLeft(toFractionDigits); + converted = converted.setScale(toFractionDigits); //XXX ugly + result.setConverted(converted); + //result.setRemainder(...); + return result; + } + + + private ConversionResult convertValue(ConvertorCurrency fromCurrency, ConvertorCurrency toCurrency,BigDecimal amount, boolean convertBack,boolean reversibleExRate) throws RuntimeException { + //result.setRemainder(...); + if (convertors.length==0) { + return convertUsingSimpleConvertor(fromCurrency, toCurrency, reversibleExRate, amount, convertBack); + } else { + ConversionResult result = null; + for (int i = 0;ivalue from fromCurrency to toCurrency. + *

+ * Exchange rate is provided by exchange rate provider which was specified when Convertor was created. + * This method is using only exchange rate from->to and not trying to use reverted excange rate to->from. + * + * @param fromCurrency Source currency to convert from. + * @param toCurrency Target currency to convert to. + * @param value Value in source currency which should be converted. + * @return Return conversion result. + * @since version2 + * @throws ConversionNotSupportedException If conversion from fromCurrency to toCurrency is not supported. + */ + public ConversionResult convert(ConvertorCurrency fromCurrency, ConvertorCurrency toCurrency, BigDecimal value) { + ConversionResult result = convertValue(fromCurrency, toCurrency, value, false,false); + if (result==null) { + //throw new ConversionNotSupportedException("Conversion from " + fromCurrency + " to " + toCurrency + " is not supported"); + throw new ConversionNotSupportedException(fromCurrency.getCurrencyCode(),toCurrency.getCurrencyCode(),false); + } + return result; + } + + /** + * Convert value from fromCurrency to toCurrency. + * Exchange rate is provided by exchange rate provider which was specified when Convertor was created. + *

+ * This method is using only exchange rate from->to and if not found, it is trying to use reverted excange rate to->from. + * + * @param fromCurrency Source currency to convert from. + * @param toCurrency Target currency to convert to. + * @param value Value in source currency which should be converted. + * @return Return conversion result. + * @since version2 + * @throws ConversionNotSupportedException If conversion from fromCurrency to toCurrency + * is not supported and neither conversion from toCurrency to fromCurrency is not supported. + */ + public ConversionResult convertWithReversibleRates(ConvertorCurrency fromCurrency, ConvertorCurrency toCurrency, BigDecimal value) { + ConversionResult result = convertValue(fromCurrency, toCurrency, value, false,true); + if (result==null) { + //throw new ConversionNotSupportedException("Neither onversion nor reverted conversion from " + fromCurrency + " to " + toCurrency + " is not supported,"); + throw new ConversionNotSupportedException(fromCurrency.getCurrencyCode(),toCurrency.getCurrencyCode(),true); + } + return result; + } + +}