task2/solution04/src/org/apidesign/apifest08/currency/ConvertorFactory.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 01 Oct 2008 10:43:05 +0200
changeset 29 f6073056b9fe
parent 17 task1/solution04/src/org/apidesign/apifest08/currency/ConvertorFactory.java@37c9921c653e
child 35 8898c620fe96
permissions -rw-r--r--
Getting ready for task2: copying all solutions to new locations
     1 package org.apidesign.apifest08.currency;
     2 
     3 import java.lang.ref.WeakReference;
     4 import java.math.BigDecimal;
     5 import java.util.Currency;
     6 import java.util.Map;
     7 import java.util.WeakHashMap;
     8 
     9 
    10 /**
    11  * Create convertors using a flyweight to reduce the number of repetative creations of the same convertor.
    12  * 
    13  * @author D'Arcy Smith
    14  * @version 1.0
    15  */
    16 public final class ConvertorFactory
    17 {
    18     /**
    19      * flyweight so that only one vestion of each converter is created at a time.
    20      */
    21     private final static Map<String, WeakReference<Convertor>> convertors;
    22     
    23     static
    24     {
    25         convertors = new WeakHashMap<String, WeakReference<Convertor>>();
    26     }
    27     
    28     /** 
    29      * Prevent accidental construction.
    30      */
    31     private ConvertorFactory()
    32     {        
    33     }
    34     
    35     /**
    36      * Get the convertor for the specified currencies.  The currency name format
    37      * must be acceptable to java.util.Currency.getInstance(String)
    38      * 
    39      * @param a the currency to convert from.
    40      * @param aRate the exchange rate for a to b.
    41      * @param b the currency to convert to.
    42      * @param bRate the echante rate for b to a.
    43      * @return the convertor for the specified currencies.
    44      * @throws IllegalArgumentException if any of the arguments are null.
    45      */
    46     public static Convertor getConvertor(final String     a,
    47                                          final BigDecimal aRate,
    48                                          final String     b,
    49                                          final BigDecimal bRate)
    50     {
    51         final Currency  currencyA;
    52         final Currency  currencyB;
    53         final Convertor convertor;
    54         
    55         currencyA = Currency.getInstance(a);
    56         currencyB = Currency.getInstance(b);        
    57         convertor = getConvertor(currencyA, aRate, currencyB, bRate);
    58         
    59         return (convertor);
    60     }
    61     
    62     /**
    63      * Get the convertor for the specified currencies.
    64      * 
    65      * @param a the currency to convert from.
    66      * @param aRate the exchange rate for a to b.
    67      * @param b the currency to convert to.
    68      * @param bRate the echante rate for b to a.
    69      * @return the convertor for the specified currencies.
    70      * @throws IllegalArgumentException if either any of the arguments are null or if either rate <= 0.
    71      */
    72     public static Convertor getConvertor(final Currency   a,
    73                                          final BigDecimal aRate,
    74                                          final Currency   b,
    75                                          final BigDecimal bRate)
    76     {
    77         final String key;        
    78         Convertor    convertor;
    79 
    80         if(a == null)
    81         {
    82             throw new IllegalArgumentException("a cannot be null");
    83         }
    84 
    85         if(b == null)
    86         {
    87             throw new IllegalArgumentException("b cannot be null");
    88         }
    89         
    90         if(aRate == null)
    91         {
    92             throw new IllegalArgumentException("aRate cannot be null");
    93         }
    94         
    95         if(bRate == null)
    96         {
    97             throw new IllegalArgumentException("bRate cannot be null");
    98         }
    99 
   100         key = a.getCurrencyCode() + aRate + b.getCurrencyCode() + bRate;
   101 
   102         // make sure that we don't try to overwrite one
   103         synchronized(convertors)
   104         {
   105             if(!(convertors.containsKey(key)))
   106             {        
   107                 convertor = new ConvertorImpl(a, aRate, b, bRate);
   108                 convertors.put(key, new WeakReference(convertor));
   109             }
   110         }
   111 
   112         convertor = convertors.get(key).get();
   113         
   114         return (convertor);
   115     }
   116 }