author | japod@localhost |
Tue, 30 Sep 2008 11:47:02 +0200 | |
changeset 16 | 2864c6d744c0 |
parent 6 | 97662396c0fd |
permissions | -rw-r--r-- |
japod@6 | 1 |
package org.apidesign.apifest08.currency; |
japod@6 | 2 |
|
japod@6 | 3 |
import java.io.Serializable; |
japod@6 | 4 |
import java.math.BigDecimal; |
japod@6 | 5 |
import java.util.Currency; |
japod@6 | 6 |
|
japod@6 | 7 |
/** |
japod@6 | 8 |
* Default implementation of {@link Money} interface. This class is immutable. |
japod@6 | 9 |
* @author lukas |
japod@6 | 10 |
* |
japod@6 | 11 |
*/ |
japod@6 | 12 |
public final class MoneyImpl implements Serializable, Money{ |
japod@6 | 13 |
private static final long serialVersionUID = -6091808475616516136L; |
japod@6 | 14 |
|
japod@6 | 15 |
private final BigDecimal amount; |
japod@6 | 16 |
|
japod@6 | 17 |
private final Currency currency; |
japod@6 | 18 |
|
japod@6 | 19 |
public MoneyImpl(BigDecimal amount, Currency currency) { |
japod@6 | 20 |
if (amount==null) throw new NullPointerException("Amount is null"); |
japod@6 | 21 |
if (currency==null) throw new NullPointerException("Currency is null"+currency); |
japod@16 | 22 |
this.amount = amount; |
japod@6 | 23 |
this.currency = currency; |
japod@6 | 24 |
} |
japod@6 | 25 |
|
japod@6 | 26 |
public MoneyImpl(long amount, Currency currency) { |
japod@6 | 27 |
this(BigDecimal.valueOf(amount), currency); |
japod@6 | 28 |
} |
japod@6 | 29 |
|
japod@6 | 30 |
public MoneyImpl(double amount, Currency currency) { |
japod@6 | 31 |
this(BigDecimal.valueOf(amount), currency); |
japod@6 | 32 |
} |
japod@6 | 33 |
|
japod@16 | 34 |
/** |
japod@16 | 35 |
* Returns amount. |
japod@16 | 36 |
* @return |
japod@6 | 37 |
*/ |
japod@6 | 38 |
public BigDecimal getAmount() { |
japod@6 | 39 |
return amount; |
japod@6 | 40 |
} |
japod@6 | 41 |
|
japod@16 | 42 |
/** |
japod@16 | 43 |
* Returns currency. |
japod@6 | 44 |
*/ |
japod@6 | 45 |
public Currency getCurrency() { |
japod@6 | 46 |
return currency; |
japod@6 | 47 |
} |
japod@6 | 48 |
|
japod@6 | 49 |
@Override |
japod@6 | 50 |
public int hashCode() { |
japod@6 | 51 |
final int prime = 31; |
japod@6 | 52 |
int result = 1; |
japod@6 | 53 |
result = prime * result + ((amount == null) ? 0 : amount.hashCode()); |
japod@6 | 54 |
result = prime * result |
japod@6 | 55 |
+ ((currency == null) ? 0 : currency.hashCode()); |
japod@6 | 56 |
return result; |
japod@6 | 57 |
} |
japod@6 | 58 |
|
japod@6 | 59 |
@Override |
japod@6 | 60 |
public boolean equals(Object obj) { |
japod@6 | 61 |
if (this == obj) |
japod@6 | 62 |
return true; |
japod@6 | 63 |
if (obj == null) |
japod@6 | 64 |
return false; |
japod@6 | 65 |
if (!(obj instanceof MoneyImpl)) |
japod@6 | 66 |
return false; |
japod@6 | 67 |
MoneyImpl other = (MoneyImpl) obj; |
japod@6 | 68 |
if (amount == null) { |
japod@6 | 69 |
if (other.amount != null) |
japod@6 | 70 |
return false; |
japod@16 | 71 |
} else if (amount.compareTo(other.amount)!=0) |
japod@6 | 72 |
return false; |
japod@6 | 73 |
if (currency == null) { |
japod@6 | 74 |
if (other.currency != null) |
japod@6 | 75 |
return false; |
japod@6 | 76 |
} else if (!currency.equals(other.currency)) |
japod@6 | 77 |
return false; |
japod@6 | 78 |
return true; |
japod@6 | 79 |
} |
japod@6 | 80 |
|
japod@6 | 81 |
@Override |
japod@6 | 82 |
public String toString() { |
japod@6 | 83 |
return getClass().getName()+"["+currency+amount+"]"; |
japod@6 | 84 |
} |
japod@6 | 85 |
|
japod@6 | 86 |
|
japod@6 | 87 |
|
japod@6 | 88 |
} |