Tighten AbstractLithiumDataOutput.writeString() 69/84269/2
authorRobert Varga <robert.varga@pantheon.tech>
Sat, 7 Sep 2019 09:55:28 +0000 (11:55 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Sat, 7 Sep 2019 10:05:43 +0000 (12:05 +0200)
We can only ever pass nulls from Revision encoding, refactor to
make that clear (and enforced) in code. This more clearly defines
the context of TokenTypes.IS_NULL_VALUE and where it can be emitted.

Change-Id: I14045c9fc3eaf04b4bde858d322de45d3160de26
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/AbstractLithiumDataOutput.java
opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/AbstractNormalizedNodeDataOutput.java

index 73a4bf7..2c3d095 100644 (file)
@@ -7,6 +7,7 @@
  */
 package org.opendaylight.controller.cluster.datastore.node.utils.stream;
 
+import static com.google.common.base.Verify.verifyNotNull;
 import static java.util.Objects.requireNonNull;
 
 import com.google.common.annotations.VisibleForTesting;
@@ -18,7 +19,9 @@ import java.math.BigInteger;
 import java.nio.charset.StandardCharsets;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Optional;
 import java.util.Set;
+import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.yangtools.yang.common.Empty;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.QNameModule;
@@ -112,23 +115,24 @@ abstract class AbstractLithiumDataOutput extends AbstractNormalizedNodeDataOutpu
 
     final void defaultWriteModule(final QNameModule module) throws IOException {
         writeString(module.getNamespace().toString());
-        writeString(module.getRevision().map(Revision::toString).orElse(null));
+        final Optional<Revision> revision = module.getRevision();
+        if (revision.isPresent()) {
+            writeString(revision.get().toString());
+        } else {
+            writeByte(TokenTypes.IS_NULL_VALUE);
+        }
     }
 
     @Override
-    protected final void writeString(final String string) throws IOException {
-        if (string != null) {
-            final Integer value = stringCodeMap.get(string);
-            if (value == null) {
-                stringCodeMap.put(string, stringCodeMap.size());
-                writeByte(TokenTypes.IS_STRING_VALUE);
-                writeUTF(string);
-            } else {
-                writeByte(TokenTypes.IS_CODE_VALUE);
-                writeInt(value);
-            }
+    protected final void writeString(final @NonNull String string) throws IOException {
+        final Integer value = stringCodeMap.get(verifyNotNull(string));
+        if (value == null) {
+            stringCodeMap.put(string, stringCodeMap.size());
+            writeByte(TokenTypes.IS_STRING_VALUE);
+            writeUTF(string);
         } else {
-            writeByte(TokenTypes.IS_NULL_VALUE);
+            writeByte(TokenTypes.IS_CODE_VALUE);
+            writeInt(value);
         }
     }
 
index 2224357..8eeef9f 100755 (executable)
@@ -77,7 +77,7 @@ abstract class AbstractNormalizedNodeDataOutput implements NormalizedNodeDataOut
 
     protected abstract short streamVersion();
 
-    protected abstract void writeString(String string) throws IOException;
+    abstract void writeString(@NonNull String string) throws IOException;
 
     @Override
     public final void write(final int value) throws IOException {

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.