task4/solution04/src/org/apidesign/apifest08/currency/ConvertorFactory.java
changeset 61 58ec6da75f6f
parent 55 14e78f48ac2b
child 69 420baec87dc5
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/task4/solution04/src/org/apidesign/apifest08/currency/ConvertorFactory.java	Sat Oct 11 23:38:46 2008 +0200
     1.3 @@ -0,0 +1,177 @@
     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 +    /**
   1.122 +     * 
   1.123 +     * @param cs
   1.124 +     * @return
   1.125 +     */
   1.126 +    public static Convertor mergeConvertors(final Convertor ... cs)
   1.127 +    {
   1.128 +        Convertor convertor;
   1.129 +        
   1.130 +        /*
   1.131 +        final String key;
   1.132 +
   1.133 +        // ISSUE: only takes into account the names... not the rates...
   1.134 +        key = getKey(cs);
   1.135 +
   1.136 +        // make sure that we don't try to overwrite one
   1.137 +        synchronized(convertors)
   1.138 +        {
   1.139 +            if(!(convertors.containsKey(key)))
   1.140 +            {        
   1.141 +                convertor = new CompositeConvertorImpl(cs);
   1.142 +                convertors.put(key, new WeakReference(convertor));
   1.143 +            }
   1.144 +
   1.145 +            convertor = convertors.get(key).get();
   1.146 +        }
   1.147 +        */
   1.148 +        
   1.149 +        convertor = new CompositeConvertorImpl(cs);
   1.150 +        
   1.151 +        return (convertor);
   1.152 +    }
   1.153 +
   1.154 +    /*
   1.155 +    private static String getKey(final Convertor ... cs)
   1.156 +    {
   1.157 +        final Set<Currency> currencies;
   1.158 +        final StringBuilder builder;
   1.159 +        
   1.160 +        currencies = new HashSet<Currency>();
   1.161 +        
   1.162 +        for(final Convertor convertor : cs)
   1.163 +        {
   1.164 +            final Set<Currency> c;
   1.165 +            
   1.166 +            c = convertor.getCurrencies();
   1.167 +            currencies.addAll(c);
   1.168 +        }
   1.169 +        
   1.170 +        builder = new StringBuilder();
   1.171 +
   1.172 +        for(final Currency currency : currencies)
   1.173 +        {
   1.174 +            builder.append(currency.getCurrencyCode());
   1.175 +        }
   1.176 +        
   1.177 +        return (builder.toString());
   1.178 +    }
   1.179 +    */
   1.180 +}