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 52f252593d217d36884b9e346658f19cbf16609b..3b92321f54d18394750167cdf8982e6848a91674 100644 (file)
@@ -11,7 +11,6 @@ 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.Strings;
 import com.google.common.collect.Iterables;
 import com.google.common.collect.Maps;
 import java.util.Arrays;
@@ -35,7 +34,18 @@ 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
+    }
+
+    /**
+     * 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 YangInstanceIdentifier rootPath,
@@ -46,11 +56,9 @@ public final class NormalizedNodes {
 
     public static Optional<NormalizedNode<?, ?>> findNode(final Optional<NormalizedNode<?, ?>> parent,
             final Iterable<PathArgument> relativePath) {
-        requireNonNull(parent, "Parent must not be null");
-        requireNonNull(relativePath, "Relative path must not be null");
-
-        Optional<NormalizedNode<?, ?>> currentNode = parent;
-        final Iterator<PathArgument> pathIterator = relativePath.iterator();
+        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());
         }
@@ -81,22 +89,20 @@ public final class NormalizedNodes {
 
     public static Optional<NormalizedNode<?, ?>> findNode(final NormalizedNode<?, ?> tree,
             final YangInstanceIdentifier path) {
-        requireNonNull(tree, "Tree must not be null");
-        requireNonNull(path, "Path must not be null");
-
-        return findNode(Optional.of(tree), path.getPathArguments());
+        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<?>) {
+        if (node instanceof ValueNode) {
             return Optional.empty();
-        } else if (node instanceof DataContainerNode<?>) {
+        } 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<?> && pathArg instanceof NodeWithValue) {
+        } else if (node instanceof LeafSetNode && pathArg instanceof NodeWithValue) {
             return (Optional) ((LeafSetNode<?>) node).getChild((NodeWithValue) pathArg);
         }
         return Optional.empty();
@@ -115,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");
@@ -135,23 +141,11 @@ public final class NormalizedNodes {
 
     private static String toStringTree(final PathArgument identifier) {
         if (identifier instanceof NodeIdentifierWithPredicates) {
-            return identifier.getNodeType().getLocalName()
-                + ((NodeIdentifierWithPredicates) identifier).getKeyValues().values();
+            return identifier.getNodeType().getLocalName() + ((NodeIdentifierWithPredicates) identifier).values();
         } else if (identifier instanceof AugmentationIdentifier) {
             return "augmentation";
         } else {
             return identifier.getNodeType().getLocalName();
         }
     }
-
-    /**
-     * 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());
-    }
 }