1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/task1/solution10/src/org/apidesign/apifest08/currency/ConstantRateConverter.java Sun Sep 28 14:12:38 2008 +0200
1.3 @@ -0,0 +1,44 @@
1.4 +package org.apidesign.apifest08.currency;
1.5 +
1.6 +import java.util.*;
1.7 +
1.8 +final class ConstantRateConverter implements CurrencyConverter {
1.9 +
1.10 + private final Currency from;
1.11 + private final Currency to;
1.12 +
1.13 + private final double rate;
1.14 +
1.15 + public ConstantRateConverter(Currency from, Currency to, double rate) throws IllegalArgumentException {
1.16 + if (from == null || to == null)
1.17 + throw new NullPointerException("None of the currencies can be null");
1.18 + if (from.equals(to))
1.19 + throw new IllegalArgumentException("Cannot create converter with two equal currencies");
1.20 + this.from = from;
1.21 + this.to = to;
1.22 + this.rate = rate;
1.23 + }
1.24 +
1.25 + @Override
1.26 + public double convert(double value, String from, String to)
1.27 + throws CurrencyConversionException, NullPointerException {
1.28 + return convert(value, Currency.getInstance(from), Currency.getInstance(to));
1.29 + }
1.30 +
1.31 + @Override
1.32 + public double convert(double value, Currency from, Currency to)
1.33 + throws NullPointerException, CurrencyConversionException {
1.34 +
1.35 + if (this.from.equals(from)) {
1.36 + if (!this.to.equals(to))
1.37 + throw new CurrencyConversionException(from, to, "Unsupported currency");
1.38 + return value * rate;
1.39 + }
1.40 + if (this.from.equals(to)) {
1.41 + if (!this.to.equals(from))
1.42 + throw new CurrencyConversionException(from, to, "Unsupported currency");
1.43 + return value / rate;
1.44 + }
1.45 + throw new CurrencyConversionException(from, to, "Unsupported currency");
1.46 + }
1.47 +}