task2/solution10/src/org/apidesign/apifest08/currency/MultiCurrencyConstantRateConverter.java
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/task2/solution10/src/org/apidesign/apifest08/currency/MultiCurrencyConstantRateConverter.java Wed Oct 01 10:43:05 2008 +0200
1.3 @@ -0,0 +1,71 @@
1.4 +package org.apidesign.apifest08.currency;
1.5 +
1.6 +import java.util.*;
1.7 +
1.8 +final class MultiCurrencyConstantRateConverter implements CurrencyConverter {
1.9 +
1.10 + private final Map<Currency, Double> rates;
1.11 +
1.12 + public MultiCurrencyConstantRateConverter(Map<Currency, Double> rates) {
1.13 + this.rates = rates;
1.14 + }
1.15 +
1.16 + /**
1.17 + * This is convenience method for convert(Currency.getInstance(from), Currency.getInstance(to)).
1.18 + *
1.19 + * @param value that should be converted form one currency to the other
1.20 + * @param from ISO-4217 code of the currency of the value provided
1.21 + * @param to ISO-4212 code of the currency to which the value should be converted
1.22 + *
1.23 + * @return value expressed in the target value
1.24 + *
1.25 + * @throws IllegalArgumentException if any of the arguments is not a valid ISO code
1.26 + * @throws CurrencyConversionException
1.27 + * if the conversion cannot be performed with desired parameters, for
1.28 + * example the exchange rates are not current, connection to exchange rates
1.29 + * provider is not available
1.30 + * @throws NullPointerException if any of the specified currency ISO codes is null
1.31 + */
1.32 + // this method is provided to ensure future compatibility for converters supporting more than 2 currencies
1.33 + // - simpler methods with fewer arguments would make using such converters less intuitive
1.34 + @Override
1.35 + public double convert(double value, /*@NotNull*/ String from, /*@NotNull*/ String to)
1.36 + throws CurrencyConversionException, NullPointerException, IllegalArgumentException {
1.37 + return convert(value, Currency.getInstance(from), Currency.getInstance(to));
1.38 + }
1.39 +
1.40 + /**
1.41 + * Converts the specified value from one currency (from) to target currency (to).
1.42 + *
1.43 + * @param value that should be converted form one currency to the other
1.44 + * @param from ISO-4217 code of the currency of the value provided
1.45 + * @param to ISO-4212 code of the currency to which the value should be converted
1.46 + *
1.47 + * @return value expressed in the target value
1.48 + *
1.49 + * @throws IllegalArgumentException if any of the arguments is not a valid ISO code
1.50 + * @throws CurrencyConversionException
1.51 + * if the conversion cannot be performed with desired parameters, for
1.52 + * example the exchange rates are not current, connection to exchange rates
1.53 + * provider is not available
1.54 + * @throws NullPointerException if any of the specified currency ISO codes is null
1.55 + */
1.56 + @Override
1.57 + public double convert(double value, /*@NotNull*/ Currency from, /*@NotNull*/ Currency to)
1.58 + throws CurrencyConversionException, NullPointerException {
1.59 +
1.60 + // this is not necessary, but we let users know that nulls are not allowed here - should be handled by annotations
1.61 + if (from == null || to == null)
1.62 + throw new NullPointerException("One of the specified currencies in null");
1.63 +
1.64 + Double fromRate = rates.get(from);
1.65 + Double toRate = rates.get(to);
1.66 +
1.67 + if (fromRate == null)
1.68 + throw new CurrencyConversionException(from, to, String.format("Currency %1$s not supported", from));
1.69 + if (toRate == null)
1.70 + throw new CurrencyConversionException(from, to, String.format("Currency %1$s not supported", to));
1.71 +
1.72 + return (value / fromRate) * toRate;
1.73 + }
1.74 +}