From e91aa63a54778bca132a22056bbc084bd7d9e1ae Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Mon, 4 Jul 2022 22:40:37 +0200 Subject: [PATCH] Use instanceof patterns in netconf-util We can improve expressiveness here, eliminating casts. Change-Id: I0f4a43b9bbbf019dbf98f36037b7d1e8db47cae9 Signed-off-by: Robert Varga --- .../netconf/util/NetconfUtil.java | 4 +-- .../netconf/util/StreamingContext.java | 35 +++++++++---------- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/NetconfUtil.java b/netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/NetconfUtil.java index 59ad61c8fe..867c782776 100644 --- a/netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/NetconfUtil.java +++ b/netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/NetconfUtil.java @@ -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); diff --git a/netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/StreamingContext.java b/netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/StreamingContext.java index 593c98e993..7283841ef9 100644 --- a/netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/StreamingContext.java +++ b/netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/StreamingContext.java @@ -72,19 +72,20 @@ abstract class StreamingContext 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 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 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 -- 2.36.6