diff -r f6073056b9fe -r a7e6f84fb078 task2/solution13/src/org/apidesign/apifest08/currency/ExchangeRateProvider.java --- a/task2/solution13/src/org/apidesign/apifest08/currency/ExchangeRateProvider.java Wed Oct 01 10:43:05 2008 +0200 +++ b/task2/solution13/src/org/apidesign/apifest08/currency/ExchangeRateProvider.java Tue Oct 07 01:18:23 2008 +0200 @@ -3,27 +3,33 @@ import java.math.BigDecimal; import java.util.Date; +import java.util.HashMap; +import java.util.Map; /** - * Exchange rate provider - can provide fixed exchange rate which does not depend - * on date (method {@link #getExchangeRate()} ) or exchange rate based on date (method {@link #getExchangeRate(java.util.Date) }). + * Exchange rate provider. Provides fixed exchange rate which does not depend + * on date (method {@link #getExchangeRate()} ). * + * Date dependend exchange rate to be implemented. * * @author arnostvalicek */ public class ExchangeRateProvider { BigDecimal fromValue, toValue; ConvertorCurrency fromCurrency, toCurrency; + + Map exchangeRateMap = new HashMap(); /** * Simple constructor for ExchangeRateProviderM which can provide fixed exchange rate. * - * Describes conversion from to to currency. + * Describes conversion from ONE to to ONE currency. * * @param fromValue From value. BigDecimal value, precision should be set to currency precision. * @param fromCurrency From currency. * @param toValue To value. BigDecimal value, precision should be set to currency precision. * @param toCurrency To currency. + * @deprecated deprecated since task2. Use {@link #createExchangeRateProvider() } instead of this constructor. */ public ExchangeRateProvider(BigDecimal fromValue, ConvertorCurrency fromCurrency, BigDecimal toValue, ConvertorCurrency toCurrency) { this.fromValue = fromValue; @@ -32,18 +38,89 @@ this.toCurrency = toCurrency; } + private ExchangeRateProvider() { + + } + + /** + * Static method to create new exchange rate provider. This exchange rate provider does not contain + * any exchange rates (this is difference to public constructor). + * @return New ExchangeRateProvider + */ + public static ExchangeRateProvider createExchangeRateProvider() { + ExchangeRateProvider provider = new ExchangeRateProvider(); + return provider; + } + + /** + * Add new exchange rate to to this exchange rate provider. + *

+ * Example of specifiing conversion rate: 100 SKK == 80 CZK:
+ * addFixedCurencyRate(ConvertorCurrency.getInstance("SKK"), new BigDecimal(100), ConvertorCurrency.getInstance("CZK"), new BigDecimal(80)); + *

+ * + * @param fromCurrency Source currency. + * @param fromValue Valye for from currency. + * @param toCurrency Target currency. + * @param toValue Value for target currency. + */ + public synchronized void addFixedCurencyRate(ConvertorCurrency fromCurrency, BigDecimal fromValue, ConvertorCurrency toCurrency, BigDecimal toValue) { + if (fromValue==null) { + throw new NullPointerException("fromValue can't be null"); + } + if (toValue==null) { + throw new NullPointerException("toValue can't be null"); + } + Map map2 = (Map) exchangeRateMap.get(fromCurrency); + if (map2==null) { + map2 = new HashMap(); + exchangeRateMap.put(fromCurrency, map2); + } + + ExchangeRate rate = new ExchangeRate(fromValue, toValue); + map2.put(toCurrency, rate); + } + /** * Get fixed exange rate for currencies (from->to). * @return Returns exchange rate. + * @deprecated deprecated since task2. Use {@link #getExchangeRate(ConvertorCurrency, ConvertorCurrency) } */ public ExchangeRate getExchangeRate() { return new ExchangeRate(fromValue, toValue); } + + + /** + * Get fixed exange rate for currencies (from->to). + * @return Returns exchange rate or null if exchange rate not found. + */ + public ExchangeRate getExchangeRate(ConvertorCurrency fromCurrency, ConvertorCurrency toCurrency) { + return getExchangeRateImpl(fromCurrency, toCurrency); + } + + /** + * Get fixed exange rate for currencies (from->to) or reversed exchange rate (to->from). + * @return Returns exchange rate or null if exchange rate not found. + */ + public ExchangeRate getReversibleExchangeRate(ConvertorCurrency fromCurrency, ConvertorCurrency toCurrency) { + ExchangeRate rate = getExchangeRateImpl(fromCurrency, toCurrency); + if (rate==null) { + ExchangeRate revertedRate = getExchangeRateImpl(toCurrency, fromCurrency); + if (revertedRate!=null) { + rate = ExchangeRate.createRevertedRate(revertedRate); + } + } + return rate; + } + + /** * Get exchange rate for currencies (from->to) based on provided date. * @param date Date for which exchange rate should be provided. * @return Returns exchange rate + * @deprecated deprecated since task2. No real implementation in version2. */ public ExchangeRate getExchangeRate(Date date) { return new ExchangeRate(fromValue, toValue); @@ -57,6 +134,19 @@ ConvertorCurrency getToCurrency() { return toCurrency; } + + private ExchangeRate getExchangeRateImpl(ConvertorCurrency fromCurrency, ConvertorCurrency toCurrency) { + if (fromValue != null && toValue != null && fromCurrency.equals(this.fromCurrency) && toCurrency.equals(this.toCurrency)) { + return new ExchangeRate(fromValue, toValue); + } + //return new ExchangeRate(fromValue, toValue); + Map map2 = (Map) exchangeRateMap.get(fromCurrency); + if (map2 == null) { + return null; + } + ExchangeRate result = (ExchangeRate) map2.get(toCurrency); + return result; + } }