Use instanceof patterns in netconf-util 28/101728/2
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 4 Jul 2022 20:40:37 +0000 (22:40 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 5 Jul 2022 02:12:30 +0000 (04:12 +0200)
We can improve expressiveness here, eliminating casts.

Change-Id: I0f4a43b9bbbf019dbf98f36037b7d1e8db47cae9
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/NetconfUtil.java
netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/StreamingContext.java

index 59ad61c8fe11c5bf29e944d0d130790c25c7b63a..867c782776c36f1cd372dcade4449e7ef4359d15 100644 (file)
@@ -335,8 +335,8 @@ public final class NetconfUtil {
 
         final Element childElement = data.getOwnerDocument().createElementNS(elementNamespace, nodeType.getLocalName());
         data.appendChild(childElement);
-        if (pathArg instanceof NodeIdentifierWithPredicates) {
-            appendListKeyNodes(childElement, (NodeIdentifierWithPredicates) pathArg);
+        if (pathArg instanceof NodeIdentifierWithPredicates nip) {
+            appendListKeyNodes(childElement, nip);
         }
         for (final PathNode childrenNode : pathArgumentTree.children()) {
             pathArgumentTreeToXmlStructure(childrenNode, childElement);
index 593c98e99357f2f762df9d1dc1859bd7e5187db9..7283841ef914ce3583e0cfa1b7a5c267253528d8 100644 (file)
@@ -72,19 +72,20 @@ abstract class StreamingContext<T extends PathArgument> implements Identifiable<
     }
 
     static StreamingContext<?> fromDataSchemaNode(final DataSchemaNode potential) {
-        if (potential instanceof ContainerSchemaNode) {
-            return new Container((ContainerSchemaNode) potential);
-        } else if (potential instanceof ListSchemaNode) {
-            return fromListSchemaNode((ListSchemaNode) potential);
-        } else if (potential instanceof LeafSchemaNode) {
-            return new Leaf((LeafSchemaNode) potential);
-        } else if (potential instanceof ChoiceSchemaNode) {
-            return new Choice((ChoiceSchemaNode) potential);
-        } else if (potential instanceof LeafListSchemaNode) {
-            return fromLeafListSchemaNode((LeafListSchemaNode) potential);
-        } else if (potential instanceof AnyxmlSchemaNode) {
-            return new AnyXml((AnyxmlSchemaNode) potential);
-        }
+        if (potential instanceof ContainerSchemaNode container) {
+            return new Container(container);
+        } else if (potential instanceof ListSchemaNode list) {
+            return fromListSchemaNode(list);
+        } else if (potential instanceof LeafSchemaNode leaf) {
+            return new Leaf(leaf);
+        } else if (potential instanceof ChoiceSchemaNode choice) {
+            return new Choice(choice);
+        } else if (potential instanceof LeafListSchemaNode leafList) {
+            return fromLeafListSchemaNode(leafList);
+        } else if (potential instanceof AnyxmlSchemaNode anyxml) {
+            return new AnyXml(anyxml);
+        }
+        // FIXME: unhandled anydata!
         return null;
     }
 
@@ -235,9 +236,8 @@ abstract class StreamingContext<T extends PathArgument> implements Identifiable<
         }
 
         private StreamingContext<?> fromLocalSchema(final PathArgument child) {
-            if (child instanceof AugmentationIdentifier) {
-                return fromSchemaAndQNameChecked(schema, ((AugmentationIdentifier) child).getPossibleChildNames()
-                        .iterator().next());
+            if (child instanceof AugmentationIdentifier aid) {
+                return fromSchemaAndQNameChecked(schema, aid.getPossibleChildNames().iterator().next());
             }
             return fromSchemaAndQNameChecked(schema, child.getNodeType());
         }
@@ -266,11 +266,10 @@ abstract class StreamingContext<T extends PathArgument> implements Identifiable<
         @Override
         final void emitChildTreeNode(final NormalizedNodeStreamWriter writer, final PathNode node) throws IOException {
             final PathArgument element = node.element();
-            if (!(element instanceof NodeIdentifierWithPredicates)) {
+            if (!(element instanceof NodeIdentifierWithPredicates childPath)) {
                 throw new IOException("Child identifier " + element + " is invalid in parent " + getIdentifier());
             }
 
-            final NodeIdentifierWithPredicates childPath = (NodeIdentifierWithPredicates) element;
             final StreamingContext<?> childOp = getChildOperation(childPath);
             if (childPath.size() == 0 && node.isEmpty() || childPath.keySet().containsAll(keyLeaves)) {
                 // This is a query for the entire list, or the query specifies everything we need