task4/solution11/src/org/apidesign/apifest08/currency/ExchangeRateDataSource.java
changeset 61 58ec6da75f6f
parent 53 09d690bb97f6
child 66 aa3f99f845ef
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/task4/solution11/src/org/apidesign/apifest08/currency/ExchangeRateDataSource.java	Sat Oct 11 23:38:46 2008 +0200
     1.3 @@ -0,0 +1,103 @@
     1.4 +package org.apidesign.apifest08.currency;
     1.5 +
     1.6 +import org.apidesign.apifest08.currency.ExchangeRateProvider.ExchangeRateRequest;
     1.7 +import org.apidesign.apifest08.currency.ExchangeRateProvider.ExchangeRateResponse;
     1.8 +
     1.9 +/**
    1.10 + * Exchange rate data source.
    1.11 + * 
    1.12 + * @author ked
    1.13 + */
    1.14 +public final class ExchangeRateDataSource<AmountType, IdentifierType> {
    1.15 +
    1.16 +  private final IdentifierType currencyAIdentifier;
    1.17 +
    1.18 +  private final IdentifierType currencyBIdentifier;
    1.19 +
    1.20 +  private final ExchangeRateProvider<AmountType, IdentifierType> exchangeRateProvider;
    1.21 +
    1.22 +  private ExchangeRateDataSource(
    1.23 +      IdentifierType currencyAIdentifier,
    1.24 +      IdentifierType currencyBIdentifier,
    1.25 +      ExchangeRateProvider<AmountType, IdentifierType> exchangeRateProvider) {
    1.26 +        if (currencyAIdentifier == null ||
    1.27 +            currencyBIdentifier == null ||
    1.28 +            currencyAIdentifier.equals(currencyBIdentifier)) {
    1.29 +                throw new IllegalArgumentException("Inappropriate exchange rates' identifiers!");
    1.30 +        }
    1.31 +  this.currencyAIdentifier = currencyAIdentifier;
    1.32 +    this.currencyBIdentifier = currencyBIdentifier;
    1.33 +    this.exchangeRateProvider = exchangeRateProvider;
    1.34 +  }
    1.35 +
    1.36 +  public IdentifierType getCurrencyAIdentifier() {
    1.37 +    return currencyAIdentifier;
    1.38 +  }
    1.39 +
    1.40 +  public IdentifierType getCurrencyBIdentifier() {
    1.41 +    return currencyBIdentifier;
    1.42 +  }
    1.43 +
    1.44 +  public ExchangeRateValue<AmountType, IdentifierType> getExchangeRate() {
    1.45 +    ExchangeRateRequest<AmountType, IdentifierType> request =
    1.46 +        new ExchangeRateRequest<AmountType, IdentifierType>();
    1.47 +    ExchangeRateResponse<AmountType, IdentifierType> response =
    1.48 +        new ExchangeRateResponse<AmountType, IdentifierType>();
    1.49 +
    1.50 +    request.setCurrencyAIdentifier(currencyAIdentifier);
    1.51 +    request.setCurrencyBIdentifier(currencyBIdentifier);
    1.52 +
    1.53 +    exchangeRateProvider.getExchangeRate(request, response);
    1.54 +
    1.55 +    if (response.getExchangeRate().getCurrencyA().getIdentifier().equals(currencyAIdentifier) &&
    1.56 +        response.getExchangeRate().getCurrencyB().getIdentifier().equals(currencyBIdentifier)) {
    1.57 +      return response.getExchangeRate();
    1.58 +    } else {
    1.59 +      throw new IllegalStateException("Data source's provider returned inappropriate exchange rate!");
    1.60 +    }
    1.61 +  }
    1.62 +
    1.63 +  public static <AmountType, IdentifierType> ExchangeRateDataSource<AmountType, IdentifierType> getExchangeRateDataSource(
    1.64 +      IdentifierType currencyAIdentifier,
    1.65 +      IdentifierType currencyBIdentifier,
    1.66 +      ExchangeRateProvider<AmountType, IdentifierType> exchangeRateProvider) {
    1.67 +    return new ExchangeRateDataSource<AmountType, IdentifierType>(
    1.68 +        currencyAIdentifier,
    1.69 +        currencyBIdentifier,
    1.70 +        exchangeRateProvider);
    1.71 +  }
    1.72 +
    1.73 +  @Override
    1.74 +  public boolean equals(Object obj) {
    1.75 +    if (obj == null) {
    1.76 +      return false;
    1.77 +    }
    1.78 +    if (getClass() != obj.getClass()) {
    1.79 +      return false;
    1.80 +    }
    1.81 +    final ExchangeRateDataSource other =
    1.82 +        (ExchangeRateDataSource) obj;
    1.83 +    if (this.currencyAIdentifier != other.currencyAIdentifier &&
    1.84 +        (this.currencyAIdentifier == null || !this.currencyAIdentifier.equals(other.currencyAIdentifier))) {
    1.85 +      return false;
    1.86 +    }
    1.87 +    if (this.currencyBIdentifier != other.currencyBIdentifier &&
    1.88 +        (this.currencyBIdentifier == null || !this.currencyBIdentifier.equals(other.currencyBIdentifier))) {
    1.89 +      return false;
    1.90 +    }
    1.91 +    if (this.exchangeRateProvider != other.exchangeRateProvider &&
    1.92 +        (this.exchangeRateProvider == null || !this.exchangeRateProvider.equals(other.exchangeRateProvider))) {
    1.93 +      return false;
    1.94 +    }
    1.95 +    return true;
    1.96 +  }
    1.97 +
    1.98 +  @Override
    1.99 +  public int hashCode() {
   1.100 +    int hash = 7;
   1.101 +    hash = 83 * hash + (this.currencyAIdentifier != null ? this.currencyAIdentifier.hashCode() : 0);
   1.102 +    hash = 83 * hash + (this.currencyBIdentifier != null ? this.currencyBIdentifier.hashCode() : 0);
   1.103 +    hash = 83 * hash + (this.exchangeRateProvider != null ? this.exchangeRateProvider.hashCode() : 0);
   1.104 +    return hash;
   1.105 +  }
   1.106 +}