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