Fix the equals
method of JsonPrimitive
to work with BigInteger
(#2311)
* Fix the `equals` method of `JsonPrimitive` to work with BigInteger * Improve the `equals` & `getAsBigInteger` methods in `JsonPrimitive`
This commit is contained in:
parent
9f26679e7a
commit
af21798436
|
@ -180,8 +180,11 @@ public final class JsonPrimitive extends JsonElement {
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public BigInteger getAsBigInteger() {
|
public BigInteger getAsBigInteger() {
|
||||||
return value instanceof BigInteger ?
|
return value instanceof BigInteger
|
||||||
(BigInteger) value : new BigInteger(getAsString());
|
? (BigInteger) value
|
||||||
|
: isIntegral(this)
|
||||||
|
? BigInteger.valueOf(this.getAsNumber().longValue())
|
||||||
|
: new BigInteger(this.getAsString());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -282,7 +285,9 @@ public final class JsonPrimitive extends JsonElement {
|
||||||
return other.value == null;
|
return other.value == null;
|
||||||
}
|
}
|
||||||
if (isIntegral(this) && isIntegral(other)) {
|
if (isIntegral(this) && isIntegral(other)) {
|
||||||
return getAsNumber().longValue() == other.getAsNumber().longValue();
|
return this.value instanceof BigInteger || other.value instanceof BigInteger
|
||||||
|
? this.getAsBigInteger().equals(other.getAsBigInteger())
|
||||||
|
: this.getAsNumber().longValue() == other.getAsNumber().longValue();
|
||||||
}
|
}
|
||||||
if (value instanceof Number && other.value instanceof Number) {
|
if (value instanceof Number && other.value instanceof Number) {
|
||||||
double a = getAsNumber().doubleValue();
|
double a = getAsNumber().doubleValue();
|
||||||
|
|
|
@ -300,10 +300,8 @@ public class JsonPrimitiveTest {
|
||||||
@Test
|
@Test
|
||||||
public void testEqualsIntegerAndBigInteger() {
|
public void testEqualsIntegerAndBigInteger() {
|
||||||
JsonPrimitive a = new JsonPrimitive(5L);
|
JsonPrimitive a = new JsonPrimitive(5L);
|
||||||
JsonPrimitive b = new JsonPrimitive(new BigInteger("18446744073709551621")); // 2^64 + 5
|
JsonPrimitive b = new JsonPrimitive(new BigInteger("18446744073709551621"));
|
||||||
// Ideally, the following assertion should have failed but the price is too much to pay
|
assertWithMessage("%s not equals %s", a, b).that(a.equals(b)).isFalse();
|
||||||
// assertFalse(a + " equals " + b, a.equals(b));
|
|
||||||
assertWithMessage("%s equals %s", a, b).that(a.equals(b)).isTrue();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
Loading…
Reference in New Issue
Block a user