1 package org.apidesign.apifest08.currency;
3 import java.io.Serializable;
4 import java.math.BigDecimal;
5 import java.util.Currency;
8 * Default implementation of {@link Money} interface. This class is immutable.
12 public final class MoneyImpl implements Serializable, Money{
13 private static final long serialVersionUID = -6091808475616516136L;
15 private final BigDecimal amount;
17 private final Currency currency;
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);
23 this.currency = currency;
26 public MoneyImpl(long amount, Currency currency) {
27 this(BigDecimal.valueOf(amount), currency);
30 public MoneyImpl(double amount, Currency currency) {
31 this(BigDecimal.valueOf(amount), currency);
38 public BigDecimal getAmount() {
45 public Currency getCurrency() {
50 public int hashCode() {
53 result = prime * result + ((amount == null) ? 0 : amount.hashCode());
54 result = prime * result
55 + ((currency == null) ? 0 : currency.hashCode());
60 public boolean equals(Object obj) {
65 if (!(obj instanceof MoneyImpl))
67 MoneyImpl other = (MoneyImpl) obj;
69 if (other.amount != null)
71 } else if (amount.compareTo(other.amount)!=0)
73 if (currency == null) {
74 if (other.currency != null)
76 } else if (!currency.equals(other.currency))
82 public String toString() {
83 return getClass().getName()+"["+currency+amount+"]";