task4/solution04/src/org/apidesign/apifest08/currency/OnlineConvertor.java
changeset 61 58ec6da75f6f
parent 55 14e78f48ac2b
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/task4/solution04/src/org/apidesign/apifest08/currency/OnlineConvertor.java	Sat Oct 11 23:38:46 2008 +0200
     1.3 @@ -0,0 +1,175 @@
     1.4 +package org.apidesign.apifest08.currency;
     1.5 +
     1.6 +
     1.7 +import java.math.BigDecimal;
     1.8 +import java.util.Currency;
     1.9 +import java.util.Set;
    1.10 +
    1.11 +
    1.12 +/**
    1.13 + * A Convertor that looks up the exchange rate with each call to "convert".
    1.14 + *
    1.15 + * @author D'Arcy Smith
    1.16 + * @version 1.0
    1.17 + */
    1.18 +public class OnlineConvertor
    1.19 +    implements Convertor
    1.20 +{
    1.21 +    /**
    1.22 +     * The currency to convert from.
    1.23 +     */
    1.24 +    private final Currency currencyA;
    1.25 +
    1.26 +    /**
    1.27 +     * The currency to convert to.
    1.28 +     */
    1.29 +    private final Currency currencyB;
    1.30 +
    1.31 +    /**
    1.32 +     * Used to find the current exchange rate.
    1.33 +     */
    1.34 +    private final ExchangeRateFinder finder;
    1.35 +    
    1.36 +    /**
    1.37 +     * The convertor to perform the conversion.
    1.38 +     */
    1.39 +    private Convertor realConvertor;
    1.40 +
    1.41 +    /**
    1.42 +     * Constructs an OnlinConvertor with the specified currencies.
    1.43 +     * 
    1.44 +     * @param a the currency to convert from.
    1.45 +     * @param b the currency to convert to.
    1.46 +     * @param f the finder used to obtanin the current exchange rate.
    1.47 +     * @throws IllegalArgumentException if either a or b are null.
    1.48 +     */
    1.49 +    public OnlineConvertor(final Currency           a,
    1.50 +                           final Currency           b,
    1.51 +                           final ExchangeRateFinder f)
    1.52 +    {
    1.53 +        if(a == null)        
    1.54 +        {
    1.55 +            throw new IllegalArgumentException("a cannot be null");
    1.56 +        }
    1.57 +        
    1.58 +        if(b == null)
    1.59 +        {
    1.60 +            throw new IllegalArgumentException("b cannot be null");
    1.61 +        }
    1.62 +        
    1.63 +        if(f == null)
    1.64 +        {
    1.65 +            throw new IllegalArgumentException("f cannot be null");
    1.66 +        }
    1.67 +        
    1.68 +        currencyA     = a;
    1.69 +        currencyB     = b;
    1.70 +        finder        = f;
    1.71 +        realConvertor = lookupRate();
    1.72 +    }
    1.73 +
    1.74 +    /**
    1.75 +     * Convert an amount from one currency to another.  Before the conversion takes place 
    1.76 +     * the current exchange rate is looked up.
    1.77 +     *
    1.78 +     * @param from the currency to convert from.
    1.79 +     * @param to the currency to convert to.
    1.80 +     * @param amount the amount to convert.
    1.81 +     * @return the converted amount.
    1.82 +     * @throws IllegalArgumentException if any of the arguments are null.
    1.83 +     * @throws InvalidConversionException if either from or to are not equal to the currencies passed to the constructor.
    1.84 +     */
    1.85 +    public BigDecimal convert(final Currency   from, 
    1.86 +                              final Currency   to, 
    1.87 +                              final BigDecimal amount) 
    1.88 +        throws InvalidConversionException
    1.89 +    {
    1.90 +        final BigDecimal value;
    1.91 +        
    1.92 +        synchronized(this)
    1.93 +        {
    1.94 +            realConvertor = lookupRate();
    1.95 +            value         = realConvertor.convert(from, to, amount);
    1.96 +        }
    1.97 +        
    1.98 +        return (value);
    1.99 +    }
   1.100 +
   1.101 +    /**
   1.102 +     * Lookup the current exchange rate.
   1.103 +     * 
   1.104 +     * @return
   1.105 +     */
   1.106 +    private final Convertor lookupRate()
   1.107 +    {
   1.108 +        final Convertor convertor;
   1.109 +        final ExchangeRate rate;
   1.110 +
   1.111 +        rate      = finder.findRate(currencyA, currencyB);
   1.112 +        convertor = ConvertorFactory.getConvertor(rate.getCurrencyA(), rate.getRateAtoB(), rate.getCurrencyB(), rate.getRateBtoA());
   1.113 +
   1.114 +        return (convertor);
   1.115 +    }
   1.116 +
   1.117 +    /**
   1.118 +     * Check to see if converting between the two currencies is possible.
   1.119 +     * 
   1.120 +     * @param from the currency to convert from.
   1.121 +     * @param to the currency to convert to.
   1.122 +     * @return true if the conversion is possible.
   1.123 +     * @throws IllegalArgumentException if either from or to are null.
   1.124 +     */
   1.125 +    public boolean canConvert(final Currency from, 
   1.126 +                              final Currency to) 
   1.127 +    {
   1.128 +        if(from == null)
   1.129 +        {
   1.130 +            throw new IllegalArgumentException("from cannot be null");
   1.131 +        }
   1.132 +        
   1.133 +        if(to == null)
   1.134 +        {
   1.135 +            throw new IllegalArgumentException("to cannot be null");
   1.136 +        }
   1.137 +        
   1.138 +        return (realConvertor.canConvert(from, to));
   1.139 +    }
   1.140 +
   1.141 +    /**
   1.142 +     * Get the currencies that the convertor supports.
   1.143 +     * 
   1.144 +     * @return the supported currencies.
   1.145 +     */
   1.146 +    public Set<Currency> getCurrencies() 
   1.147 +    {
   1.148 +        return (realConvertor.getCurrencies());
   1.149 +    }
   1.150 +
   1.151 +    /**
   1.152 +     * Get the conversion rate between two currencies.  This does not lookup the current
   1.153 +     * conversion rate (it probably should, but given the way the contest works that might 
   1.154 +     * not be a good idea - if it were the real world way it might be a good idea).
   1.155 +     * 
   1.156 +     * @param from the currency to convert from.
   1.157 +     * @param to the currency to convert to.
   1.158 +     * @return the conversion rate between the two currencies.
   1.159 +     * @throws InvalidConversionException if canConvert would return false.
   1.160 +     * @throws IllegalArgumentException if either from or to are null.
   1.161 +     */
   1.162 +    public BigDecimal getConversionRate(final Currency from, 
   1.163 +                                        final Currency to) 
   1.164 +        throws InvalidConversionException 
   1.165 +    {        
   1.166 +        if(from == null)
   1.167 +        {
   1.168 +            throw new IllegalArgumentException("from cannot be null");
   1.169 +        }
   1.170 +        
   1.171 +        if(to == null)
   1.172 +        {
   1.173 +            throw new IllegalArgumentException("to cannot be null");
   1.174 +        }
   1.175 +        
   1.176 +        return (realConvertor.getConversionRate(from, to));
   1.177 +    }    
   1.178 +}