task4/solution02/src/org/apidesign/apifest08/currency/MoneyImpl.java
changeset 61 58ec6da75f6f
parent 45 251d0ed461fb
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/task4/solution02/src/org/apidesign/apifest08/currency/MoneyImpl.java	Sat Oct 11 23:38:46 2008 +0200
     1.3 @@ -0,0 +1,121 @@
     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;
    1.26 +		this.currency = currency;
    1.27 +	}
    1.28 +	
    1.29 +	
    1.30 +	
    1.31 +	public MoneyImpl(long amount, Currency currency) {
    1.32 +		this(BigDecimal.valueOf(amount), currency);
    1.33 +	}
    1.34 +	
    1.35 +	public MoneyImpl(double amount, Currency currency) {
    1.36 +		this(BigDecimal.valueOf(amount), currency);
    1.37 +	}
    1.38 +	
    1.39 +	/**
    1.40 +	 * Factory method.
    1.41 +	 * @param amount
    1.42 +	 * @param currency
    1.43 +	 * @return
    1.44 +	 */
    1.45 +	public static final Money money(BigDecimal amount, Currency currency)
    1.46 +	{
    1.47 +		return new MoneyImpl(amount, currency);
    1.48 +	}
    1.49 +	/**
    1.50 +	 * Factory method.
    1.51 +	 * @param amount
    1.52 +	 * @param currency
    1.53 +	 * @return
    1.54 +	 */
    1.55 +	public static final Money money(long amount, Currency currency)
    1.56 +	{
    1.57 +		return new MoneyImpl(amount, currency);
    1.58 +	}
    1.59 +	/**
    1.60 +	 * Factory method.
    1.61 +	 * @param amount
    1.62 +	 * @param currency
    1.63 +	 * @return
    1.64 +	 */
    1.65 +	public static final Money money(double amount, Currency currency)
    1.66 +	{
    1.67 +		return new MoneyImpl(amount, currency);
    1.68 +	}
    1.69 +
    1.70 +	/**
    1.71 +	 * Returns amount.
    1.72 +	 * @return
    1.73 +	 */
    1.74 +	public BigDecimal getAmount() {
    1.75 +		return amount;
    1.76 +	}
    1.77 +	
    1.78 +	/**
    1.79 +	 * Returns currency.
    1.80 +	 */
    1.81 +	public Currency getCurrency() {
    1.82 +		return currency;
    1.83 +	}
    1.84 +
    1.85 +	@Override
    1.86 +	public int hashCode() {
    1.87 +		final int prime = 31;
    1.88 +		int result = 1;
    1.89 +		result = prime * result + ((amount == null) ? 0 : amount.hashCode());
    1.90 +		result = prime * result
    1.91 +				+ ((currency == null) ? 0 : currency.hashCode());
    1.92 +		return result;
    1.93 +	}
    1.94 +
    1.95 +	@Override
    1.96 +	public boolean equals(Object obj) {
    1.97 +		if (this == obj)
    1.98 +			return true;
    1.99 +		if (obj == null)
   1.100 +			return false;
   1.101 +		if (!(obj instanceof MoneyImpl))
   1.102 +			return false;
   1.103 +		MoneyImpl other = (MoneyImpl) obj;
   1.104 +		if (amount == null) {
   1.105 +			if (other.amount != null)
   1.106 +				return false;
   1.107 +		} else if (amount.compareTo(other.amount)!=0)
   1.108 +			return false;
   1.109 +		if (currency == null) {
   1.110 +			if (other.currency != null)
   1.111 +				return false;
   1.112 +		} else if (!currency.equals(other.currency))
   1.113 +			return false;
   1.114 +		return true;
   1.115 +	}
   1.116 +	
   1.117 +	@Override
   1.118 +	public String toString() {
   1.119 +		return getClass().getName()+"["+currency+amount+"]";
   1.120 +	}
   1.121 +
   1.122 +	
   1.123 +	
   1.124 +}