1 package org.apidesign.apifest08.currency;
5 final class ConstantRateConverter implements CurrencyConverter {
7 private final Currency from;
8 private final Currency to;
10 private final double rate;
12 public ConstantRateConverter(Currency from, Currency to, double rate) throws IllegalArgumentException {
13 if (from == null || to == null)
14 throw new NullPointerException("None of the currencies can be null");
16 throw new IllegalArgumentException("Cannot create converter with two equal currencies");
23 public double convert(double value, String from, String to)
24 throws CurrencyConversionException, NullPointerException {
25 return convert(value, Currency.getInstance(from), Currency.getInstance(to));
29 public double convert(double value, Currency from, Currency to)
30 throws NullPointerException, CurrencyConversionException {
32 if (this.from.equals(from)) {
33 if (!this.to.equals(to))
34 throw new CurrencyConversionException(from, to, "Unsupported currency");
37 if (this.from.equals(to)) {
38 if (!this.to.equals(from))
39 throw new CurrencyConversionException(from, to, "Unsupported currency");
42 throw new CurrencyConversionException(from, to, "Unsupported currency");