task3/solution04/src/org/apidesign/apifest08/currency/ConvertorFactory.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 07 Oct 2008 11:05:34 +0200
changeset 45 251d0ed461fb
parent 35 task2/solution04/src/org/apidesign/apifest08/currency/ConvertorFactory.java@8898c620fe96
child 55 14e78f48ac2b
permissions -rw-r--r--
Copying all solution that advanced into round #3 into task3 directory
     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     public static Convertor mergeConvertors(final Convertor ... cs)
   119     {
   120         Convertor convertor;
   121         
   122         /*
   123         final String key;
   124 
   125         // ISSUE: only takes into account the names... not the rates...
   126         key = getKey(cs);
   127 
   128         // make sure that we don't try to overwrite one
   129         synchronized(convertors)
   130         {
   131             if(!(convertors.containsKey(key)))
   132             {        
   133                 convertor = new CompositeConvertorImpl(cs);
   134                 convertors.put(key, new WeakReference(convertor));
   135             }
   136 
   137             convertor = convertors.get(key).get();
   138         }
   139         */
   140         
   141         convertor = new CompositeConvertorImpl(cs);
   142         
   143         return (convertor);
   144     }
   145 
   146     /*
   147     private static String getKey(final Convertor ... cs)
   148     {
   149         final Set<Currency> currencies;
   150         final StringBuilder builder;
   151         
   152         currencies = new HashSet<Currency>();
   153         
   154         for(final Convertor convertor : cs)
   155         {
   156             final Set<Currency> c;
   157             
   158             c = convertor.getCurrencies();
   159             currencies.addAll(c);
   160         }
   161         
   162         builder = new StringBuilder();
   163 
   164         for(final Currency currency : currencies)
   165         {
   166             builder.append(currency.getCurrencyCode());
   167         }
   168         
   169         return (builder.toString());
   170     }
   171     */
   172 }