Reduce use of DataSchemaContextTree.getChild() 50/81450/2
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 8 Apr 2019 19:16:26 +0000 (21:16 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Mon, 8 Apr 2019 21:46:35 +0000 (23:46 +0200)
findChild() is a better replacement, use that instead of the
deprecated method.

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

index 7a984d98240f663156b252e72dea9e101c8f0cd1..1990b1590ea35fd6db614922a0450d4d86b8fd21 100644 (file)
@@ -11,6 +11,7 @@ import static com.google.common.base.Preconditions.checkArgument;
 import static java.util.Objects.requireNonNull;
 
 import com.google.common.base.MoreObjects;
+import java.util.Optional;
 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
@@ -90,13 +91,13 @@ final class InMemoryDataTree extends AbstractDataTreeTip implements DataTree {
         LOG.debug("Following schema contexts will be attempted {}", newSchemaContext);
 
         final DataSchemaContextTree contextTree = DataSchemaContextTree.from(newSchemaContext);
-        final DataSchemaContextNode<?> rootContextNode = contextTree.getChild(getRootPath());
-        if (rootContextNode == null) {
+        final Optional<DataSchemaContextNode<?>> rootContextNode = contextTree.findChild(getRootPath());
+        if (!rootContextNode.isPresent()) {
             LOG.warn("Could not find root {} in new schema context, not upgrading", getRootPath());
             return;
         }
 
-        final DataSchemaNode rootSchemaNode = rootContextNode.getDataSchemaNode();
+        final DataSchemaNode rootSchemaNode = rootContextNode.get().getDataSchemaNode();
         if (!(rootSchemaNode instanceof DataNodeContainer)) {
             LOG.warn("Root {} resolves to non-container type {}, not upgrading", getRootPath(), rootSchemaNode);
             return;
index b36776d88c479ed86695185e768c801830cce934..bbd229cb8371067da55367eb1ff105545e36bee9 100644 (file)
@@ -7,7 +7,9 @@
  */
 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
 
-import com.google.common.base.Preconditions;
+import static com.google.common.base.Preconditions.checkArgument;
+
+import java.util.Optional;
 import org.kohsuke.MetaInfServices;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
@@ -78,34 +80,33 @@ public final class InMemoryDataTreeFactory implements DataTreeFactory {
     private static DataSchemaNode getRootSchemaNode(final SchemaContext schemaContext,
             final YangInstanceIdentifier rootPath) {
         final DataSchemaContextTree contextTree = DataSchemaContextTree.from(schemaContext);
-        final DataSchemaContextNode<?> rootContextNode = contextTree.getChild(rootPath);
-        Preconditions.checkArgument(rootContextNode != null, "Failed to find root %s in schema context", rootPath);
+        final Optional<DataSchemaContextNode<?>> rootContextNode = contextTree.findChild(rootPath);
+        checkArgument(rootContextNode.isPresent(), "Failed to find root %s in schema context", rootPath);
 
-        final DataSchemaNode rootSchemaNode = rootContextNode.getDataSchemaNode();
-        Preconditions.checkArgument(rootSchemaNode instanceof DataNodeContainer,
-            "Root %s resolves to non-container type %s", rootPath, rootSchemaNode);
+        final DataSchemaNode rootSchemaNode = rootContextNode.get().getDataSchemaNode();
+        checkArgument(rootSchemaNode instanceof DataNodeContainer, "Root %s resolves to non-container type %s",
+            rootPath, rootSchemaNode);
         return rootSchemaNode;
     }
 
     private static NormalizedNode<?, ?> createRoot(final DataNodeContainer schemaNode,
             final YangInstanceIdentifier path) {
         if (path.isEmpty()) {
-            Preconditions.checkArgument(schemaNode instanceof ContainerSchemaNode,
+            checkArgument(schemaNode instanceof ContainerSchemaNode,
                 "Conceptual tree root has to be a container, not %s", schemaNode);
             return ROOT_CONTAINER;
         }
 
         final PathArgument arg = path.getLastPathArgument();
         if (schemaNode instanceof ContainerSchemaNode) {
-            Preconditions.checkArgument(arg instanceof NodeIdentifier, "Mismatched container %s path %s", schemaNode,
-                path);
+            checkArgument(arg instanceof NodeIdentifier, "Mismatched container %s path %s", schemaNode, path);
             return ImmutableContainerNodeBuilder.create().withNodeIdentifier((NodeIdentifier) arg).build();
         } else if (schemaNode instanceof ListSchemaNode) {
             // This can either be a top-level list or its individual entry
             if (arg instanceof NodeIdentifierWithPredicates) {
                 return ImmutableNodes.mapEntryBuilder().withNodeIdentifier((NodeIdentifierWithPredicates) arg).build();
             }
-            Preconditions.checkArgument(arg instanceof NodeIdentifier, "Mismatched list %s path %s", schemaNode, path);
+            checkArgument(arg instanceof NodeIdentifier, "Mismatched list %s path %s", schemaNode, path);
             return ImmutableNodes.mapNodeBuilder().withNodeIdentifier((NodeIdentifier) arg).build();
         } else {
             throw new IllegalArgumentException("Unsupported root schema " + schemaNode);