diff -r 14e78f48ac2b -r 58ec6da75f6f task4/solution04/src/org/apidesign/apifest08/currency/ConvertorFactory.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/task4/solution04/src/org/apidesign/apifest08/currency/ConvertorFactory.java Sat Oct 11 23:38:46 2008 +0200 @@ -0,0 +1,177 @@ +package org.apidesign.apifest08.currency; + +import java.math.BigDecimal; +import java.util.Currency; + + +/** + * Create convertors using a flyweight to reduce the number of repetative creations of the same convertor. + * + * @author D'Arcy Smith + * @version 1.0 + */ +public final class ConvertorFactory +{ + /* + * flyweight so that only one vestion of each converter is created at a time. + private final static Map> convertors; + + static + { + convertors = new WeakHashMap>(); + } + */ + + /** + * Prevent accidental construction. + */ + private ConvertorFactory() + { + } + + /** + * Get the convertor for the specified currencies. The currency name format + * must be acceptable to java.util.Currency.getInstance(String) + * + * @param a the currency to convert from. + * @param aRate the exchange rate for a to b. + * @param b the currency to convert to. + * @param bRate the echante rate for b to a. + * @return the convertor for the specified currencies. + * @throws IllegalArgumentException if any of the arguments are null. + */ + public static Convertor getConvertor(final String a, + final BigDecimal aRate, + final String b, + final BigDecimal bRate) + { + final Currency currencyA; + final Currency currencyB; + final Convertor convertor; + + currencyA = Currency.getInstance(a); + currencyB = Currency.getInstance(b); + convertor = getConvertor(currencyA, aRate, currencyB, bRate); + + return (convertor); + } + + /** + * Get the convertor for the specified currencies. + * + * @param a the currency to convert from. + * @param aRate the exchange rate for a to b. + * @param b the currency to convert to. + * @param bRate the echante rate for b to a. + * @return the convertor for the specified currencies. + * @throws IllegalArgumentException if either any of the arguments are null or if either rate <= 0. + */ + public static Convertor getConvertor(final Currency a, + final BigDecimal aRate, + final Currency b, + final BigDecimal bRate) + { + // final String key; + Convertor convertor; + + if(a == null) + { + throw new IllegalArgumentException("a cannot be null"); + } + + if(b == null) + { + throw new IllegalArgumentException("b cannot be null"); + } + + if(aRate == null) + { + throw new IllegalArgumentException("aRate cannot be null"); + } + + if(bRate == null) + { + throw new IllegalArgumentException("bRate cannot be null"); + } + + /* + key = a.getCurrencyCode() + aRate + b.getCurrencyCode() + bRate; + + // make sure that we don't try to overwrite one + synchronized(convertors) + { + if(!(convertors.containsKey(key))) + { + convertor = new ConvertorImpl(a, aRate, b, bRate); + convertors.put(key, new WeakReference(convertor)); + } + + convertor = convertors.get(key).get(); + } + */ + + convertor = new ConvertorImpl(a, aRate, b, bRate); + + return (convertor); + } + + /** + * + * @param cs + * @return + */ + public static Convertor mergeConvertors(final Convertor ... cs) + { + Convertor convertor; + + /* + final String key; + + // ISSUE: only takes into account the names... not the rates... + key = getKey(cs); + + // make sure that we don't try to overwrite one + synchronized(convertors) + { + if(!(convertors.containsKey(key))) + { + convertor = new CompositeConvertorImpl(cs); + convertors.put(key, new WeakReference(convertor)); + } + + convertor = convertors.get(key).get(); + } + */ + + convertor = new CompositeConvertorImpl(cs); + + return (convertor); + } + + /* + private static String getKey(final Convertor ... cs) + { + final Set currencies; + final StringBuilder builder; + + currencies = new HashSet(); + + for(final Convertor convertor : cs) + { + final Set c; + + c = convertor.getCurrencies(); + currencies.addAll(c); + } + + builder = new StringBuilder(); + + for(final Currency currency : currencies) + { + builder.append(currency.getCurrencyCode()); + } + + return (builder.toString()); + } + */ +}