Start using JsonNull.INSTANCE everywhere and remove creation method.

This commit is contained in:
Joel Leitch 2011-06-03 19:02:28 +00:00
parent 8fd4072f9b
commit c9ee7adcc5
3 changed files with 9 additions and 22 deletions

View File

@ -28,7 +28,7 @@ import java.io.IOException;
public final class JsonNull extends JsonElement { public final class JsonNull extends JsonElement {
/** /**
* singleton for JsonNull * singleton for JsonNull
* *
* @since 1.8 * @since 1.8
*/ */
public static final JsonNull INSTANCE = new JsonNull(); public static final JsonNull INSTANCE = new JsonNull();
@ -46,7 +46,7 @@ public final class JsonNull extends JsonElement {
protected void toString(Appendable sb, Escaper escaper) throws IOException { protected void toString(Appendable sb, Escaper escaper) throws IOException {
sb.append("null"); sb.append("null");
} }
/** /**
* All instances of JsonNull have the same hash code since they are indistinguishable * All instances of JsonNull have the same hash code since they are indistinguishable
*/ */
@ -54,7 +54,7 @@ public final class JsonNull extends JsonElement {
public int hashCode() { public int hashCode() {
return JsonNull.class.hashCode(); return JsonNull.class.hashCode();
} }
/** /**
* All instances of JsonNull are the same * All instances of JsonNull are the same
*/ */
@ -62,18 +62,4 @@ public final class JsonNull extends JsonElement {
public boolean equals(Object other) { public boolean equals(Object other) {
return this == other || other instanceof JsonNull; return this == other || other instanceof JsonNull;
} }
/**
* Creation method used to return an instance of a {@link JsonNull}. To reduce the memory
* footprint, a single object has been created for this class; therefore the same instance is
* being returned for each invocation of this method. This method is kept private since we
* prefer the users to use {@link JsonNull#JsonNull()} which is similar to how other JsonElements
* are created. Note that all instances of JsonNull return true for {@link #equals(Object)}
* when compared to each other.
*
* @return a instance of a {@link JsonNull}
*/
static JsonNull createJsonNull() {
return INSTANCE;
}
} }

View File

@ -17,6 +17,7 @@
package com.google.gson; package com.google.gson;
import com.google.gson.common.MoreAsserts; import com.google.gson.common.MoreAsserts;
import junit.framework.TestCase; import junit.framework.TestCase;
/** /**
@ -28,7 +29,6 @@ public final class JsonNullTest extends TestCase {
public void testEqualsAndHashcode() { public void testEqualsAndHashcode() {
MoreAsserts.assertEqualsAndHashCode(new JsonNull(), new JsonNull()); MoreAsserts.assertEqualsAndHashCode(new JsonNull(), new JsonNull());
MoreAsserts.assertEqualsAndHashCode(new JsonNull(), JsonNull.INSTANCE); MoreAsserts.assertEqualsAndHashCode(new JsonNull(), JsonNull.INSTANCE);
MoreAsserts.assertEqualsAndHashCode(new JsonNull(), JsonNull.createJsonNull()); MoreAsserts.assertEqualsAndHashCode(JsonNull.INSTANCE, JsonNull.INSTANCE);
MoreAsserts.assertEqualsAndHashCode(JsonNull.createJsonNull(), JsonNull.createJsonNull());
} }
} }

View File

@ -17,6 +17,7 @@
package com.google.gson; package com.google.gson;
import com.google.gson.common.MoreAsserts; import com.google.gson.common.MoreAsserts;
import junit.framework.TestCase; import junit.framework.TestCase;
/** /**
@ -56,12 +57,12 @@ public class JsonObjectTest extends TestCase {
public void testAddingNullOrEmptyPropertyName() throws Exception { public void testAddingNullOrEmptyPropertyName() throws Exception {
JsonObject jsonObj = new JsonObject(); JsonObject jsonObj = new JsonObject();
try { try {
jsonObj.add(null, JsonNull.createJsonNull()); jsonObj.add(null, JsonNull.INSTANCE);
fail("Should not allow null property names."); fail("Should not allow null property names.");
} catch (NullPointerException expected) { } } catch (NullPointerException expected) { }
jsonObj.add("", JsonNull.createJsonNull()); jsonObj.add("", JsonNull.INSTANCE);
jsonObj.add(" \t", JsonNull.createJsonNull()); jsonObj.add(" \t", JsonNull.INSTANCE);
} }
public void testAddingBooleanProperties() throws Exception { public void testAddingBooleanProperties() throws Exception {