task4/solution11/src/org/apidesign/apifest08/currency/ExchangeRateDataSource.java
1.1 --- a/task4/solution11/src/org/apidesign/apifest08/currency/ExchangeRateDataSource.java Sat Oct 11 23:38:46 2008 +0200
1.2 +++ b/task4/solution11/src/org/apidesign/apifest08/currency/ExchangeRateDataSource.java Fri Oct 17 17:34:40 2008 +0200
1.3 @@ -1,5 +1,6 @@
1.4 package org.apidesign.apifest08.currency;
1.5
1.6 +import java.util.Date;
1.7 import org.apidesign.apifest08.currency.ExchangeRateProvider.ExchangeRateRequest;
1.8 import org.apidesign.apifest08.currency.ExchangeRateProvider.ExchangeRateResponse;
1.9
1.10 @@ -10,94 +11,144 @@
1.11 */
1.12 public final class ExchangeRateDataSource<AmountType, IdentifierType> {
1.13
1.14 - private final IdentifierType currencyAIdentifier;
1.15 + private final IdentifierType currencyAIdentifier;
1.16 + private final IdentifierType currencyBIdentifier;
1.17 + private final ExchangeRateProvider<AmountType, IdentifierType> exchangeRateProvider;
1.18 + private final Date validFrom;
1.19 + private final Date validTill;
1.20
1.21 - private final IdentifierType currencyBIdentifier;
1.22 + private ExchangeRateDataSource(
1.23 + IdentifierType currencyAIdentifier,
1.24 + IdentifierType currencyBIdentifier,
1.25 + ExchangeRateProvider<AmountType, IdentifierType> exchangeRateProvider,
1.26 + Date validFrom,
1.27 + Date validTill) {
1.28 + if (currencyAIdentifier == null ||
1.29 + currencyBIdentifier == null ||
1.30 + currencyAIdentifier.equals(currencyBIdentifier)) {
1.31 + throw new IllegalArgumentException("Inappropriate exchange rates' identifiers!");
1.32 + }
1.33 + if (validFrom != null &&
1.34 + validTill != null &&
1.35 + !validTill.after(validFrom)) {
1.36 + throw new IllegalArgumentException("Inappropriate exchange rate validity!");
1.37 + }
1.38
1.39 - private final ExchangeRateProvider<AmountType, IdentifierType> exchangeRateProvider;
1.40 + this.currencyAIdentifier = currencyAIdentifier;
1.41 + this.currencyBIdentifier = currencyBIdentifier;
1.42 + this.exchangeRateProvider = exchangeRateProvider;
1.43 + this.validFrom = validFrom;
1.44 + this.validTill = validTill;
1.45 + }
1.46
1.47 - private ExchangeRateDataSource(
1.48 - IdentifierType currencyAIdentifier,
1.49 - IdentifierType currencyBIdentifier,
1.50 - ExchangeRateProvider<AmountType, IdentifierType> exchangeRateProvider) {
1.51 - if (currencyAIdentifier == null ||
1.52 - currencyBIdentifier == null ||
1.53 - currencyAIdentifier.equals(currencyBIdentifier)) {
1.54 - throw new IllegalArgumentException("Inappropriate exchange rates' identifiers!");
1.55 + public IdentifierType getCurrencyAIdentifier() {
1.56 + return currencyAIdentifier;
1.57 + }
1.58 +
1.59 + public IdentifierType getCurrencyBIdentifier() {
1.60 + return currencyBIdentifier;
1.61 + }
1.62 +
1.63 + ExchangeRateProvider<AmountType, IdentifierType> getExchangeRateProvider() {
1.64 + return exchangeRateProvider;
1.65 + }
1.66 +
1.67 + public Date getValidFrom() {
1.68 + return validFrom != null ? (Date) validFrom.clone() : null;
1.69 + }
1.70 +
1.71 + public Date getValidTill() {
1.72 + return validTill != null ? (Date) validTill.clone() : null;
1.73 + }
1.74 +
1.75 + public ExchangeRateValue<AmountType, IdentifierType> getExchangeRate() {
1.76 + return getExchangeRate(new Date()); // System.currentTimeMillis()
1.77 + }
1.78 +
1.79 + public ExchangeRateValue<AmountType, IdentifierType> getExchangeRate(Date instant) {
1.80 + ExchangeRateRequest<AmountType, IdentifierType> request =
1.81 + new ExchangeRateRequest<AmountType, IdentifierType>();
1.82 + ExchangeRateResponse<AmountType, IdentifierType> response =
1.83 + new ExchangeRateResponse<AmountType, IdentifierType>();
1.84 +
1.85 + request.setCurrencyAIdentifier(currencyAIdentifier);
1.86 + request.setCurrencyBIdentifier(currencyBIdentifier);
1.87 + request.setInstant(instant);
1.88 +
1.89 + exchangeRateProvider.getExchangeRate(request, response);
1.90 +
1.91 + ExchangeRateValue<AmountType, IdentifierType> result = response.getExchangeRate();
1.92 + if (result.getCurrencyA().getIdentifier().equals(currencyAIdentifier) &&
1.93 + result.getCurrencyB().getIdentifier().equals(currencyBIdentifier)) {
1.94 + return result;
1.95 + } else {
1.96 + throw new IllegalStateException("Data source's provider returned inappropriate exchange rate!");
1.97 }
1.98 - this.currencyAIdentifier = currencyAIdentifier;
1.99 - this.currencyBIdentifier = currencyBIdentifier;
1.100 - this.exchangeRateProvider = exchangeRateProvider;
1.101 - }
1.102 + }
1.103
1.104 - public IdentifierType getCurrencyAIdentifier() {
1.105 - return currencyAIdentifier;
1.106 - }
1.107 + public static <AmountType, IdentifierType> ExchangeRateDataSource<AmountType, IdentifierType> getExchangeRateDataSource(
1.108 + IdentifierType currencyAIdentifier,
1.109 + IdentifierType currencyBIdentifier,
1.110 + ExchangeRateProvider<AmountType, IdentifierType> exchangeRateProvider) {
1.111 + return getExchangeRateDataSource(
1.112 + currencyAIdentifier,
1.113 + currencyBIdentifier,
1.114 + exchangeRateProvider,
1.115 + null,
1.116 + null);
1.117 + }
1.118
1.119 - public IdentifierType getCurrencyBIdentifier() {
1.120 - return currencyBIdentifier;
1.121 - }
1.122 + public static <AmountType, IdentifierType> ExchangeRateDataSource<AmountType, IdentifierType> getExchangeRateDataSource(
1.123 + IdentifierType currencyAIdentifier,
1.124 + IdentifierType currencyBIdentifier,
1.125 + ExchangeRateProvider<AmountType, IdentifierType> exchangeRateProvider,
1.126 + Date validFrom,
1.127 + Date validTill) {
1.128 + return new ExchangeRateDataSource<AmountType, IdentifierType>(
1.129 + currencyAIdentifier,
1.130 + currencyBIdentifier,
1.131 + exchangeRateProvider,
1.132 + validFrom,
1.133 + validTill);
1.134 + }
1.135
1.136 - public ExchangeRateValue<AmountType, IdentifierType> getExchangeRate() {
1.137 - ExchangeRateRequest<AmountType, IdentifierType> request =
1.138 - new ExchangeRateRequest<AmountType, IdentifierType>();
1.139 - ExchangeRateResponse<AmountType, IdentifierType> response =
1.140 - new ExchangeRateResponse<AmountType, IdentifierType>();
1.141 + @Override
1.142 + public boolean equals(Object obj) {
1.143 + if (obj == null) {
1.144 + return false;
1.145 + }
1.146 + if (getClass() != obj.getClass()) {
1.147 + return false;
1.148 + }
1.149 + final ExchangeRateDataSource other =
1.150 + (ExchangeRateDataSource) obj;
1.151 + if (this.currencyAIdentifier != other.currencyAIdentifier &&
1.152 + (this.currencyAIdentifier == null || !this.currencyAIdentifier.equals(other.currencyAIdentifier))) {
1.153 + return false;
1.154 + }
1.155 + if (this.currencyBIdentifier != other.currencyBIdentifier &&
1.156 + (this.currencyBIdentifier == null || !this.currencyBIdentifier.equals(other.currencyBIdentifier))) {
1.157 + return false;
1.158 + }
1.159 + if (this.exchangeRateProvider != other.exchangeRateProvider &&
1.160 + (this.exchangeRateProvider == null || !this.exchangeRateProvider.equals(other.exchangeRateProvider))) {
1.161 + return false;
1.162 + }
1.163 + if (this.validFrom != other.validFrom && (this.validFrom == null || !this.validFrom.equals(other.validFrom))) {
1.164 + return false;
1.165 + }
1.166 + if (this.validTill != other.validTill && (this.validTill == null || !this.validTill.equals(other.validTill))) {
1.167 + return false;
1.168 + }
1.169 + return true;
1.170 + }
1.171
1.172 - request.setCurrencyAIdentifier(currencyAIdentifier);
1.173 - request.setCurrencyBIdentifier(currencyBIdentifier);
1.174 -
1.175 - exchangeRateProvider.getExchangeRate(request, response);
1.176 -
1.177 - if (response.getExchangeRate().getCurrencyA().getIdentifier().equals(currencyAIdentifier) &&
1.178 - response.getExchangeRate().getCurrencyB().getIdentifier().equals(currencyBIdentifier)) {
1.179 - return response.getExchangeRate();
1.180 - } else {
1.181 - throw new IllegalStateException("Data source's provider returned inappropriate exchange rate!");
1.182 + @Override
1.183 + public int hashCode() {
1.184 + int hash = 7;
1.185 + hash = 83 * hash + (this.currencyAIdentifier != null ? this.currencyAIdentifier.hashCode() : 0);
1.186 + hash = 83 * hash + (this.currencyBIdentifier != null ? this.currencyBIdentifier.hashCode() : 0);
1.187 + hash = 83 * hash + (this.exchangeRateProvider != null ? this.exchangeRateProvider.hashCode() : 0);
1.188 + return hash;
1.189 }
1.190 - }
1.191 -
1.192 - public static <AmountType, IdentifierType> ExchangeRateDataSource<AmountType, IdentifierType> getExchangeRateDataSource(
1.193 - IdentifierType currencyAIdentifier,
1.194 - IdentifierType currencyBIdentifier,
1.195 - ExchangeRateProvider<AmountType, IdentifierType> exchangeRateProvider) {
1.196 - return new ExchangeRateDataSource<AmountType, IdentifierType>(
1.197 - currencyAIdentifier,
1.198 - currencyBIdentifier,
1.199 - exchangeRateProvider);
1.200 - }
1.201 -
1.202 - @Override
1.203 - public boolean equals(Object obj) {
1.204 - if (obj == null) {
1.205 - return false;
1.206 - }
1.207 - if (getClass() != obj.getClass()) {
1.208 - return false;
1.209 - }
1.210 - final ExchangeRateDataSource other =
1.211 - (ExchangeRateDataSource) obj;
1.212 - if (this.currencyAIdentifier != other.currencyAIdentifier &&
1.213 - (this.currencyAIdentifier == null || !this.currencyAIdentifier.equals(other.currencyAIdentifier))) {
1.214 - return false;
1.215 - }
1.216 - if (this.currencyBIdentifier != other.currencyBIdentifier &&
1.217 - (this.currencyBIdentifier == null || !this.currencyBIdentifier.equals(other.currencyBIdentifier))) {
1.218 - return false;
1.219 - }
1.220 - if (this.exchangeRateProvider != other.exchangeRateProvider &&
1.221 - (this.exchangeRateProvider == null || !this.exchangeRateProvider.equals(other.exchangeRateProvider))) {
1.222 - return false;
1.223 - }
1.224 - return true;
1.225 - }
1.226 -
1.227 - @Override
1.228 - public int hashCode() {
1.229 - int hash = 7;
1.230 - hash = 83 * hash + (this.currencyAIdentifier != null ? this.currencyAIdentifier.hashCode() : 0);
1.231 - hash = 83 * hash + (this.currencyBIdentifier != null ? this.currencyBIdentifier.hashCode() : 0);
1.232 - hash = 83 * hash + (this.exchangeRateProvider != null ? this.exchangeRateProvider.hashCode() : 0);
1.233 - return hash;
1.234 - }
1.235 }