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