Use instanceof patterns in DataSchemaContextNode 68/104468/1
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 21 Feb 2023 14:46:58 +0000 (15:46 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 21 Feb 2023 16:21:21 +0000 (17:21 +0100)
Reduce the number of casts by using an instanceof pattern.

Change-Id: Ia9f4a26143d5d19b339bae2d74af0c7572d99e2f
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit 722154b4a6d7cfa080f1f3d0db291bca212b383e)

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

index 57d0bf03cd6a4e9ceb26d8bee58fd9b6644c3320..562d012f49138ed5977f9162880f86e02ff182cd 100644 (file)
@@ -73,7 +73,7 @@ public abstract class DataSchemaContextNode<T extends PathArgument> extends Abst
     // FIXME: remove this constructor. Once we do, adjust 'enterChild' visibility to package-private
     @Deprecated(forRemoval = true, since = "8.0.2")
     protected DataSchemaContextNode(final T identifier, final SchemaNode schema) {
-        this(identifier, schema instanceof DataSchemaNode ? (DataSchemaNode) schema : null);
+        this(identifier, schema instanceof DataSchemaNode dataSchema ? dataSchema : null);
     }
 
     /**
@@ -245,20 +245,20 @@ public abstract class DataSchemaContextNode<T extends PathArgument> extends Abst
     }
 
     static @NonNull DataSchemaContextNode<?> of(final @NonNull DataSchemaNode schema) {
-        if (schema instanceof ContainerLike) {
-            return new ContainerContextNode((ContainerLike) schema);
-        } else if (schema instanceof ListSchemaNode) {
-            return fromListSchemaNode((ListSchemaNode) schema);
-        } else if (schema instanceof LeafSchemaNode) {
-            return new LeafContextNode((LeafSchemaNode) schema);
-        } else if (schema instanceof ChoiceSchemaNode) {
-            return new ChoiceNodeContextNode((ChoiceSchemaNode) schema);
-        } else if (schema instanceof LeafListSchemaNode) {
-            return fromLeafListSchemaNode((LeafListSchemaNode) schema);
-        } else if (schema instanceof AnydataSchemaNode) {
-            return new AnydataContextNode((AnydataSchemaNode) schema);
-        } else if (schema instanceof AnyxmlSchemaNode) {
-            return new AnyXmlContextNode((AnyxmlSchemaNode) schema);
+        if (schema instanceof ContainerLike containerLike) {
+            return new ContainerContextNode(containerLike);
+        } else if (schema instanceof ListSchemaNode list) {
+            return fromListSchemaNode(list);
+        } else if (schema instanceof LeafSchemaNode leaf) {
+            return new LeafContextNode(leaf);
+        } else if (schema instanceof ChoiceSchemaNode choice) {
+            return new ChoiceNodeContextNode(choice);
+        } else if (schema instanceof LeafListSchemaNode leafList) {
+            return fromLeafListSchemaNode(leafList);
+        } else if (schema instanceof AnydataSchemaNode anydata) {
+            return new AnydataContextNode(anydata);
+        } else if (schema instanceof AnyxmlSchemaNode anyxml) {
+            return new AnyXmlContextNode(anyxml);
         } else {
             throw new IllegalStateException("Unhandled schema " + schema);
         }
@@ -266,20 +266,20 @@ public abstract class DataSchemaContextNode<T extends PathArgument> extends Abst
 
     // FIXME: do we tolerate null argument? do we tolerate unknown subclasses?
     static @Nullable DataSchemaContextNode<?> lenientOf(final @Nullable DataSchemaNode schema) {
-        if (schema instanceof ContainerLike) {
-            return new ContainerContextNode((ContainerLike) schema);
-        } else if (schema instanceof ListSchemaNode) {
-            return fromListSchemaNode((ListSchemaNode) schema);
-        } else if (schema instanceof LeafSchemaNode) {
-            return new LeafContextNode((LeafSchemaNode) schema);
-        } else if (schema instanceof ChoiceSchemaNode) {
-            return new ChoiceNodeContextNode((ChoiceSchemaNode) schema);
-        } else if (schema instanceof LeafListSchemaNode) {
-            return fromLeafListSchemaNode((LeafListSchemaNode) schema);
-        } else if (schema instanceof AnydataSchemaNode) {
-            return new AnydataContextNode((AnydataSchemaNode) schema);
-        } else if (schema instanceof AnyxmlSchemaNode) {
-            return new AnyXmlContextNode((AnyxmlSchemaNode) schema);
+        if (schema instanceof ContainerLike containerLike) {
+            return new ContainerContextNode(containerLike);
+        } else if (schema instanceof ListSchemaNode list) {
+            return fromListSchemaNode(list);
+        } else if (schema instanceof LeafSchemaNode leaf) {
+            return new LeafContextNode(leaf);
+        } else if (schema instanceof ChoiceSchemaNode choice) {
+            return new ChoiceNodeContextNode(choice);
+        } else if (schema instanceof LeafListSchemaNode leafList) {
+            return fromLeafListSchemaNode(leafList);
+        } else if (schema instanceof AnydataSchemaNode anydata) {
+            return new AnydataContextNode(anydata);
+        } else if (schema instanceof AnyxmlSchemaNode anyxml) {
+            return new AnyXmlContextNode(anyxml);
         } else {
             return null;
         }