task4/solution04/src/org/apidesign/apifest08/currency/ConvertorFactory.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 25 Oct 2008 20:53:00 +0200
changeset 84 2ae6e4aa7aef
parent 61 58ec6da75f6f
permissions -rw-r--r--
Solutions by Petr Smid
     1 package org.apidesign.apifest08.currency;
     2 
     3 import java.math.BigDecimal;
     4 import java.util.Currency;
     5 import java.util.Date;
     6 
     7 
     8 /**
     9  * Create convertors using a flyweight to reduce the number of repetative creations of the same convertor.
    10  * 
    11  * @author D'Arcy Smith
    12  * @version 1.2
    13  */
    14 public final class ConvertorFactory
    15 {
    16     /** 
    17      * Prevent accidental construction.
    18      */
    19     private ConvertorFactory()
    20     {        
    21     }
    22     
    23     /**
    24      * Get the convertor for the specified currencies.  The currency name format
    25      * must be acceptable to java.util.Currency.getInstance(String)
    26      * 
    27      * @param a the currency to convert from.
    28      * @param aRate the exchange rate for a to b.
    29      * @param b the currency to convert to.
    30      * @param bRate the echante rate for b to a.
    31      * @return the convertor for the specified currencies.
    32      * @throws IllegalArgumentException if any of the arguments are null.
    33      */
    34     public static Convertor getConvertor(final String     a,
    35                                          final BigDecimal aRate,
    36                                          final String     b,
    37                                          final BigDecimal bRate)
    38     {
    39         final Currency  currencyA;
    40         final Currency  currencyB;
    41         final Convertor convertor;
    42         
    43         currencyA = Currency.getInstance(a);
    44         currencyB = Currency.getInstance(b);        
    45         convertor = getConvertor(currencyA, aRate, currencyB, bRate);
    46         
    47         return (convertor);
    48     }
    49     
    50     /**
    51      * Get the convertor for the specified currencies.
    52      * 
    53      * @param a the currency to convert from.
    54      * @param aRate the exchange rate for a to b.
    55      * @param b the currency to convert to.
    56      * @param bRate the echante rate for b to a.
    57      * @return the convertor for the specified currencies.
    58      * @throws IllegalArgumentException if either any of the arguments are null or if either rate <= 0.
    59      */
    60     public static Convertor getConvertor(final Currency   a,
    61                                          final BigDecimal aRate,
    62                                          final Currency   b,
    63                                          final BigDecimal bRate)
    64     {
    65         Convertor          convertor;
    66         final ExchangeRate rate;
    67 
    68         if(a == null)
    69         {
    70             throw new IllegalArgumentException("a cannot be null");
    71         }
    72 
    73         if(b == null)
    74         {
    75             throw new IllegalArgumentException("b cannot be null");
    76         }
    77         
    78         if(aRate == null)
    79         {
    80             throw new IllegalArgumentException("aRate cannot be null");
    81         }
    82         
    83         if(bRate == null)
    84         {
    85             throw new IllegalArgumentException("bRate cannot be null");
    86         }
    87 
    88         rate      = ExchangeRate.getExchangeRate(a, b, aRate, bRate);
    89         convertor = getConvertor(rate);
    90                 
    91         return (convertor);
    92     }
    93     
    94     public static Convertor getConvertor(final ExchangeRate rate)
    95     {
    96         final ConvertorImpl convertor;
    97 
    98         if(rate == null)
    99         {
   100             throw new IllegalArgumentException("rate cannot be null");
   101         }
   102         
   103         convertor = new ConvertorImpl(rate);
   104                 
   105         return (convertor);        
   106     }
   107 
   108     public static DatedConvertor getConvertor(final Date      from,
   109                                               final Date      till,
   110                                               final Convertor convertor)
   111     {
   112         final DateRange      range;
   113         final ExchangeRate   rate;
   114         final DatedConvertor datedConvertor;
   115 
   116         if(from == null)
   117         {
   118             throw new IllegalArgumentException("from cannot be null");
   119         }
   120 
   121         if(till == null)
   122         {
   123             throw new IllegalArgumentException("till cannot be null");
   124         }
   125 
   126         if(convertor == null)
   127         {
   128             throw new IllegalArgumentException("convertor cannot be null");
   129         }
   130         
   131         if(from.after(till))
   132         {
   133             throw new IllegalArgumentException(from + " cannot be after " + till);
   134         }
   135         
   136         if(convertor instanceof ExchangeRateConvertor)
   137         {
   138             rate = ((ExchangeRateConvertor)convertor).getExchangeRate();
   139         }
   140         else
   141         {
   142             throw new Error();
   143         }
   144 
   145         range          = new DateRange(from, till);
   146         datedConvertor = new DatedConvertorImpl(range, rate);
   147         
   148         return (datedConvertor);
   149     }
   150 
   151     /**
   152      * 
   153      * @param cs
   154      * @return
   155      */
   156     public static Convertor mergeConvertors(final Convertor ... cs)
   157     {
   158         Convertor convertor;
   159         int       dated;
   160         int       nonDated;
   161 
   162         dated    = 0;
   163         nonDated = 0;
   164 
   165         for(final Convertor c : cs)
   166         {
   167             if(c instanceof DatedConvertor)
   168             {
   169                 dated++;
   170             }
   171             else
   172             {
   173                 nonDated++;
   174             }
   175         }
   176 
   177         if(dated != 0 && nonDated != 0)
   178         {
   179             throw new IllegalArgumentException("cannot mix DatedConvertors and non-DatedConvertors");
   180         }
   181 
   182         if(dated != 0)
   183         {
   184             convertor = new DatedCompositeConvertorImpl(cs);
   185         }
   186         else
   187         {
   188             convertor = new CompositeConvertorImpl(cs);
   189         }
   190 
   191         return (convertor);
   192     }
   193 }