Clean up XMLStreamWriterUtils 87/79587/5
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 16 Jan 2019 21:27:05 +0000 (22:27 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Fri, 18 Jan 2019 10:40:43 +0000 (11:40 +0100)
This is slight refactor propagating @NonNull annotations and
acting on them:
- we do not need to use String.valueOf()
- we do not need guard against null values
- ValueWriter is a nice place to have a utility for doing
  writeCharacters(obj.toString())

Change-Id: Idcbc24036c2ae07cf93481cba1a2e311db7def6f
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
yang/yang-data-codec-xml/src/main/java/org/opendaylight/yangtools/yang/data/codec/xml/SchemalessXMLStreamNormalizedNodeStreamWriter.java
yang/yang-data-codec-xml/src/main/java/org/opendaylight/yangtools/yang/data/codec/xml/ValueWriter.java
yang/yang-data-codec-xml/src/main/java/org/opendaylight/yangtools/yang/data/codec/xml/XMLStreamWriterUtils.java

index b793ded638defe7aa6b9822a0ae2a60c2b3c8342..e54770a4266d6434467a59dc29c61238b24ba6ee 100644 (file)
@@ -98,7 +98,7 @@ final class SchemalessXMLStreamNormalizedNodeStreamWriter extends XMLStreamNorma
     @Override
     void writeValue(final ValueWriter xmlWriter, final QName qname, final Object value, final Object context)
             throws XMLStreamException {
-        xmlWriter.writeCharacters(value.toString());
+        xmlWriter.writeToStringCharacters(value);
     }
 
     @Override
index 2efa92f84d4127efcd5697c1123389927d397228..ed25e7dcedf5c86168c8d9faf2c0490bd08a9cc6 100644 (file)
@@ -10,6 +10,7 @@ package org.opendaylight.yangtools.yang.data.codec.xml;
 import javax.xml.namespace.NamespaceContext;
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamWriter;
+import org.eclipse.jdt.annotation.NonNull;
 
 /**
  * A minimal facade for exposing just enough information from {@link XMLStreamWriter} for the purposes of encoding
@@ -32,4 +33,9 @@ abstract class ValueWriter {
 
     // Note: lookup results may change if there is other interaction
     abstract NamespaceContext getNamespaceContext();
+
+    // Utility shortcut
+    final void writeToStringCharacters(final @NonNull Object obj) throws XMLStreamException {
+        writeCharacters(obj.toString());
+    }
 }
index 1f738a8c7b10c7662fd513b1c8c7ebf1f4eeb6cb..ff86e2e7987281c55803ecda471dd29daf012dd4 100644 (file)
@@ -43,13 +43,7 @@ abstract class XMLStreamWriterUtils {
      * @throws XMLStreamException if an encoding problem occurs
      */
     void writeValue(final @NonNull ValueWriter writer, final @NonNull SchemaNode schemaNode,
-            final Object value, final QNameModule parent) throws XMLStreamException {
-        if (value == null) {
-            LOG.debug("Value of {}:{} is null, not encoding it", schemaNode.getQName().getNamespace(),
-                    schemaNode.getQName().getLocalName());
-            return;
-        }
-
+            final @NonNull Object value, final QNameModule parent) throws XMLStreamException {
         checkArgument(schemaNode instanceof TypedDataSchemaNode,
             "Unable to write value for node %s, only nodes of type: leaf and leaf-list can be written at this point",
             schemaNode.getQName());
@@ -73,33 +67,29 @@ abstract class XMLStreamWriterUtils {
      * @throws XMLStreamException if an encoding problem occurs
      */
     private void writeValue(final @NonNull ValueWriter writer, final @NonNull TypeDefinition<?> type,
-            final Object value, final QNameModule parent) throws XMLStreamException {
-        if (value == null) {
-            LOG.debug("Value of {}:{} is null, not encoding it", type.getQName().getNamespace(),
-                    type.getQName().getLocalName());
-            return;
-        }
-
+            final @NonNull Object value, final QNameModule parent) throws XMLStreamException {
         if (type instanceof IdentityrefTypeDefinition) {
             write(writer, (IdentityrefTypeDefinition) type, value, parent);
         } else if (type instanceof InstanceIdentifierTypeDefinition) {
             write(writer, (InstanceIdentifierTypeDefinition) type, value);
         } else {
-            final TypeDefinitionAwareCodec<Object, ?> codec = TypeDefinitionAwareCodec.from(type);
-            String text;
-            if (codec != null) {
-                try {
-                    text = codec.serialize(value);
-                } catch (ClassCastException e) {
-                    LOG.warn("Provided node value {} did not have type {} required by mapping. Using stream instead.",
-                            value, type, e);
-                    text = String.valueOf(value);
-                }
-            } else {
-                LOG.warn("Failed to find codec for {}, falling back to using stream", type);
-                text = String.valueOf(value);
-            }
-            writer.writeCharacters(text);
+            writer.writeCharacters(serialize(type, value));
+        }
+    }
+
+    private static String serialize(final @NonNull TypeDefinition<?> type, final @NonNull Object value) {
+        final TypeDefinitionAwareCodec<Object, ?> codec = TypeDefinitionAwareCodec.from(type);
+        if (codec == null) {
+            LOG.warn("Failed to find codec for {}, falling back to using stream", type);
+            return value.toString();
+        }
+
+        try {
+            return codec.serialize(value);
+        } catch (ClassCastException e) {
+            LOG.warn("Provided node value {} did not have type {} required by mapping. Using stream instead.",
+                value, type, e);
+            return value.toString();
         }
     }
 
@@ -118,11 +108,11 @@ abstract class XMLStreamWriterUtils {
                 writer.writeNamespace(prefix, ns);
                 writer.writeCharacters(prefix + ':' + qname.getLocalName());
             }
-
         } else {
-            LOG.debug("Value of {}:{} is not a QName but {}", type.getQName().getNamespace(),
-                    type.getQName().getLocalName(), value.getClass());
-            writer.writeCharacters(String.valueOf(value));
+            final QName qname = type.getQName();
+            LOG.debug("Value of {}:{} is not a QName but {}", qname.getNamespace(), qname.getLocalName(),
+                value.getClass());
+            writer.writeToStringCharacters(value);
         }
     }
 
@@ -131,13 +121,15 @@ abstract class XMLStreamWriterUtils {
         if (value instanceof YangInstanceIdentifier) {
             writeInstanceIdentifier(writer, (YangInstanceIdentifier)value);
         } else {
-            LOG.warn("Value of {}:{} is not an InstanceIdentifier but {}", type.getQName().getNamespace(),
-                    type.getQName().getLocalName(), value.getClass());
-            writer.writeCharacters(String.valueOf(value));
+            final QName qname = type.getQName();
+            LOG.warn("Value of {}:{} is not an InstanceIdentifier but {}", qname.getNamespace(), qname.getLocalName(),
+                value.getClass());
+            writer.writeToStringCharacters(value);
         }
     }
 
-    abstract TypeDefinition<?> getBaseTypeForLeafRef(SchemaNode schemaNode, LeafrefTypeDefinition type);
+    abstract @NonNull TypeDefinition<?> getBaseTypeForLeafRef(SchemaNode schemaNode,
+            @NonNull LeafrefTypeDefinition type);
 
     abstract void writeInstanceIdentifier(@NonNull ValueWriter writer, YangInstanceIdentifier value)
             throws XMLStreamException;