japod@6
|
1 |
package org.apidesign.apifest08.currency;
|
japod@6
|
2 |
|
japod@21
|
3 |
import static org.apidesign.apifest08.currency.Assert.notNull;
|
japod@21
|
4 |
|
japod@6
|
5 |
import java.math.BigDecimal;
|
japod@6
|
6 |
import java.math.RoundingMode;
|
japod@6
|
7 |
import java.util.Currency;
|
japod@6
|
8 |
|
japod@6
|
9 |
/**
|
japod@6
|
10 |
* An amount representation. Amount is represented as composition of a value and
|
japod@6
|
11 |
* a currency.
|
japod@6
|
12 |
*/
|
japod@6
|
13 |
public final class Amount {
|
japod@6
|
14 |
|
japod@6
|
15 |
private final BigDecimal value;
|
japod@6
|
16 |
private final Currency currency;
|
japod@6
|
17 |
private final int scale;
|
japod@6
|
18 |
private final RoundingMode roundingMode;
|
japod@6
|
19 |
|
japod@6
|
20 |
public static final RoundingMode DEFAULT_ROUNDING = RoundingMode.HALF_EVEN;
|
japod@6
|
21 |
|
japod@6
|
22 |
public Amount(final BigDecimal value, final Currency currency) {
|
japod@6
|
23 |
notNull(value, "value");
|
japod@6
|
24 |
notNull(currency, "currency");
|
japod@6
|
25 |
this.value = value;
|
japod@6
|
26 |
this.currency = currency;
|
japod@6
|
27 |
this.scale = currency.getDefaultFractionDigits();
|
japod@6
|
28 |
this.roundingMode = DEFAULT_ROUNDING;
|
japod@6
|
29 |
}
|
japod@6
|
30 |
|
japod@6
|
31 |
public Amount(final BigDecimal value, final Currency currency, final RoundingMode roundingMode) {
|
japod@6
|
32 |
notNull(value, "value");
|
japod@6
|
33 |
notNull(currency, "currency");
|
japod@6
|
34 |
notNull(roundingMode, "roundingMode");
|
japod@6
|
35 |
|
japod@6
|
36 |
this.value = value;
|
japod@6
|
37 |
this.currency = currency;
|
japod@6
|
38 |
this.scale = currency.getDefaultFractionDigits();
|
japod@6
|
39 |
this.roundingMode = roundingMode;
|
japod@6
|
40 |
}
|
japod@6
|
41 |
|
japod@6
|
42 |
public Amount(final long value, final Currency currency) {
|
japod@6
|
43 |
this(BigDecimal.valueOf(value), currency);
|
japod@6
|
44 |
}
|
japod@6
|
45 |
|
japod@6
|
46 |
public Amount(final String value, final Currency currency) {
|
japod@6
|
47 |
this(new BigDecimal(value), currency);
|
japod@6
|
48 |
}
|
japod@6
|
49 |
|
japod@6
|
50 |
/**
|
japod@6
|
51 |
* @return the value with scale of the associated currency and rounded by
|
japod@6
|
52 |
* the rounding mode.
|
japod@6
|
53 |
*/
|
japod@6
|
54 |
public BigDecimal getValue() {
|
japod@6
|
55 |
return value.setScale(scale, roundingMode);
|
japod@6
|
56 |
}
|
japod@6
|
57 |
|
japod@6
|
58 |
/**
|
japod@6
|
59 |
* @return the raw (no explicit scale, no explicit rounding) value
|
japod@6
|
60 |
*/
|
japod@6
|
61 |
public BigDecimal getRawValue() {
|
japod@6
|
62 |
return value;
|
japod@6
|
63 |
}
|
japod@6
|
64 |
|
japod@6
|
65 |
public Currency getCurrency() {
|
japod@6
|
66 |
return currency;
|
japod@6
|
67 |
}
|
japod@6
|
68 |
|
japod@6
|
69 |
public int getScale() {
|
japod@6
|
70 |
return scale;
|
japod@6
|
71 |
}
|
japod@6
|
72 |
|
japod@6
|
73 |
public RoundingMode getRoundingMode() {
|
japod@6
|
74 |
return roundingMode;
|
japod@6
|
75 |
}
|
japod@6
|
76 |
|
japod@6
|
77 |
@Override
|
japod@6
|
78 |
public String toString() {
|
japod@6
|
79 |
return value + ",- " + currency.toString();
|
japod@6
|
80 |
}
|
japod@6
|
81 |
}
|