japod@23:
japod@23: package org.apidesign.apifest08.currency;
japod@23:
japod@23: import java.math.BigDecimal;
japod@23: import java.util.Date;
japod@41: import java.util.HashMap;
japod@41: import java.util.Map;
japod@23:
japod@23: /**
japod@41: * Exchange rate provider. Provides fixed exchange rate which does not depend
japod@41: * on date (method {@link #getExchangeRate()} ).
japod@23: *
japod@41: * Date dependend exchange rate to be implemented.
japod@23: *
japod@23: * @author arnostvalicek
japod@23: */
japod@23: public class ExchangeRateProvider {
japod@23: BigDecimal fromValue, toValue;
japod@23: ConvertorCurrency fromCurrency, toCurrency;
japod@41:
japod@41: Map exchangeRateMap = new HashMap();
japod@23:
japod@23: /**
japod@23: * Simple constructor for ExchangeRateProviderM
which can provide fixed exchange rate.
japod@23: *
japod@41: * Describes conversion from ONE to to ONE currency.
japod@23: *
japod@23: * @param fromValue From value. BigDecimal value, precision should be set to currency precision.
japod@23: * @param fromCurrency From currency.
japod@23: * @param toValue To value. BigDecimal value, precision should be set to currency precision.
japod@23: * @param toCurrency To currency.
japod@41: * @deprecated deprecated since task2. Use {@link #createExchangeRateProvider() } instead of this constructor.
japod@23: */
japod@23: public ExchangeRateProvider(BigDecimal fromValue, ConvertorCurrency fromCurrency, BigDecimal toValue, ConvertorCurrency toCurrency) {
japod@23: this.fromValue = fromValue;
japod@23: this.toValue = toValue;
japod@23: this.fromCurrency = fromCurrency;
japod@23: this.toCurrency = toCurrency;
japod@23: }
japod@23:
japod@41: private ExchangeRateProvider() {
japod@41:
japod@41: }
japod@41:
japod@41: /**
japod@41: * Static method to create new exchange rate provider. This exchange rate provider does not contain
japod@41: * any exchange rates (this is difference to public constructor).
japod@41: * @return New ExchangeRateProvider
japod@41: */
japod@41: public static ExchangeRateProvider createExchangeRateProvider() {
japod@41: ExchangeRateProvider provider = new ExchangeRateProvider();
japod@41: return provider;
japod@41: }
japod@41:
japod@41: /**
japod@41: * Add new exchange rate to to this exchange rate provider.
japod@41: *
japod@41: * Example of specifiing conversion rate: 100 SKK == 80 CZK:
japod@41: * addFixedCurencyRate(ConvertorCurrency.getInstance("SKK"), new BigDecimal(100), ConvertorCurrency.getInstance("CZK"), new BigDecimal(80));
japod@41: *
japod@41: *
japod@41: * @param fromCurrency Source currency.
japod@41: * @param fromValue Valye for from currency.
japod@41: * @param toCurrency Target currency.
japod@41: * @param toValue Value for target currency.
japod@41: */
japod@41: public synchronized void addFixedCurencyRate(ConvertorCurrency fromCurrency, BigDecimal fromValue, ConvertorCurrency toCurrency, BigDecimal toValue) {
japod@41: if (fromValue==null) {
japod@41: throw new NullPointerException("fromValue can't be null");
japod@41: }
japod@41: if (toValue==null) {
japod@41: throw new NullPointerException("toValue can't be null");
japod@41: }
japod@41: Map map2 = (Map) exchangeRateMap.get(fromCurrency);
japod@41: if (map2==null) {
japod@41: map2 = new HashMap();
japod@41: exchangeRateMap.put(fromCurrency, map2);
japod@41: }
japod@41:
japod@41: ExchangeRate rate = new ExchangeRate(fromValue, toValue);
japod@41: map2.put(toCurrency, rate);
japod@41: }
japod@41:
japod@23: /**
japod@23: * Get fixed exange rate for currencies (from->to).
japod@23: * @return Returns exchange rate.
japod@41: * @deprecated deprecated since task2. Use {@link #getExchangeRate(ConvertorCurrency, ConvertorCurrency) }
japod@23: */
japod@23: public ExchangeRate getExchangeRate() {
japod@23: return new ExchangeRate(fromValue, toValue);
japod@23: }
japod@41:
japod@41:
japod@41: /**
japod@41: * Get fixed exange rate for currencies (from->to).
japod@41: * @return Returns exchange rate or null
if exchange rate not found.
japod@41: */
japod@41: public ExchangeRate getExchangeRate(ConvertorCurrency fromCurrency, ConvertorCurrency toCurrency) {
japod@41: return getExchangeRateImpl(fromCurrency, toCurrency);
japod@41: }
japod@41:
japod@41: /**
japod@41: * Get fixed exange rate for currencies (from->to) or reversed exchange rate (to->from).
japod@41: * @return Returns exchange rate or null
if exchange rate not found.
japod@41: */
japod@41: public ExchangeRate getReversibleExchangeRate(ConvertorCurrency fromCurrency, ConvertorCurrency toCurrency) {
japod@41: ExchangeRate rate = getExchangeRateImpl(fromCurrency, toCurrency);
japod@41: if (rate==null) {
japod@41: ExchangeRate revertedRate = getExchangeRateImpl(toCurrency, fromCurrency);
japod@41: if (revertedRate!=null) {
japod@41: rate = ExchangeRate.createRevertedRate(revertedRate);
japod@41: }
japod@41: }
japod@41: return rate;
japod@41: }
japod@41:
japod@41:
japod@23:
japod@23: /**
japod@23: * Get exchange rate for currencies (from->to) based on provided date.
japod@23: * @param date Date for which exchange rate should be provided.
japod@23: * @return Returns exchange rate
japod@41: * @deprecated deprecated since task2. No real implementation in version2.
japod@23: */
japod@23: public ExchangeRate getExchangeRate(Date date) {
japod@23: return new ExchangeRate(fromValue, toValue);
japod@23: }
japod@23:
japod@23:
japod@23: ConvertorCurrency getFromCurrency() {
japod@23: return fromCurrency;
japod@23: }
japod@23:
japod@23: ConvertorCurrency getToCurrency() {
japod@23: return toCurrency;
japod@23: }
japod@41:
japod@41: private ExchangeRate getExchangeRateImpl(ConvertorCurrency fromCurrency, ConvertorCurrency toCurrency) {
japod@41: if (fromValue != null && toValue != null && fromCurrency.equals(this.fromCurrency) && toCurrency.equals(this.toCurrency)) {
japod@41: return new ExchangeRate(fromValue, toValue);
japod@41: }
japod@41: //return new ExchangeRate(fromValue, toValue);
japod@41: Map map2 = (Map) exchangeRateMap.get(fromCurrency);
japod@41: if (map2 == null) {
japod@41: return null;
japod@41: }
japod@41: ExchangeRate result = (ExchangeRate) map2.get(toCurrency);
japod@41: return result;
japod@41: }
japod@23:
japod@23:
japod@23: }