1 | package geniusweb.issuevalue;
|
---|
2 |
|
---|
3 | import java.math.BigDecimal;
|
---|
4 |
|
---|
5 | import org.eclipse.jdt.annotation.NonNull;
|
---|
6 |
|
---|
7 | import com.fasterxml.jackson.annotation.JsonValue;
|
---|
8 |
|
---|
9 | /**
|
---|
10 | * A value containing a number, that should be in some {@link NumberValueSet}.
|
---|
11 | * immutable
|
---|
12 | */
|
---|
13 | public class NumberValue implements Value {
|
---|
14 | // deserializes nicely but needs custom deserializer.
|
---|
15 | private final @NonNull BigDecimal value;
|
---|
16 |
|
---|
17 | @SuppressWarnings("unused")
|
---|
18 | public NumberValue(@NonNull String vnew) {
|
---|
19 | if (vnew == null)
|
---|
20 | throw new NullPointerException("value must be not null");
|
---|
21 | this.value = new BigDecimal(vnew);
|
---|
22 | }
|
---|
23 |
|
---|
24 | @SuppressWarnings("unused")
|
---|
25 | public NumberValue(@NonNull BigDecimal vnew) {
|
---|
26 | if (vnew == null)
|
---|
27 | throw new NullPointerException("value must be not null");
|
---|
28 | this.value = vnew;
|
---|
29 | }
|
---|
30 |
|
---|
31 | @Override
|
---|
32 | @JsonValue
|
---|
33 | public @NonNull BigDecimal getValue() {
|
---|
34 | return value;
|
---|
35 | }
|
---|
36 |
|
---|
37 | @Override
|
---|
38 | public @NonNull String toString() {
|
---|
39 | return value.toString();
|
---|
40 | }
|
---|
41 |
|
---|
42 | @Override
|
---|
43 | public int hashCode() {
|
---|
44 | // WORKAROUND FOR BigDecimal hashCode. NOT auto generated.
|
---|
45 | // I think this is because a single number can be represented in
|
---|
46 | // BigDecimal in different ways.
|
---|
47 | // Very large numbers and numbers that differ only in a fraction
|
---|
48 | // beyond reach of double may get the same hashcode,
|
---|
49 | // but this is not an issue as equal hashcode only indicates that
|
---|
50 | // numbers MAY be equal.
|
---|
51 | return Double.hashCode(value.doubleValue());
|
---|
52 | }
|
---|
53 |
|
---|
54 | @Override
|
---|
55 | public boolean equals(Object obj) {
|
---|
56 | if (this == obj)
|
---|
57 | return true;
|
---|
58 | if (obj == null)
|
---|
59 | return false;
|
---|
60 | if (getClass() != obj.getClass())
|
---|
61 | return false;
|
---|
62 | NumberValue other = (NumberValue) obj;
|
---|
63 | if (value == null) {
|
---|
64 | if (other.value != null)
|
---|
65 | return false;
|
---|
66 | } else if (value.compareTo(other.value) != 0)
|
---|
67 | // MANUAL FIX, DO NOT USE value.equals
|
---|
68 | return false;
|
---|
69 | return true;
|
---|
70 | }
|
---|
71 |
|
---|
72 | }
|
---|