task4/solution02/src/org/apidesign/apifest08/currency/MoneyImpl.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 11 Oct 2008 23:38:46 +0200
changeset 61 58ec6da75f6f
parent 45 task3/solution02/src/org/apidesign/apifest08/currency/MoneyImpl.java@251d0ed461fb
permissions -rw-r--r--
Copying structure for task4
     1 package org.apidesign.apifest08.currency;
     2 
     3 import java.io.Serializable;
     4 import java.math.BigDecimal;
     5 import java.util.Currency;
     6 
     7 /**
     8  * Default implementation of {@link Money} interface. This class is immutable.
     9  * @author lukas
    10  *
    11  */
    12 public final class MoneyImpl implements Serializable, Money{
    13 	private static final long serialVersionUID = -6091808475616516136L;
    14 
    15 	private final BigDecimal amount;
    16 	
    17 	private final Currency currency;
    18 	
    19 	public MoneyImpl(BigDecimal amount, Currency currency) {
    20 		if (amount==null) throw new NullPointerException("Amount is null");
    21 		if (currency==null) throw new NullPointerException("Currency is null"+currency);
    22 		this.amount = amount;
    23 		this.currency = currency;
    24 	}
    25 	
    26 	
    27 	
    28 	public MoneyImpl(long amount, Currency currency) {
    29 		this(BigDecimal.valueOf(amount), currency);
    30 	}
    31 	
    32 	public MoneyImpl(double amount, Currency currency) {
    33 		this(BigDecimal.valueOf(amount), currency);
    34 	}
    35 	
    36 	/**
    37 	 * Factory method.
    38 	 * @param amount
    39 	 * @param currency
    40 	 * @return
    41 	 */
    42 	public static final Money money(BigDecimal amount, Currency currency)
    43 	{
    44 		return new MoneyImpl(amount, currency);
    45 	}
    46 	/**
    47 	 * Factory method.
    48 	 * @param amount
    49 	 * @param currency
    50 	 * @return
    51 	 */
    52 	public static final Money money(long amount, Currency currency)
    53 	{
    54 		return new MoneyImpl(amount, currency);
    55 	}
    56 	/**
    57 	 * Factory method.
    58 	 * @param amount
    59 	 * @param currency
    60 	 * @return
    61 	 */
    62 	public static final Money money(double amount, Currency currency)
    63 	{
    64 		return new MoneyImpl(amount, currency);
    65 	}
    66 
    67 	/**
    68 	 * Returns amount.
    69 	 * @return
    70 	 */
    71 	public BigDecimal getAmount() {
    72 		return amount;
    73 	}
    74 	
    75 	/**
    76 	 * Returns currency.
    77 	 */
    78 	public Currency getCurrency() {
    79 		return currency;
    80 	}
    81 
    82 	@Override
    83 	public int hashCode() {
    84 		final int prime = 31;
    85 		int result = 1;
    86 		result = prime * result + ((amount == null) ? 0 : amount.hashCode());
    87 		result = prime * result
    88 				+ ((currency == null) ? 0 : currency.hashCode());
    89 		return result;
    90 	}
    91 
    92 	@Override
    93 	public boolean equals(Object obj) {
    94 		if (this == obj)
    95 			return true;
    96 		if (obj == null)
    97 			return false;
    98 		if (!(obj instanceof MoneyImpl))
    99 			return false;
   100 		MoneyImpl other = (MoneyImpl) obj;
   101 		if (amount == null) {
   102 			if (other.amount != null)
   103 				return false;
   104 		} else if (amount.compareTo(other.amount)!=0)
   105 			return false;
   106 		if (currency == null) {
   107 			if (other.currency != null)
   108 				return false;
   109 		} else if (!currency.equals(other.currency))
   110 			return false;
   111 		return true;
   112 	}
   113 	
   114 	@Override
   115 	public String toString() {
   116 		return getClass().getName()+"["+currency+amount+"]";
   117 	}
   118 
   119 	
   120 	
   121 }