Refactor yang-model-api child traversal return types
[yangtools.git] / yang / yang-data-api / src / main / java / org / opendaylight / yangtools / yang / data / api / schema / NormalizedNodes.java
index 5f305d988c02e5d691e00405bff98775596ff950..3b92321f54d18394750167cdf8982e6848a91674 100644 (file)
@@ -7,16 +7,24 @@
  */
 package org.opendaylight.yangtools.yang.data.api.schema;
 
-import static com.google.common.base.Preconditions.checkNotNull;
+import static com.google.common.base.Preconditions.checkArgument;
+import static java.util.Objects.requireNonNull;
+
 import com.google.common.annotations.Beta;
-import com.google.common.base.Optional;
-import com.google.common.base.Strings;
+import com.google.common.collect.Iterables;
+import com.google.common.collect.Maps;
+import java.util.Arrays;
 import java.util.Iterator;
+import java.util.Map;
+import java.util.Optional;
+import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
+import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 
 /**
  * A set of utility methods for interacting with {@link NormalizedNode} objects.
@@ -26,42 +34,78 @@ public final class NormalizedNodes {
     private static final int STRINGTREE_INDENT = 4;
 
     private NormalizedNodes() {
-        throw new UnsupportedOperationException("Utility class should not be instantiated");
+        // Hidden on purpose
     }
 
-    public static Optional<NormalizedNode<?, ?>> findNode(final YangInstanceIdentifier rootPath, final NormalizedNode<?, ?> rootNode, final YangInstanceIdentifier childPath) {
-        final Optional<YangInstanceIdentifier> relativePath = childPath.relativeTo(rootPath);
-        if (relativePath.isPresent()) {
-            return findNode(rootNode, relativePath.get());
-        } else {
-            return Optional.absent();
-        }
+    /**
+     * Find duplicate NormalizedNode instances within a subtree. Duplicates are those, which compare
+     * as equal, but do not refer to the same object.
+     *
+     * @param node A normalized node subtree, may not be null
+     * @return A Map of NormalizedNode/DuplicateEntry relationships.
+     */
+    public static Map<NormalizedNode<?, ?>, DuplicateEntry> findDuplicates(final @NonNull NormalizedNode<?, ?> node) {
+        return Maps.filterValues(DuplicateFinder.findDuplicates(node), input -> !input.getDuplicates().isEmpty());
     }
 
-    public static Optional<NormalizedNode<?, ?>> findNode(final NormalizedNode<?, ?> tree, final YangInstanceIdentifier path) {
-        checkNotNull(tree, "Tree must not be null");
-        checkNotNull(path, "Path must not be null");
+    public static Optional<NormalizedNode<?, ?>> findNode(final YangInstanceIdentifier rootPath,
+            final NormalizedNode<?, ?> rootNode, final YangInstanceIdentifier childPath) {
+        final Optional<YangInstanceIdentifier> relativePath = childPath.relativeTo(rootPath);
+        return relativePath.isPresent() ? findNode(rootNode, relativePath.get()) : Optional.empty();
+    }
 
-        Optional<NormalizedNode<?, ?>> currentNode = Optional.<NormalizedNode<?, ?>> of(tree);
-        final Iterator<PathArgument> pathIterator = path.getPathArguments().iterator();
+    public static Optional<NormalizedNode<?, ?>> findNode(final Optional<NormalizedNode<?, ?>> parent,
+            final Iterable<PathArgument> relativePath) {
+        final Iterator<PathArgument> pathIterator = requireNonNull(relativePath, "Relative path must not be null")
+                .iterator();
+        Optional<NormalizedNode<?, ?>> currentNode = requireNonNull(parent, "Parent must not be null");
         while (currentNode.isPresent() && pathIterator.hasNext()) {
             currentNode = getDirectChild(currentNode.get(), pathIterator.next());
         }
         return currentNode;
     }
 
+    public static Optional<NormalizedNode<?, ?>> findNode(final Optional<NormalizedNode<?, ?>> parent,
+            final PathArgument... relativePath) {
+        return findNode(parent, Arrays.asList(relativePath));
+    }
+
+    public static Optional<NormalizedNode<?, ?>> findNode(final NormalizedNode<?, ?> parent,
+            final Iterable<PathArgument> relativePath) {
+        return findNode(Optional.ofNullable(parent), relativePath);
+    }
+
+    public static Optional<NormalizedNode<?, ?>> findNode(final NormalizedNode<?, ?> parent,
+            final SchemaPath relativePath) {
+        checkArgument(!relativePath.isAbsolute(), "%s is not a relative path", relativePath);
+        return findNode(Optional.ofNullable(parent), Iterables.transform(relativePath.getPathFromRoot(),
+            NodeIdentifier::new));
+    }
+
+    public static Optional<NormalizedNode<?, ?>> findNode(final NormalizedNode<?, ?> parent,
+            final PathArgument... relativePath) {
+        return findNode(parent, Arrays.asList(relativePath));
+    }
+
+    public static Optional<NormalizedNode<?, ?>> findNode(final NormalizedNode<?, ?> tree,
+            final YangInstanceIdentifier path) {
+        return findNode(Optional.of(requireNonNull(tree, "Tree must not be null")),
+            requireNonNull(path, "Path must not be null").getPathArguments());
+    }
+
     @SuppressWarnings({ "unchecked", "rawtypes" })
-    public static Optional<NormalizedNode<?, ?>> getDirectChild(final NormalizedNode<?, ?> node, final PathArgument pathArg) {
-        if (node instanceof LeafNode<?> || node instanceof LeafSetEntryNode<?>) {
-            return Optional.absent();
-        } else if (node instanceof DataContainerNode<?>) {
+    public static Optional<NormalizedNode<?, ?>> getDirectChild(final NormalizedNode<?, ?> node,
+            final PathArgument pathArg) {
+        if (node instanceof ValueNode) {
+            return Optional.empty();
+        } else if (node instanceof DataContainerNode) {
             return (Optional) ((DataContainerNode<?>) node).getChild(pathArg);
         } else if (node instanceof MapNode && pathArg instanceof NodeIdentifierWithPredicates) {
             return (Optional) ((MapNode) node).getChild((NodeIdentifierWithPredicates) pathArg);
-        } else if (node instanceof LeafSetNode<?>) {
+        } else if (node instanceof LeafSetNode && pathArg instanceof NodeWithValue) {
             return (Optional) ((LeafSetNode<?>) node).getChild((NodeWithValue) pathArg);
         }
-        return Optional.absent();
+        return Optional.empty();
     }
 
     /**
@@ -77,10 +121,10 @@ public final class NormalizedNodes {
     }
 
     private static void toStringTree(final StringBuilder builder, final NormalizedNode<?, ?> node, final int offset) {
-        final String prefix = Strings.repeat(" ", offset);
+        final String prefix = " ".repeat(offset);
 
         builder.append(prefix).append(toStringTree(node.getIdentifier()));
-        if (node instanceof NormalizedNodeContainer<?, ?, ?>) {
+        if (node instanceof NormalizedNodeContainer) {
             final NormalizedNodeContainer<?, ?, ?> container = (NormalizedNodeContainer<?, ?, ?>) node;
 
             builder.append(" {\n");
@@ -97,10 +141,7 @@ public final class NormalizedNodes {
 
     private static String toStringTree(final PathArgument identifier) {
         if (identifier instanceof NodeIdentifierWithPredicates) {
-            StringBuilder builder = new StringBuilder();
-            builder.append(identifier.getNodeType().getLocalName());
-            builder.append(((NodeIdentifierWithPredicates) identifier).getKeyValues().values());
-            return builder.toString();
+            return identifier.getNodeType().getLocalName() + ((NodeIdentifierWithPredicates) identifier).values();
         } else if (identifier instanceof AugmentationIdentifier) {
             return "augmentation";
         } else {