1 package org.apidesign.apifest08.currency;
3 import java.io.Serializable;
6 * Value class, holding an amount of the currency & an identifier of the currency.
7 * Designed to be an immutable.
9 * Because of a vague definition of types of the both fields,
10 * the class has generic types, used as types of the fields.
11 * These types should be immutable classes, too.
15 public final class CurrencyValue<AmountType, IdentifierType> implements Serializable {
17 private final AmountType amount;
18 private final IdentifierType identifier;
20 private CurrencyValue(AmountType amount, IdentifierType identifier) {
22 this.identifier = identifier;
25 public AmountType getAmount() {
29 public IdentifierType getIdentifier() {
34 public boolean equals(Object obj) {
38 if (getClass() != obj.getClass()) {
41 final CurrencyValue other = (CurrencyValue) obj;
42 if (this.amount != other.amount && (this.amount == null || !this.amount.equals(other.amount))) {
45 if (this.identifier != other.identifier && (this.identifier == null || !this.identifier.equals(other.identifier))) {
52 public int hashCode() {
54 hash = 97 * hash + (this.amount != null ? this.amount.hashCode() : 0);
55 hash = 97 * hash + (this.identifier != null ? this.identifier.hashCode() : 0);
60 * Creates new instance.
61 * Generic types of the new instance are derived from types of the parameters.
63 * @param <AmountType> type of the currency amount
64 * @param <IdentifierType> type of the currency identifier
65 * @param amount currency amount
66 * @param identifier currency identifier
67 * @return new instance
69 public static <AmountType, IdentifierType> CurrencyValue<AmountType, IdentifierType> getCurrencyValue(AmountType amount, IdentifierType identifier) {
70 return new CurrencyValue<AmountType, IdentifierType>(amount, identifier);