task3/solution13/src/org/apidesign/apifest08/currency/ExchangeRateProvider.java
changeset 45 251d0ed461fb
parent 41 a7e6f84fb078
child 58 07c16ec15a25
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/task3/solution13/src/org/apidesign/apifest08/currency/ExchangeRateProvider.java	Tue Oct 07 11:05:34 2008 +0200
     1.3 @@ -0,0 +1,152 @@
     1.4 +
     1.5 +package org.apidesign.apifest08.currency;
     1.6 +
     1.7 +import java.math.BigDecimal;
     1.8 +import java.util.Date;
     1.9 +import java.util.HashMap;
    1.10 +import java.util.Map;
    1.11 +
    1.12 +/**
    1.13 + * Exchange rate provider. Provides fixed exchange rate which does not depend
    1.14 + * on date (method {@link #getExchangeRate()} ).
    1.15 + * 
    1.16 + * Date dependend exchange rate to be implemented.
    1.17 + * 
    1.18 + * @author arnostvalicek
    1.19 + */
    1.20 +public class ExchangeRateProvider {
    1.21 +    BigDecimal fromValue, toValue;
    1.22 +    ConvertorCurrency fromCurrency, toCurrency;
    1.23 +    
    1.24 +    Map exchangeRateMap = new HashMap();
    1.25 +
    1.26 +    /**
    1.27 +     * Simple constructor for <code>ExchangeRateProviderM</code> which can provide fixed exchange rate.
    1.28 +     * 
    1.29 +     * Describes conversion <em>from ONE</em> to <em>to ONE</em> currency.
    1.30 +     * 
    1.31 +     * @param fromValue From value. BigDecimal value, precision should be set to currency precision.
    1.32 +     * @param fromCurrency From currency.
    1.33 +     * @param toValue To value. BigDecimal value, precision should be set to currency precision.
    1.34 +     * @param toCurrency To currency.
    1.35 +     * @deprecated deprecated since task2. Use {@link #createExchangeRateProvider() } instead of this constructor.
    1.36 +     */
    1.37 +    public ExchangeRateProvider(BigDecimal fromValue, ConvertorCurrency fromCurrency, BigDecimal toValue, ConvertorCurrency toCurrency) {
    1.38 +        this.fromValue = fromValue;
    1.39 +        this.toValue = toValue;
    1.40 +        this.fromCurrency = fromCurrency;
    1.41 +        this.toCurrency = toCurrency;
    1.42 +    }
    1.43 +    
    1.44 +    private ExchangeRateProvider() {
    1.45 +        
    1.46 +    }
    1.47 +    
    1.48 +    /**
    1.49 +     * Static method to create new exchange rate provider. This exchange rate provider does not contain
    1.50 +     * any exchange rates (this is difference to public constructor).
    1.51 +     * @return New <code>ExchangeRateProvider</code>
    1.52 +     */
    1.53 +    public static ExchangeRateProvider createExchangeRateProvider() {
    1.54 +        ExchangeRateProvider provider = new ExchangeRateProvider();
    1.55 +        return provider;
    1.56 +    }
    1.57 +    
    1.58 +    /**
    1.59 +     * Add new exchange rate to <code></code> to this exchange rate provider.
    1.60 +     * <p>
    1.61 +     * Example of specifiing conversion rate: 100 SKK == 80 CZK:<br>
    1.62 +     * <code>addFixedCurencyRate(ConvertorCurrency.getInstance("SKK"), new BigDecimal(100), ConvertorCurrency.getInstance("CZK"),  new BigDecimal(80));</code>
    1.63 +     * <p>
    1.64 +     * 
    1.65 +     * @param fromCurrency Source currency.
    1.66 +     * @param fromValue Valye for from currency.
    1.67 +     * @param toCurrency Target currency.
    1.68 +     * @param toValue Value for target currency.
    1.69 +     */
    1.70 +    public synchronized void addFixedCurencyRate(ConvertorCurrency fromCurrency, BigDecimal fromValue, ConvertorCurrency toCurrency, BigDecimal toValue) {
    1.71 +        if (fromValue==null) {
    1.72 +            throw new NullPointerException("fromValue can't be null");
    1.73 +        }
    1.74 +        if (toValue==null) {
    1.75 +            throw new NullPointerException("toValue can't be null");
    1.76 +        }
    1.77 +        Map map2 = (Map) exchangeRateMap.get(fromCurrency);
    1.78 +        if (map2==null) {
    1.79 +            map2 = new HashMap();
    1.80 +            exchangeRateMap.put(fromCurrency, map2);
    1.81 +        }
    1.82 +        
    1.83 +        ExchangeRate rate = new ExchangeRate(fromValue, toValue);
    1.84 +        map2.put(toCurrency, rate);
    1.85 +    }
    1.86 +    
    1.87 +    /**
    1.88 +     * Get fixed exange rate for currencies (from->to).
    1.89 +     * @return Returns exchange rate.
    1.90 +     * @deprecated deprecated since task2. Use {@link #getExchangeRate(ConvertorCurrency, ConvertorCurrency) }
    1.91 +     */
    1.92 +    public ExchangeRate getExchangeRate() {
    1.93 +        return new ExchangeRate(fromValue, toValue);
    1.94 +    }
    1.95 +
    1.96 +    
    1.97 +    /**
    1.98 +     * Get fixed exange rate for currencies (from->to).
    1.99 +     * @return Returns exchange rate or <code>null</code> if exchange rate not found.
   1.100 +     */
   1.101 +    public ExchangeRate getExchangeRate(ConvertorCurrency fromCurrency, ConvertorCurrency toCurrency) {
   1.102 +        return getExchangeRateImpl(fromCurrency, toCurrency);
   1.103 +    }
   1.104 +    
   1.105 +    /**
   1.106 +     * Get fixed exange rate for currencies (from->to) or reversed exchange rate (to->from).
   1.107 +     * @return Returns exchange rate or <code>null</code> if exchange rate not found.
   1.108 +     */
   1.109 +    public ExchangeRate getReversibleExchangeRate(ConvertorCurrency fromCurrency, ConvertorCurrency toCurrency) {
   1.110 +       ExchangeRate rate = getExchangeRateImpl(fromCurrency, toCurrency);
   1.111 +       if (rate==null) {
   1.112 +           ExchangeRate revertedRate = getExchangeRateImpl(toCurrency, fromCurrency);
   1.113 +           if (revertedRate!=null) {
   1.114 +            rate = ExchangeRate.createRevertedRate(revertedRate);
   1.115 +           }
   1.116 +       }
   1.117 +       return rate;
   1.118 +    }
   1.119 +        
   1.120 +
   1.121 +    
   1.122 +    /**
   1.123 +     * Get exchange rate for currencies (from->to) based on provided date.
   1.124 +     * @param date Date for which exchange rate should be provided.
   1.125 +     * @return Returns exchange rate 
   1.126 +     * @deprecated deprecated since task2. No real implementation in version2.
   1.127 +     */
   1.128 +    public ExchangeRate getExchangeRate(Date date) {
   1.129 +        return new ExchangeRate(fromValue, toValue);
   1.130 +    }
   1.131 +    
   1.132 +
   1.133 +    ConvertorCurrency getFromCurrency() {
   1.134 +        return fromCurrency;
   1.135 +    }
   1.136 +    
   1.137 +    ConvertorCurrency getToCurrency() {
   1.138 +        return toCurrency;
   1.139 +    }
   1.140 +
   1.141 +    private ExchangeRate getExchangeRateImpl(ConvertorCurrency fromCurrency, ConvertorCurrency toCurrency) {
   1.142 +        if (fromValue != null && toValue != null && fromCurrency.equals(this.fromCurrency) && toCurrency.equals(this.toCurrency)) {
   1.143 +            return new ExchangeRate(fromValue, toValue);
   1.144 +        }
   1.145 +        //return new ExchangeRate(fromValue, toValue);
   1.146 +        Map map2 = (Map) exchangeRateMap.get(fromCurrency);
   1.147 +        if (map2 == null) {
   1.148 +            return null;
   1.149 +        }
   1.150 +        ExchangeRate result = (ExchangeRate) map2.get(toCurrency);
   1.151 +        return result;
   1.152 +    }
   1.153 +    
   1.154 +
   1.155 +}