From: Robert Varga Date: Sun, 13 Sep 2015 16:57:58 +0000 (+0200) Subject: Introduce YangInstanceIdentifier.getAncestor() X-Git-Tag: release/beryllium~319 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F92%2F26892%2F4;p=yangtools.git Introduce YangInstanceIdentifier.getAncestor() We have callsites which want to create a truncated YangInstanceIdentifier and open-code instantiation. This is not efficient, as those users do not have an understanding of the implementation details, thus they have to resort to Iterables.limit() and similar -- which forces copying of path arguments and untrusted instantiation of YangInstanceIdentifier. This patch introduces getAncestor(int), which performs this operation with explicit knowledge of a particular implementation, resulting in as much reuse as possible: for FixedYangInstanceIdentifier it uses a subList(), for StackedYangInstanceIdentifier we return either a direct/indirect parent, or use FixedYangInstanceIdentifier's version. Change-Id: I355200199ac9791bb6d027fcdfa2a1162208a627 Signed-off-by: Robert Varga --- diff --git a/yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/FixedYangInstanceIdentifier.java b/yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/FixedYangInstanceIdentifier.java index df9c0842f0..47561ffcb4 100644 --- a/yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/FixedYangInstanceIdentifier.java +++ b/yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/FixedYangInstanceIdentifier.java @@ -53,6 +53,21 @@ final class FixedYangInstanceIdentifier extends YangInstanceIdentifier implement return ret; } + @Override + public YangInstanceIdentifier getAncestor(final int depth) { + Preconditions.checkArgument(depth >= 0, "Negative depth is not allowed"); + Preconditions.checkArgument(depth <= path.size(), "Depth %s exceeds maximum depth %s", depth, path.size()); + + if (depth == path.size()) { + return this; + } + if (depth == path.size() - 1) { + // Use the parent cache + return getParent(); + } + return YangInstanceIdentifier.create(path.subList(0, depth)); + } + @Override public List getPathArguments() { return path; diff --git a/yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/StackedYangInstanceIdentifier.java b/yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/StackedYangInstanceIdentifier.java index 6b1b58a38a..3b6a634b6f 100644 --- a/yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/StackedYangInstanceIdentifier.java +++ b/yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/StackedYangInstanceIdentifier.java @@ -56,6 +56,38 @@ final class StackedYangInstanceIdentifier extends YangInstanceIdentifier impleme return parent; } + @Override + public YangInstanceIdentifier getAncestor(final int depth) { + Preconditions.checkArgument(depth >= 0, "Steps cannot be negative"); + + // Calculate how far up our FixedYangInstanceIdentifier ancestor is + int stackedDepth = 1; + YangInstanceIdentifier wlk = getParent(); + while (wlk instanceof StackedYangInstanceIdentifier) { + wlk = wlk.getParent(); + stackedDepth++; + } + + // Guaranteed to come from FixedYangInstanceIdentifier + final int fixedDepth = wlk.getPathArguments().size(); + if (fixedDepth >= depth) { + return wlk.getAncestor(depth); + } + + // Calculate our depth and check argument + final int ourDepth = stackedDepth + fixedDepth; + Preconditions.checkArgument(depth <= ourDepth, "Depth %s exceeds maximum depth %s", depth, ourDepth); + + // Requested depth is covered by the stack, traverse up for specified number of steps + final int toWalk = ourDepth - depth; + YangInstanceIdentifier result = this; + for (int i = 0; i < toWalk; ++i) { + result = result.getParent(); + } + + return result; + } + @Override public boolean isEmpty() { return false; diff --git a/yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/YangInstanceIdentifier.java b/yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/YangInstanceIdentifier.java index 849f7f0843..781891e2bb 100644 --- a/yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/YangInstanceIdentifier.java +++ b/yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/YangInstanceIdentifier.java @@ -114,10 +114,19 @@ public abstract class YangInstanceIdentifier implements Path