task2/solution13/src/org/apidesign/apifest08/currency/ExchangeRateProvider.java
author japod@localhost
Tue, 07 Oct 2008 01:18:23 +0200
changeset 41 a7e6f84fb078
parent 29 f6073056b9fe
permissions -rw-r--r--
adding solution13 for task2
     1 
     2 package org.apidesign.apifest08.currency;
     3 
     4 import java.math.BigDecimal;
     5 import java.util.Date;
     6 import java.util.HashMap;
     7 import java.util.Map;
     8 
     9 /**
    10  * Exchange rate provider. Provides fixed exchange rate which does not depend
    11  * on date (method {@link #getExchangeRate()} ).
    12  * 
    13  * Date dependend exchange rate to be implemented.
    14  * 
    15  * @author arnostvalicek
    16  */
    17 public class ExchangeRateProvider {
    18     BigDecimal fromValue, toValue;
    19     ConvertorCurrency fromCurrency, toCurrency;
    20     
    21     Map exchangeRateMap = new HashMap();
    22 
    23     /**
    24      * Simple constructor for <code>ExchangeRateProviderM</code> which can provide fixed exchange rate.
    25      * 
    26      * Describes conversion <em>from ONE</em> to <em>to ONE</em> currency.
    27      * 
    28      * @param fromValue From value. BigDecimal value, precision should be set to currency precision.
    29      * @param fromCurrency From currency.
    30      * @param toValue To value. BigDecimal value, precision should be set to currency precision.
    31      * @param toCurrency To currency.
    32      * @deprecated deprecated since task2. Use {@link #createExchangeRateProvider() } instead of this constructor.
    33      */
    34     public ExchangeRateProvider(BigDecimal fromValue, ConvertorCurrency fromCurrency, BigDecimal toValue, ConvertorCurrency toCurrency) {
    35         this.fromValue = fromValue;
    36         this.toValue = toValue;
    37         this.fromCurrency = fromCurrency;
    38         this.toCurrency = toCurrency;
    39     }
    40     
    41     private ExchangeRateProvider() {
    42         
    43     }
    44     
    45     /**
    46      * Static method to create new exchange rate provider. This exchange rate provider does not contain
    47      * any exchange rates (this is difference to public constructor).
    48      * @return New <code>ExchangeRateProvider</code>
    49      */
    50     public static ExchangeRateProvider createExchangeRateProvider() {
    51         ExchangeRateProvider provider = new ExchangeRateProvider();
    52         return provider;
    53     }
    54     
    55     /**
    56      * Add new exchange rate to <code></code> to this exchange rate provider.
    57      * <p>
    58      * Example of specifiing conversion rate: 100 SKK == 80 CZK:<br>
    59      * <code>addFixedCurencyRate(ConvertorCurrency.getInstance("SKK"), new BigDecimal(100), ConvertorCurrency.getInstance("CZK"),  new BigDecimal(80));</code>
    60      * <p>
    61      * 
    62      * @param fromCurrency Source currency.
    63      * @param fromValue Valye for from currency.
    64      * @param toCurrency Target currency.
    65      * @param toValue Value for target currency.
    66      */
    67     public synchronized void addFixedCurencyRate(ConvertorCurrency fromCurrency, BigDecimal fromValue, ConvertorCurrency toCurrency, BigDecimal toValue) {
    68         if (fromValue==null) {
    69             throw new NullPointerException("fromValue can't be null");
    70         }
    71         if (toValue==null) {
    72             throw new NullPointerException("toValue can't be null");
    73         }
    74         Map map2 = (Map) exchangeRateMap.get(fromCurrency);
    75         if (map2==null) {
    76             map2 = new HashMap();
    77             exchangeRateMap.put(fromCurrency, map2);
    78         }
    79         
    80         ExchangeRate rate = new ExchangeRate(fromValue, toValue);
    81         map2.put(toCurrency, rate);
    82     }
    83     
    84     /**
    85      * Get fixed exange rate for currencies (from->to).
    86      * @return Returns exchange rate.
    87      * @deprecated deprecated since task2. Use {@link #getExchangeRate(ConvertorCurrency, ConvertorCurrency) }
    88      */
    89     public ExchangeRate getExchangeRate() {
    90         return new ExchangeRate(fromValue, toValue);
    91     }
    92 
    93     
    94     /**
    95      * Get fixed exange rate for currencies (from->to).
    96      * @return Returns exchange rate or <code>null</code> if exchange rate not found.
    97      */
    98     public ExchangeRate getExchangeRate(ConvertorCurrency fromCurrency, ConvertorCurrency toCurrency) {
    99         return getExchangeRateImpl(fromCurrency, toCurrency);
   100     }
   101     
   102     /**
   103      * Get fixed exange rate for currencies (from->to) or reversed exchange rate (to->from).
   104      * @return Returns exchange rate or <code>null</code> if exchange rate not found.
   105      */
   106     public ExchangeRate getReversibleExchangeRate(ConvertorCurrency fromCurrency, ConvertorCurrency toCurrency) {
   107        ExchangeRate rate = getExchangeRateImpl(fromCurrency, toCurrency);
   108        if (rate==null) {
   109            ExchangeRate revertedRate = getExchangeRateImpl(toCurrency, fromCurrency);
   110            if (revertedRate!=null) {
   111             rate = ExchangeRate.createRevertedRate(revertedRate);
   112            }
   113        }
   114        return rate;
   115     }
   116         
   117 
   118     
   119     /**
   120      * Get exchange rate for currencies (from->to) based on provided date.
   121      * @param date Date for which exchange rate should be provided.
   122      * @return Returns exchange rate 
   123      * @deprecated deprecated since task2. No real implementation in version2.
   124      */
   125     public ExchangeRate getExchangeRate(Date date) {
   126         return new ExchangeRate(fromValue, toValue);
   127     }
   128     
   129 
   130     ConvertorCurrency getFromCurrency() {
   131         return fromCurrency;
   132     }
   133     
   134     ConvertorCurrency getToCurrency() {
   135         return toCurrency;
   136     }
   137 
   138     private ExchangeRate getExchangeRateImpl(ConvertorCurrency fromCurrency, ConvertorCurrency toCurrency) {
   139         if (fromValue != null && toValue != null && fromCurrency.equals(this.fromCurrency) && toCurrency.equals(this.toCurrency)) {
   140             return new ExchangeRate(fromValue, toValue);
   141         }
   142         //return new ExchangeRate(fromValue, toValue);
   143         Map map2 = (Map) exchangeRateMap.get(fromCurrency);
   144         if (map2 == null) {
   145             return null;
   146         }
   147         ExchangeRate result = (ExchangeRate) map2.get(toCurrency);
   148         return result;
   149     }
   150     
   151 
   152 }