Incorporated comments from r726

This commit is contained in:
Inderjeet Singh 2011-04-04 22:09:51 +00:00
parent a147164852
commit feb9617bf0
7 changed files with 19 additions and 4 deletions

View File

@ -297,7 +297,7 @@ public final class JsonArray extends JsonElement implements Iterable<JsonElement
@Override
public boolean equals(Object o) {
return o instanceof JsonArray && ((JsonArray) o).elements.equals(elements);
return (o == this) || (o instanceof JsonArray && ((JsonArray) o).elements.equals(elements));
}
@Override

View File

@ -53,7 +53,7 @@ public final class JsonNull extends JsonElement {
*/
@Override
public boolean equals(Object other) {
return other instanceof JsonNull;
return this == other || other instanceof JsonNull;
}
/**

View File

@ -191,8 +191,8 @@ public final class JsonObject extends JsonElement {
@Override
public boolean equals(Object o) {
return o instanceof JsonObject
&& ((JsonObject) o).members.equals(members);
return (o == this) || (o instanceof JsonObject
&& ((JsonObject) o).members.equals(members));
}
@Override

View File

@ -356,6 +356,8 @@ public final class JsonPrimitive extends JsonElement {
}
if (isFloatingPoint(this) && isFloatingPoint(other)) {
double a = getAsNumber().doubleValue();
// Java standard types other than double return true for two NaN. So, need
// special handling for double.
double b = other.getAsNumber().doubleValue();
return a == b || (Double.isNaN(a) && Double.isNaN(b));
}

View File

@ -32,6 +32,8 @@ public final class JsonArrayTest extends TestCase {
JsonArray a = new JsonArray();
JsonArray b = new JsonArray();
assertEquals(a, a);
a.add(new JsonObject());
assertFalse(a.equals(b));
assertFalse(b.equals(a));

View File

@ -138,6 +138,8 @@ public class JsonObjectTest extends TestCase {
JsonObject a = new JsonObject();
JsonObject b = new JsonObject();
assertEquals(a, a);
a.add("foo", new JsonObject());
assertFalse(a.equals(b));
assertFalse(b.equals(a));

View File

@ -186,6 +186,15 @@ public class JsonPrimitiveTest extends TestCase {
MoreAsserts.assertEqualsAndHashCode(new JsonPrimitive(5L), new JsonPrimitive(5L));
MoreAsserts.assertEqualsAndHashCode(new JsonPrimitive('a'), new JsonPrimitive('a'));
MoreAsserts.assertEqualsAndHashCode(new JsonPrimitive(Float.NaN), new JsonPrimitive(Float.NaN));
MoreAsserts.assertEqualsAndHashCode(new JsonPrimitive(Float.NEGATIVE_INFINITY),
new JsonPrimitive(Float.NEGATIVE_INFINITY));
MoreAsserts.assertEqualsAndHashCode(new JsonPrimitive(Float.POSITIVE_INFINITY),
new JsonPrimitive(Float.POSITIVE_INFINITY));
MoreAsserts.assertEqualsAndHashCode(new JsonPrimitive(Double.NaN), new JsonPrimitive(Double.NaN));
MoreAsserts.assertEqualsAndHashCode(new JsonPrimitive(Double.NEGATIVE_INFINITY),
new JsonPrimitive(Double.NEGATIVE_INFINITY));
MoreAsserts.assertEqualsAndHashCode(new JsonPrimitive(Double.POSITIVE_INFINITY),
new JsonPrimitive(Double.POSITIVE_INFINITY));
assertFalse(new JsonPrimitive("a").equals(new JsonPrimitive("b")));
assertFalse(new JsonPrimitive(true).equals(new JsonPrimitive(false)));
assertFalse(new JsonPrimitive(0).equals(new JsonPrimitive(1)));