1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/task1/solution11/src/org/apidesign/apifest08/currency/CurrencyValue.java Sun Sep 28 14:12:38 2008 +0200
1.3 @@ -0,0 +1,72 @@
1.4 +package org.apidesign.apifest08.currency;
1.5 +
1.6 +import java.io.Serializable;
1.7 +
1.8 +/**
1.9 + * Value class, holding an amount of the currency & an identifier of the currency.
1.10 + * Designed to be an immutable.
1.11 + *
1.12 + * Because of a vague definition of types of the both fields,
1.13 + * the class has generic types, used as types of the fields.
1.14 + * These types should be immutable classes, too.
1.15 + *
1.16 + * @author ked
1.17 + */
1.18 +public final class CurrencyValue<AmountType, IdentifierType> implements Serializable {
1.19 +
1.20 + private final AmountType amount;
1.21 + private final IdentifierType identifier;
1.22 +
1.23 + private CurrencyValue(AmountType amount, IdentifierType identifier) {
1.24 + this.amount = amount;
1.25 + this.identifier = identifier;
1.26 + }
1.27 +
1.28 + public AmountType getAmount() {
1.29 + return amount;
1.30 + }
1.31 +
1.32 + public IdentifierType getIdentifier() {
1.33 + return identifier;
1.34 + }
1.35 +
1.36 + @Override
1.37 + public boolean equals(Object obj) {
1.38 + if (obj == null) {
1.39 + return false;
1.40 + }
1.41 + if (getClass() != obj.getClass()) {
1.42 + return false;
1.43 + }
1.44 + final CurrencyValue other = (CurrencyValue) obj;
1.45 + if (this.amount != other.amount && (this.amount == null || !this.amount.equals(other.amount))) {
1.46 + return false;
1.47 + }
1.48 + if (this.identifier != other.identifier && (this.identifier == null || !this.identifier.equals(other.identifier))) {
1.49 + return false;
1.50 + }
1.51 + return true;
1.52 + }
1.53 +
1.54 + @Override
1.55 + public int hashCode() {
1.56 + int hash = 7;
1.57 + hash = 97 * hash + (this.amount != null ? this.amount.hashCode() : 0);
1.58 + hash = 97 * hash + (this.identifier != null ? this.identifier.hashCode() : 0);
1.59 + return hash;
1.60 + }
1.61 +
1.62 + /**
1.63 + * Creates new instance.
1.64 + * Generic types of the new instance are derived from types of the parameters.
1.65 + *
1.66 + * @param <AmountType> type of the currency amount
1.67 + * @param <IdentifierType> type of the currency identifier
1.68 + * @param amount currency amount
1.69 + * @param identifier currency identifier
1.70 + * @return new instance
1.71 + */
1.72 + public static <AmountType, IdentifierType> CurrencyValue<AmountType, IdentifierType> getCurrencyValue(AmountType amount, IdentifierType identifier) {
1.73 + return new CurrencyValue<AmountType, IdentifierType>(amount, identifier);
1.74 + }
1.75 +}