japod@6: package org.apidesign.apifest08.currency; japod@6: japod@6: import java.io.Serializable; japod@6: japod@6: /** japod@6: * Value class, holding an amount of the currency & an identifier of the currency. japod@6: * Designed to be an immutable. japod@6: * japod@6: * Because of a vague definition of types of the both fields, japod@6: * the class has generic types, used as types of the fields. japod@6: * These types should be immutable classes, too. japod@6: * japod@6: * @author ked japod@6: */ japod@6: public final class CurrencyValue implements Serializable { japod@6: japod@6: private final AmountType amount; japod@6: private final IdentifierType identifier; japod@6: japod@6: private CurrencyValue(AmountType amount, IdentifierType identifier) { japod@6: this.amount = amount; japod@6: this.identifier = identifier; japod@6: } japod@6: japod@6: public AmountType getAmount() { japod@6: return amount; japod@6: } japod@6: japod@6: public IdentifierType getIdentifier() { japod@6: return identifier; japod@6: } japod@6: japod@6: @Override japod@6: public boolean equals(Object obj) { japod@6: if (obj == null) { japod@6: return false; japod@6: } japod@6: if (getClass() != obj.getClass()) { japod@6: return false; japod@6: } japod@6: final CurrencyValue other = (CurrencyValue) obj; japod@6: if (this.amount != other.amount && (this.amount == null || !this.amount.equals(other.amount))) { japod@6: return false; japod@6: } japod@6: if (this.identifier != other.identifier && (this.identifier == null || !this.identifier.equals(other.identifier))) { japod@6: return false; japod@6: } japod@6: return true; japod@6: } japod@6: japod@6: @Override japod@6: public int hashCode() { japod@6: int hash = 7; japod@6: hash = 97 * hash + (this.amount != null ? this.amount.hashCode() : 0); japod@6: hash = 97 * hash + (this.identifier != null ? this.identifier.hashCode() : 0); japod@6: return hash; japod@6: } japod@6: japod@6: /** japod@6: * Creates new instance. japod@6: * Generic types of the new instance are derived from types of the parameters. japod@6: * japod@6: * @param type of the currency amount japod@6: * @param type of the currency identifier japod@6: * @param amount currency amount japod@6: * @param identifier currency identifier japod@6: * @return new instance japod@6: */ japod@6: public static CurrencyValue getCurrencyValue(AmountType amount, IdentifierType identifier) { japod@6: return new CurrencyValue(amount, identifier); japod@6: } japod@6: }