Use instanceof patterns in CompositeNodeDataWithSchema 71/104471/1
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 21 Feb 2023 14:50:16 +0000 (15:50 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 21 Feb 2023 16:21:21 +0000 (17:21 +0100)
Reduce the number of explicit casts.

Change-Id: Idd89ac1e59eb479b6b7ed837ff2a665b49b183f7
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit 6566c6bca77698bc09e31a01b73b1707b14e86b9)

data/yang-data-util/src/main/java/org/opendaylight/yangtools/yang/data/util/CompositeNodeDataWithSchema.java

index bea54065d292a70e82ab414329d6afdf9545158f..5b7ff3cb305baf1edd3ab939868ca469faa9d85f 100644 (file)
@@ -174,12 +174,12 @@ public class CompositeNodeDataWithSchema<T extends DataSchemaNode> extends Abstr
 
     private AbstractNodeDataWithSchema<?> addSimpleChild(final DataSchemaNode schema, final ChildReusePolicy policy) {
         final SimpleNodeDataWithSchema<?> newChild;
-        if (schema instanceof LeafSchemaNode) {
-            newChild = new LeafNodeDataWithSchema((LeafSchemaNode) schema);
-        } else if (schema instanceof AnyxmlSchemaNode) {
-            newChild = new AnyXmlNodeDataWithSchema((AnyxmlSchemaNode) schema);
-        } else if (schema instanceof AnydataSchemaNode) {
-            newChild = new AnydataNodeDataWithSchema((AnydataSchemaNode) schema);
+        if (schema instanceof LeafSchemaNode leaf) {
+            newChild = new LeafNodeDataWithSchema(leaf);
+        } else if (schema instanceof AnyxmlSchemaNode anyxml) {
+            newChild = new AnyXmlNodeDataWithSchema(anyxml);
+        } else if (schema instanceof AnydataSchemaNode anydata) {
+            newChild = new AnydataNodeDataWithSchema(anydata);
         } else {
             return null;
         }
@@ -205,9 +205,9 @@ public class CompositeNodeDataWithSchema<T extends DataSchemaNode> extends Abstr
             final DataSchemaNode choiceCandidate, final DataSchemaNode caseCandidate) {
         if (childNodes != null) {
             for (AbstractNodeDataWithSchema<?> nodeDataWithSchema : childNodes) {
-                if (nodeDataWithSchema instanceof ChoiceNodeDataWithSchema
+                if (nodeDataWithSchema instanceof ChoiceNodeDataWithSchema childChoice
                         && nodeDataWithSchema.getSchema().getQName().equals(choiceCandidate.getQName())) {
-                    CaseNodeDataWithSchema casePrevious = ((ChoiceNodeDataWithSchema) nodeDataWithSchema).getCase();
+                    CaseNodeDataWithSchema casePrevious = childChoice.getCase();
 
                     checkArgument(casePrevious.getSchema().getQName().equals(caseCandidate.getQName()),
                         "Data from case %s are specified but other data from case %s were specified earlier."
@@ -224,12 +224,12 @@ public class CompositeNodeDataWithSchema<T extends DataSchemaNode> extends Abstr
     AbstractNodeDataWithSchema<?> addCompositeChild(final DataSchemaNode schema, final ChildReusePolicy policy) {
         final CompositeNodeDataWithSchema<?> newChild;
 
-        if (schema instanceof ListSchemaNode) {
-            newChild = new ListNodeDataWithSchema((ListSchemaNode) schema);
-        } else if (schema instanceof LeafListSchemaNode) {
-            newChild = new LeafListNodeDataWithSchema((LeafListSchemaNode) schema);
-        } else if (schema instanceof ContainerLike) {
-            newChild = new ContainerNodeDataWithSchema((ContainerLike) schema);
+        if (schema instanceof ListSchemaNode list) {
+            newChild = new ListNodeDataWithSchema(list);
+        } else if (schema instanceof LeafListSchemaNode leafList) {
+            newChild = new LeafListNodeDataWithSchema(leafList);
+        } else if (schema instanceof ContainerLike containerLike) {
+            newChild = new ContainerNodeDataWithSchema(containerLike);
         } else {
             newChild = new CompositeNodeDataWithSchema<>(schema);
         }