task4/solution11/src/org/apidesign/apifest08/currency/ExchangeRateValue.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/ExchangeRateValue.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 exchange rate between two currencies.
     7  * Designed to be an immutable.
     8  * 
     9  * @author ked
    10  */
    11 public final class ExchangeRateValue<AmountType, IdentifierType> implements Serializable {
    12 
    13     private final CurrencyValue<AmountType, IdentifierType> currencyA;
    14     private final CurrencyValue<AmountType, IdentifierType> currencyB;
    15 
    16     private ExchangeRateValue(
    17             CurrencyValue<AmountType, IdentifierType> currencyA,
    18             CurrencyValue<AmountType, IdentifierType> currencyB) {
    19         if (currencyA.getIdentifier() == null ||
    20             currencyB.getIdentifier() == null ||
    21             currencyA.getIdentifier().equals(currencyB)) {
    22                 throw new IllegalArgumentException("Inappropriate exchange rates' identifiers!");
    23         }
    24 
    25         this.currencyA = currencyA;
    26         this.currencyB = currencyB;
    27     }
    28 
    29     public CurrencyValue<AmountType, IdentifierType> getCurrencyA() {
    30         return currencyA;
    31     }
    32 
    33     public CurrencyValue<AmountType, IdentifierType> getCurrencyB() {
    34         return currencyB;
    35     }
    36 
    37     @Override
    38     public boolean equals(Object obj) {
    39         if (obj == null) {
    40             return false;
    41         }
    42         if (getClass() != obj.getClass()) {
    43             return false;
    44         }
    45         final ExchangeRateValue other = (ExchangeRateValue) obj;
    46         if (this.currencyA != other.currencyA && (this.currencyA == null || !this.currencyA.equals(other.currencyA))) {
    47             return false;
    48         }
    49         if (this.currencyB != other.currencyB && (this.currencyB == null || !this.currencyB.equals(other.currencyB))) {
    50             return false;
    51         }
    52         return true;
    53     }
    54 
    55     @Override
    56     public int hashCode() {
    57         int hash = 3;
    58         hash = 71 * hash + (this.currencyA != null ? this.currencyA.hashCode() : 0);
    59         hash = 71 * hash + (this.currencyB != null ? this.currencyB.hashCode() : 0);
    60         return hash;
    61     }
    62 
    63     /**
    64      * Creates new instance.
    65      * Generic types of the new instance are derived from types of the parameters.
    66      * 
    67      * @param <AmountType> type of the currency amount
    68      * @param <IdentifierType> type of the currency identifier
    69      * @param currencyA one currency of the exchange rate
    70      * @param currencyB another currency of the exchange rate
    71      * @return new instance
    72      */
    73     public static <AmountType, IdentifierType> ExchangeRateValue<AmountType, IdentifierType> getExchangeRate(
    74             CurrencyValue<AmountType, IdentifierType> currencyA,
    75             CurrencyValue<AmountType, IdentifierType> currencyB) {
    76         return new ExchangeRateValue<AmountType, IdentifierType>(currencyA, currencyB);
    77     }
    78 }