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