task2/solution13/src/org/apidesign/apifest08/currency/ExchangeRateProvider.java
changeset 41 a7e6f84fb078
parent 29 f6073056b9fe
     1.1 --- a/task2/solution13/src/org/apidesign/apifest08/currency/ExchangeRateProvider.java	Wed Oct 01 10:43:05 2008 +0200
     1.2 +++ b/task2/solution13/src/org/apidesign/apifest08/currency/ExchangeRateProvider.java	Tue Oct 07 01:18:23 2008 +0200
     1.3 @@ -3,27 +3,33 @@
     1.4  
     1.5  import java.math.BigDecimal;
     1.6  import java.util.Date;
     1.7 +import java.util.HashMap;
     1.8 +import java.util.Map;
     1.9  
    1.10  /**
    1.11 - * Exchange rate provider - can provide fixed exchange rate which does not depend
    1.12 - * on date (method {@link #getExchangeRate()} ) or exchange rate based on date (method {@link #getExchangeRate(java.util.Date) }).
    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</em> to <em>to</em> currency.
    1.30 +     * Describes conversion <em>from ONE</em> to <em>to ONE</em> currency.
    1.31       * 
    1.32       * @param fromValue From value. BigDecimal value, precision should be set to currency precision.
    1.33       * @param fromCurrency From currency.
    1.34       * @param toValue To value. BigDecimal value, precision should be set to currency precision.
    1.35       * @param toCurrency To currency.
    1.36 +     * @deprecated deprecated since task2. Use {@link #createExchangeRateProvider() } instead of this constructor.
    1.37       */
    1.38      public ExchangeRateProvider(BigDecimal fromValue, ConvertorCurrency fromCurrency, BigDecimal toValue, ConvertorCurrency toCurrency) {
    1.39          this.fromValue = fromValue;
    1.40 @@ -32,18 +38,89 @@
    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 @@ -57,6 +134,19 @@
   1.131      ConvertorCurrency getToCurrency() {
   1.132          return toCurrency;
   1.133      }
   1.134 +
   1.135 +    private ExchangeRate getExchangeRateImpl(ConvertorCurrency fromCurrency, ConvertorCurrency toCurrency) {
   1.136 +        if (fromValue != null && toValue != null && fromCurrency.equals(this.fromCurrency) && toCurrency.equals(this.toCurrency)) {
   1.137 +            return new ExchangeRate(fromValue, toValue);
   1.138 +        }
   1.139 +        //return new ExchangeRate(fromValue, toValue);
   1.140 +        Map map2 = (Map) exchangeRateMap.get(fromCurrency);
   1.141 +        if (map2 == null) {
   1.142 +            return null;
   1.143 +        }
   1.144 +        ExchangeRate result = (ExchangeRate) map2.get(toCurrency);
   1.145 +        return result;
   1.146 +    }
   1.147      
   1.148  
   1.149  }