1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/task2/solution02/src/org/apidesign/apifest08/currency/MoneyImpl.java Wed Oct 01 10:43:05 2008 +0200
1.3 @@ -0,0 +1,88 @@
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 + 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 + /**
1.38 + * Returns amount.
1.39 + * @return
1.40 + */
1.41 + public BigDecimal getAmount() {
1.42 + return amount;
1.43 + }
1.44 +
1.45 + /**
1.46 + * Returns currency.
1.47 + */
1.48 + public Currency getCurrency() {
1.49 + return currency;
1.50 + }
1.51 +
1.52 + @Override
1.53 + public int hashCode() {
1.54 + final int prime = 31;
1.55 + int result = 1;
1.56 + result = prime * result + ((amount == null) ? 0 : amount.hashCode());
1.57 + result = prime * result
1.58 + + ((currency == null) ? 0 : currency.hashCode());
1.59 + return result;
1.60 + }
1.61 +
1.62 + @Override
1.63 + public boolean equals(Object obj) {
1.64 + if (this == obj)
1.65 + return true;
1.66 + if (obj == null)
1.67 + return false;
1.68 + if (!(obj instanceof MoneyImpl))
1.69 + return false;
1.70 + MoneyImpl other = (MoneyImpl) obj;
1.71 + if (amount == null) {
1.72 + if (other.amount != null)
1.73 + return false;
1.74 + } else if (amount.compareTo(other.amount)!=0)
1.75 + return false;
1.76 + if (currency == null) {
1.77 + if (other.currency != null)
1.78 + return false;
1.79 + } else if (!currency.equals(other.currency))
1.80 + return false;
1.81 + return true;
1.82 + }
1.83 +
1.84 + @Override
1.85 + public String toString() {
1.86 + return getClass().getName()+"["+currency+amount+"]";
1.87 + }
1.88 +
1.89 +
1.90 +
1.91 +}