diff -r f6073056b9fe -r a7e6f84fb078 task2/solution13/src/org/apidesign/apifest08/currency/Convertor.java --- a/task2/solution13/src/org/apidesign/apifest08/currency/Convertor.java Wed Oct 01 10:43:05 2008 +0200 +++ b/task2/solution13/src/org/apidesign/apifest08/currency/Convertor.java Tue Oct 07 01:18:23 2008 +0200 @@ -5,23 +5,54 @@ 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 { - boolean remainderAllowed = false; //if false, remained is not allowed (should be true ideally, but can't handle it now) + 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; } /** - * Static method used to create convertor. + * 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(); @@ -36,9 +67,11 @@ * * @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(amount, false); + return convertValue(exchangeRateProvider.getFromCurrency(), exchangeRateProvider.getToCurrency(),amount, false,false); } /** @@ -47,32 +80,44 @@ * * @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(amount, true); + return convertValue(exchangeRateProvider.getFromCurrency(), exchangeRateProvider.getToCurrency(),amount, true,false); } - private ConversionResult convertValue(BigDecimal amount, boolean convertBack) throws RuntimeException { + private ConversionResult convertUsingSimpleConvertor(ConvertorCurrency fromCurrency, ConvertorCurrency toCurrency, boolean reversibleExRate, BigDecimal amount, boolean convertBack) throws ConversionNotSupportedException, RuntimeException { ConversionResult result = new ConversionResult(); - - ExchangeRate rate = exchangeRateProvider.getExchangeRate(); - int fromFranctionDigits = exchangeRateProvider.getFromCurrency().getDefaultFractionDigits(); - int toFractionDigits = exchangeRateProvider.getToCurrency().getDefaultFractionDigits(); - - if (toFractionDigits!=2) { - throw new RuntimeException("Can't process currency with defaultFractionDigits!=2, "+exchangeRateProvider.getToCurrency()+" has "+toFractionDigits+" defaultFractionDigits"); + + //ExchangeRate rate = exchangeRateProvider.getExchangeRate(); + ExchangeRate rate; + if (reversibleExRate) { + rate = exchangeRateProvider.getReversibleExchangeRate(fromCurrency, toCurrency); + } else { + rate = exchangeRateProvider.getExchangeRate(fromCurrency, toCurrency); } - if (fromFranctionDigits!=2) { - throw new RuntimeException("Can't process currency with defaultFractionDigits!=2, "+exchangeRateProvider.getFromCurrency()+" has "+fromFranctionDigits+" defaultFractionDigits"); + if (rate == null) { + return null; } - if (amount.signum()==-1) { - throw new RuntimeException("Can convert only non-negative value, current value is "+amount); + 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) { @@ -80,26 +125,89 @@ to = rate.getFromValue(); from = rate.getToValue(); } else { - //converting in mornak way + //converting in normal way from = rate.getFromValue(); to = rate.getToValue(); } BigDecimal amountCent = amount.movePointRight(2); - - final BigDecimal multiplied = amountCent.multiply(to,context); - BigDecimal[] division = multiplied.divideAndRemainder(from,context); + + 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"); + throw new RuntimeException("Remained is not allowed - remaining amount is " + division[1] + " cents"); } else { result.setRemainder(BigDecimal.ZERO); } - - final BigDecimal converted = division[0].movePointLeft(2); + + final BigDecimal converted = division[0].movePointLeft(2); 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; + } + }