task3/solution04/src/org/apidesign/apifest08/currency/CompositeConvertorImpl.java
1.1 --- a/task3/solution04/src/org/apidesign/apifest08/currency/CompositeConvertorImpl.java Tue Oct 07 11:05:34 2008 +0200
1.2 +++ b/task3/solution04/src/org/apidesign/apifest08/currency/CompositeConvertorImpl.java Fri Oct 10 22:05:15 2008 +0200
1.3 @@ -16,7 +16,7 @@
1.4 * A composite convertor will build all possible conversions that are allowed by the underlying set of convertors.
1.5 *
1.6 * @author D'Arcy Smith
1.7 - * @verson 1.0
1.8 + * @verson 1.1
1.9 */
1.10 final class CompositeConvertorImpl
1.11 implements Convertor
1.12 @@ -220,6 +220,38 @@
1.13 return (convertor.getConversionRate(from, to));
1.14 }
1.15
1.16 + private Convertor getConvertor(final Currency from, final Currency to)
1.17 + throws InvalidConversionException
1.18 + {
1.19 + final Map<Currency, Convertor> possible;
1.20 + Convertor convertor;
1.21 +
1.22 + if(from == null)
1.23 + {
1.24 + throw new IllegalArgumentException("from cannot be null");
1.25 + }
1.26 +
1.27 + if(to == null)
1.28 + {
1.29 + throw new IllegalArgumentException("to cannot be null");
1.30 + }
1.31 +
1.32 + if(!(canConvert(from, to)))
1.33 + {
1.34 + throw new InvalidConversionException("cannot convert", to);
1.35 + }
1.36 +
1.37 + possible = possibleConversions.get(from);
1.38 + convertor = possible.get(to);
1.39 +
1.40 + if(convertor == null)
1.41 + {
1.42 + throw new Error();
1.43 + }
1.44 +
1.45 + return (convertor);
1.46 + }
1.47 +
1.48 /**
1.49 * Convert an amount from one currency to another.
1.50 *
1.51 @@ -236,6 +268,7 @@
1.52 throws InvalidConversionException
1.53 {
1.54 final BigDecimal result;
1.55 + final Convertor convertor;
1.56
1.57 if(amount == null)
1.58 {
1.59 @@ -252,7 +285,11 @@
1.60 throw new IllegalArgumentException("to cannot be null");
1.61 }
1.62
1.63 - result = amount.multiply(getConversionRate(from, to));
1.64 + // fixed a bug from Task2 that showed up in Task3... before we did the conversion here,
1.65 + // but that meant that the underlying covnerter convert method never got called... which
1.66 + // meant that in Task3 the exchange rate never changed.
1.67 + convertor = getConvertor(from, to);
1.68 + result = convertor.convert(from, to, amount);
1.69
1.70 return (result.setScale(2, RoundingMode.HALF_DOWN));
1.71 }
1.72 @@ -300,8 +337,7 @@
1.73
1.74 fromRate = fromIntermediary.getConversionRate(from, intermediary);
1.75 toRate = toIntermediary.getConversionRate(intermediary, to);
1.76 - rate = fromRate.multiply(toRate);
1.77 -
1.78 + rate = fromRate.multiply(toRate);
1.79 convertor = ConvertorFactory.getConvertor(from, BigDecimal.ONE, to, rate);
1.80
1.81 return (convertor);