task1/solution04/src/org/apidesign/apifest08/currency/ConvertorFactory.java
author japod@localhost
Sun, 28 Sep 2008 14:12:38 +0200
changeset 6 97662396c0fd
child 17 37c9921c653e
permissions -rw-r--r--
Adding solutions received for task1
     1 package org.apidesign.apifest08.currency;
     2 
     3 import java.lang.ref.WeakReference;
     4 import java.util.Currency;
     5 import java.util.Map;
     6 import java.util.WeakHashMap;
     7 
     8 
     9 /**
    10  * Create convertors using a flyweight to reduce the number of repetative creations of the same convertor.
    11  * 
    12  * @author D'Arcy Smith
    13  * @version 1.0
    14  */
    15 public final class ConvertorFactory
    16 {
    17     /**
    18      * flyweight so that only one vestion of each converter is created at a time.
    19      */
    20     private final static Map<String, WeakReference<Convertor>> convertors;
    21     
    22     static
    23     {
    24         convertors = new WeakHashMap<String, WeakReference<Convertor>>();
    25     }
    26     
    27     /** 
    28      * Prevent accidental construction.
    29      */
    30     private ConvertorFactory()
    31     {        
    32     }
    33     
    34     /**
    35      * Get the convertor for the specified currencies.  The currency name format
    36      * must be acceptable to java.util.Currency.getInstance(String)
    37      * 
    38      * @param a the currency to convert from.
    39      * @param b the currency to convert to.
    40      * @return the convertor for the specified currencies.
    41      */
    42     public static Convertor getConvertor(final String a,
    43                                          final String b)
    44     {
    45         final Currency  currencyA;
    46         final Currency  currencyB;
    47         final Convertor convertor;
    48         
    49         currencyA = Currency.getInstance(a);
    50         currencyB = Currency.getInstance(b);        
    51         convertor = getConvertor(currencyA, currencyB);
    52         
    53         return (convertor);
    54     }
    55     
    56     /**
    57      * Get the convertor for the specified currencies.
    58      * 
    59      * @param a the currency to convert from.
    60      * @param b the currency to convert to.
    61      * @return the convertor for the specified currencies.
    62      */
    63     public static Convertor getConvertor(final Currency a,
    64                                          final Currency b)
    65     {
    66         final String key;        
    67         Convertor    convertor;
    68 
    69         if(a == null)
    70         {
    71             throw new IllegalArgumentException("a cannot be null");
    72         }
    73 
    74         if(b == null)
    75         {
    76             throw new IllegalArgumentException("b cannot be null");
    77         }
    78 
    79         key = a.getCurrencyCode() + b.getCurrencyCode();
    80 
    81         // make sure that we don't try to overwrite one
    82         synchronized(convertors)
    83         {
    84             if(!(convertors.containsKey(key)))
    85             {        
    86                 convertor = new Convertor(a, b);
    87                 convertors.put(key, new WeakReference(convertor));
    88             }
    89         }
    90 
    91         convertor = convertors.get(key).get();
    92         
    93         return (convertor);
    94     }
    95 }