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<PathArgument> getPathArguments() {
return path;
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;
* Return the conceptual parent {@link YangInstanceIdentifier}, which has
* one item less in {@link #getPathArguments()}.
*
- * @return Parent {@link YangInstanceIdentifier}, or null if this is object is {@link #EMPTY}.
+ * @return Parent {@link YangInstanceIdentifier}, or null if this object is {@link #EMPTY}.
*/
@Nullable public abstract YangInstanceIdentifier getParent();
+ /**
+ * Return the ancestor {@link YangInstanceIdentifier} with a particular depth, e.g. number of path arguments.
+ *
+ * @param depth Ancestor depth
+ * @return Ancestor {@link YangInstanceIdentifier}
+ * @throws IllegalArgumentException if the specified depth is negative or is greater than the depth of this object.
+ */
+ @Nonnull public abstract YangInstanceIdentifier getAncestor(int depth);
+
/**
* Returns an ordered iteration of path arguments.
*