1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/task2/solution04/src/org/apidesign/apifest08/currency/ConvertorFactory.java Wed Oct 01 10:43:05 2008 +0200
1.3 @@ -0,0 +1,116 @@
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 + * Create convertors using a flyweight to reduce the number of repetative creations of the same convertor.
1.15 + *
1.16 + * @author D'Arcy Smith
1.17 + * @version 1.0
1.18 + */
1.19 +public final class ConvertorFactory
1.20 +{
1.21 + /**
1.22 + * flyweight so that only one vestion of each converter is created at a time.
1.23 + */
1.24 + private final static Map<String, WeakReference<Convertor>> convertors;
1.25 +
1.26 + static
1.27 + {
1.28 + convertors = new WeakHashMap<String, WeakReference<Convertor>>();
1.29 + }
1.30 +
1.31 + /**
1.32 + * Prevent accidental construction.
1.33 + */
1.34 + private ConvertorFactory()
1.35 + {
1.36 + }
1.37 +
1.38 + /**
1.39 + * Get the convertor for the specified currencies. The currency name format
1.40 + * must be acceptable to java.util.Currency.getInstance(String)
1.41 + *
1.42 + * @param a the currency to convert from.
1.43 + * @param aRate the exchange rate for a to b.
1.44 + * @param b the currency to convert to.
1.45 + * @param bRate the echante rate for b to a.
1.46 + * @return the convertor for the specified currencies.
1.47 + * @throws IllegalArgumentException if any of the arguments are null.
1.48 + */
1.49 + public static Convertor getConvertor(final String a,
1.50 + final BigDecimal aRate,
1.51 + final String b,
1.52 + final BigDecimal bRate)
1.53 + {
1.54 + final Currency currencyA;
1.55 + final Currency currencyB;
1.56 + final Convertor convertor;
1.57 +
1.58 + currencyA = Currency.getInstance(a);
1.59 + currencyB = Currency.getInstance(b);
1.60 + convertor = getConvertor(currencyA, aRate, currencyB, bRate);
1.61 +
1.62 + return (convertor);
1.63 + }
1.64 +
1.65 + /**
1.66 + * Get the convertor for the specified currencies.
1.67 + *
1.68 + * @param a the currency to convert from.
1.69 + * @param aRate the exchange rate for a to b.
1.70 + * @param b the currency to convert to.
1.71 + * @param bRate the echante rate for b to a.
1.72 + * @return the convertor for the specified currencies.
1.73 + * @throws IllegalArgumentException if either any of the arguments are null or if either rate <= 0.
1.74 + */
1.75 + public static Convertor getConvertor(final Currency a,
1.76 + final BigDecimal aRate,
1.77 + final Currency b,
1.78 + final BigDecimal bRate)
1.79 + {
1.80 + final String key;
1.81 + Convertor convertor;
1.82 +
1.83 + if(a == null)
1.84 + {
1.85 + throw new IllegalArgumentException("a cannot be null");
1.86 + }
1.87 +
1.88 + if(b == null)
1.89 + {
1.90 + throw new IllegalArgumentException("b cannot be null");
1.91 + }
1.92 +
1.93 + if(aRate == null)
1.94 + {
1.95 + throw new IllegalArgumentException("aRate cannot be null");
1.96 + }
1.97 +
1.98 + if(bRate == null)
1.99 + {
1.100 + throw new IllegalArgumentException("bRate cannot be null");
1.101 + }
1.102 +
1.103 + key = a.getCurrencyCode() + aRate + b.getCurrencyCode() + bRate;
1.104 +
1.105 + // make sure that we don't try to overwrite one
1.106 + synchronized(convertors)
1.107 + {
1.108 + if(!(convertors.containsKey(key)))
1.109 + {
1.110 + convertor = new ConvertorImpl(a, aRate, b, bRate);
1.111 + convertors.put(key, new WeakReference(convertor));
1.112 + }
1.113 + }
1.114 +
1.115 + convertor = convertors.get(key).get();
1.116 +
1.117 + return (convertor);
1.118 + }
1.119 +}