Modernize DataNodeContainerModificationStrategy 58/103158/1
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 8 Nov 2022 18:12:45 +0000 (19:12 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 8 Nov 2022 18:12:45 +0000 (19:12 +0100)
Use:
- local variable type inference
- fluent builder to simplify Immutable construction
- DataNodeContainer.dataChildByName() to remove Optional

Change-Id: Ic6f31ef98f98edcd3608bac11dc3a6170f659a5f
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
data/yang-data-tree-ri/src/main/java/org/opendaylight/yangtools/yang/data/tree/impl/DataNodeContainerModificationStrategy.java

index e99e64af6d57ab2fbf39955d6c1c79cefd3c8602..343803202bfcf3eb06fa3b9d144dbf38de4f273a 100644 (file)
@@ -10,19 +10,15 @@ package org.opendaylight.yangtools.yang.data.tree.impl;
 import static java.util.Objects.requireNonNull;
 
 import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.ImmutableMap.Builder;
-import java.util.Optional;
 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
 import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.Nullable;
-import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
 import org.opendaylight.yangtools.yang.data.tree.impl.AbstractNodeContainerModificationStrategy.Visible;
 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
-import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.DocumentedNode.WithStatus;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -52,13 +48,13 @@ class DataNodeContainerModificationStrategy<T extends DataNodeContainer & WithSt
 
     @Override
     public final ModificationApplyOperation childByArg(final PathArgument arg) {
-        final ImmutableMap<PathArgument, ModificationApplyOperation> local = children;
-        final ModificationApplyOperation existing = local.get(arg);
+        final var local = children;
+        final var existing = local.get(arg);
         if (existing != null) {
             return existing;
         }
 
-        final ModificationApplyOperation childOperation = resolveChild(arg);
+        final var childOperation = resolveChild(arg);
         return childOperation != null ? appendChild(local, arg, childOperation) : null;
     }
 
@@ -69,16 +65,16 @@ class DataNodeContainerModificationStrategy<T extends DataNodeContainer & WithSt
                 (AugmentationIdentifier) identifier, treeConfig);
         }
 
-        final QName qname = identifier.getNodeType();
-        final Optional<DataSchemaNode> child = schema.findDataChildByName(qname);
-        if (!child.isPresent()) {
+        final var qname = identifier.getNodeType();
+        final var child = schema.dataChildByName(qname);
+        if (child == null) {
             LOG.trace("Child {} not present in container schema {} children {}", identifier, this,
                 schema.getChildNodes());
             return null;
         }
 
         try {
-            return SchemaAwareApplyOperation.from(child.get(), treeConfig);
+            return SchemaAwareApplyOperation.from(child, treeConfig);
         } catch (ExcludedDataSchemaNodeException e) {
             LOG.trace("Failed to instantiate child {} in container schema {} children {}", identifier, this,
                 schema.getChildNodes(), e);
@@ -89,15 +85,14 @@ class DataNodeContainerModificationStrategy<T extends DataNodeContainer & WithSt
     private @Nullable ModificationApplyOperation appendChild(
             final ImmutableMap<PathArgument, ModificationApplyOperation> initial, final PathArgument identifier,
             final ModificationApplyOperation computed) {
-
-        ImmutableMap<PathArgument, ModificationApplyOperation> previous = initial;
+        var previous = initial;
         while (true) {
             // Build up a new map based on observed snapshot and computed child
-            final Builder<PathArgument, ModificationApplyOperation> builder = ImmutableMap.builderWithExpectedSize(
-                previous.size() + 1);
-            builder.putAll(previous);
-            builder.put(identifier, computed);
-            final ImmutableMap<PathArgument, ModificationApplyOperation> updated = builder.build();
+            final var updated = ImmutableMap
+                .<PathArgument, ModificationApplyOperation>builderWithExpectedSize(previous.size() + 1)
+                .putAll(previous)
+                .put(identifier, computed)
+                .build();
 
             // Attempt to install the updated map
             if (UPDATER.compareAndSet(this, previous, updated)) {
@@ -106,7 +101,7 @@ class DataNodeContainerModificationStrategy<T extends DataNodeContainer & WithSt
 
             // We have raced, acquire a new snapshot, recheck presence and retry if needed
             previous = children;
-            final ModificationApplyOperation raced = previous.get(identifier);
+            final var raced = previous.get(identifier);
             if (raced != null) {
                 return raced;
             }