task1/solution02/src/org/apidesign/apifest08/currency/MoneyImpl.java
changeset 6 97662396c0fd
child 16 2864c6d744c0
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/task1/solution02/src/org/apidesign/apifest08/currency/MoneyImpl.java	Sun Sep 28 14:12:38 2008 +0200
     1.3 @@ -0,0 +1,87 @@
     1.4 +package org.apidesign.apifest08.currency;
     1.5 +
     1.6 +import java.io.Serializable;
     1.7 +import java.math.BigDecimal;
     1.8 +import java.util.Currency;
     1.9 +
    1.10 +/**
    1.11 + * Default implementation of {@link Money} interface. This class is immutable.
    1.12 + * @author lukas
    1.13 + *
    1.14 + */
    1.15 +public final class MoneyImpl implements Serializable, Money{
    1.16 +	private static final long serialVersionUID = -6091808475616516136L;
    1.17 +
    1.18 +	private final BigDecimal amount;
    1.19 +	
    1.20 +	private final Currency currency;
    1.21 +
    1.22 +	public MoneyImpl(BigDecimal amount, Currency currency) {
    1.23 +		if (amount==null) throw new NullPointerException("Amount is null");
    1.24 +		if (currency==null) throw new NullPointerException("Currency is null"+currency);
    1.25 +		this.amount = amount.setScale(2);
    1.26 +		this.currency = currency;
    1.27 +	}
    1.28 +	
    1.29 +	public MoneyImpl(long amount, Currency currency) {
    1.30 +		this(BigDecimal.valueOf(amount), currency);
    1.31 +	}
    1.32 +	
    1.33 +	public MoneyImpl(double amount, Currency currency) {
    1.34 +		this(BigDecimal.valueOf(amount), currency);
    1.35 +	}
    1.36 +
    1.37 +	/* (non-Javadoc)
    1.38 +	 * @see org.apidesign.apifest08.currency.Money#getAmount()
    1.39 +	 */
    1.40 +	public BigDecimal getAmount() {
    1.41 +		return amount;
    1.42 +	}
    1.43 +	
    1.44 +	/* (non-Javadoc)
    1.45 +	 * @see org.apidesign.apifest08.currency.Money#getCurrency()
    1.46 +	 */
    1.47 +	public Currency getCurrency() {
    1.48 +		return currency;
    1.49 +	}
    1.50 +
    1.51 +	@Override
    1.52 +	public int hashCode() {
    1.53 +		final int prime = 31;
    1.54 +		int result = 1;
    1.55 +		result = prime * result + ((amount == null) ? 0 : amount.hashCode());
    1.56 +		result = prime * result
    1.57 +				+ ((currency == null) ? 0 : currency.hashCode());
    1.58 +		return result;
    1.59 +	}
    1.60 +
    1.61 +	@Override
    1.62 +	public boolean equals(Object obj) {
    1.63 +		if (this == obj)
    1.64 +			return true;
    1.65 +		if (obj == null)
    1.66 +			return false;
    1.67 +		if (!(obj instanceof MoneyImpl))
    1.68 +			return false;
    1.69 +		MoneyImpl other = (MoneyImpl) obj;
    1.70 +		if (amount == null) {
    1.71 +			if (other.amount != null)
    1.72 +				return false;
    1.73 +		} else if (!amount.equals(other.amount))
    1.74 +			return false;
    1.75 +		if (currency == null) {
    1.76 +			if (other.currency != null)
    1.77 +				return false;
    1.78 +		} else if (!currency.equals(other.currency))
    1.79 +			return false;
    1.80 +		return true;
    1.81 +	}
    1.82 +	
    1.83 +	@Override
    1.84 +	public String toString() {
    1.85 +		return getClass().getName()+"["+currency+amount+"]";
    1.86 +	}
    1.87 +
    1.88 +	
    1.89 +	
    1.90 +}