Add YangInstanceIdentifier.coerceParent() 05/90305/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 15:45:37 +0000 (17:45 +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>
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 a183e47639e846c88321df40ccb0fa01202a39eb..955ef2543eebe13c6682b91c4bc85d20b537a15d 100644 (file)
@@ -70,6 +70,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 ffcf3f34670fd933e5530496f5ada13cbb347b1b..55fd0aab67443d0eb0eb9f29647e538ede5c15bd 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;
@@ -140,6 +141,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.
      *