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