task4/solution11/src/org/apidesign/apifest08/currency/ExchangeRateDataSource.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 25 Oct 2008 20:53:00 +0200
changeset 84 2ae6e4aa7aef
parent 61 58ec6da75f6f
permissions -rw-r--r--
Solutions by Petr Smid
     1 package org.apidesign.apifest08.currency;
     2 
     3 import java.util.Date;
     4 import org.apidesign.apifest08.currency.ExchangeRateProvider.ExchangeRateRequest;
     5 import org.apidesign.apifest08.currency.ExchangeRateProvider.ExchangeRateResponse;
     6 
     7 /**
     8  * Exchange rate data source.
     9  * 
    10  * @author ked
    11  */
    12 public final class ExchangeRateDataSource<AmountType, IdentifierType> {
    13 
    14     private final IdentifierType currencyAIdentifier;
    15     private final IdentifierType currencyBIdentifier;
    16     private final ExchangeRateProvider<AmountType, IdentifierType> exchangeRateProvider;
    17     private final Date validFrom;
    18     private final Date validTill;
    19 
    20     private ExchangeRateDataSource(
    21             IdentifierType currencyAIdentifier,
    22             IdentifierType currencyBIdentifier,
    23             ExchangeRateProvider<AmountType, IdentifierType> exchangeRateProvider,
    24             Date validFrom,
    25             Date validTill) {
    26         if (currencyAIdentifier == null ||
    27                 currencyBIdentifier == null ||
    28                 currencyAIdentifier.equals(currencyBIdentifier)) {
    29             throw new IllegalArgumentException("Inappropriate exchange rates' identifiers!");
    30         }
    31         if (validFrom != null &&
    32                 validTill != null &&
    33                 !validTill.after(validFrom)) {
    34             throw new IllegalArgumentException("Inappropriate exchange rate validity!");
    35         }
    36 
    37         this.currencyAIdentifier = currencyAIdentifier;
    38         this.currencyBIdentifier = currencyBIdentifier;
    39         this.exchangeRateProvider = exchangeRateProvider;
    40         this.validFrom = validFrom;
    41         this.validTill = validTill;
    42     }
    43 
    44     public IdentifierType getCurrencyAIdentifier() {
    45         return currencyAIdentifier;
    46     }
    47 
    48     public IdentifierType getCurrencyBIdentifier() {
    49         return currencyBIdentifier;
    50     }
    51 
    52     ExchangeRateProvider<AmountType, IdentifierType> getExchangeRateProvider() {
    53         return exchangeRateProvider;
    54     }
    55 
    56     public Date getValidFrom() {
    57         return validFrom != null ? (Date) validFrom.clone() : null;
    58     }
    59 
    60     public Date getValidTill() {
    61         return validTill != null ? (Date) validTill.clone() : null;
    62     }
    63 
    64     public ExchangeRateValue<AmountType, IdentifierType> getExchangeRate() {
    65         return getExchangeRate(new Date()); // System.currentTimeMillis()
    66     }
    67 
    68     public ExchangeRateValue<AmountType, IdentifierType> getExchangeRate(Date instant) {
    69         ExchangeRateRequest<AmountType, IdentifierType> request =
    70                 new ExchangeRateRequest<AmountType, IdentifierType>();
    71         ExchangeRateResponse<AmountType, IdentifierType> response =
    72                 new ExchangeRateResponse<AmountType, IdentifierType>();
    73 
    74         request.setCurrencyAIdentifier(currencyAIdentifier);
    75         request.setCurrencyBIdentifier(currencyBIdentifier);
    76         request.setInstant(instant);
    77 
    78         exchangeRateProvider.getExchangeRate(request, response);
    79 
    80         ExchangeRateValue<AmountType, IdentifierType> result = response.getExchangeRate();
    81         if (result.getCurrencyA().getIdentifier().equals(currencyAIdentifier) &&
    82                 result.getCurrencyB().getIdentifier().equals(currencyBIdentifier)) {
    83             return result;
    84         } else {
    85             throw new IllegalStateException("Data source's provider returned inappropriate exchange rate!");
    86         }
    87     }
    88 
    89     public static <AmountType, IdentifierType> ExchangeRateDataSource<AmountType, IdentifierType> getExchangeRateDataSource(
    90             IdentifierType currencyAIdentifier,
    91             IdentifierType currencyBIdentifier,
    92             ExchangeRateProvider<AmountType, IdentifierType> exchangeRateProvider) {
    93         return getExchangeRateDataSource(
    94                 currencyAIdentifier,
    95                 currencyBIdentifier,
    96                 exchangeRateProvider,
    97                 null,
    98                 null);
    99     }
   100 
   101     public static <AmountType, IdentifierType> ExchangeRateDataSource<AmountType, IdentifierType> getExchangeRateDataSource(
   102             IdentifierType currencyAIdentifier,
   103             IdentifierType currencyBIdentifier,
   104             ExchangeRateProvider<AmountType, IdentifierType> exchangeRateProvider,
   105             Date validFrom,
   106             Date validTill) {
   107         return new ExchangeRateDataSource<AmountType, IdentifierType>(
   108                 currencyAIdentifier,
   109                 currencyBIdentifier,
   110                 exchangeRateProvider,
   111                 validFrom,
   112                 validTill);
   113     }
   114 
   115     @Override
   116     public boolean equals(Object obj) {
   117         if (obj == null) {
   118             return false;
   119         }
   120         if (getClass() != obj.getClass()) {
   121             return false;
   122         }
   123         final ExchangeRateDataSource other =
   124                 (ExchangeRateDataSource) obj;
   125         if (this.currencyAIdentifier != other.currencyAIdentifier &&
   126                 (this.currencyAIdentifier == null || !this.currencyAIdentifier.equals(other.currencyAIdentifier))) {
   127             return false;
   128         }
   129         if (this.currencyBIdentifier != other.currencyBIdentifier &&
   130                 (this.currencyBIdentifier == null || !this.currencyBIdentifier.equals(other.currencyBIdentifier))) {
   131             return false;
   132         }
   133         if (this.exchangeRateProvider != other.exchangeRateProvider &&
   134                 (this.exchangeRateProvider == null || !this.exchangeRateProvider.equals(other.exchangeRateProvider))) {
   135             return false;
   136         }
   137         if (this.validFrom != other.validFrom && (this.validFrom == null || !this.validFrom.equals(other.validFrom))) {
   138             return false;
   139         }
   140         if (this.validTill != other.validTill && (this.validTill == null || !this.validTill.equals(other.validTill))) {
   141             return false;
   142         }
   143         return true;
   144     }
   145 
   146     @Override
   147     public int hashCode() {
   148         int hash = 7;
   149         hash = 83 * hash + (this.currencyAIdentifier != null ? this.currencyAIdentifier.hashCode() : 0);
   150         hash = 83 * hash + (this.currencyBIdentifier != null ? this.currencyBIdentifier.hashCode() : 0);
   151         hash = 83 * hash + (this.exchangeRateProvider != null ? this.exchangeRateProvider.hashCode() : 0);
   152         return hash;
   153     }
   154 }