1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/task3/solution04/src/org/apidesign/apifest08/currency/ConvertorFactory.java Tue Oct 07 11:05:34 2008 +0200
1.3 @@ -0,0 +1,172 @@
1.4 +package org.apidesign.apifest08.currency;
1.5 +
1.6 +import java.math.BigDecimal;
1.7 +import java.util.Currency;
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 + */
1.16 +public final class ConvertorFactory
1.17 +{
1.18 + /*
1.19 + * flyweight so that only one vestion of each converter is created at a time.
1.20 + private final static Map<String, WeakReference<Convertor>> convertors;
1.21 +
1.22 + static
1.23 + {
1.24 + convertors = new WeakHashMap<String, WeakReference<Convertor>>();
1.25 + }
1.26 + */
1.27 +
1.28 + /**
1.29 + * Prevent accidental construction.
1.30 + */
1.31 + private ConvertorFactory()
1.32 + {
1.33 + }
1.34 +
1.35 + /**
1.36 + * Get the convertor for the specified currencies. The currency name format
1.37 + * must be acceptable to java.util.Currency.getInstance(String)
1.38 + *
1.39 + * @param a the currency to convert from.
1.40 + * @param aRate the exchange rate for a to b.
1.41 + * @param b the currency to convert to.
1.42 + * @param bRate the echante rate for b to a.
1.43 + * @return the convertor for the specified currencies.
1.44 + * @throws IllegalArgumentException if any of the arguments are null.
1.45 + */
1.46 + public static Convertor getConvertor(final String a,
1.47 + final BigDecimal aRate,
1.48 + final String b,
1.49 + final BigDecimal bRate)
1.50 + {
1.51 + final Currency currencyA;
1.52 + final Currency currencyB;
1.53 + final Convertor convertor;
1.54 +
1.55 + currencyA = Currency.getInstance(a);
1.56 + currencyB = Currency.getInstance(b);
1.57 + convertor = getConvertor(currencyA, aRate, currencyB, bRate);
1.58 +
1.59 + return (convertor);
1.60 + }
1.61 +
1.62 + /**
1.63 + * Get the convertor for the specified currencies.
1.64 + *
1.65 + * @param a the currency to convert from.
1.66 + * @param aRate the exchange rate for a to b.
1.67 + * @param b the currency to convert to.
1.68 + * @param bRate the echante rate for b to a.
1.69 + * @return the convertor for the specified currencies.
1.70 + * @throws IllegalArgumentException if either any of the arguments are null or if either rate <= 0.
1.71 + */
1.72 + public static Convertor getConvertor(final Currency a,
1.73 + final BigDecimal aRate,
1.74 + final Currency b,
1.75 + final BigDecimal bRate)
1.76 + {
1.77 + // final String key;
1.78 + Convertor convertor;
1.79 +
1.80 + if(a == null)
1.81 + {
1.82 + throw new IllegalArgumentException("a cannot be null");
1.83 + }
1.84 +
1.85 + if(b == null)
1.86 + {
1.87 + throw new IllegalArgumentException("b cannot be null");
1.88 + }
1.89 +
1.90 + if(aRate == null)
1.91 + {
1.92 + throw new IllegalArgumentException("aRate cannot be null");
1.93 + }
1.94 +
1.95 + if(bRate == null)
1.96 + {
1.97 + throw new IllegalArgumentException("bRate cannot be null");
1.98 + }
1.99 +
1.100 + /*
1.101 + key = a.getCurrencyCode() + aRate + b.getCurrencyCode() + bRate;
1.102 +
1.103 + // make sure that we don't try to overwrite one
1.104 + synchronized(convertors)
1.105 + {
1.106 + if(!(convertors.containsKey(key)))
1.107 + {
1.108 + convertor = new ConvertorImpl(a, aRate, b, bRate);
1.109 + convertors.put(key, new WeakReference(convertor));
1.110 + }
1.111 +
1.112 + convertor = convertors.get(key).get();
1.113 + }
1.114 + */
1.115 +
1.116 + convertor = new ConvertorImpl(a, aRate, b, bRate);
1.117 +
1.118 + return (convertor);
1.119 + }
1.120 +
1.121 + public static Convertor mergeConvertors(final Convertor ... cs)
1.122 + {
1.123 + Convertor convertor;
1.124 +
1.125 + /*
1.126 + final String key;
1.127 +
1.128 + // ISSUE: only takes into account the names... not the rates...
1.129 + key = getKey(cs);
1.130 +
1.131 + // make sure that we don't try to overwrite one
1.132 + synchronized(convertors)
1.133 + {
1.134 + if(!(convertors.containsKey(key)))
1.135 + {
1.136 + convertor = new CompositeConvertorImpl(cs);
1.137 + convertors.put(key, new WeakReference(convertor));
1.138 + }
1.139 +
1.140 + convertor = convertors.get(key).get();
1.141 + }
1.142 + */
1.143 +
1.144 + convertor = new CompositeConvertorImpl(cs);
1.145 +
1.146 + return (convertor);
1.147 + }
1.148 +
1.149 + /*
1.150 + private static String getKey(final Convertor ... cs)
1.151 + {
1.152 + final Set<Currency> currencies;
1.153 + final StringBuilder builder;
1.154 +
1.155 + currencies = new HashSet<Currency>();
1.156 +
1.157 + for(final Convertor convertor : cs)
1.158 + {
1.159 + final Set<Currency> c;
1.160 +
1.161 + c = convertor.getCurrencies();
1.162 + currencies.addAll(c);
1.163 + }
1.164 +
1.165 + builder = new StringBuilder();
1.166 +
1.167 + for(final Currency currency : currencies)
1.168 + {
1.169 + builder.append(currency.getCurrencyCode());
1.170 + }
1.171 +
1.172 + return (builder.toString());
1.173 + }
1.174 + */
1.175 +}