task1/solution04/src/org/apidesign/apifest08/currency/ConvertorFactory.java
changeset 17 37c9921c653e
parent 6 97662396c0fd
     1.1 --- a/task1/solution04/src/org/apidesign/apifest08/currency/ConvertorFactory.java	Sun Sep 28 14:12:38 2008 +0200
     1.2 +++ b/task1/solution04/src/org/apidesign/apifest08/currency/ConvertorFactory.java	Tue Sep 30 11:50:09 2008 +0200
     1.3 @@ -1,6 +1,7 @@
     1.4  package org.apidesign.apifest08.currency;
     1.5  
     1.6  import java.lang.ref.WeakReference;
     1.7 +import java.math.BigDecimal;
     1.8  import java.util.Currency;
     1.9  import java.util.Map;
    1.10  import java.util.WeakHashMap;
    1.11 @@ -36,11 +37,16 @@
    1.12       * must be acceptable to java.util.Currency.getInstance(String)
    1.13       * 
    1.14       * @param a the currency to convert from.
    1.15 +     * @param aRate the exchange rate for a to b.
    1.16       * @param b the currency to convert to.
    1.17 +     * @param bRate the echante rate for b to a.
    1.18       * @return the convertor for the specified currencies.
    1.19 +     * @throws IllegalArgumentException if any of the arguments are null.
    1.20       */
    1.21 -    public static Convertor getConvertor(final String a,
    1.22 -                                         final String b)
    1.23 +    public static Convertor getConvertor(final String     a,
    1.24 +                                         final BigDecimal aRate,
    1.25 +                                         final String     b,
    1.26 +                                         final BigDecimal bRate)
    1.27      {
    1.28          final Currency  currencyA;
    1.29          final Currency  currencyB;
    1.30 @@ -48,7 +54,7 @@
    1.31          
    1.32          currencyA = Currency.getInstance(a);
    1.33          currencyB = Currency.getInstance(b);        
    1.34 -        convertor = getConvertor(currencyA, currencyB);
    1.35 +        convertor = getConvertor(currencyA, aRate, currencyB, bRate);
    1.36          
    1.37          return (convertor);
    1.38      }
    1.39 @@ -57,11 +63,16 @@
    1.40       * Get the convertor for the specified currencies.
    1.41       * 
    1.42       * @param a the currency to convert from.
    1.43 +     * @param aRate the exchange rate for a to b.
    1.44       * @param b the currency to convert to.
    1.45 +     * @param bRate the echante rate for b to a.
    1.46       * @return the convertor for the specified currencies.
    1.47 +     * @throws IllegalArgumentException if either any of the arguments are null or if either rate <= 0.
    1.48       */
    1.49 -    public static Convertor getConvertor(final Currency a,
    1.50 -                                         final Currency b)
    1.51 +    public static Convertor getConvertor(final Currency   a,
    1.52 +                                         final BigDecimal aRate,
    1.53 +                                         final Currency   b,
    1.54 +                                         final BigDecimal bRate)
    1.55      {
    1.56          final String key;        
    1.57          Convertor    convertor;
    1.58 @@ -75,15 +86,25 @@
    1.59          {
    1.60              throw new IllegalArgumentException("b cannot be null");
    1.61          }
    1.62 +        
    1.63 +        if(aRate == null)
    1.64 +        {
    1.65 +            throw new IllegalArgumentException("aRate cannot be null");
    1.66 +        }
    1.67 +        
    1.68 +        if(bRate == null)
    1.69 +        {
    1.70 +            throw new IllegalArgumentException("bRate cannot be null");
    1.71 +        }
    1.72  
    1.73 -        key = a.getCurrencyCode() + b.getCurrencyCode();
    1.74 +        key = a.getCurrencyCode() + aRate + b.getCurrencyCode() + bRate;
    1.75  
    1.76          // make sure that we don't try to overwrite one
    1.77          synchronized(convertors)
    1.78          {
    1.79              if(!(convertors.containsKey(key)))
    1.80              {        
    1.81 -                convertor = new Convertor(a, b);
    1.82 +                convertor = new ConvertorImpl(a, aRate, b, bRate);
    1.83                  convertors.put(key, new WeakReference(convertor));
    1.84              }
    1.85          }