Add YangInstanceIdentifier.coerceParent() 07/90307/1
authorRobert Varga <robert.varga@pantheon.tech>
Fri, 5 Jun 2020 15:41:08 +0000 (17:41 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Fri, 5 Jun 2020 16:31:03 +0000 (18:31 +0200)
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>
(cherry picked from commit 3c488d8405b860834471acc96e7c562156a1c777)

yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/FixedYangInstanceIdentifier.java
yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/StackedYangInstanceIdentifier.java
yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/YangInstanceIdentifier.java

index ff7499d9fd9f32a7d7a5044f95e1d628c5ab8daa..7614b96ead5cbed9d8c3eb98e1777976f9f10d9d 100644 (file)
@@ -8,6 +8,7 @@
 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;
@@ -62,6 +63,11 @@ final class FixedYangInstanceIdentifier extends YangInstanceIdentifier implement
         return ret;
     }
 
+    @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");
index 0fb647e36cf1a88a4bdd0f1f251e75007e94d3e0..7d5477be0e43fee685e7f8c0331bb35cffd38cb2 100644 (file)
@@ -69,6 +69,11 @@ final class StackedYangInstanceIdentifier extends YangInstanceIdentifier impleme
         return parent;
     }
 
+    @Override
+    public YangInstanceIdentifier coerceParent() {
+        return parent;
+    }
+
     @Override
     public YangInstanceIdentifier getAncestor(final int depth) {
         checkArgument(depth >= 0, "Steps cannot be negative");
index 542c802171c19b62ee75cf1c3f0ba24dc5eb8935..44f29d779f191e56250fbc16fd2d13356398d796 100644 (file)
@@ -12,6 +12,7 @@ import static com.google.common.base.Verify.verify;
 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;
@@ -149,6 +150,15 @@ public abstract class YangInstanceIdentifier implements Path<YangInstanceIdentif
      */
     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.
      *