task4/solution04/src/org/apidesign/apifest08/currency/OnlineConvertor.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/OnlineConvertor.java@14e78f48ac2b
permissions -rw-r--r--
Copying structure for task4
     1 package org.apidesign.apifest08.currency;
     2 
     3 
     4 import java.math.BigDecimal;
     5 import java.util.Currency;
     6 import java.util.Set;
     7 
     8 
     9 /**
    10  * A Convertor that looks up the exchange rate with each call to "convert".
    11  *
    12  * @author D'Arcy Smith
    13  * @version 1.0
    14  */
    15 public class OnlineConvertor
    16     implements Convertor
    17 {
    18     /**
    19      * The currency to convert from.
    20      */
    21     private final Currency currencyA;
    22 
    23     /**
    24      * The currency to convert to.
    25      */
    26     private final Currency currencyB;
    27 
    28     /**
    29      * Used to find the current exchange rate.
    30      */
    31     private final ExchangeRateFinder finder;
    32     
    33     /**
    34      * The convertor to perform the conversion.
    35      */
    36     private Convertor realConvertor;
    37 
    38     /**
    39      * Constructs an OnlinConvertor with the specified currencies.
    40      * 
    41      * @param a the currency to convert from.
    42      * @param b the currency to convert to.
    43      * @param f the finder used to obtanin the current exchange rate.
    44      * @throws IllegalArgumentException if either a or b are null.
    45      */
    46     public OnlineConvertor(final Currency           a,
    47                            final Currency           b,
    48                            final ExchangeRateFinder f)
    49     {
    50         if(a == null)        
    51         {
    52             throw new IllegalArgumentException("a cannot be null");
    53         }
    54         
    55         if(b == null)
    56         {
    57             throw new IllegalArgumentException("b cannot be null");
    58         }
    59         
    60         if(f == null)
    61         {
    62             throw new IllegalArgumentException("f cannot be null");
    63         }
    64         
    65         currencyA     = a;
    66         currencyB     = b;
    67         finder        = f;
    68         realConvertor = lookupRate();
    69     }
    70 
    71     /**
    72      * Convert an amount from one currency to another.  Before the conversion takes place 
    73      * the current exchange rate is looked up.
    74      *
    75      * @param from the currency to convert from.
    76      * @param to the currency to convert to.
    77      * @param amount the amount to convert.
    78      * @return the converted amount.
    79      * @throws IllegalArgumentException if any of the arguments are null.
    80      * @throws InvalidConversionException if either from or to are not equal to the currencies passed to the constructor.
    81      */
    82     public BigDecimal convert(final Currency   from, 
    83                               final Currency   to, 
    84                               final BigDecimal amount) 
    85         throws InvalidConversionException
    86     {
    87         final BigDecimal value;
    88         
    89         synchronized(this)
    90         {
    91             realConvertor = lookupRate();
    92             value         = realConvertor.convert(from, to, amount);
    93         }
    94         
    95         return (value);
    96     }
    97 
    98     /**
    99      * Lookup the current exchange rate.
   100      * 
   101      * @return
   102      */
   103     private final Convertor lookupRate()
   104     {
   105         final Convertor convertor;
   106         final ExchangeRate rate;
   107 
   108         rate      = finder.findRate(currencyA, currencyB);
   109         convertor = ConvertorFactory.getConvertor(rate.getCurrencyA(), rate.getRateAtoB(), rate.getCurrencyB(), rate.getRateBtoA());
   110 
   111         return (convertor);
   112     }
   113 
   114     /**
   115      * Check to see if converting between the two currencies is possible.
   116      * 
   117      * @param from the currency to convert from.
   118      * @param to the currency to convert to.
   119      * @return true if the conversion is possible.
   120      * @throws IllegalArgumentException if either from or to are null.
   121      */
   122     public boolean canConvert(final Currency from, 
   123                               final Currency to) 
   124     {
   125         if(from == null)
   126         {
   127             throw new IllegalArgumentException("from cannot be null");
   128         }
   129         
   130         if(to == null)
   131         {
   132             throw new IllegalArgumentException("to cannot be null");
   133         }
   134         
   135         return (realConvertor.canConvert(from, to));
   136     }
   137 
   138     /**
   139      * Get the currencies that the convertor supports.
   140      * 
   141      * @return the supported currencies.
   142      */
   143     public Set<Currency> getCurrencies() 
   144     {
   145         return (realConvertor.getCurrencies());
   146     }
   147 
   148     /**
   149      * Get the conversion rate between two currencies.  This does not lookup the current
   150      * conversion rate (it probably should, but given the way the contest works that might 
   151      * not be a good idea - if it were the real world way it might be a good idea).
   152      * 
   153      * @param from the currency to convert from.
   154      * @param to the currency to convert to.
   155      * @return the conversion rate between the two currencies.
   156      * @throws InvalidConversionException if canConvert would return false.
   157      * @throws IllegalArgumentException if either from or to are null.
   158      */
   159     public BigDecimal getConversionRate(final Currency from, 
   160                                         final Currency to) 
   161         throws InvalidConversionException 
   162     {        
   163         if(from == null)
   164         {
   165             throw new IllegalArgumentException("from cannot be null");
   166         }
   167         
   168         if(to == null)
   169         {
   170             throw new IllegalArgumentException("to cannot be null");
   171         }
   172         
   173         return (realConvertor.getConversionRate(from, to));
   174     }    
   175 }