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