task4/solution11/src/org/apidesign/apifest08/currency/ExchangeRateValue.java
changeset 61 58ec6da75f6f
parent 45 251d0ed461fb
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/task4/solution11/src/org/apidesign/apifest08/currency/ExchangeRateValue.java	Sat Oct 11 23:38:46 2008 +0200
     1.3 @@ -0,0 +1,78 @@
     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 exchange rate between two currencies.
    1.10 + * Designed to be an immutable.
    1.11 + * 
    1.12 + * @author ked
    1.13 + */
    1.14 +public final class ExchangeRateValue<AmountType, IdentifierType> implements Serializable {
    1.15 +
    1.16 +    private final CurrencyValue<AmountType, IdentifierType> currencyA;
    1.17 +    private final CurrencyValue<AmountType, IdentifierType> currencyB;
    1.18 +
    1.19 +    private ExchangeRateValue(
    1.20 +            CurrencyValue<AmountType, IdentifierType> currencyA,
    1.21 +            CurrencyValue<AmountType, IdentifierType> currencyB) {
    1.22 +        if (currencyA.getIdentifier() == null ||
    1.23 +            currencyB.getIdentifier() == null ||
    1.24 +            currencyA.getIdentifier().equals(currencyB)) {
    1.25 +                throw new IllegalArgumentException("Inappropriate exchange rates' identifiers!");
    1.26 +        }
    1.27 +
    1.28 +        this.currencyA = currencyA;
    1.29 +        this.currencyB = currencyB;
    1.30 +    }
    1.31 +
    1.32 +    public CurrencyValue<AmountType, IdentifierType> getCurrencyA() {
    1.33 +        return currencyA;
    1.34 +    }
    1.35 +
    1.36 +    public CurrencyValue<AmountType, IdentifierType> getCurrencyB() {
    1.37 +        return currencyB;
    1.38 +    }
    1.39 +
    1.40 +    @Override
    1.41 +    public boolean equals(Object obj) {
    1.42 +        if (obj == null) {
    1.43 +            return false;
    1.44 +        }
    1.45 +        if (getClass() != obj.getClass()) {
    1.46 +            return false;
    1.47 +        }
    1.48 +        final ExchangeRateValue other = (ExchangeRateValue) obj;
    1.49 +        if (this.currencyA != other.currencyA && (this.currencyA == null || !this.currencyA.equals(other.currencyA))) {
    1.50 +            return false;
    1.51 +        }
    1.52 +        if (this.currencyB != other.currencyB && (this.currencyB == null || !this.currencyB.equals(other.currencyB))) {
    1.53 +            return false;
    1.54 +        }
    1.55 +        return true;
    1.56 +    }
    1.57 +
    1.58 +    @Override
    1.59 +    public int hashCode() {
    1.60 +        int hash = 3;
    1.61 +        hash = 71 * hash + (this.currencyA != null ? this.currencyA.hashCode() : 0);
    1.62 +        hash = 71 * hash + (this.currencyB != null ? this.currencyB.hashCode() : 0);
    1.63 +        return hash;
    1.64 +    }
    1.65 +
    1.66 +    /**
    1.67 +     * Creates new instance.
    1.68 +     * Generic types of the new instance are derived from types of the parameters.
    1.69 +     * 
    1.70 +     * @param <AmountType> type of the currency amount
    1.71 +     * @param <IdentifierType> type of the currency identifier
    1.72 +     * @param currencyA one currency of the exchange rate
    1.73 +     * @param currencyB another currency of the exchange rate
    1.74 +     * @return new instance
    1.75 +     */
    1.76 +    public static <AmountType, IdentifierType> ExchangeRateValue<AmountType, IdentifierType> getExchangeRate(
    1.77 +            CurrencyValue<AmountType, IdentifierType> currencyA,
    1.78 +            CurrencyValue<AmountType, IdentifierType> currencyB) {
    1.79 +        return new ExchangeRateValue<AmountType, IdentifierType>(currencyA, currencyB);
    1.80 +    }
    1.81 +}