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
     1 package org.apidesign.apifest08.currency;
     2 
     3 import java.io.Serializable;
     4 
     5 /**
     6  * Value class, holding an amount of the currency & an identifier of the currency.
     7  * Designed to be an immutable.
     8  * 
     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.
    12  * 
    13  * @author ked
    14  */
    15 public final class CurrencyValue<AmountType, IdentifierType> implements Serializable {
    16 
    17     private final AmountType amount;
    18     private final IdentifierType identifier;
    19 
    20     private CurrencyValue(AmountType amount, IdentifierType identifier) {
    21         this.amount = amount;
    22         this.identifier = identifier;
    23     }
    24 
    25     public AmountType getAmount() {
    26         return amount;
    27     }
    28 
    29     public IdentifierType getIdentifier() {
    30         return identifier;
    31     }
    32 
    33     @Override
    34     public boolean equals(Object obj) {
    35         if (obj == null) {
    36             return false;
    37         }
    38         if (getClass() != obj.getClass()) {
    39             return false;
    40         }
    41         final CurrencyValue other = (CurrencyValue) obj;
    42         if (this.amount != other.amount && (this.amount == null || !this.amount.equals(other.amount))) {
    43             return false;
    44         }
    45         if (this.identifier != other.identifier && (this.identifier == null || !this.identifier.equals(other.identifier))) {
    46             return false;
    47         }
    48         return true;
    49     }
    50 
    51     @Override
    52     public int hashCode() {
    53         int hash = 7;
    54         hash = 97 * hash + (this.amount != null ? this.amount.hashCode() : 0);
    55         hash = 97 * hash + (this.identifier != null ? this.identifier.hashCode() : 0);
    56         return hash;
    57     }
    58 
    59     /**
    60      * Creates new instance.
    61      * Generic types of the new instance are derived from types of the parameters.
    62      * 
    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
    68      */
    69     public static <AmountType, IdentifierType> CurrencyValue<AmountType, IdentifierType> getCurrencyValue(AmountType amount, IdentifierType identifier) {
    70         return new CurrencyValue<AmountType, IdentifierType>(amount, identifier);
    71     }
    72 }