1.1 --- a/task4/solution13/src/org/apidesign/apifest08/currency/ExchangeRateProvider.java Sat Oct 11 23:38:46 2008 +0200
1.2 +++ b/task4/solution13/src/org/apidesign/apifest08/currency/ExchangeRateProvider.java Fri Oct 17 17:31:48 2008 +0200
1.3 @@ -12,6 +12,7 @@
1.4 * <ul>
1.5 * <li>{@link #createExchangeRateProvider() } - create <em>simple</em> exchange rate provider using fixed echange rate.
1.6 * <li>{@link #createExchangeRateProvider(IExchangeRateEngine) } - create exchange rate provider using custom {@link IExchangeRateEngine}.
1.7 + * <li>{@link #createDateExchangeRateProvider(IDateExchangeRateEngine) } - create exchange rate provider using custom {@link IDateExchangeRateEngine}.
1.8 * </ul>
1.9 * <p>
1.10 * Date dependend exchange rate to be implemented.
1.11 @@ -21,6 +22,7 @@
1.12 public class ExchangeRateProvider {
1.13
1.14 IExchangeRateEngine exrateEngine;
1.15 + IDateExchangeRateEngine dateExrateEngine;
1.16
1.17 /**
1.18 * Simple constructor for <code>ExchangeRateProviderM</code> which can provide fixed exchange rate.
1.19 @@ -62,6 +64,18 @@
1.20 provider.exrateEngine = exchangeRateEngine;
1.21 return provider;
1.22 }
1.23 +
1.24 + /**
1.25 + * Static method to create exchange rate provider which is using provided <code>IExIDateExchangeRateEnginechangeRateEngine</code>. This exchange rate provider is using
1.26 + * <code>IExchangeRateEngine</code> to get actual exchange rate.
1.27 + * @param exchangeRateEngine <code>IDateExchangeRateEngine</code> used to get exchange rate.
1.28 + * @return Returns instance of <code>ExchangeRateProvider</code>
1.29 + */
1.30 + public static ExchangeRateProvider createDateExchangeRateProvider(IDateExchangeRateEngine exchangeRateEngine) {
1.31 + ExchangeRateProvider provider = new ExchangeRateProvider();
1.32 + provider.dateExrateEngine = exchangeRateEngine;
1.33 + return provider;
1.34 + }
1.35
1.36 /**
1.37 * Add new exchange rate to <code></code> to this <em>simple</em> exchange rate provider.
1.38 @@ -103,25 +117,44 @@
1.39
1.40 /**
1.41 * Get fixed exange rate for currencies (from->to).
1.42 + * @param fromCurrency Source currency.
1.43 + * @param toCurrency Target currency.
1.44 * @return Returns exchange rate or <code>null</code> if exchange rate not found.
1.45 */
1.46 public ExchangeRate getExchangeRate(ConvertorCurrency fromCurrency, ConvertorCurrency toCurrency) {
1.47 - return getExchangeRateImpl(fromCurrency, toCurrency);
1.48 + return getExchangeRateImpl(fromCurrency, toCurrency,null);
1.49 + }
1.50 +
1.51 + /**
1.52 + * Get exange rate for currencies (from->to) for date.
1.53 + * @param fromCurrency Source currency.
1.54 + * @param toCurrency Target currency.
1.55 + * @param date Conversion date.
1.56 + * @return Returns exchange rate or <code>null</code> if exchange rate not found.
1.57 + */
1.58 + public ExchangeRate getExchangeRate(ConvertorCurrency fromCurrency, ConvertorCurrency toCurrency, Date date) {
1.59 + return getExchangeRateImpl(fromCurrency, toCurrency,date);
1.60 }
1.61
1.62 /**
1.63 * Get fixed exange rate for currencies (from->to) or reversed exchange rate (to->from).
1.64 + * @param fromCurrency Source currency.
1.65 + * @param toCurrency Target currency.
1.66 * @return Returns exchange rate or <code>null</code> if exchange rate not found.
1.67 */
1.68 public ExchangeRate getReversibleExchangeRate(ConvertorCurrency fromCurrency, ConvertorCurrency toCurrency) {
1.69 - ExchangeRate rate = getExchangeRateImpl(fromCurrency, toCurrency);
1.70 - if (rate == null) {
1.71 - ExchangeRate revertedRate = getExchangeRateImpl(toCurrency, fromCurrency);
1.72 - if (revertedRate != null) {
1.73 - rate = ExchangeRate.createRevertedRate(revertedRate);
1.74 - }
1.75 - }
1.76 - return rate;
1.77 + return getReversibleExrateImpl(fromCurrency, toCurrency,null);
1.78 + }
1.79 +
1.80 + /**
1.81 + * Get exange rate for currencies (from->to) or reversed exchange rate (to->from) for date.
1.82 + * @param fromCurrency Source currency.
1.83 + * @param toCurrency Target currency.
1.84 + * @param date Conversion date.
1.85 + * @return Returns exchange rate or <code>null</code> if exchange rate not found.
1.86 + */
1.87 + public ExchangeRate getReversibleExchangeRate(ConvertorCurrency fromCurrency, ConvertorCurrency toCurrency, Date date) {
1.88 + return getReversibleExrateImpl(fromCurrency, toCurrency, date);
1.89 }
1.90
1.91 /**
1.92 @@ -157,9 +190,28 @@
1.93 throw new IllegalStateException("Method supported only for FixedOneExchangeRateEngine. This method is deprecated");
1.94 }
1.95 }
1.96 +
1.97
1.98 - private ExchangeRate getExchangeRateImpl(ConvertorCurrency fromCurrency, ConvertorCurrency toCurrency) {
1.99 - ExchangeRate result = exrateEngine.getExchangeRate(fromCurrency, toCurrency);
1.100 + private ExchangeRate getReversibleExrateImpl(ConvertorCurrency fromCurrency, ConvertorCurrency toCurrency,Date date) {
1.101 + ExchangeRate rate = getExchangeRateImpl(fromCurrency, toCurrency, date);
1.102 + if (rate == null) {
1.103 + ExchangeRate revertedRate = getExchangeRateImpl(toCurrency, fromCurrency, date);
1.104 + if (revertedRate != null) {
1.105 + rate = ExchangeRate.createRevertedRate(revertedRate);
1.106 + }
1.107 + }
1.108 + return rate;
1.109 + }
1.110 +
1.111 + private ExchangeRate getExchangeRateImpl(ConvertorCurrency fromCurrency, ConvertorCurrency toCurrency,Date date) {
1.112 + ExchangeRate result;
1.113 + if (exrateEngine!=null) {
1.114 + result = exrateEngine.getExchangeRate(fromCurrency, toCurrency);
1.115 + } else if (dateExrateEngine!=null) {
1.116 + result = dateExrateEngine.getExchangeRate(fromCurrency, toCurrency, date);
1.117 + } else {
1.118 + throw new IllegalStateException("No exchange rate engine provided");
1.119 + }
1.120 return result;
1.121 }
1.122
1.123 @@ -224,7 +276,7 @@
1.124 */
1.125 private static class FixedExchangeRateEngine implements IExchangeRateEngine {
1.126
1.127 - private Map exchangeRateMap = new HashMap();
1.128 + private Map<ConvertorCurrency, Map<ConvertorCurrency, ExchangeRate>> exchangeRateMap = new HashMap<ConvertorCurrency, Map<ConvertorCurrency, ExchangeRate>>();
1.129
1.130 private FixedExchangeRateEngine() {
1.131 }
1.132 @@ -238,15 +290,14 @@
1.133 }
1.134
1.135 public ExchangeRate getExchangeRate(ConvertorCurrency fromCurrency, ConvertorCurrency toCurrency) {
1.136 - Map map2 = (Map) exchangeRateMap.get(fromCurrency);
1.137 + Map<ConvertorCurrency,ExchangeRate> map2 = exchangeRateMap.get(fromCurrency);
1.138 if (map2 == null) {
1.139 return null;
1.140 }
1.141 - ExchangeRate result = (ExchangeRate) map2.get(toCurrency);
1.142 + ExchangeRate result = map2.get(toCurrency);
1.143 return result;
1.144 }
1.145
1.146 - @SuppressWarnings("unchecked")
1.147 public synchronized void addFixedCurencyRate(ConvertorCurrency fromCurrency, BigDecimal fromValue, ConvertorCurrency toCurrency, BigDecimal toValue) {
1.148 if (fromValue == null) {
1.149 throw new NullPointerException("fromValue can't be null");
1.150 @@ -254,9 +305,9 @@
1.151 if (toValue == null) {
1.152 throw new NullPointerException("toValue can't be null");
1.153 }
1.154 - Map map2 = (Map) exchangeRateMap.get(fromCurrency);
1.155 + Map<ConvertorCurrency,ExchangeRate> map2 = exchangeRateMap.get(fromCurrency);
1.156 if (map2 == null) {
1.157 - map2 = new HashMap();
1.158 + map2 = new HashMap<ConvertorCurrency,ExchangeRate>();
1.159 exchangeRateMap.put(fromCurrency, map2);
1.160 }
1.161