task1/solution10/src/org/apidesign/apifest08/currency/MultiCurrencyConstantRateConverter.java
1 package org.apidesign.apifest08.currency;
5 final class MultiCurrencyConstantRateConverter implements CurrencyConverter {
7 private final Map<Currency, Double> rates;
9 public MultiCurrencyConstantRateConverter(Map<Currency, Double> rates) {
14 * This is convenience method for convert(Currency.getInstance(from), Currency.getInstance(to)).
16 * @param value that should be converted form one currency to the other
17 * @param from ISO-4217 code of the currency of the value provided
18 * @param to ISO-4212 code of the currency to which the value should be converted
20 * @return value expressed in the target value
22 * @throws IllegalArgumentException if any of the arguments is not a valid ISO code
23 * @throws CurrencyConversionException
24 * if the conversion cannot be performed with desired parameters, for
25 * example the exchange rates are not current, connection to exchange rates
26 * provider is not available
27 * @throws NullPointerException if any of the specified currency ISO codes is null
29 // this method is provided to ensure future compatibility for converters supporting more than 2 currencies
30 // - simpler methods with fewer arguments would make using such converters less intuitive
32 public double convert(double value, /*@NotNull*/ String from, /*@NotNull*/ String to)
33 throws CurrencyConversionException, NullPointerException, IllegalArgumentException {
34 return convert(value, Currency.getInstance(from), Currency.getInstance(to));
38 * Converts the specified value from one currency (from) to target currency (to).
40 * @param value that should be converted form one currency to the other
41 * @param from ISO-4217 code of the currency of the value provided
42 * @param to ISO-4212 code of the currency to which the value should be converted
44 * @return value expressed in the target value
46 * @throws IllegalArgumentException if any of the arguments is not a valid ISO code
47 * @throws CurrencyConversionException
48 * if the conversion cannot be performed with desired parameters, for
49 * example the exchange rates are not current, connection to exchange rates
50 * provider is not available
51 * @throws NullPointerException if any of the specified currency ISO codes is null
54 public double convert(double value, /*@NotNull*/ Currency from, /*@NotNull*/ Currency to)
55 throws CurrencyConversionException, NullPointerException {
57 // this is not necessary, but we let users know that nulls are not allowed here - should be handled by annotations
58 if (from == null || to == null)
59 throw new NullPointerException("One of the specified currencies in null");
61 Double fromRate = rates.get(from);
62 Double toRate = rates.get(to);
65 throw new CurrencyConversionException(from, to, String.format("Currency %1$s not supported", from));
67 throw new CurrencyConversionException(from, to, String.format("Currency %1$s not supported", to));
69 return (value / fromRate) * toRate;