Use floating point comparison for all non-integral Number types (such as LazilyParsedNumber)

This commit is contained in:
Jesse Wilson 2011-09-09 08:01:51 +00:00
parent 2ee89879e1
commit 2f0c617d8d

View File

@ -318,7 +318,7 @@ public final class JsonPrimitive extends JsonElement {
long value = getAsNumber().longValue();
return (int) (value ^ (value >>> 32));
}
if (isFloatingPoint(this)) {
if (value instanceof Number) {
long value = Double.doubleToLongBits(getAsNumber().doubleValue());
return (int) (value ^ (value >>> 32));
}
@ -340,7 +340,7 @@ public final class JsonPrimitive extends JsonElement {
if (isIntegral(this) && isIntegral(other)) {
return getAsNumber().longValue() == other.getAsNumber().longValue();
}
if (isFloatingPoint(this) && isFloatingPoint(other)) {
if (value instanceof Number && other.value instanceof Number) {
double a = getAsNumber().doubleValue();
// Java standard types other than double return true for two NaN. So, need
// special handling for double.
@ -362,15 +362,4 @@ public final class JsonPrimitive extends JsonElement {
}
return false;
}
/**
* Returns true if the specified number is a floating point type (BigDecimal, double, float)
*/
private static boolean isFloatingPoint(JsonPrimitive primitive) {
if (primitive.value instanceof Number) {
Number number = (Number) primitive.value;
return number instanceof BigDecimal || number instanceof Double || number instanceof Float;
}
return false;
}
}