1.1 --- a/task2/solution04/src/org/apidesign/apifest08/currency/ConvertorFactory.java Wed Oct 01 10:43:05 2008 +0200
1.2 +++ b/task2/solution04/src/org/apidesign/apifest08/currency/ConvertorFactory.java Tue Oct 07 00:21:03 2008 +0200
1.3 @@ -1,10 +1,7 @@
1.4 package org.apidesign.apifest08.currency;
1.5
1.6 -import java.lang.ref.WeakReference;
1.7 import java.math.BigDecimal;
1.8 import java.util.Currency;
1.9 -import java.util.Map;
1.10 -import java.util.WeakHashMap;
1.11
1.12
1.13 /**
1.14 @@ -15,15 +12,15 @@
1.15 */
1.16 public final class ConvertorFactory
1.17 {
1.18 - /**
1.19 + /*
1.20 * flyweight so that only one vestion of each converter is created at a time.
1.21 - */
1.22 private final static Map<String, WeakReference<Convertor>> convertors;
1.23
1.24 static
1.25 {
1.26 convertors = new WeakHashMap<String, WeakReference<Convertor>>();
1.27 }
1.28 + */
1.29
1.30 /**
1.31 * Prevent accidental construction.
1.32 @@ -74,7 +71,7 @@
1.33 final Currency b,
1.34 final BigDecimal bRate)
1.35 {
1.36 - final String key;
1.37 + // final String key;
1.38 Convertor convertor;
1.39
1.40 if(a == null)
1.41 @@ -97,6 +94,7 @@
1.42 throw new IllegalArgumentException("bRate cannot be null");
1.43 }
1.44
1.45 + /*
1.46 key = a.getCurrencyCode() + aRate + b.getCurrencyCode() + bRate;
1.47
1.48 // make sure that we don't try to overwrite one
1.49 @@ -107,10 +105,68 @@
1.50 convertor = new ConvertorImpl(a, aRate, b, bRate);
1.51 convertors.put(key, new WeakReference(convertor));
1.52 }
1.53 +
1.54 + convertor = convertors.get(key).get();
1.55 }
1.56 + */
1.57 +
1.58 + convertor = new ConvertorImpl(a, aRate, b, bRate);
1.59 +
1.60 + return (convertor);
1.61 + }
1.62 +
1.63 + public static Convertor mergeConvertors(final Convertor ... cs)
1.64 + {
1.65 + Convertor convertor;
1.66 +
1.67 + /*
1.68 + final String key;
1.69
1.70 - convertor = convertors.get(key).get();
1.71 + // ISSUE: only takes into account the names... not the rates...
1.72 + key = getKey(cs);
1.73 +
1.74 + // make sure that we don't try to overwrite one
1.75 + synchronized(convertors)
1.76 + {
1.77 + if(!(convertors.containsKey(key)))
1.78 + {
1.79 + convertor = new CompositeConvertorImpl(cs);
1.80 + convertors.put(key, new WeakReference(convertor));
1.81 + }
1.82 +
1.83 + convertor = convertors.get(key).get();
1.84 + }
1.85 + */
1.86 +
1.87 + convertor = new CompositeConvertorImpl(cs);
1.88
1.89 return (convertor);
1.90 }
1.91 +
1.92 + /*
1.93 + private static String getKey(final Convertor ... cs)
1.94 + {
1.95 + final Set<Currency> currencies;
1.96 + final StringBuilder builder;
1.97 +
1.98 + currencies = new HashSet<Currency>();
1.99 +
1.100 + for(final Convertor convertor : cs)
1.101 + {
1.102 + final Set<Currency> c;
1.103 +
1.104 + c = convertor.getCurrencies();
1.105 + currencies.addAll(c);
1.106 + }
1.107 +
1.108 + builder = new StringBuilder();
1.109 +
1.110 + for(final Currency currency : currencies)
1.111 + {
1.112 + builder.append(currency.getCurrencyCode());
1.113 + }
1.114 +
1.115 + return (builder.toString());
1.116 + }
1.117 + */
1.118 }