package geniusweb.issuevalue; import java.math.BigDecimal; import org.eclipse.jdt.annotation.NonNull; import com.fasterxml.jackson.annotation.JsonValue; /** * A value containing a number, that should be in some {@link NumberValueSet}. * immutable */ public class NumberValue implements Value { // deserializes nicely but needs custom deserializer. private final @NonNull BigDecimal value; @SuppressWarnings("unused") public NumberValue(@NonNull String vnew) { if (vnew == null) throw new NullPointerException("value must be not null"); this.value = new BigDecimal(vnew); } @SuppressWarnings("unused") public NumberValue(@NonNull BigDecimal vnew) { if (vnew == null) throw new NullPointerException("value must be not null"); this.value = vnew; } @Override @JsonValue public @NonNull BigDecimal getValue() { return value; } @Override public @NonNull String toString() { return value.toString(); } @Override public int hashCode() { // WORKAROUND FOR BigDecimal hashCode. NOT auto generated. // I think this is because a single number can be represented in // BigDecimal in different ways. // Very large numbers and numbers that differ only in a fraction // beyond reach of double may get the same hashcode, // but this is not an issue as equal hashcode only indicates that // numbers MAY be equal. return Double.hashCode(value.doubleValue()); } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; NumberValue other = (NumberValue) obj; if (value == null) { if (other.value != null) return false; } else if (value.compareTo(other.value) != 0) // MANUAL FIX, DO NOT USE value.equals return false; return true; } }