Use String concatenation instead of StringBuffer/Builder
[yangtools.git] / yang / yang-data-api / src / main / java / org / opendaylight / yangtools / yang / data / api / schema / NormalizedNodes.java
index 5f305d988c02e5d691e00405bff98775596ff950..835855f45e109f87ef68100ebedec18ea25bbcdb 100644 (file)
@@ -10,8 +10,13 @@ package org.opendaylight.yangtools.yang.data.api.schema;
 import static com.google.common.base.Preconditions.checkNotNull;
 import com.google.common.annotations.Beta;
 import com.google.common.base.Optional;
+import com.google.common.base.Predicate;
 import com.google.common.base.Strings;
+import com.google.common.collect.Maps;
+import java.util.Arrays;
 import java.util.Iterator;
+import java.util.Map;
+import javax.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.NodeIdentifierWithPredicates;
@@ -24,6 +29,12 @@ import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgum
 @Beta
 public final class NormalizedNodes {
     private static final int STRINGTREE_INDENT = 4;
+    private static final Predicate<DuplicateEntry> DUPLICATES_ONLY = new Predicate<DuplicateEntry>() {
+        @Override
+        public boolean apply(final DuplicateEntry input) {
+            return !input.getDuplicates().isEmpty();
+        }
+    };
 
     private NormalizedNodes() {
         throw new UnsupportedOperationException("Utility class should not be instantiated");
@@ -38,18 +49,37 @@ public final class NormalizedNodes {
         }
     }
 
-    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 Optional<NormalizedNode<?, ?>> parent, final Iterable<PathArgument> relativePath) {
+        checkNotNull(parent, "Parent must not be null");
+        checkNotNull(relativePath, "Relative path must not be null");
 
-        Optional<NormalizedNode<?, ?>> currentNode = Optional.<NormalizedNode<?, ?>> of(tree);
-        final Iterator<PathArgument> pathIterator = path.getPathArguments().iterator();
+        Optional<NormalizedNode<?, ?>> currentNode = parent;
+        final Iterator<PathArgument> pathIterator = relativePath.iterator();
         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.<NormalizedNode<?, ?>>fromNullable(parent), relativePath);
+    }
+
+    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) {
+        checkNotNull(tree, "Tree must not be null");
+        checkNotNull(path, "Path must not be null");
+
+        return findNode(Optional.<NormalizedNode<?, ?>>of(tree), path.getPathArguments());
+    }
+
     @SuppressWarnings({ "unchecked", "rawtypes" })
     public static Optional<NormalizedNode<?, ?>> getDirectChild(final NormalizedNode<?, ?> node, final PathArgument pathArg) {
         if (node instanceof LeafNode<?> || node instanceof LeafSetEntryNode<?>) {
@@ -97,14 +127,23 @@ 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).getKeyValues().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(@Nonnull final NormalizedNode<?, ?> node) {
+        return Maps.filterValues(DuplicateFinder.findDuplicates(node), DUPLICATES_ONLY);
+    }
 }