package geniusweb.issuevalue;

import java.math.BigDecimal;

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 BigDecimal value;

	public NumberValue(String vnew) {
		this.value = new BigDecimal(vnew);
	}

	public NumberValue(BigDecimal vnew) {
		this.value = vnew;
	}

	@JsonValue
	public BigDecimal getValue() {
		return value;
	}

	@Override
	public String toString() {
		return value.toString();
	}

	@Override
	public int hashCode() {
		// WORKAROUND FOR BigDecimal hashCode. NOT auto generated.
		// 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;
	}

}
