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 ConstantRateConverter implements CurrencyConverter {
|
japod@6
|
6 |
|
japod@6
|
7 |
private final Currency from;
|
japod@6
|
8 |
private final Currency to;
|
japod@6
|
9 |
|
japod@6
|
10 |
private final double rate;
|
japod@6
|
11 |
|
japod@6
|
12 |
public ConstantRateConverter(Currency from, Currency to, double rate) throws IllegalArgumentException {
|
japod@6
|
13 |
if (from == null || to == null)
|
japod@6
|
14 |
throw new NullPointerException("None of the currencies can be null");
|
japod@6
|
15 |
if (from.equals(to))
|
japod@6
|
16 |
throw new IllegalArgumentException("Cannot create converter with two equal currencies");
|
japod@6
|
17 |
this.from = from;
|
japod@6
|
18 |
this.to = to;
|
japod@6
|
19 |
this.rate = rate;
|
japod@6
|
20 |
}
|
japod@6
|
21 |
|
japod@6
|
22 |
@Override
|
japod@6
|
23 |
public double convert(double value, String from, String to)
|
japod@6
|
24 |
throws CurrencyConversionException, NullPointerException {
|
japod@6
|
25 |
return convert(value, Currency.getInstance(from), Currency.getInstance(to));
|
japod@6
|
26 |
}
|
japod@6
|
27 |
|
japod@6
|
28 |
@Override
|
japod@6
|
29 |
public double convert(double value, Currency from, Currency to)
|
japod@6
|
30 |
throws NullPointerException, CurrencyConversionException {
|
japod@6
|
31 |
|
japod@6
|
32 |
if (this.from.equals(from)) {
|
japod@6
|
33 |
if (!this.to.equals(to))
|
japod@6
|
34 |
throw new CurrencyConversionException(from, to, "Unsupported currency");
|
japod@6
|
35 |
return value * rate;
|
japod@6
|
36 |
}
|
japod@6
|
37 |
if (this.from.equals(to)) {
|
japod@6
|
38 |
if (!this.to.equals(from))
|
japod@6
|
39 |
throw new CurrencyConversionException(from, to, "Unsupported currency");
|
japod@6
|
40 |
return value / rate;
|
japod@6
|
41 |
}
|
japod@6
|
42 |
throw new CurrencyConversionException(from, to, "Unsupported currency");
|
japod@6
|
43 |
}
|
japod@6
|
44 |
}
|