BUG-592: Introduce InstanceIdentifier.relativeTo() 37/6037/5
authorRobert Varga <rovarga@cisco.com>
Thu, 10 Apr 2014 21:32:03 +0000 (23:32 +0200)
committerRobert Varga <rovarga@cisco.com>
Wed, 7 May 2014 09:19:11 +0000 (09:19 +0000)
This centralizes another useful utility method.

Change-Id: Ifbc1dbb08150ff1d9805f27bad3416b5f1afece0
Signed-off-by: Robert Varga <rovarga@cisco.com>
yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/InstanceIdentifier.java
yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/schema/NormalizedNodeUtils.java

index 5d154fa112699811d699171cfd557ed15324db04..c41dff83a18f7a5f20c9f896b889e700f40b27ab 100644 (file)
@@ -22,6 +22,7 @@ import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
 
+import com.google.common.base.Optional;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableSet;
@@ -101,6 +102,23 @@ public class InstanceIdentifier implements Path<InstanceIdentifier>, Immutable,
         return new InstanceIdentifier(ImmutableList.<PathArgument>builder().addAll(path).add(arg).build());
     }
 
+    /**
+     * Get the relative path from an ancestor. This method attempts to perform the reverse
+     * of concatenating a base (ancestor) and a path.
+     *
+     * @param ancestor Ancestor against which the relative path should be calculated
+     * @return This object's relative path from parent, or Optional.absent() if the
+     *         specified parent is not in fact an ancestor of this object.
+     */
+    public Optional<InstanceIdentifier> relativeTo(final InstanceIdentifier ancestor) {
+        if (ancestor.contains(this)) {
+            final int common = ancestor.path.size();
+            return Optional.of(new InstanceIdentifier(path.subList(common, path.size())));
+        } else {
+            return Optional.absent();
+        }
+    }
+
     // Static factories & helpers
 
     public static InstanceIdentifier of(final QName name) {
index 2e9b90e066800e323f12e029ae1d200e386f099b..9542a2b0a12bc1cd5af1eb1da08468c0e0e738ff 100644 (file)
@@ -30,12 +30,12 @@ public final class NormalizedNodeUtils {
     }
 
     public static Optional<NormalizedNode<?, ?>> findNode(final InstanceIdentifier rootPath, final NormalizedNode<?, ?> rootNode, final InstanceIdentifier childPath) {
-        if(rootPath.contains(childPath)) {
-            int common = rootPath.getPath().size();
-            InstanceIdentifier relativePath = new InstanceIdentifier(childPath.getPath().subList(common, childPath.getPath().size()));
-            return findNode(rootNode, relativePath);
+        final Optional<InstanceIdentifier> relativePath = childPath.relativeTo(rootPath);
+        if (relativePath.isPresent()) {
+            return findNode(rootNode, relativePath.get());
+        } else {
+            return Optional.absent();
         }
-        return Optional.absent();
     }
 
     public static Optional<NormalizedNode<?, ?>> findNode(final NormalizedNode<?, ?> tree, final InstanceIdentifier path) {
@@ -43,7 +43,7 @@ public final class NormalizedNodeUtils {
         checkNotNull(path, "Path must not be null");
 
         Optional<NormalizedNode<?, ?>> currentNode = Optional.<NormalizedNode<?, ?>> of(tree);
-        Iterator<PathArgument> pathIterator = path.getPath().iterator();
+        final Iterator<PathArgument> pathIterator = path.getPath().iterator();
         while (currentNode.isPresent() && pathIterator.hasNext()) {
             currentNode = getDirectChild(currentNode.get(), pathIterator.next());
         }