BUG-1440: simplify parsing 58/10558/3
authorRobert Varga <rovarga@cisco.com>
Sun, 31 Aug 2014 11:43:32 +0000 (13:43 +0200)
committerRobert Varga <rovarga@cisco.com>
Mon, 1 Sep 2014 11:00:15 +0000 (13:00 +0200)
Merges the switch statement and push out the value setting into a common
method.

Change-Id: I6b8d8271bd3daab29db8f90a3e7a0a2cd1594043
Signed-off-by: Robert Varga <rovarga@cisco.com>
yang/yang-data-codec-gson/src/main/java/org/opendaylight/yangtools/yang/data/codec/gson/JsonParserStream.java

index 3bdd6de6a4a84396d8051f33d163cc4d751c4990..f3cf17aee35420ab815d4dda9300469868db0e80 100644 (file)
@@ -14,7 +14,6 @@ import com.google.gson.JsonIOException;
 import com.google.gson.JsonParseException;
 import com.google.gson.JsonSyntaxException;
 import com.google.gson.stream.JsonReader;
-import com.google.gson.stream.JsonToken;
 import com.google.gson.stream.MalformedJsonException;
 
 import java.io.Closeable;
@@ -108,31 +107,26 @@ public final class JsonParserStream implements Closeable, Flushable {
         }
     }
 
-    public void read(final JsonReader in, final AbstractNodeDataWithSchema parent) throws IOException {
+    private final void setValue(final AbstractNodeDataWithSchema parent, final String value) {
+        Preconditions.checkArgument(parent instanceof SimpleNodeDataWithSchema, "Node %s is not a simple type", parent);
+
+        final Object translatedValue = translateValueByType(value, parent.getSchema());
+        ((SimpleNodeDataWithSchema) parent).setValue(translatedValue);
+    }
 
-        final JsonToken peek = in.peek();
-        Optional<String> value = Optional.absent();
-        switch (peek) {
+    public void read(final JsonReader in, final AbstractNodeDataWithSchema parent) throws IOException {
+        switch (in.peek()) {
         case STRING:
         case NUMBER:
-            value = Optional.of(in.nextString());
+            setValue(parent, in.nextString());
             break;
         case BOOLEAN:
-            value = Optional.of(Boolean.toString(in.nextBoolean()));
+            setValue(parent, Boolean.toString(in.nextBoolean()));
             break;
         case NULL:
             in.nextNull();
-            value = Optional.of((String) null);
-            break;
-        default:
+            setValue(parent, null);
             break;
-        }
-        if (value.isPresent()) {
-            final Object translatedValue = translateValueByType(value.get(), parent.getSchema());
-            ((SimpleNodeDataWithSchema) parent).setValue(translatedValue);
-        }
-
-        switch (peek) {
         case BEGIN_ARRAY:
             in.beginArray();
             while (in.hasNext()) {
@@ -183,6 +177,7 @@ public final class JsonParserStream implements Closeable, Flushable {
         case NAME:
         case END_OBJECT:
         case END_ARRAY:
+            break;
         }
     }