japod@37
|
1 |
package org.apidesign.apifest08.currency;
|
japod@37
|
2 |
|
japod@37
|
3 |
import java.io.Serializable;
|
japod@37
|
4 |
|
japod@37
|
5 |
/**
|
japod@37
|
6 |
* Value class, holding an exchange rate between two currencies.
|
japod@37
|
7 |
* Designed to be an immutable.
|
japod@37
|
8 |
*
|
japod@37
|
9 |
* @author ked
|
japod@37
|
10 |
*/
|
japod@37
|
11 |
public final class ExchangeRateValue<AmountType, IdentifierType> implements Serializable {
|
japod@37
|
12 |
|
japod@37
|
13 |
private final CurrencyValue<AmountType, IdentifierType> currencyA;
|
japod@37
|
14 |
private final CurrencyValue<AmountType, IdentifierType> currencyB;
|
japod@37
|
15 |
|
japod@37
|
16 |
private ExchangeRateValue(
|
japod@37
|
17 |
CurrencyValue<AmountType, IdentifierType> currencyA,
|
japod@37
|
18 |
CurrencyValue<AmountType, IdentifierType> currencyB) {
|
japod@37
|
19 |
if (currencyA.getIdentifier() == null ||
|
japod@37
|
20 |
currencyB.getIdentifier() == null ||
|
japod@37
|
21 |
currencyA.getIdentifier().equals(currencyB)) {
|
japod@37
|
22 |
throw new IllegalArgumentException("Inappropriate exchange rates' identifiers!");
|
japod@37
|
23 |
}
|
japod@37
|
24 |
|
japod@37
|
25 |
this.currencyA = currencyA;
|
japod@37
|
26 |
this.currencyB = currencyB;
|
japod@37
|
27 |
}
|
japod@37
|
28 |
|
japod@37
|
29 |
public CurrencyValue<AmountType, IdentifierType> getCurrencyA() {
|
japod@37
|
30 |
return currencyA;
|
japod@37
|
31 |
}
|
japod@37
|
32 |
|
japod@37
|
33 |
public CurrencyValue<AmountType, IdentifierType> getCurrencyB() {
|
japod@37
|
34 |
return currencyB;
|
japod@37
|
35 |
}
|
japod@37
|
36 |
|
japod@37
|
37 |
@Override
|
japod@37
|
38 |
public boolean equals(Object obj) {
|
japod@37
|
39 |
if (obj == null) {
|
japod@37
|
40 |
return false;
|
japod@37
|
41 |
}
|
japod@37
|
42 |
if (getClass() != obj.getClass()) {
|
japod@37
|
43 |
return false;
|
japod@37
|
44 |
}
|
japod@37
|
45 |
final ExchangeRateValue other = (ExchangeRateValue) obj;
|
japod@37
|
46 |
if (this.currencyA != other.currencyA && (this.currencyA == null || !this.currencyA.equals(other.currencyA))) {
|
japod@37
|
47 |
return false;
|
japod@37
|
48 |
}
|
japod@37
|
49 |
if (this.currencyB != other.currencyB && (this.currencyB == null || !this.currencyB.equals(other.currencyB))) {
|
japod@37
|
50 |
return false;
|
japod@37
|
51 |
}
|
japod@37
|
52 |
return true;
|
japod@37
|
53 |
}
|
japod@37
|
54 |
|
japod@37
|
55 |
@Override
|
japod@37
|
56 |
public int hashCode() {
|
japod@37
|
57 |
int hash = 3;
|
japod@37
|
58 |
hash = 71 * hash + (this.currencyA != null ? this.currencyA.hashCode() : 0);
|
japod@37
|
59 |
hash = 71 * hash + (this.currencyB != null ? this.currencyB.hashCode() : 0);
|
japod@37
|
60 |
return hash;
|
japod@37
|
61 |
}
|
japod@37
|
62 |
|
japod@37
|
63 |
/**
|
japod@37
|
64 |
* Creates new instance.
|
japod@37
|
65 |
* Generic types of the new instance are derived from types of the parameters.
|
japod@37
|
66 |
*
|
japod@37
|
67 |
* @param <AmountType> type of the currency amount
|
japod@37
|
68 |
* @param <IdentifierType> type of the currency identifier
|
japod@37
|
69 |
* @param currencyA one currency of the exchange rate
|
japod@37
|
70 |
* @param currencyB another currency of the exchange rate
|
japod@37
|
71 |
* @return new instance
|
japod@37
|
72 |
*/
|
japod@37
|
73 |
public static <AmountType, IdentifierType> ExchangeRateValue<AmountType, IdentifierType> getExchangeRate(
|
japod@37
|
74 |
CurrencyValue<AmountType, IdentifierType> currencyA,
|
japod@37
|
75 |
CurrencyValue<AmountType, IdentifierType> currencyB) {
|
japod@37
|
76 |
return new ExchangeRateValue<AmountType, IdentifierType>(currencyA, currencyB);
|
japod@37
|
77 |
}
|
japod@37
|
78 |
}
|