1.1 --- a/task4/solution04/src/org/apidesign/apifest08/currency/ConvertorFactory.java Sat Oct 11 23:38:46 2008 +0200
1.2 +++ b/task4/solution04/src/org/apidesign/apifest08/currency/ConvertorFactory.java Fri Oct 17 17:40:14 2008 +0200
1.3 @@ -2,26 +2,17 @@
1.4
1.5 import java.math.BigDecimal;
1.6 import java.util.Currency;
1.7 +import java.util.Date;
1.8
1.9
1.10 /**
1.11 * Create convertors using a flyweight to reduce the number of repetative creations of the same convertor.
1.12 *
1.13 * @author D'Arcy Smith
1.14 - * @version 1.0
1.15 + * @version 1.2
1.16 */
1.17 public final class ConvertorFactory
1.18 {
1.19 - /*
1.20 - * flyweight so that only one vestion of each converter is created at a time.
1.21 - private final static Map<String, WeakReference<Convertor>> convertors;
1.22 -
1.23 - static
1.24 - {
1.25 - convertors = new WeakHashMap<String, WeakReference<Convertor>>();
1.26 - }
1.27 - */
1.28 -
1.29 /**
1.30 * Prevent accidental construction.
1.31 */
1.32 @@ -71,8 +62,8 @@
1.33 final Currency b,
1.34 final BigDecimal bRate)
1.35 {
1.36 - // final String key;
1.37 - Convertor convertor;
1.38 + Convertor convertor;
1.39 + final ExchangeRate rate;
1.40
1.41 if(a == null)
1.42 {
1.43 @@ -94,27 +85,69 @@
1.44 throw new IllegalArgumentException("bRate cannot be null");
1.45 }
1.46
1.47 - /*
1.48 - key = a.getCurrencyCode() + aRate + b.getCurrencyCode() + bRate;
1.49 -
1.50 - // make sure that we don't try to overwrite one
1.51 - synchronized(convertors)
1.52 - {
1.53 - if(!(convertors.containsKey(key)))
1.54 - {
1.55 - convertor = new ConvertorImpl(a, aRate, b, bRate);
1.56 - convertors.put(key, new WeakReference(convertor));
1.57 - }
1.58 -
1.59 - convertor = convertors.get(key).get();
1.60 - }
1.61 - */
1.62 -
1.63 - convertor = new ConvertorImpl(a, aRate, b, bRate);
1.64 + rate = ExchangeRate.getExchangeRate(a, b, aRate, bRate);
1.65 + convertor = getConvertor(rate);
1.66
1.67 return (convertor);
1.68 }
1.69
1.70 + public static Convertor getConvertor(final ExchangeRate rate)
1.71 + {
1.72 + final ConvertorImpl convertor;
1.73 +
1.74 + if(rate == null)
1.75 + {
1.76 + throw new IllegalArgumentException("rate cannot be null");
1.77 + }
1.78 +
1.79 + convertor = new ConvertorImpl(rate);
1.80 +
1.81 + return (convertor);
1.82 + }
1.83 +
1.84 + public static DatedConvertor getConvertor(final Date from,
1.85 + final Date till,
1.86 + final Convertor convertor)
1.87 + {
1.88 + final DateRange range;
1.89 + final ExchangeRate rate;
1.90 + final DatedConvertor datedConvertor;
1.91 +
1.92 + if(from == null)
1.93 + {
1.94 + throw new IllegalArgumentException("from cannot be null");
1.95 + }
1.96 +
1.97 + if(till == null)
1.98 + {
1.99 + throw new IllegalArgumentException("till cannot be null");
1.100 + }
1.101 +
1.102 + if(convertor == null)
1.103 + {
1.104 + throw new IllegalArgumentException("convertor cannot be null");
1.105 + }
1.106 +
1.107 + if(from.after(till))
1.108 + {
1.109 + throw new IllegalArgumentException(from + " cannot be after " + till);
1.110 + }
1.111 +
1.112 + if(convertor instanceof ExchangeRateConvertor)
1.113 + {
1.114 + rate = ((ExchangeRateConvertor)convertor).getExchangeRate();
1.115 + }
1.116 + else
1.117 + {
1.118 + throw new Error();
1.119 + }
1.120 +
1.121 + range = new DateRange(from, till);
1.122 + datedConvertor = new DatedConvertorImpl(range, rate);
1.123 +
1.124 + return (datedConvertor);
1.125 + }
1.126 +
1.127 /**
1.128 *
1.129 * @param cs
1.130 @@ -123,55 +156,38 @@
1.131 public static Convertor mergeConvertors(final Convertor ... cs)
1.132 {
1.133 Convertor convertor;
1.134 -
1.135 - /*
1.136 - final String key;
1.137 + int dated;
1.138 + int nonDated;
1.139
1.140 - // ISSUE: only takes into account the names... not the rates...
1.141 - key = getKey(cs);
1.142 + dated = 0;
1.143 + nonDated = 0;
1.144
1.145 - // make sure that we don't try to overwrite one
1.146 - synchronized(convertors)
1.147 + for(final Convertor c : cs)
1.148 {
1.149 - if(!(convertors.containsKey(key)))
1.150 - {
1.151 - convertor = new CompositeConvertorImpl(cs);
1.152 - convertors.put(key, new WeakReference(convertor));
1.153 + if(c instanceof DatedConvertor)
1.154 + {
1.155 + dated++;
1.156 }
1.157 + else
1.158 + {
1.159 + nonDated++;
1.160 + }
1.161 + }
1.162
1.163 - convertor = convertors.get(key).get();
1.164 + if(dated != 0 && nonDated != 0)
1.165 + {
1.166 + throw new IllegalArgumentException("cannot mix DatedConvertors and non-DatedConvertors");
1.167 }
1.168 - */
1.169 -
1.170 - convertor = new CompositeConvertorImpl(cs);
1.171 -
1.172 +
1.173 + if(dated != 0)
1.174 + {
1.175 + convertor = new DatedCompositeConvertorImpl(cs);
1.176 + }
1.177 + else
1.178 + {
1.179 + convertor = new CompositeConvertorImpl(cs);
1.180 + }
1.181 +
1.182 return (convertor);
1.183 }
1.184 -
1.185 - /*
1.186 - private static String getKey(final Convertor ... cs)
1.187 - {
1.188 - final Set<Currency> currencies;
1.189 - final StringBuilder builder;
1.190 -
1.191 - currencies = new HashSet<Currency>();
1.192 -
1.193 - for(final Convertor convertor : cs)
1.194 - {
1.195 - final Set<Currency> c;
1.196 -
1.197 - c = convertor.getCurrencies();
1.198 - currencies.addAll(c);
1.199 - }
1.200 -
1.201 - builder = new StringBuilder();
1.202 -
1.203 - for(final Currency currency : currencies)
1.204 - {
1.205 - builder.append(currency.getCurrencyCode());
1.206 - }
1.207 -
1.208 - return (builder.toString());
1.209 - }
1.210 - */
1.211 }