1 package org.apidesign.apifest08.currency;
3 import java.math.BigDecimal;
4 import java.util.Currency;
9 * Create convertors using a flyweight to reduce the number of repetative creations of the same convertor.
11 * @author D'Arcy Smith
14 public final class ConvertorFactory
17 * Prevent accidental construction.
19 private ConvertorFactory()
24 * Get the convertor for the specified currencies. The currency name format
25 * must be acceptable to java.util.Currency.getInstance(String)
27 * @param a the currency to convert from.
28 * @param aRate the exchange rate for a to b.
29 * @param b the currency to convert to.
30 * @param bRate the echante rate for b to a.
31 * @return the convertor for the specified currencies.
32 * @throws IllegalArgumentException if any of the arguments are null.
34 public static Convertor getConvertor(final String a,
35 final BigDecimal aRate,
37 final BigDecimal bRate)
39 final Currency currencyA;
40 final Currency currencyB;
41 final Convertor convertor;
43 currencyA = Currency.getInstance(a);
44 currencyB = Currency.getInstance(b);
45 convertor = getConvertor(currencyA, aRate, currencyB, bRate);
51 * Get the convertor for the specified currencies.
53 * @param a the currency to convert from.
54 * @param aRate the exchange rate for a to b.
55 * @param b the currency to convert to.
56 * @param bRate the echante rate for b to a.
57 * @return the convertor for the specified currencies.
58 * @throws IllegalArgumentException if either any of the arguments are null or if either rate <= 0.
60 public static Convertor getConvertor(final Currency a,
61 final BigDecimal aRate,
63 final BigDecimal bRate)
66 final ExchangeRate rate;
70 throw new IllegalArgumentException("a cannot be null");
75 throw new IllegalArgumentException("b cannot be null");
80 throw new IllegalArgumentException("aRate cannot be null");
85 throw new IllegalArgumentException("bRate cannot be null");
88 rate = ExchangeRate.getExchangeRate(a, b, aRate, bRate);
89 convertor = getConvertor(rate);
94 public static Convertor getConvertor(final ExchangeRate rate)
96 final ConvertorImpl convertor;
100 throw new IllegalArgumentException("rate cannot be null");
103 convertor = new ConvertorImpl(rate);
108 public static DatedConvertor getConvertor(final Date from,
110 final Convertor convertor)
112 final DateRange range;
113 final ExchangeRate rate;
114 final DatedConvertor datedConvertor;
118 throw new IllegalArgumentException("from cannot be null");
123 throw new IllegalArgumentException("till cannot be null");
126 if(convertor == null)
128 throw new IllegalArgumentException("convertor cannot be null");
133 throw new IllegalArgumentException(from + " cannot be after " + till);
136 if(convertor instanceof ExchangeRateConvertor)
138 rate = ((ExchangeRateConvertor)convertor).getExchangeRate();
145 range = new DateRange(from, till);
146 datedConvertor = new DatedConvertorImpl(range, rate);
148 return (datedConvertor);
156 public static Convertor mergeConvertors(final Convertor ... cs)
165 for(final Convertor c : cs)
167 if(c instanceof DatedConvertor)
177 if(dated != 0 && nonDated != 0)
179 throw new IllegalArgumentException("cannot mix DatedConvertors and non-DatedConvertors");
184 convertor = new DatedCompositeConvertorImpl(cs);
188 convertor = new CompositeConvertorImpl(cs);