task4/solution11/src/org/apidesign/apifest08/currency/CurrencyValue.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 11 Oct 2008 23:38:46 +0200
changeset 61 58ec6da75f6f
parent 45 task3/solution11/src/org/apidesign/apifest08/currency/CurrencyValue.java@251d0ed461fb
permissions -rw-r--r--
Copying structure for task4
japod@6
     1
package org.apidesign.apifest08.currency;
japod@6
     2
japod@6
     3
import java.io.Serializable;
japod@6
     4
japod@6
     5
/**
japod@6
     6
 * Value class, holding an amount of the currency & an identifier of the currency.
japod@6
     7
 * Designed to be an immutable.
japod@6
     8
 * 
japod@6
     9
 * Because of a vague definition of types of the both fields,
japod@6
    10
 * the class has generic types, used as types of the fields.
japod@6
    11
 * These types should be immutable classes, too.
japod@6
    12
 * 
japod@6
    13
 * @author ked
japod@6
    14
 */
japod@6
    15
public final class CurrencyValue<AmountType, IdentifierType> implements Serializable {
japod@6
    16
japod@6
    17
    private final AmountType amount;
japod@6
    18
    private final IdentifierType identifier;
japod@6
    19
japod@6
    20
    private CurrencyValue(AmountType amount, IdentifierType identifier) {
japod@6
    21
        this.amount = amount;
japod@6
    22
        this.identifier = identifier;
japod@6
    23
    }
japod@6
    24
japod@6
    25
    public AmountType getAmount() {
japod@6
    26
        return amount;
japod@6
    27
    }
japod@6
    28
japod@6
    29
    public IdentifierType getIdentifier() {
japod@6
    30
        return identifier;
japod@6
    31
    }
japod@6
    32
japod@6
    33
    @Override
japod@6
    34
    public boolean equals(Object obj) {
japod@6
    35
        if (obj == null) {
japod@6
    36
            return false;
japod@6
    37
        }
japod@6
    38
        if (getClass() != obj.getClass()) {
japod@6
    39
            return false;
japod@6
    40
        }
japod@6
    41
        final CurrencyValue other = (CurrencyValue) obj;
japod@6
    42
        if (this.amount != other.amount && (this.amount == null || !this.amount.equals(other.amount))) {
japod@6
    43
            return false;
japod@6
    44
        }
japod@6
    45
        if (this.identifier != other.identifier && (this.identifier == null || !this.identifier.equals(other.identifier))) {
japod@6
    46
            return false;
japod@6
    47
        }
japod@6
    48
        return true;
japod@6
    49
    }
japod@6
    50
japod@6
    51
    @Override
japod@6
    52
    public int hashCode() {
japod@6
    53
        int hash = 7;
japod@6
    54
        hash = 97 * hash + (this.amount != null ? this.amount.hashCode() : 0);
japod@6
    55
        hash = 97 * hash + (this.identifier != null ? this.identifier.hashCode() : 0);
japod@6
    56
        return hash;
japod@6
    57
    }
japod@6
    58
japod@6
    59
    /**
japod@6
    60
     * Creates new instance.
japod@6
    61
     * Generic types of the new instance are derived from types of the parameters.
japod@6
    62
     * 
japod@6
    63
     * @param <AmountType> type of the currency amount
japod@6
    64
     * @param <IdentifierType> type of the currency identifier
japod@6
    65
     * @param amount currency amount
japod@6
    66
     * @param identifier currency identifier
japod@6
    67
     * @return new instance
japod@6
    68
     */
japod@6
    69
    public static <AmountType, IdentifierType> CurrencyValue<AmountType, IdentifierType> getCurrencyValue(AmountType amount, IdentifierType identifier) {
japod@6
    70
        return new CurrencyValue<AmountType, IdentifierType>(amount, identifier);
japod@6
    71
    }
japod@6
    72
}