diff --git a/gson/src/main/java/com/google/gson/FieldNamingPolicy.java b/gson/src/main/java/com/google/gson/FieldNamingPolicy.java index 5b20df67..e1826243 100644 --- a/gson/src/main/java/com/google/gson/FieldNamingPolicy.java +++ b/gson/src/main/java/com/google/gson/FieldNamingPolicy.java @@ -64,7 +64,10 @@ public enum FieldNamingPolicy { *
This class is thread-compatible. For some more literature on these definitions, refer to - * Effective Java. - * - *
To properly use this class across multiple thread, you will need to add some external - * synchronization to your classes/thread to get this to work properly. For example: + *
This class is conditionally thread-safe (see Item 70, Effective Java second edition). To + * properly use this class across multiple threads, you will need to add some external + * synchronization. For example: * *
- * JsonStreamParser parser = new JsonStreamParser("blah blah blah"); + * JsonStreamParser parser = new JsonStreamParser("['first'] {'second':10} 'third'"); * JsonElement element; * synchronized (someCommonObject) { * if (parser.hasNext()) { @@ -104,6 +102,11 @@ public final class JsonStreamParser implements Iterator{ } } + /** + * Returns true if a {@link JsonElement} is available on the input for consumption + * @return true if a {@link JsonElement} is available on the input, false otherwise + * @since 1.4 + */ public boolean hasNext() { synchronized (lock) { try { @@ -116,6 +119,11 @@ public final class JsonStreamParser implements Iterator { } } + /** + * This optional {@link Iterator} method is not relevant for stream parsing and hence is not + * implemented. + * @since 1.4 + */ public void remove() { throw new UnsupportedOperationException(); } diff --git a/gson/src/main/java/com/google/gson/MemoryRefStack.java b/gson/src/main/java/com/google/gson/MemoryRefStack.java index 7db962fc..c4103aa6 100644 --- a/gson/src/main/java/com/google/gson/MemoryRefStack.java +++ b/gson/src/main/java/com/google/gson/MemoryRefStack.java @@ -77,7 +77,8 @@ final class MemoryRefStack { } for (ObjectTypePair stackObject : stack) { - if (stackObject.getObj() == obj.getObj() && stackObject.getType().equals(obj.getType()) ) { + if (stackObject.getObject() == obj.getObject() + && stackObject.getType().equals(obj.getType()) ) { return true; } } diff --git a/gson/src/main/java/com/google/gson/ObjectNavigator.java b/gson/src/main/java/com/google/gson/ObjectNavigator.java index 7a56756b..118442f6 100644 --- a/gson/src/main/java/com/google/gson/ObjectNavigator.java +++ b/gson/src/main/java/com/google/gson/ObjectNavigator.java @@ -93,7 +93,7 @@ final class ObjectNavigator { public void accept(Visitor visitor) { boolean visitedWithCustomHandler = visitor.visitUsingCustomHandler(objTypePair); if (!visitedWithCustomHandler) { - Object obj = objTypePair.getObj(); + Object obj = objTypePair.getObject(); Object objectToVisit = (obj == null) ? visitor.getTarget() : obj; if (objectToVisit == null) { return; diff --git a/gson/src/main/java/com/google/gson/ObjectTypePair.java b/gson/src/main/java/com/google/gson/ObjectTypePair.java index 79b72182..ea4dc35f 100644 --- a/gson/src/main/java/com/google/gson/ObjectTypePair.java +++ b/gson/src/main/java/com/google/gson/ObjectTypePair.java @@ -1,47 +1,79 @@ +/* + * Copyright (C) 2009 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.google.gson; import java.lang.reflect.Type; - final class ObjectTypePair { +/** + * A holder class for an object and its type + * + * @author Inderjeet Singh + */ +final class ObjectTypePair { + private static final int PRIME = 31; + private final Object obj; private final Type type; + public ObjectTypePair(Object obj, Type type) { this.obj = obj; this.type = type; } - public Object getObj() { + + public Object getObject() { return obj; } + public Type getType() { return type; } + @Override public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((obj == null) ? 0 : obj.hashCode()); - result = prime * result + ((type == null) ? 0 : type.hashCode()); - return result; + // Not using type.hashCode() since I am not sure if the subclasses of type reimplement + // hashCode() to be equal for equal types + return ((obj == null) ? PRIME : obj.hashCode()); } + @Override public boolean equals(Object obj) { - if (this == obj) + if (this == obj) { return true; - if (obj == null) + } + if (obj == null) { return false; - if (getClass() != obj.getClass()) + } + if (getClass() != obj.getClass()) { return false; + } ObjectTypePair other = (ObjectTypePair) obj; if (this.obj == null) { - if (other.obj != null) + if (other.obj != null) { return false; - } else if (!this.obj.equals(other.obj)) + } + } else if (this.obj != other.obj) { // Checking for reference equality return false; + } if (type == null) { - if (other.type != null) + if (other.type != null) { return false; - } else if (!type.equals(other.type)) + } + } else if (!type.equals(other.type)) { return false; + } return true; } } diff --git a/gson/src/main/java/com/google/gson/annotations/Expose.java b/gson/src/main/java/com/google/gson/annotations/Expose.java index 706f76e0..1b9c70df 100644 --- a/gson/src/main/java/com/google/gson/annotations/Expose.java +++ b/gson/src/main/java/com/google/gson/annotations/Expose.java @@ -62,17 +62,17 @@ import java.lang.annotation.Target; public @interface Expose { /** - * If true, the field marked with this annotation is written out in the JSON while serializing. - * If false, the field marked with this annotation is skipped from the serialized output. - * Defaults to true. + * If {@code true}, the field marked with this annotation is written out in the JSON while + * serializing. If {@code false}, the field marked with this annotation is skipped from the + * serialized output. Defaults to {@code true}. * @since 1.4 */ public boolean serialize() default true; /** - * If true, the field marked with this annotation is deserialized from the JSON. - * If false, the field marked with this annotation is skipped during deserialization. - * Defaults to true. + * If {@code true}, the field marked with this annotation is deserialized from the JSON. + * If {@code false}, the field marked with this annotation is skipped during deserialization. + * Defaults to {@code true}. * @since 1.4 */ public boolean deserialize() default true; diff --git a/gson/src/test/java/com/google/gson/MemoryRefStackTest.java b/gson/src/test/java/com/google/gson/MemoryRefStackTest.java index 908bfebb..d146038b 100644 --- a/gson/src/test/java/com/google/gson/MemoryRefStackTest.java +++ b/gson/src/test/java/com/google/gson/MemoryRefStackTest.java @@ -61,13 +61,12 @@ public class MemoryRefStackTest extends TestCase { } public void testContains() throws Exception { - ObjectTypePair objA = new ObjectTypePair(new MockObject(), MockObject.class); - ObjectTypePair objB = new ObjectTypePair(new MockObject(), MockObject.class); + MockObject objA = new MockObject(); + MockObject objB = new MockObject(); assertEquals(objA, objB); - - stack.push(objA); - assertFalse(stack.contains(objB)); - assertTrue(stack.contains(objA)); + stack.push(new ObjectTypePair(objA, MockObject.class)); + assertTrue(stack.contains(new ObjectTypePair(objA, MockObject.class))); + assertFalse(stack.contains(new ObjectTypePair(objB, MockObject.class))); } private static class MockObject {