japod@37: package org.apidesign.apifest08.currency; japod@37: japod@37: import java.io.Serializable; japod@37: japod@37: /** japod@37: * Value class, holding an exchange rate between two currencies. japod@37: * Designed to be an immutable. japod@37: * japod@37: * @author ked japod@37: */ japod@37: public final class ExchangeRateValue implements Serializable { japod@37: japod@37: private final CurrencyValue currencyA; japod@37: private final CurrencyValue currencyB; japod@37: japod@37: private ExchangeRateValue( japod@37: CurrencyValue currencyA, japod@37: CurrencyValue currencyB) { japod@37: if (currencyA.getIdentifier() == null || japod@37: currencyB.getIdentifier() == null || japod@37: currencyA.getIdentifier().equals(currencyB)) { japod@37: throw new IllegalArgumentException("Inappropriate exchange rates' identifiers!"); japod@37: } japod@37: japod@37: this.currencyA = currencyA; japod@37: this.currencyB = currencyB; japod@37: } japod@37: japod@37: public CurrencyValue getCurrencyA() { japod@37: return currencyA; japod@37: } japod@37: japod@37: public CurrencyValue getCurrencyB() { japod@37: return currencyB; japod@37: } japod@37: japod@37: @Override japod@37: public boolean equals(Object obj) { japod@37: if (obj == null) { japod@37: return false; japod@37: } japod@37: if (getClass() != obj.getClass()) { japod@37: return false; japod@37: } japod@37: final ExchangeRateValue other = (ExchangeRateValue) obj; japod@37: if (this.currencyA != other.currencyA && (this.currencyA == null || !this.currencyA.equals(other.currencyA))) { japod@37: return false; japod@37: } japod@37: if (this.currencyB != other.currencyB && (this.currencyB == null || !this.currencyB.equals(other.currencyB))) { japod@37: return false; japod@37: } japod@37: return true; japod@37: } japod@37: japod@37: @Override japod@37: public int hashCode() { japod@37: int hash = 3; japod@37: hash = 71 * hash + (this.currencyA != null ? this.currencyA.hashCode() : 0); japod@37: hash = 71 * hash + (this.currencyB != null ? this.currencyB.hashCode() : 0); japod@37: return hash; japod@37: } japod@37: japod@37: /** japod@37: * Creates new instance. japod@37: * Generic types of the new instance are derived from types of the parameters. japod@37: * japod@37: * @param type of the currency amount japod@37: * @param type of the currency identifier japod@37: * @param currencyA one currency of the exchange rate japod@37: * @param currencyB another currency of the exchange rate japod@37: * @return new instance japod@37: */ japod@37: public static ExchangeRateValue getExchangeRate( japod@37: CurrencyValue currencyA, japod@37: CurrencyValue currencyB) { japod@37: return new ExchangeRateValue(currencyA, currencyB); japod@37: } japod@37: }