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@34
|
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@34
|
26 |
|
japod@34
|
27 |
|
japod@6
|
28 |
public MoneyImpl(long amount, Currency currency) {
|
japod@6
|
29 |
this(BigDecimal.valueOf(amount), currency);
|
japod@6
|
30 |
}
|
japod@6
|
31 |
|
japod@6
|
32 |
public MoneyImpl(double amount, Currency currency) {
|
japod@6
|
33 |
this(BigDecimal.valueOf(amount), currency);
|
japod@6
|
34 |
}
|
japod@34
|
35 |
|
japod@34
|
36 |
/**
|
japod@34
|
37 |
* Factory method.
|
japod@34
|
38 |
* @param amount
|
japod@34
|
39 |
* @param currency
|
japod@34
|
40 |
* @return
|
japod@34
|
41 |
*/
|
japod@34
|
42 |
public static final Money money(BigDecimal amount, Currency currency)
|
japod@34
|
43 |
{
|
japod@34
|
44 |
return new MoneyImpl(amount, currency);
|
japod@34
|
45 |
}
|
japod@34
|
46 |
/**
|
japod@34
|
47 |
* Factory method.
|
japod@34
|
48 |
* @param amount
|
japod@34
|
49 |
* @param currency
|
japod@34
|
50 |
* @return
|
japod@34
|
51 |
*/
|
japod@34
|
52 |
public static final Money money(long amount, Currency currency)
|
japod@34
|
53 |
{
|
japod@34
|
54 |
return new MoneyImpl(amount, currency);
|
japod@34
|
55 |
}
|
japod@34
|
56 |
/**
|
japod@34
|
57 |
* Factory method.
|
japod@34
|
58 |
* @param amount
|
japod@34
|
59 |
* @param currency
|
japod@34
|
60 |
* @return
|
japod@34
|
61 |
*/
|
japod@34
|
62 |
public static final Money money(double amount, Currency currency)
|
japod@34
|
63 |
{
|
japod@34
|
64 |
return new MoneyImpl(amount, currency);
|
japod@34
|
65 |
}
|
japod@6
|
66 |
|
japod@16
|
67 |
/**
|
japod@16
|
68 |
* Returns amount.
|
japod@16
|
69 |
* @return
|
japod@6
|
70 |
*/
|
japod@6
|
71 |
public BigDecimal getAmount() {
|
japod@6
|
72 |
return amount;
|
japod@6
|
73 |
}
|
japod@6
|
74 |
|
japod@16
|
75 |
/**
|
japod@16
|
76 |
* Returns currency.
|
japod@6
|
77 |
*/
|
japod@6
|
78 |
public Currency getCurrency() {
|
japod@6
|
79 |
return currency;
|
japod@6
|
80 |
}
|
japod@6
|
81 |
|
japod@6
|
82 |
@Override
|
japod@6
|
83 |
public int hashCode() {
|
japod@6
|
84 |
final int prime = 31;
|
japod@6
|
85 |
int result = 1;
|
japod@6
|
86 |
result = prime * result + ((amount == null) ? 0 : amount.hashCode());
|
japod@6
|
87 |
result = prime * result
|
japod@6
|
88 |
+ ((currency == null) ? 0 : currency.hashCode());
|
japod@6
|
89 |
return result;
|
japod@6
|
90 |
}
|
japod@6
|
91 |
|
japod@6
|
92 |
@Override
|
japod@6
|
93 |
public boolean equals(Object obj) {
|
japod@6
|
94 |
if (this == obj)
|
japod@6
|
95 |
return true;
|
japod@6
|
96 |
if (obj == null)
|
japod@6
|
97 |
return false;
|
japod@6
|
98 |
if (!(obj instanceof MoneyImpl))
|
japod@6
|
99 |
return false;
|
japod@6
|
100 |
MoneyImpl other = (MoneyImpl) obj;
|
japod@6
|
101 |
if (amount == null) {
|
japod@6
|
102 |
if (other.amount != null)
|
japod@6
|
103 |
return false;
|
japod@16
|
104 |
} else if (amount.compareTo(other.amount)!=0)
|
japod@6
|
105 |
return false;
|
japod@6
|
106 |
if (currency == null) {
|
japod@6
|
107 |
if (other.currency != null)
|
japod@6
|
108 |
return false;
|
japod@6
|
109 |
} else if (!currency.equals(other.currency))
|
japod@6
|
110 |
return false;
|
japod@6
|
111 |
return true;
|
japod@6
|
112 |
}
|
japod@6
|
113 |
|
japod@6
|
114 |
@Override
|
japod@6
|
115 |
public String toString() {
|
japod@6
|
116 |
return getClass().getName()+"["+currency+amount+"]";
|
japod@6
|
117 |
}
|
japod@6
|
118 |
|
japod@6
|
119 |
|
japod@6
|
120 |
|
japod@6
|
121 |
}
|