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);
22 this.amount = amount.setScale(2);
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);
35 * @see org.apidesign.apifest08.currency.Money#getAmount()
37 public BigDecimal getAmount() {
42 * @see org.apidesign.apifest08.currency.Money#getCurrency()
44 public Currency getCurrency() {
49 public int hashCode() {
52 result = prime * result + ((amount == null) ? 0 : amount.hashCode());
53 result = prime * result
54 + ((currency == null) ? 0 : currency.hashCode());
59 public boolean equals(Object obj) {
64 if (!(obj instanceof MoneyImpl))
66 MoneyImpl other = (MoneyImpl) obj;
68 if (other.amount != null)
70 } else if (!amount.equals(other.amount))
72 if (currency == null) {
73 if (other.currency != null)
75 } else if (!currency.equals(other.currency))
81 public String toString() {
82 return getClass().getName()+"["+currency+amount+"]";