Modernize yang-data-impl 15/104115/2
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 24 Jan 2023 19:11:38 +0000 (20:11 +0100)
committerRobert Varga <nite@hq.sk>
Mon, 30 Jan 2023 08:59:27 +0000 (08:59 +0000)
Use instanceof patterns and local variable type inference where
practicable.

Change-Id: I09b47ca59d6da7dee576e3e1457f37665f3ec4e4
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
23 files changed:
data/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/codec/TypeDefinitionAwareCodec.java
data/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/codec/UnionStringCodec.java
data/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/ImmutableMountPointNormalizedNodeStreamWriter.java
data/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/ImmutableNodes.java
data/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/ImmutableNormalizedNodeStreamWriter.java
data/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/InterningLeafNodeBuilder.java
data/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/InterningLeafSetNodeBuilder.java
data/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/SchemaOrderedNormalizedNodeWriter.java
data/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/builder/impl/ImmutableAugmentationNodeBuilder.java
data/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/builder/impl/ImmutableChoiceNodeBuilder.java
data/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/builder/impl/ImmutableContainerNodeBuilder.java
data/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/builder/impl/ImmutableLeafSetNodeBuilder.java
data/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/builder/impl/ImmutableMapEntryNodeBuilder.java
data/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/builder/impl/ImmutableMapNodeBuilder.java
data/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/builder/impl/ImmutableUnkeyedListEntryNodeBuilder.java
data/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/builder/impl/ImmutableUnkeyedListNodeBuilder.java
data/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/builder/impl/ImmutableUserLeafSetNodeBuilder.java
data/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/builder/impl/ImmutableUserMapNodeBuilder.java
data/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/builder/impl/valid/DataNodeContainerValidator.java
data/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/nodes/AbstractImmutableDataContainerNode.java
data/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/nodes/LazyLeafOperations.java
data/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/nodes/LazyValues.java
data/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/nodes/UnmodifiableChildrenMap.java

index 81f6d8f8e6f95665be0e5a14359d328d1985013e..0f6effab68983a92ed2e646120f097951a2cd86b 100644 (file)
@@ -67,38 +67,38 @@ public abstract class TypeDefinitionAwareCodec<J, T extends TypeDefinition<T>> e
 
     // FIXME: do we want an Optional or a throws instead of @Nullable here?
     public static @Nullable TypeDefinitionAwareCodec<?, ?> fromType(final TypeDefinition<?> typeDefinition) {
-        if (typeDefinition instanceof BinaryTypeDefinition) {
-            return BinaryStringCodec.from((BinaryTypeDefinition)typeDefinition);
-        } else if (typeDefinition instanceof BitsTypeDefinition) {
-            return BitsStringCodec.from((BitsTypeDefinition)typeDefinition);
-        } else if (typeDefinition instanceof BooleanTypeDefinition) {
-            return BooleanStringCodec.from((BooleanTypeDefinition)typeDefinition);
-        } else if (typeDefinition instanceof DecimalTypeDefinition) {
-            return  DecimalStringCodec.from((DecimalTypeDefinition)typeDefinition);
+        if (typeDefinition instanceof BinaryTypeDefinition binaryType) {
+            return BinaryStringCodec.from(binaryType);
+        } else if (typeDefinition instanceof BitsTypeDefinition bitsType) {
+            return BitsStringCodec.from(bitsType);
+        } else if (typeDefinition instanceof BooleanTypeDefinition booleanType) {
+            return BooleanStringCodec.from(booleanType);
+        } else if (typeDefinition instanceof DecimalTypeDefinition decimalType) {
+            return  DecimalStringCodec.from(decimalType);
         } else if (typeDefinition instanceof EmptyTypeDefinition) {
             return EmptyStringCodec.INSTANCE;
-        } else if (typeDefinition instanceof EnumTypeDefinition) {
-            return EnumStringCodec.from((EnumTypeDefinition)typeDefinition);
-        } else if (typeDefinition instanceof Int8TypeDefinition) {
-            return AbstractIntegerStringCodec.from((Int8TypeDefinition) typeDefinition);
-        } else if (typeDefinition instanceof Int16TypeDefinition) {
-            return AbstractIntegerStringCodec.from((Int16TypeDefinition) typeDefinition);
-        } else if (typeDefinition instanceof Int32TypeDefinition) {
-            return AbstractIntegerStringCodec.from((Int32TypeDefinition) typeDefinition);
-        } else if (typeDefinition instanceof Int64TypeDefinition) {
-            return AbstractIntegerStringCodec.from((Int64TypeDefinition) typeDefinition);
-        } else if (typeDefinition instanceof StringTypeDefinition) {
-            return StringStringCodec.from((StringTypeDefinition)typeDefinition);
-        } else if (typeDefinition instanceof Uint8TypeDefinition) {
-            return AbstractIntegerStringCodec.from((Uint8TypeDefinition) typeDefinition);
-        } else if (typeDefinition instanceof Uint16TypeDefinition) {
-            return AbstractIntegerStringCodec.from((Uint16TypeDefinition) typeDefinition);
-        } else if (typeDefinition instanceof Uint32TypeDefinition) {
-            return AbstractIntegerStringCodec.from((Uint32TypeDefinition) typeDefinition);
-        } else if (typeDefinition instanceof Uint64TypeDefinition) {
-            return AbstractIntegerStringCodec.from((Uint64TypeDefinition) typeDefinition);
-        } else if (ENABLE_UNION_CODEC && typeDefinition instanceof UnionTypeDefinition) {
-            return UnionStringCodec.from((UnionTypeDefinition)typeDefinition);
+        } else if (typeDefinition instanceof EnumTypeDefinition enumType) {
+            return EnumStringCodec.from(enumType);
+        } else if (typeDefinition instanceof Int8TypeDefinition int8Type) {
+            return AbstractIntegerStringCodec.from(int8Type);
+        } else if (typeDefinition instanceof Int16TypeDefinition int16Type) {
+            return AbstractIntegerStringCodec.from(int16Type);
+        } else if (typeDefinition instanceof Int32TypeDefinition int32Type) {
+            return AbstractIntegerStringCodec.from(int32Type);
+        } else if (typeDefinition instanceof Int64TypeDefinition int64Type) {
+            return AbstractIntegerStringCodec.from(int64Type);
+        } else if (typeDefinition instanceof StringTypeDefinition stringType) {
+            return StringStringCodec.from(stringType);
+        } else if (typeDefinition instanceof Uint8TypeDefinition uint8Type) {
+            return AbstractIntegerStringCodec.from(uint8Type);
+        } else if (typeDefinition instanceof Uint16TypeDefinition uint16Type) {
+            return AbstractIntegerStringCodec.from(uint16Type);
+        } else if (typeDefinition instanceof Uint32TypeDefinition uint32Type) {
+            return AbstractIntegerStringCodec.from(uint32Type);
+        } else if (typeDefinition instanceof Uint64TypeDefinition uint64Type) {
+            return AbstractIntegerStringCodec.from(uint64Type);
+        } else if (ENABLE_UNION_CODEC && typeDefinition instanceof UnionTypeDefinition unionType) {
+            return UnionStringCodec.from(unionType);
         } else {
             return null;
         }
index d5a47c0e37437180bacf27375c3f8552354b5366..9adbbce0fbfd1a9af68df2dc9d682b82df5de5a0 100644 (file)
@@ -70,6 +70,6 @@ final class UnionStringCodec extends TypeDefinitionAwareCodec<Object, UnionTypeD
 
     @Override
     protected String serializeImpl(final Object data) {
-        return data instanceof byte[] ? Base64.getEncoder().encodeToString((byte[]) data) : data.toString();
+        return data instanceof byte[] bytes ? Base64.getEncoder().encodeToString(bytes) : data.toString();
     }
 }
index 35539c27c22e6831bc36b6be6f4351aa3593227f..78fc599b1cd141b72b042976244ed0f37d9b8f93 100644 (file)
@@ -16,7 +16,6 @@ import org.opendaylight.yangtools.rfc8528.data.api.MountPointIdentifier;
 import org.opendaylight.yangtools.rfc8528.data.api.StreamWriterMountPointExtension;
 import org.opendaylight.yangtools.rfc8528.data.util.ImmutableMountPointNode;
 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
-import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.opendaylight.yangtools.yang.data.api.schema.stream.ForwardingNormalizedNodeStreamWriter;
 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriterExtension;
@@ -50,12 +49,12 @@ public abstract class ImmutableMountPointNormalizedNodeStreamWriter extends Immu
             public void close() throws IOException {
                 super.close();
 
-                final NormalizedNode data = mountResult.getResult();
-                if (!(data instanceof ContainerNode)) {
+                final var data = mountResult.getResult();
+                if (!(data instanceof ContainerNode container)) {
                     throw new IOException("Unhandled mount data " + data);
                 }
 
-                writeChild(ImmutableMountPointNode.of(mountId, mountCtx, (ContainerNode) data));
+                writeChild(ImmutableMountPointNode.of(mountId, mountCtx, container));
             }
         };
     }
index 91e7459beaa644838f1dcdf14514bc156f5c3b2d..e134419787db35d294d6c079519fb3dda0853793 100644 (file)
@@ -212,8 +212,8 @@ public final class ImmutableNodes {
                 // leaf-list entry nodes are special: they require a value and we can derive it from our instance
                 // identitifier
                 final var lastArg = id.getLastPathArgument();
-                if (lastArg instanceof NodeWithValue) {
-                    writer.scalarValue(((NodeWithValue<?>) lastArg).getValue());
+                if (lastArg instanceof NodeWithValue<?> withValue) {
+                    writer.scalarValue(withValue.getValue());
                 }
             }
         } catch (IOException e) {
index 46f2b7c25bd5631bb2ec82afc5840687de40c314..792eeb12bc28729b4c3e6c014cd2c70f8087f1fc 100644 (file)
@@ -100,7 +100,7 @@ public class ImmutableNormalizedNodeStreamWriter implements NormalizedNodeStream
      * @return {@link NormalizedNodeStreamWriter} which will write item to supplied result holder.
      */
     public static @NonNull NormalizedNodeStreamWriter from(final NormalizedNodeResult result) {
-        return result instanceof NormalizedNodeMetadataResult ? from((NormalizedNodeMetadataResult) result)
+        return result instanceof NormalizedNodeMetadataResult metadataResult ? from(metadataResult)
                 : new ImmutableNormalizedNodeStreamWriter(result);
     }
 
index f50ba27fa2cbe89827917026659e2e85cbbb76aa..f415899f7e55c77771898ac8870efe2590aa2529 100644 (file)
@@ -26,10 +26,10 @@ final class InterningLeafNodeBuilder<T> extends ImmutableLeafNodeBuilder<T> {
     }
 
     static <T> @Nullable InterningLeafNodeBuilder<T> forSchema(final @Nullable DataSchemaNode schema) {
-        if (schema instanceof LeafSchemaNode) {
-            final Optional<Interner<LeafNode<T>>> interner = LeafInterner.forSchema((LeafSchemaNode)schema);
+        if (schema instanceof LeafSchemaNode leafSchema) {
+            final Optional<Interner<LeafNode<T>>> interner = LeafInterner.forSchema(leafSchema);
             if (interner.isPresent()) {
-                return new InterningLeafNodeBuilder<>(interner.get());
+                return new InterningLeafNodeBuilder<>(interner.orElseThrow());
             }
         }
         return null;
index 65107f70199b4acd32c005fa585f19736cb98525..82c29a3bbc5b4e46664ca7b19624c6263e8bd69e 100644 (file)
@@ -31,7 +31,7 @@ final class InterningLeafSetNodeBuilder<T> extends ImmutableLeafSetNodeBuilder<T
     }
 
     private static @Nullable LeafsetEntryInterner getInterner(final @Nullable DataSchemaNode schema) {
-        return schema instanceof LeafListSchemaNode ? LeafsetEntryInterner.forSchema((LeafListSchemaNode) schema)
+        return schema instanceof LeafListSchemaNode leafListSchema ? LeafsetEntryInterner.forSchema(leafListSchema)
                 : null;
     }
 
index ccd20dac160fb98f5408bf92baa374a37b3d421e..fcc1420821c8e8ab087b48fba1fe7afafd53a80b 100644 (file)
@@ -154,21 +154,21 @@ public class SchemaOrderedNormalizedNodeWriter extends NormalizedNodeWriter {
     private boolean writeChildren(final Iterable<? extends NormalizedNode> children, final SchemaNode parentSchemaNode,
             final boolean endParent) throws IOException {
         // Augmentations cannot be gotten with node.getChild so create our own structure with augmentations resolved
-        final Multimap<QName, NormalizedNode> qnameToNodes = ArrayListMultimap.create();
+        final var qnameToNodes = ArrayListMultimap.<QName, NormalizedNode>create();
         for (final NormalizedNode child : children) {
             putChild(qnameToNodes, child);
         }
 
-        if (parentSchemaNode instanceof DataNodeContainer) {
-            if (parentSchemaNode instanceof ListSchemaNode && qnameToNodes.containsKey(parentSchemaNode.getQName())) {
+        if (parentSchemaNode instanceof DataNodeContainer parentContainer) {
+            if (parentContainer instanceof ListSchemaNode && qnameToNodes.containsKey(parentSchemaNode.getQName())) {
                 write(qnameToNodes.get(parentSchemaNode.getQName()), parentSchemaNode);
             } else {
-                for (final DataSchemaNode schemaNode : ((DataNodeContainer) parentSchemaNode).getChildNodes()) {
+                for (final DataSchemaNode schemaNode : parentContainer.getChildNodes()) {
                     write(qnameToNodes.get(schemaNode.getQName()), schemaNode);
                 }
             }
-        } else if (parentSchemaNode instanceof ChoiceSchemaNode) {
-            for (final CaseSchemaNode ccNode : ((ChoiceSchemaNode) parentSchemaNode).getCases()) {
+        } else if (parentSchemaNode instanceof ChoiceSchemaNode parentChoice) {
+            for (final CaseSchemaNode ccNode : parentChoice.getCases()) {
                 for (final DataSchemaNode dsn : ccNode.getChildNodes()) {
                     if (qnameToNodes.containsKey(dsn.getQName())) {
                         write(qnameToNodes.get(dsn.getQName()), dsn);
@@ -195,8 +195,8 @@ public class SchemaOrderedNormalizedNodeWriter extends NormalizedNodeWriter {
     }
 
     private static void putChild(final Multimap<QName, NormalizedNode> qnameToNodes, final NormalizedNode child) {
-        if (child instanceof AugmentationNode) {
-            for (DataContainerChild grandChild : ((AugmentationNode) child).body()) {
+        if (child instanceof AugmentationNode augmentChild) {
+            for (DataContainerChild grandChild : augmentChild.body()) {
                 putChild(qnameToNodes, grandChild);
             }
         } else {
index 22e2473caca32ffef05c4dfe8667e8a38a625f6f..76fb1d671643d704224565edc402a2070bb2f6ea 100644 (file)
@@ -42,21 +42,20 @@ public class ImmutableAugmentationNodeBuilder
 
     public static @NonNull DataContainerNodeBuilder<AugmentationIdentifier, AugmentationNode> create(
             final AugmentationNode node) {
-        if (!(node instanceof ImmutableAugmentationNode)) {
-            throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
+        if (!(node instanceof ImmutableAugmentationNode immutableNode)) {
+            throw new UnsupportedOperationException("Cannot initialize from class " + node.getClass());
         }
-
-        return new ImmutableAugmentationNodeBuilder((ImmutableAugmentationNode)node);
+        return new ImmutableAugmentationNodeBuilder(immutableNode);
     }
 
     @Override
     public DataContainerNodeBuilder<AugmentationIdentifier, AugmentationNode> withChild(
             final DataContainerChild child) {
         // Check nested augments
-        if (child instanceof AugmentationNode) {
-            final AugmentationIdentifier myId = getNodeIdentifier();
+        if (child instanceof AugmentationNode aug) {
+            final var myId = getNodeIdentifier();
             throw new DataValidationException(String.format(
-                "Unable to add: %s, as a child for: %s, Nested augmentations are not permitted", child.getIdentifier(),
+                "Unable to add: %s, as a child for: %s, Nested augmentations are not permitted", aug.getIdentifier(),
                 myId == null ? this : myId));
         }
 
index a7b9ab73f9cf44881f57f84553e90e04ace3f297..445191fc2420f0b20241095438a262c5905fa0ff 100644 (file)
@@ -37,11 +37,10 @@ public class ImmutableChoiceNodeBuilder extends AbstractImmutableDataContainerNo
     }
 
     public static @NonNull DataContainerNodeBuilder<NodeIdentifier, ChoiceNode> create(final ChoiceNode node) {
-        if (!(node instanceof ImmutableChoiceNode)) {
-            throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
+        if (!(node instanceof ImmutableChoiceNode immutableNode)) {
+            throw new UnsupportedOperationException("Cannot initialize from class " + node.getClass());
         }
-
-        return new ImmutableChoiceNodeBuilder((ImmutableChoiceNode)node);
+        return new ImmutableChoiceNodeBuilder(immutableNode);
     }
 
     @Override
index 35abfd734fbbd23653cf8df9092b18afe2df4b01..e62e81e80db74767dafbce99ebf3ad0ed8b4a9dc 100644 (file)
@@ -38,10 +38,10 @@ public class ImmutableContainerNodeBuilder
     }
 
     public static @NonNull DataContainerNodeBuilder<NodeIdentifier, ContainerNode> create(final ContainerNode node) {
-        if (!(node instanceof ImmutableContainerNode)) {
-            throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
+        if (!(node instanceof ImmutableContainerNode immutableNode)) {
+            throw new UnsupportedOperationException("Cannot initialize from class " + node.getClass());
         }
-        return new ImmutableContainerNodeBuilder((ImmutableContainerNode) node);
+        return new ImmutableContainerNodeBuilder(immutableNode);
     }
 
     @Override
index 155790315e297cae894e18ad4fdadba3dd0cd2b4..96f48c8b016abbf7f332a3e8d426766b7627f582 100644 (file)
@@ -145,8 +145,8 @@ public class ImmutableLeafSetNodeBuilder<T> implements ListNodeBuilder<T, System
 
         @Override
         protected boolean valueEquals(final SystemLeafSetNode<?> other) {
-            if (other instanceof ImmutableLeafSetNode) {
-                return children.equals(((ImmutableLeafSetNode<?>) other).children);
+            if (other instanceof ImmutableLeafSetNode<?> otherImmutable) {
+                return children.equals(otherImmutable.children);
             }
             if (size() != other.size()) {
                 return false;
index 693c55290c39cc3293f4f7a3c608ec0e152f3b59..96c826736bba0782273c0e0c1c73961c71f0a026 100644 (file)
@@ -57,11 +57,10 @@ public class ImmutableMapEntryNodeBuilder
 
     public static @NonNull DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> create(
             final MapEntryNode node) {
-        if (!(node instanceof ImmutableMapEntryNode)) {
+        if (!(node instanceof ImmutableMapEntryNode immutableNode)) {
             throw new UnsupportedOperationException("Cannot initialize from class " + node.getClass());
         }
-
-        return new ImmutableMapEntryNodeBuilder((ImmutableMapEntryNode)node);
+        return new ImmutableMapEntryNodeBuilder(immutableNode);
     }
 
     private static void fillQNames(final Iterable<DataContainerChild> iterable, final Map<QName, PathArgument> out) {
index 728e977e1db192401d833ee37b928e6d8cd36bd9..939efe0dfee273345114868902f5a034cf15d258 100644 (file)
@@ -33,21 +33,20 @@ public class ImmutableMapNodeBuilder implements CollectionNodeBuilder<MapEntryNo
     private @Nullable NodeIdentifier nodeIdentifier = null;
 
     protected ImmutableMapNodeBuilder() {
-        this.value = new HashMap<>(DEFAULT_CAPACITY);
+        value = new HashMap<>(DEFAULT_CAPACITY);
     }
 
     protected ImmutableMapNodeBuilder(final int sizeHint) {
         if (sizeHint >= 0) {
-            this.value = Maps.newHashMapWithExpectedSize(sizeHint);
+            value = Maps.newHashMapWithExpectedSize(sizeHint);
         } else {
-            this.value = new HashMap<>(DEFAULT_CAPACITY);
+            value = new HashMap<>(DEFAULT_CAPACITY);
         }
     }
 
     protected ImmutableMapNodeBuilder(final SystemMapNode node) {
-        this.nodeIdentifier = node.getIdentifier();
-        this.value = MapAdaptor.getDefaultInstance().takeSnapshot(
-            node instanceof ImmutableMapNode ? ((ImmutableMapNode) node).children : node.asMap());
+        nodeIdentifier = node.getIdentifier();
+        value = MapAdaptor.getDefaultInstance().takeSnapshot(accessChildren(node));
     }
 
     public static @NonNull CollectionNodeBuilder<MapEntryNode, SystemMapNode> create() {
@@ -64,13 +63,13 @@ public class ImmutableMapNodeBuilder implements CollectionNodeBuilder<MapEntryNo
 
     @Override
     public ImmutableMapNodeBuilder withChild(final MapEntryNode child) {
-        this.value.put(child.getIdentifier(), child);
+        value.put(child.getIdentifier(), child);
         return this;
     }
 
     @Override
     public ImmutableMapNodeBuilder withoutChild(final PathArgument key) {
-        this.value.remove(key);
+        value.remove(key);
         return this;
     }
 
@@ -86,7 +85,7 @@ public class ImmutableMapNodeBuilder implements CollectionNodeBuilder<MapEntryNo
 
     @Override
     public ImmutableMapNodeBuilder withNodeIdentifier(final NodeIdentifier withNodeIdentifier) {
-        this.nodeIdentifier = withNodeIdentifier;
+        nodeIdentifier = withNodeIdentifier;
         return this;
     }
 
@@ -143,9 +142,11 @@ public class ImmutableMapNodeBuilder implements CollectionNodeBuilder<MapEntryNo
 
         @Override
         protected boolean valueEquals(final SystemMapNode other) {
-            final Map<NodeIdentifierWithPredicates, MapEntryNode> otherChildren =
-                other instanceof ImmutableMapNode ? ((ImmutableMapNode) other).children : other.asMap();
-            return children.equals(otherChildren);
+            return children.equals(accessChildren(other));
         }
     }
+
+    private static @NonNull Map<NodeIdentifierWithPredicates, MapEntryNode> accessChildren(final SystemMapNode node) {
+        return node instanceof ImmutableMapNode immutableNode ? immutableNode.children : node.asMap();
+    }
 }
index ae80a7e80e4661a8d34448a80757e264e55ff996..e15d08bf6920263cc9479f8e32ff32791954cab9 100644 (file)
@@ -41,10 +41,10 @@ public class ImmutableUnkeyedListEntryNodeBuilder
 
     public static @NonNull DataContainerNodeBuilder<NodeIdentifier, UnkeyedListEntryNode> create(
             final UnkeyedListEntryNode node) {
-        if (!(node instanceof ImmutableUnkeyedListEntryNode)) {
-            throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
+        if (!(node instanceof ImmutableUnkeyedListEntryNode immutableNode)) {
+            throw new UnsupportedOperationException("Cannot initialize from class " + node.getClass());
         }
-        return new ImmutableUnkeyedListEntryNodeBuilder((ImmutableUnkeyedListEntryNode) node);
+        return new ImmutableUnkeyedListEntryNodeBuilder(immutableNode);
     }
 
     @Override
index dfb2a49ca51c857340177a30ca920a485ed71551..38fcd3395bbcd42fc21f6e9eaa09b87b9432468d 100644 (file)
@@ -28,16 +28,16 @@ public class ImmutableUnkeyedListNodeBuilder implements CollectionNodeBuilder<Un
     private boolean dirty;
 
     protected ImmutableUnkeyedListNodeBuilder() {
-        this.value = new LinkedList<>();
-        this.dirty = false;
+        value = new LinkedList<>();
+        dirty = false;
     }
 
     protected ImmutableUnkeyedListNodeBuilder(final ImmutableUnkeyedListNode node) {
-        this.nodeIdentifier = node.getIdentifier();
+        nodeIdentifier = node.getIdentifier();
         // FIXME: clean this up, notably reuse unmodified lists
-        this.value = new LinkedList<>();
+        value = new LinkedList<>();
         Iterables.addAll(value, node.body());
-        this.dirty = true;
+        dirty = true;
     }
 
     public static CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> create() {
@@ -50,11 +50,10 @@ public class ImmutableUnkeyedListNodeBuilder implements CollectionNodeBuilder<Un
 
     public static CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> create(
             final UnkeyedListNode node) {
-        if (!(node instanceof ImmutableUnkeyedListNode)) {
-            throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
+        if (!(node instanceof ImmutableUnkeyedListNode immutableNode)) {
+            throw new UnsupportedOperationException("Cannot initialize from class " + node.getClass());
         }
-
-        return new ImmutableUnkeyedListNodeBuilder((ImmutableUnkeyedListNode) node);
+        return new ImmutableUnkeyedListNodeBuilder(immutableNode);
     }
 
     private void checkDirty() {
@@ -67,7 +66,7 @@ public class ImmutableUnkeyedListNodeBuilder implements CollectionNodeBuilder<Un
     @Override
     public CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> withChild(final UnkeyedListEntryNode child) {
         checkDirty();
-        this.value.add(child);
+        value.add(child);
         return this;
     }
 
@@ -92,7 +91,7 @@ public class ImmutableUnkeyedListNodeBuilder implements CollectionNodeBuilder<Un
     @Override
     public CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> withNodeIdentifier(
             final NodeIdentifier withNodeIdentifier) {
-        this.nodeIdentifier = withNodeIdentifier;
+        nodeIdentifier = withNodeIdentifier;
         return this;
     }
 
@@ -189,8 +188,8 @@ public class ImmutableUnkeyedListNodeBuilder implements CollectionNodeBuilder<Un
         @Override
         protected boolean valueEquals(final UnkeyedListNode other) {
             final Collection<UnkeyedListEntryNode> otherChildren;
-            if (other instanceof ImmutableUnkeyedListNode) {
-                otherChildren = ((ImmutableUnkeyedListNode) other).children;
+            if (other instanceof ImmutableUnkeyedListNode immutableOther) {
+                otherChildren = immutableOther.children;
             } else {
                 otherChildren = other.body();
             }
index 5f64b8709f1d7618ba8825826d52b3fe03a02477..c206e9fa039685a1b222c8c34d4b07c8340ea5c0 100644 (file)
@@ -45,7 +45,7 @@ public class ImmutableUserLeafSetNodeBuilder<T> implements ListNodeBuilder<T, Us
     public static <T> @NonNull ListNodeBuilder<T, UserLeafSetNode<T>> create(
             final UserLeafSetNode<T> node) {
         if (!(node instanceof ImmutableUserLeafSetNode<?>)) {
-            throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
+            throw new UnsupportedOperationException("Cannot initialize from class " + node.getClass());
         }
 
         return new ImmutableUserLeafSetNodeBuilder<>((ImmutableUserLeafSetNode<T>) node);
@@ -143,8 +143,8 @@ public class ImmutableUserLeafSetNodeBuilder<T> implements ListNodeBuilder<T, Us
 
         @Override
         protected boolean valueEquals(final UserLeafSetNode<?> other) {
-            if (other instanceof ImmutableUserLeafSetNode) {
-                return children.equals(((ImmutableUserLeafSetNode<?>) other).children);
+            if (other instanceof ImmutableUserLeafSetNode<?> immutableOther) {
+                return children.equals(immutableOther.children);
             }
             // Note: performs a size() check first
             return Iterables.elementsEqual(children.values(), other.body());
index ef4ed526d62d2b824a9a4e7438f73dfaa97b42b5..0ab49807c341652de1719bb277b416b7de1535b6 100644 (file)
@@ -59,11 +59,10 @@ public class ImmutableUserMapNodeBuilder implements CollectionNodeBuilder<MapEnt
     }
 
     public static @NonNull CollectionNodeBuilder<MapEntryNode, UserMapNode> create(final UserMapNode node) {
-        if (!(node instanceof ImmutableUserMapNode)) {
-            throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
+        if (!(node instanceof ImmutableUserMapNode immutableNode)) {
+            throw new UnsupportedOperationException("Cannot initialize from class " + node.getClass());
         }
-
-        return new ImmutableUserMapNodeBuilder((ImmutableUserMapNode) node);
+        return new ImmutableUserMapNodeBuilder(immutableNode);
     }
 
     private void checkDirty() {
@@ -175,12 +174,8 @@ public class ImmutableUserMapNodeBuilder implements CollectionNodeBuilder<MapEnt
 
         @Override
         protected boolean valueEquals(final UserMapNode other) {
-            final Map<NodeIdentifierWithPredicates, MapEntryNode> otherChildren;
-            if (other instanceof ImmutableUserMapNode) {
-                otherChildren = ((ImmutableUserMapNode) other).children;
-            } else {
-                otherChildren = other.asMap();
-            }
+            final var otherChildren = other instanceof ImmutableUserMapNode immutableOther ? immutableOther.children
+                : other.asMap();
             return Iterables.elementsEqual(children.values(), otherChildren.values());
         }
     }
index abdfaabd92722cbc7641acf19ebf0e8fb19d6c87..f2d95a76c5c05ea6cf22e5d005bbfcc291458928 100644 (file)
@@ -20,7 +20,6 @@ import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
-import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
 
 /**
  * General validator for container like statements, e.g. container, list-entry, choice, augment
@@ -32,10 +31,10 @@ public class DataNodeContainerValidator {
 
     public DataNodeContainerValidator(final DataNodeContainer schema) {
         this.schema = requireNonNull(schema, "Schema was null");
-        this.childNodes = getChildNodes(schema);
+        childNodes = getChildNodes(schema);
 
-        if (schema instanceof AugmentationTarget) {
-            for (AugmentationSchemaNode augmentation : ((AugmentationTarget) schema).getAvailableAugmentations()) {
+        if (schema instanceof AugmentationTarget target) {
+            for (var augmentation : target.getAvailableAugmentations()) {
                 augments.add(DataSchemaContextNode.augmentationIdentifierFrom(augmentation));
             }
         }
@@ -65,9 +64,9 @@ public class DataNodeContainerValidator {
     private static Set<QName> getChildNodes(final DataNodeContainer nodeContainer) {
         Set<QName> allChildNodes = new HashSet<>();
 
-        for (DataSchemaNode childSchema : nodeContainer.getChildNodes()) {
-            if (childSchema instanceof CaseSchemaNode) {
-                allChildNodes.addAll(getChildNodes((DataNodeContainer) childSchema));
+        for (var childSchema : nodeContainer.getChildNodes()) {
+            if (childSchema instanceof CaseSchemaNode caseChildSchema) {
+                allChildNodes.addAll(getChildNodes(caseChildSchema));
             } else if (!(childSchema instanceof AugmentationSchemaNode)) {
                 allChildNodes.add(childSchema.getQName());
             }
index 8490d827d79b775f47dc66573edd280a25212ef4..fa8e37ff0d075a0635eca0fecf67670d668c6dc6 100644 (file)
@@ -59,8 +59,8 @@ public abstract class AbstractImmutableDataContainerNode<K extends PathArgument,
 
     @Override
     protected boolean valueEquals(final N other) {
-        if (other instanceof AbstractImmutableDataContainerNode) {
-            return children.equals(((AbstractImmutableDataContainerNode<?, ?>) other).children);
+        if (other instanceof AbstractImmutableDataContainerNode<?, ?> immutableContainer) {
+            return children.equals(immutableContainer.children);
         }
         if (size() != other.size()) {
             return false;
index e90fa9f5329de34c44859bd82dba0c828eed3ce6..3a0e707fec345eb859c4e970827ae2e6bc8bbae7 100644 (file)
@@ -51,11 +51,11 @@ public final class LazyLeafOperations {
 
     private static @NonNull DataContainerChild decodeExpendableChild(final PathArgument key,
             final @NonNull Object value) {
-        return value instanceof DataContainerChild ? (DataContainerChild) value : coerceLeaf(key, value);
+        return value instanceof DataContainerChild child ? child : coerceLeaf(key, value);
     }
 
     private static @NonNull Object encodeExpendableChild(final @NonNull DataContainerChild node) {
-        return node instanceof LeafNode ? verifyEncode(((LeafNode<?>) node).body()) : node;
+        return node instanceof LeafNode<?> leafNode ? verifyEncode(leafNode.body()) : node;
     }
 
     private static @NonNull Object verifyEncode(final @NonNull Object value) {
index 19a0cafaec2d71560b333a2560c5e2e9054b3852..60ca848087351d659ea528c56e6d34d11c856006 100644 (file)
@@ -50,7 +50,7 @@ final class LazyValues extends AbstractCollection<DataContainerChild> {
 
     @Override
     public boolean equals(final Object obj) {
-        return this == obj || obj instanceof LazyValues && map.equals(((LazyValues)obj).map);
+        return this == obj || obj instanceof LazyValues other && map.equals(other.map);
     }
 
     private static final class Iter implements Iterator<DataContainerChild> {
@@ -67,10 +67,10 @@ final class LazyValues extends AbstractCollection<DataContainerChild> {
 
         @Override
         public DataContainerChild next() {
-            final Entry<PathArgument, Object> entry = iterator.next();
-            final Object value = entry.getValue();
-            return value instanceof DataContainerChild ? (DataContainerChild) value
-                    : LazyLeafOperations.coerceLeaf(entry.getKey(), value);
+            final var entry = iterator.next();
+            final var value = entry.getValue();
+            return value instanceof DataContainerChild child ? child
+                : LazyLeafOperations.coerceLeaf(entry.getKey(), value);
         }
     }
 }
index be92269ce10fa4cf8a04258f66554a65c336d4c5..02b355ea7529c0e57a1376bf7fdd4c2da6617f80 100644 (file)
@@ -49,10 +49,7 @@ final class UnmodifiableChildrenMap implements CloneableMap<PathArgument, DataCo
      * @return Unmodifiable view
      */
     static Map<PathArgument, DataContainerChild> create(final Map<PathArgument, DataContainerChild> map) {
-        if (map instanceof UnmodifiableChildrenMap) {
-            return map;
-        }
-        if (map instanceof ImmutableMap) {
+        if (map instanceof UnmodifiableChildrenMap || map instanceof ImmutableMap) {
             return map;
         }
         if (map.isEmpty()) {
@@ -61,7 +58,6 @@ final class UnmodifiableChildrenMap implements CloneableMap<PathArgument, DataCo
         if (map.size() < WRAP_THRESHOLD) {
             return ImmutableMap.copyOf(map);
         }
-
         return new UnmodifiableChildrenMap(map);
     }
 
@@ -154,8 +150,8 @@ final class UnmodifiableChildrenMap implements CloneableMap<PathArgument, DataCo
     @Override
     @SuppressWarnings("unchecked")
     public Map<PathArgument, DataContainerChild> createMutableClone() {
-        if (delegate instanceof HashMap) {
-            return (Map<PathArgument, DataContainerChild>) ((HashMap<?, ?>) delegate).clone();
+        if (delegate instanceof HashMap<?, ?> hashMap) {
+            return (Map<PathArgument, DataContainerChild>) hashMap.clone();
         }
         return new HashMap<>(delegate);
     }