task2/solution10/src/org/apidesign/apifest08/currency/MultiCurrencyConstantRateConverter.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 01 Oct 2008 10:43:05 +0200
changeset 29 f6073056b9fe
parent 6 task1/solution10/src/org/apidesign/apifest08/currency/MultiCurrencyConstantRateConverter.java@97662396c0fd
permissions -rw-r--r--
Getting ready for task2: copying all solutions to new locations
     1 package org.apidesign.apifest08.currency;
     2 
     3 import java.util.*;
     4 
     5 final class MultiCurrencyConstantRateConverter implements CurrencyConverter {
     6 
     7 	private final Map<Currency, Double> rates;
     8 
     9 	public MultiCurrencyConstantRateConverter(Map<Currency, Double> rates) {
    10 		this.rates = rates;
    11 	}
    12 
    13 	/**
    14 	 * This is convenience method for convert(Currency.getInstance(from), Currency.getInstance(to)).
    15 	 *
    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
    19 	 *
    20 	 * @return value expressed in the target value
    21 	 *
    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
    28 	 */
    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
    31 	@Override
    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));
    35 	}
    36 
    37 	/**
    38 	 * Converts the specified value from one currency (from) to target currency (to).
    39 	 *
    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
    43 	 *
    44 	 * @return value expressed in the target value
    45 	 *
    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
    52 	 */
    53 	@Override
    54 	public double convert(double value, /*@NotNull*/ Currency from, /*@NotNull*/ Currency to)
    55 			throws CurrencyConversionException, NullPointerException {
    56 
    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");
    60 
    61 		Double fromRate = rates.get(from);
    62 		Double toRate = rates.get(to);
    63 
    64 		if (fromRate == null)
    65 			throw new CurrencyConversionException(from, to, String.format("Currency %1$s not supported", from));
    66 		if (toRate == null)
    67 			throw new CurrencyConversionException(from, to, String.format("Currency %1$s not supported", to));
    68 
    69 		return (value / fromRate) * toRate;
    70 	}
    71 }