japod@53
|
1 |
package org.apidesign.apifest08.currency;
|
japod@53
|
2 |
|
japod@53
|
3 |
import org.apidesign.apifest08.currency.ExchangeRateProvider.ExchangeRateRequest;
|
japod@53
|
4 |
import org.apidesign.apifest08.currency.ExchangeRateProvider.ExchangeRateResponse;
|
japod@53
|
5 |
|
japod@53
|
6 |
/**
|
japod@53
|
7 |
* Exchange rate data source.
|
japod@53
|
8 |
*
|
japod@53
|
9 |
* @author ked
|
japod@53
|
10 |
*/
|
japod@53
|
11 |
public final class ExchangeRateDataSource<AmountType, IdentifierType> {
|
japod@53
|
12 |
|
japod@53
|
13 |
private final IdentifierType currencyAIdentifier;
|
japod@53
|
14 |
|
japod@53
|
15 |
private final IdentifierType currencyBIdentifier;
|
japod@53
|
16 |
|
japod@53
|
17 |
private final ExchangeRateProvider<AmountType, IdentifierType> exchangeRateProvider;
|
japod@53
|
18 |
|
japod@53
|
19 |
private ExchangeRateDataSource(
|
japod@53
|
20 |
IdentifierType currencyAIdentifier,
|
japod@53
|
21 |
IdentifierType currencyBIdentifier,
|
japod@53
|
22 |
ExchangeRateProvider<AmountType, IdentifierType> exchangeRateProvider) {
|
japod@53
|
23 |
if (currencyAIdentifier == null ||
|
japod@53
|
24 |
currencyBIdentifier == null ||
|
japod@53
|
25 |
currencyAIdentifier.equals(currencyBIdentifier)) {
|
japod@53
|
26 |
throw new IllegalArgumentException("Inappropriate exchange rates' identifiers!");
|
japod@53
|
27 |
}
|
japod@53
|
28 |
this.currencyAIdentifier = currencyAIdentifier;
|
japod@53
|
29 |
this.currencyBIdentifier = currencyBIdentifier;
|
japod@53
|
30 |
this.exchangeRateProvider = exchangeRateProvider;
|
japod@53
|
31 |
}
|
japod@53
|
32 |
|
japod@53
|
33 |
public IdentifierType getCurrencyAIdentifier() {
|
japod@53
|
34 |
return currencyAIdentifier;
|
japod@53
|
35 |
}
|
japod@53
|
36 |
|
japod@53
|
37 |
public IdentifierType getCurrencyBIdentifier() {
|
japod@53
|
38 |
return currencyBIdentifier;
|
japod@53
|
39 |
}
|
japod@53
|
40 |
|
japod@53
|
41 |
public ExchangeRateValue<AmountType, IdentifierType> getExchangeRate() {
|
japod@53
|
42 |
ExchangeRateRequest<AmountType, IdentifierType> request =
|
japod@53
|
43 |
new ExchangeRateRequest<AmountType, IdentifierType>();
|
japod@53
|
44 |
ExchangeRateResponse<AmountType, IdentifierType> response =
|
japod@53
|
45 |
new ExchangeRateResponse<AmountType, IdentifierType>();
|
japod@53
|
46 |
|
japod@53
|
47 |
request.setCurrencyAIdentifier(currencyAIdentifier);
|
japod@53
|
48 |
request.setCurrencyBIdentifier(currencyBIdentifier);
|
japod@53
|
49 |
|
japod@53
|
50 |
exchangeRateProvider.getExchangeRate(request, response);
|
japod@53
|
51 |
|
japod@53
|
52 |
if (response.getExchangeRate().getCurrencyA().getIdentifier().equals(currencyAIdentifier) &&
|
japod@53
|
53 |
response.getExchangeRate().getCurrencyB().getIdentifier().equals(currencyBIdentifier)) {
|
japod@53
|
54 |
return response.getExchangeRate();
|
japod@53
|
55 |
} else {
|
japod@53
|
56 |
throw new IllegalStateException("Data source's provider returned inappropriate exchange rate!");
|
japod@53
|
57 |
}
|
japod@53
|
58 |
}
|
japod@53
|
59 |
|
japod@53
|
60 |
public static <AmountType, IdentifierType> ExchangeRateDataSource<AmountType, IdentifierType> getExchangeRateDataSource(
|
japod@53
|
61 |
IdentifierType currencyAIdentifier,
|
japod@53
|
62 |
IdentifierType currencyBIdentifier,
|
japod@53
|
63 |
ExchangeRateProvider<AmountType, IdentifierType> exchangeRateProvider) {
|
japod@53
|
64 |
return new ExchangeRateDataSource<AmountType, IdentifierType>(
|
japod@53
|
65 |
currencyAIdentifier,
|
japod@53
|
66 |
currencyBIdentifier,
|
japod@53
|
67 |
exchangeRateProvider);
|
japod@53
|
68 |
}
|
japod@53
|
69 |
|
japod@53
|
70 |
@Override
|
japod@53
|
71 |
public boolean equals(Object obj) {
|
japod@53
|
72 |
if (obj == null) {
|
japod@53
|
73 |
return false;
|
japod@53
|
74 |
}
|
japod@53
|
75 |
if (getClass() != obj.getClass()) {
|
japod@53
|
76 |
return false;
|
japod@53
|
77 |
}
|
japod@53
|
78 |
final ExchangeRateDataSource other =
|
japod@53
|
79 |
(ExchangeRateDataSource) obj;
|
japod@53
|
80 |
if (this.currencyAIdentifier != other.currencyAIdentifier &&
|
japod@53
|
81 |
(this.currencyAIdentifier == null || !this.currencyAIdentifier.equals(other.currencyAIdentifier))) {
|
japod@53
|
82 |
return false;
|
japod@53
|
83 |
}
|
japod@53
|
84 |
if (this.currencyBIdentifier != other.currencyBIdentifier &&
|
japod@53
|
85 |
(this.currencyBIdentifier == null || !this.currencyBIdentifier.equals(other.currencyBIdentifier))) {
|
japod@53
|
86 |
return false;
|
japod@53
|
87 |
}
|
japod@53
|
88 |
if (this.exchangeRateProvider != other.exchangeRateProvider &&
|
japod@53
|
89 |
(this.exchangeRateProvider == null || !this.exchangeRateProvider.equals(other.exchangeRateProvider))) {
|
japod@53
|
90 |
return false;
|
japod@53
|
91 |
}
|
japod@53
|
92 |
return true;
|
japod@53
|
93 |
}
|
japod@53
|
94 |
|
japod@53
|
95 |
@Override
|
japod@53
|
96 |
public int hashCode() {
|
japod@53
|
97 |
int hash = 7;
|
japod@53
|
98 |
hash = 83 * hash + (this.currencyAIdentifier != null ? this.currencyAIdentifier.hashCode() : 0);
|
japod@53
|
99 |
hash = 83 * hash + (this.currencyBIdentifier != null ? this.currencyBIdentifier.hashCode() : 0);
|
japod@53
|
100 |
hash = 83 * hash + (this.exchangeRateProvider != null ? this.exchangeRateProvider.hashCode() : 0);
|
japod@53
|
101 |
return hash;
|
japod@53
|
102 |
}
|
japod@53
|
103 |
}
|