1 package org.apidesign.apifest08.currency;
3 import java.io.Serializable;
6 * Value class, holding an exchange rate between two currencies.
7 * Designed to be an immutable.
11 public final class ExchangeRateValue<AmountType, IdentifierType> implements Serializable {
13 private final CurrencyValue<AmountType, IdentifierType> currencyA;
14 private final CurrencyValue<AmountType, IdentifierType> currencyB;
16 private ExchangeRateValue(
17 CurrencyValue<AmountType, IdentifierType> currencyA,
18 CurrencyValue<AmountType, IdentifierType> currencyB) {
19 if (currencyA.getIdentifier() == null ||
20 currencyB.getIdentifier() == null ||
21 currencyA.getIdentifier().equals(currencyB)) {
22 throw new IllegalArgumentException("Inappropriate exchange rates' identifiers!");
25 this.currencyA = currencyA;
26 this.currencyB = currencyB;
29 public CurrencyValue<AmountType, IdentifierType> getCurrencyA() {
33 public CurrencyValue<AmountType, IdentifierType> getCurrencyB() {
38 public boolean equals(Object obj) {
42 if (getClass() != obj.getClass()) {
45 final ExchangeRateValue other = (ExchangeRateValue) obj;
46 if (this.currencyA != other.currencyA && (this.currencyA == null || !this.currencyA.equals(other.currencyA))) {
49 if (this.currencyB != other.currencyB && (this.currencyB == null || !this.currencyB.equals(other.currencyB))) {
56 public int hashCode() {
58 hash = 71 * hash + (this.currencyA != null ? this.currencyA.hashCode() : 0);
59 hash = 71 * hash + (this.currencyB != null ? this.currencyB.hashCode() : 0);
64 * Creates new instance.
65 * Generic types of the new instance are derived from types of the parameters.
67 * @param <AmountType> type of the currency amount
68 * @param <IdentifierType> type of the currency identifier
69 * @param currencyA one currency of the exchange rate
70 * @param currencyB another currency of the exchange rate
71 * @return new instance
73 public static <AmountType, IdentifierType> ExchangeRateValue<AmountType, IdentifierType> getExchangeRate(
74 CurrencyValue<AmountType, IdentifierType> currencyA,
75 CurrencyValue<AmountType, IdentifierType> currencyB) {
76 return new ExchangeRateValue<AmountType, IdentifierType>(currencyA, currencyB);