There are a number of YangInstanceIdentifier users who perform
checks (or otherwise ensure) that YangInstanceIdentifier they are
dealing with is non-empty and hence its getParent() method cannot
possibly return null. These users still interact with methods
which explicitly require a non-null YangInstanceIdentifier -- hence
it would be useful to provide a bridge method which performs this
validation.
Change-Id: I7acb4b04ccbe3c536b690573519171af32089d41
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
package org.opendaylight.yangtools.yang.data.api;
import static com.google.common.base.Preconditions.checkArgument;
package org.opendaylight.yangtools.yang.data.api;
import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Verify.verifyNotNull;
import static java.util.Objects.requireNonNull;
import com.google.common.collect.ImmutableList;
import static java.util.Objects.requireNonNull;
import com.google.common.collect.ImmutableList;
+ @Override
+ public YangInstanceIdentifier coerceParent() {
+ return verifyNotNull(getParent(), "Empty instance identifier does not have a parent");
+ }
+
@Override
public YangInstanceIdentifier getAncestor(final int depth) {
checkArgument(depth >= 0, "Negative depth is not allowed");
@Override
public YangInstanceIdentifier getAncestor(final int depth) {
checkArgument(depth >= 0, "Negative depth is not allowed");
+ @Override
+ public YangInstanceIdentifier coerceParent() {
+ return parent;
+ }
+
@Override
public YangInstanceIdentifier getAncestor(final int depth) {
checkArgument(depth >= 0, "Steps cannot be negative");
@Override
public YangInstanceIdentifier getAncestor(final int depth) {
checkArgument(depth >= 0, "Steps cannot be negative");
import static java.util.Objects.requireNonNull;
import com.google.common.annotations.Beta;
import static java.util.Objects.requireNonNull;
import com.google.common.annotations.Beta;
+import com.google.common.base.VerifyException;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
*/
public abstract @Nullable YangInstanceIdentifier getParent();
*/
public abstract @Nullable YangInstanceIdentifier getParent();
+ /**
+ * Return the conceptual parent {@link YangInstanceIdentifier}, which has one item less in
+ * {@link #getPathArguments()}.
+ *
+ * @return Parent {@link YangInstanceIdentifier}
+ * @throws VerifyException if this object is {@link #empty()}.
+ */
+ public abstract @NonNull YangInstanceIdentifier coerceParent();
+
/**
* Return the ancestor {@link YangInstanceIdentifier} with a particular depth, e.g. number of path arguments.
*
/**
* Return the ancestor {@link YangInstanceIdentifier} with a particular depth, e.g. number of path arguments.
*