diff -r 251d0ed461fb -r 58ec6da75f6f task4/solution11/src/org/apidesign/apifest08/currency/ExchangeRateValue.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/task4/solution11/src/org/apidesign/apifest08/currency/ExchangeRateValue.java Sat Oct 11 23:38:46 2008 +0200 @@ -0,0 +1,78 @@ +package org.apidesign.apifest08.currency; + +import java.io.Serializable; + +/** + * Value class, holding an exchange rate between two currencies. + * Designed to be an immutable. + * + * @author ked + */ +public final class ExchangeRateValue implements Serializable { + + private final CurrencyValue currencyA; + private final CurrencyValue currencyB; + + private ExchangeRateValue( + CurrencyValue currencyA, + CurrencyValue currencyB) { + if (currencyA.getIdentifier() == null || + currencyB.getIdentifier() == null || + currencyA.getIdentifier().equals(currencyB)) { + throw new IllegalArgumentException("Inappropriate exchange rates' identifiers!"); + } + + this.currencyA = currencyA; + this.currencyB = currencyB; + } + + public CurrencyValue getCurrencyA() { + return currencyA; + } + + public CurrencyValue getCurrencyB() { + return currencyB; + } + + @Override + public boolean equals(Object obj) { + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final ExchangeRateValue other = (ExchangeRateValue) obj; + if (this.currencyA != other.currencyA && (this.currencyA == null || !this.currencyA.equals(other.currencyA))) { + return false; + } + if (this.currencyB != other.currencyB && (this.currencyB == null || !this.currencyB.equals(other.currencyB))) { + return false; + } + return true; + } + + @Override + public int hashCode() { + int hash = 3; + hash = 71 * hash + (this.currencyA != null ? this.currencyA.hashCode() : 0); + hash = 71 * hash + (this.currencyB != null ? this.currencyB.hashCode() : 0); + return hash; + } + + /** + * Creates new instance. + * Generic types of the new instance are derived from types of the parameters. + * + * @param type of the currency amount + * @param type of the currency identifier + * @param currencyA one currency of the exchange rate + * @param currencyB another currency of the exchange rate + * @return new instance + */ + public static ExchangeRateValue getExchangeRate( + CurrencyValue currencyA, + CurrencyValue currencyB) { + return new ExchangeRateValue(currencyA, currencyB); + } +}