Revert "BUG-7262: Operational data tree should enforce mandatory nodes"
[yangtools.git] / yang / yang-data-api / src / main / java / org / opendaylight / yangtools / yang / data / api / schema / tree / StoreTreeNodes.java
index 8ba44d93e96e243b2009b372cff0c2f00efa8978..8d786f107a0b3fd7679157f597f15273a25d8789 100644 (file)
@@ -9,10 +9,9 @@ package org.opendaylight.yangtools.yang.data.api.schema.tree;
 
 import com.google.common.annotations.Beta;
 import com.google.common.base.Optional;
-import com.google.common.base.Preconditions;
 import com.google.common.base.Predicate;
 import com.google.common.base.Predicates;
-import com.google.common.collect.Iterables;
+import com.google.common.base.Verify;
 import java.util.AbstractMap.SimpleImmutableEntry;
 import java.util.Iterator;
 import java.util.Map.Entry;
@@ -29,14 +28,16 @@ public final class StoreTreeNodes {
     }
 
     /**
-     * Finds a node in tree
+     * Finds a node in tree.
      *
+     * @param <T>
+     *          Store tree node type.
      * @param tree Data Tree
      * @param path Path to the node
      * @return Optional with node if the node is present in tree, {@link Optional#absent()} otherwise.
      */
     public static <T extends StoreTreeNode<T>> Optional<T> findNode(final T tree, final YangInstanceIdentifier path) {
-        Optional<T> current = Optional.<T> of(tree);
+        Optional<T> current = Optional.of(tree);
         Iterator<PathArgument> pathIter = path.getPathArguments().iterator();
         while (current.isPresent() && pathIter.hasNext()) {
             current = current.get().getChild(pathIter.next());
@@ -47,36 +48,37 @@ public final class StoreTreeNodes {
     public static <T extends StoreTreeNode<T>> T findNodeChecked(final T tree, final YangInstanceIdentifier path) {
         T current = tree;
 
-        int i = 1;
-        for(PathArgument pathArg : path.getPathArguments()) {
+        int depth = 1;
+        for (PathArgument pathArg : path.getPathArguments()) {
             Optional<T> potential = current.getChild(pathArg);
             if (!potential.isPresent()) {
                 throw new IllegalArgumentException(String.format("Child %s is not present in tree.",
-                        Iterables.toString(Iterables.limit(path.getPathArguments(), i))));
+                        path.getAncestor(depth)));
             }
             current = potential.get();
-            ++i;
+            ++depth;
         }
         return current;
     }
 
     /**
-     * Finds a node or closest parent in  the tree
+     * Finds a node or closest parent in the tree.
      *
+     * @param <T>
+     *          Store tree node type.
      * @param tree Data Tree
      * @param path Path to the node
      * @return Map.Entry Entry with key which is path to closest parent and value is parent node.
-     *
      */
     public static <T extends StoreTreeNode<T>> Entry<YangInstanceIdentifier, T> findClosest(final T tree,
             final YangInstanceIdentifier path) {
-        return findClosestsOrFirstMatch(tree, path, Predicates.<T>alwaysFalse());
+        return findClosestsOrFirstMatch(tree, path, Predicates.alwaysFalse());
     }
 
     public static <T extends StoreTreeNode<T>> Entry<YangInstanceIdentifier, T> findClosestsOrFirstMatch(final T tree,
             final YangInstanceIdentifier path, final Predicate<T> predicate) {
-        Optional<T> parent = Optional.<T>of(tree);
-        Optional<T> current = Optional.<T> of(tree);
+        Optional<T> parent = Optional.of(tree);
+        Optional<T> current = Optional.of(tree);
 
         int nesting = 0;
         Iterator<PathArgument> pathIter = path.getPathArguments().iterator();
@@ -86,22 +88,22 @@ public final class StoreTreeNodes {
             nesting++;
         }
         if (current.isPresent()) {
-            final YangInstanceIdentifier currentPath = YangInstanceIdentifier.create(Iterables.limit(path.getPathArguments(), nesting));
-            return new SimpleImmutableEntry<YangInstanceIdentifier,T>(currentPath,current.get());
+            final YangInstanceIdentifier currentPath = path.getAncestor(nesting);
+            return new SimpleImmutableEntry<>(currentPath, current.get());
         }
 
         /*
          * Subtracting 1 from nesting level at this point is safe, because we
          * cannot reach here with nesting == 0: that would mean the above check
          * for current.isPresent() failed, which it cannot, as current is always
-         * present. At any rate we check state just to be on the safe side.
+         * present. At any rate we verify state just to be on the safe side.
          */
-        Preconditions.checkState(nesting > 0);
-        final YangInstanceIdentifier parentPath = YangInstanceIdentifier.create(Iterables.limit(path.getPathArguments(), nesting - 1));
-        return new SimpleImmutableEntry<YangInstanceIdentifier,T>(parentPath,parent.get());
+        Verify.verify(nesting > 0);
+        return new SimpleImmutableEntry<>(path.getAncestor(nesting - 1), parent.get());
     }
 
-    public static <T extends StoreTreeNode<T>> Optional<T> getChild(final Optional<T> parent, final PathArgument child) {
+    public static <T extends StoreTreeNode<T>> Optional<T> getChild(final Optional<T> parent,
+            final PathArgument child) {
         if (parent.isPresent()) {
             return parent.get().getChild(child);
         }