task4/solution04/src/org/apidesign/apifest08/currency/ConvertorFactory.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 11 Oct 2008 23:38:46 +0200
changeset 61 58ec6da75f6f
parent 55 task3/solution04/src/org/apidesign/apifest08/currency/ConvertorFactory.java@14e78f48ac2b
child 69 420baec87dc5
permissions -rw-r--r--
Copying structure for task4
japod@6
     1
package org.apidesign.apifest08.currency;
japod@6
     2
japod@17
     3
import java.math.BigDecimal;
japod@6
     4
import java.util.Currency;
japod@6
     5
japod@6
     6
japod@6
     7
/**
japod@6
     8
 * Create convertors using a flyweight to reduce the number of repetative creations of the same convertor.
japod@6
     9
 * 
japod@6
    10
 * @author D'Arcy Smith
japod@6
    11
 * @version 1.0
japod@6
    12
 */
japod@6
    13
public final class ConvertorFactory
japod@6
    14
{
japod@35
    15
    /*
japod@6
    16
     * flyweight so that only one vestion of each converter is created at a time.
japod@6
    17
    private final static Map<String, WeakReference<Convertor>> convertors;
japod@6
    18
    
japod@6
    19
    static
japod@6
    20
    {
japod@6
    21
        convertors = new WeakHashMap<String, WeakReference<Convertor>>();
japod@6
    22
    }
japod@35
    23
    */
japod@6
    24
    
japod@6
    25
    /** 
japod@6
    26
     * Prevent accidental construction.
japod@6
    27
     */
japod@6
    28
    private ConvertorFactory()
japod@6
    29
    {        
japod@6
    30
    }
japod@6
    31
    
japod@6
    32
    /**
japod@6
    33
     * Get the convertor for the specified currencies.  The currency name format
japod@6
    34
     * must be acceptable to java.util.Currency.getInstance(String)
japod@6
    35
     * 
japod@6
    36
     * @param a the currency to convert from.
japod@17
    37
     * @param aRate the exchange rate for a to b.
japod@6
    38
     * @param b the currency to convert to.
japod@17
    39
     * @param bRate the echante rate for b to a.
japod@6
    40
     * @return the convertor for the specified currencies.
japod@17
    41
     * @throws IllegalArgumentException if any of the arguments are null.
japod@6
    42
     */
japod@17
    43
    public static Convertor getConvertor(final String     a,
japod@17
    44
                                         final BigDecimal aRate,
japod@17
    45
                                         final String     b,
japod@17
    46
                                         final BigDecimal bRate)
japod@6
    47
    {
japod@6
    48
        final Currency  currencyA;
japod@6
    49
        final Currency  currencyB;
japod@6
    50
        final Convertor convertor;
japod@6
    51
        
japod@6
    52
        currencyA = Currency.getInstance(a);
japod@6
    53
        currencyB = Currency.getInstance(b);        
japod@17
    54
        convertor = getConvertor(currencyA, aRate, currencyB, bRate);
japod@6
    55
        
japod@6
    56
        return (convertor);
japod@6
    57
    }
japod@6
    58
    
japod@6
    59
    /**
japod@6
    60
     * Get the convertor for the specified currencies.
japod@6
    61
     * 
japod@6
    62
     * @param a the currency to convert from.
japod@17
    63
     * @param aRate the exchange rate for a to b.
japod@6
    64
     * @param b the currency to convert to.
japod@17
    65
     * @param bRate the echante rate for b to a.
japod@6
    66
     * @return the convertor for the specified currencies.
japod@17
    67
     * @throws IllegalArgumentException if either any of the arguments are null or if either rate <= 0.
japod@6
    68
     */
japod@17
    69
    public static Convertor getConvertor(final Currency   a,
japod@17
    70
                                         final BigDecimal aRate,
japod@17
    71
                                         final Currency   b,
japod@17
    72
                                         final BigDecimal bRate)
japod@6
    73
    {
japod@35
    74
        // final String key;        
japod@6
    75
        Convertor    convertor;
japod@6
    76
japod@6
    77
        if(a == null)
japod@6
    78
        {
japod@6
    79
            throw new IllegalArgumentException("a cannot be null");
japod@6
    80
        }
japod@6
    81
japod@6
    82
        if(b == null)
japod@6
    83
        {
japod@6
    84
            throw new IllegalArgumentException("b cannot be null");
japod@6
    85
        }
japod@17
    86
        
japod@17
    87
        if(aRate == null)
japod@17
    88
        {
japod@17
    89
            throw new IllegalArgumentException("aRate cannot be null");
japod@17
    90
        }
japod@17
    91
        
japod@17
    92
        if(bRate == null)
japod@17
    93
        {
japod@17
    94
            throw new IllegalArgumentException("bRate cannot be null");
japod@17
    95
        }
japod@6
    96
japod@35
    97
        /*
japod@17
    98
        key = a.getCurrencyCode() + aRate + b.getCurrencyCode() + bRate;
japod@6
    99
japod@6
   100
        // make sure that we don't try to overwrite one
japod@6
   101
        synchronized(convertors)
japod@6
   102
        {
japod@6
   103
            if(!(convertors.containsKey(key)))
japod@6
   104
            {        
japod@17
   105
                convertor = new ConvertorImpl(a, aRate, b, bRate);
japod@6
   106
                convertors.put(key, new WeakReference(convertor));
japod@6
   107
            }
japod@35
   108
japod@35
   109
            convertor = convertors.get(key).get();
japod@6
   110
        }
japod@35
   111
        */
japod@35
   112
        
japod@35
   113
        convertor = new ConvertorImpl(a, aRate, b, bRate);
japod@35
   114
                
japod@35
   115
        return (convertor);
japod@35
   116
    }
japod@35
   117
    
japod@55
   118
    /**
japod@55
   119
     * 
japod@55
   120
     * @param cs
japod@55
   121
     * @return
japod@55
   122
     */
japod@35
   123
    public static Convertor mergeConvertors(final Convertor ... cs)
japod@35
   124
    {
japod@35
   125
        Convertor convertor;
japod@35
   126
        
japod@35
   127
        /*
japod@35
   128
        final String key;
japod@6
   129
japod@35
   130
        // ISSUE: only takes into account the names... not the rates...
japod@35
   131
        key = getKey(cs);
japod@35
   132
japod@35
   133
        // make sure that we don't try to overwrite one
japod@35
   134
        synchronized(convertors)
japod@35
   135
        {
japod@35
   136
            if(!(convertors.containsKey(key)))
japod@35
   137
            {        
japod@35
   138
                convertor = new CompositeConvertorImpl(cs);
japod@35
   139
                convertors.put(key, new WeakReference(convertor));
japod@35
   140
            }
japod@35
   141
japod@35
   142
            convertor = convertors.get(key).get();
japod@35
   143
        }
japod@35
   144
        */
japod@35
   145
        
japod@35
   146
        convertor = new CompositeConvertorImpl(cs);
japod@6
   147
        
japod@6
   148
        return (convertor);
japod@6
   149
    }
japod@35
   150
japod@35
   151
    /*
japod@35
   152
    private static String getKey(final Convertor ... cs)
japod@35
   153
    {
japod@35
   154
        final Set<Currency> currencies;
japod@35
   155
        final StringBuilder builder;
japod@35
   156
        
japod@35
   157
        currencies = new HashSet<Currency>();
japod@35
   158
        
japod@35
   159
        for(final Convertor convertor : cs)
japod@35
   160
        {
japod@35
   161
            final Set<Currency> c;
japod@35
   162
            
japod@35
   163
            c = convertor.getCurrencies();
japod@35
   164
            currencies.addAll(c);
japod@35
   165
        }
japod@35
   166
        
japod@35
   167
        builder = new StringBuilder();
japod@35
   168
japod@35
   169
        for(final Currency currency : currencies)
japod@35
   170
        {
japod@35
   171
            builder.append(currency.getCurrencyCode());
japod@35
   172
        }
japod@35
   173
        
japod@35
   174
        return (builder.toString());
japod@35
   175
    }
japod@35
   176
    */
japod@6
   177
}