Enforce InstanceIdentifier creation
[mdsal.git] / binding / yang-binding / src / main / java / org / opendaylight / yangtools / yang / binding / InstanceIdentifier.java
index f99f3a45dea4570f6dfb5723960808c89389a3b2..0c89142533d5c337fc8a503337f7b5b949fc8177 100644 (file)
@@ -7,30 +7,35 @@
  */
 package org.opendaylight.yangtools.yang.binding;
 
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Verify.verify;
+import static com.google.common.base.Verify.verifyNotNull;
+import static java.util.Objects.requireNonNull;
+
 import com.google.common.base.MoreObjects;
 import com.google.common.base.MoreObjects.ToStringHelper;
-import com.google.common.base.Preconditions;
+import com.google.common.base.VerifyException;
 import com.google.common.collect.ImmutableCollection;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Iterables;
-import java.io.IOException;
+import java.io.ObjectStreamException;
 import java.io.Serializable;
-import java.lang.reflect.Field;
-import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Iterator;
-import java.util.List;
-import org.opendaylight.yangtools.concepts.Builder;
-import org.opendaylight.yangtools.concepts.Immutable;
-import org.opendaylight.yangtools.concepts.Path;
+import java.util.Objects;
+import java.util.Optional;
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.Nullable;
+import org.opendaylight.yangtools.concepts.HierarchicalIdentifier;
 import org.opendaylight.yangtools.util.HashCodeBuilder;
 
 /**
- *
  * This instance identifier uniquely identifies a specific DataObject in the data tree modeled by YANG.
  *
+ * <p>
  * For Example let's say you were trying to refer to a node in inventory which was modeled in YANG as follows,
  *
+ * <p>
  * <pre>
  * module opendaylight-inventory {
  *      ....
@@ -47,40 +52,32 @@ import org.opendaylight.yangtools.util.HashCodeBuilder;
  * }
  * </pre>
  *
- * You could create an instance identifier as follows to get to a node with id "openflow:1"
- *
+ * <p>
+ * You can create an instance identifier as follows to get to a node with id "openflow:1": {@code
  * InstanceIdentifierBuilder.builder(Nodes.class).child(Node.class, new NodeKey(new NodeId("openflow:1")).build();
+ * }
  *
+ * <p>
  * This would be the same as using a path like so, "/nodes/node/openflow:1" to refer to the openflow:1 node
- *
  */
-public class InstanceIdentifier<T extends DataObject> implements Path<InstanceIdentifier<? extends DataObject>>, Immutable, Serializable {
-    private static final Field PATHARGUMENTS_FIELD;
-    private static final long serialVersionUID = 2L;
+public class InstanceIdentifier<T extends DataObject>
+        implements HierarchicalIdentifier<InstanceIdentifier<? extends DataObject>> {
+    private static final long serialVersionUID = 3L;
+
     /*
-     * Protected to differentiate internal and external access. Internal
-     * access is required never to modify the contents. References passed
-     * to outside entities have to be wrapped in an unmodifiable view.
+     * Protected to differentiate internal and external access. Internal access is required never to modify
+     * the contents. References passed to outside entities have to be wrapped in an unmodifiable view.
      */
-    protected transient final Iterable<PathArgument> pathArguments;
-    private final Class<T> targetType;
+    final Iterable<PathArgument> pathArguments;
+
+    private final @NonNull Class<T> targetType;
     private final boolean wildcarded;
     private final int hash;
 
-    static {
-        final Field f;
-        try {
-            f = InstanceIdentifier.class.getDeclaredField("pathArguments");
-        } catch (NoSuchFieldException | SecurityException e) {
-            throw new ExceptionInInitializerError(e);
-        }
-        f.setAccessible(true);
-        PATHARGUMENTS_FIELD = f;
-    }
-
-    InstanceIdentifier(final Class<T> type, final Iterable<PathArgument> pathArguments, final boolean wildcarded, final int hash) {
-        this.pathArguments = Preconditions.checkNotNull(pathArguments);
-        this.targetType = Preconditions.checkNotNull(type);
+    InstanceIdentifier(final Class<T> type, final Iterable<PathArgument> pathArguments, final boolean wildcarded,
+            final int hash) {
+        this.pathArguments = requireNonNull(pathArguments);
+        this.targetType = requireNonNull(type);
         this.wildcarded = wildcarded;
         this.hash = hash;
     }
@@ -90,22 +87,35 @@ public class InstanceIdentifier<T extends DataObject> implements Path<InstanceId
      *
      * @return Target type
      */
-    public final Class<T> getTargetType() {
+    public final @NonNull Class<T> getTargetType() {
         return targetType;
     }
 
+    /**
+     * Perform a safe target type adaptation of this instance identifier to target type. This method is useful when
+     * dealing with type-squashed instances.
+     *
+     * @return Path argument with target type
+     * @throws VerifyException if this instance identifier cannot be adapted to target type
+     * @throws NullPointerException if {@code target} is null
+     */
+    @SuppressWarnings("unchecked")
+    public final <N extends DataObject> @NonNull InstanceIdentifier<N> verifyTarget(final Class<@NonNull N> target) {
+        verify(target.equals(targetType), "Cannot adapt %s to %s", this, target);
+        return (InstanceIdentifier<N>) this;
+    }
+
     /**
      * Return the path argument chain which makes up this instance identifier.
      *
      * @return Path argument chain. Immutable and does not contain nulls.
      */
-    public final Iterable<PathArgument> getPathArguments() {
+    public final @NonNull Iterable<PathArgument> getPathArguments() {
         return Iterables.unmodifiableIterable(pathArguments);
     }
 
     /**
-     * Check whether an instance identifier contains any wildcards. A wildcard
-     * is an path argument which has a null key.
+     * Check whether an instance identifier contains any wildcards. A wildcard is an path argument which has a null key.
      *
      * @return true if any of the path arguments has a null key.
      */
@@ -159,9 +169,8 @@ public class InstanceIdentifier<T extends DataObject> implements Path<InstanceId
     }
 
     /**
-     * Perform class-specific fast checks for non-equality. This allows
-     * subclasses to avoid iterating over the pathArguments by performing
-     * quick checks on their specific fields.
+     * Perform class-specific fast checks for non-equality. This allows subclasses to avoid iterating over the
+     * pathArguments by performing quick checks on their specific fields.
      *
      * @param other The other identifier, guaranteed to be the same class
      * @return true if the other identifier cannot be equal to this one.
@@ -186,16 +195,19 @@ public class InstanceIdentifier<T extends DataObject> implements Path<InstanceId
     }
 
     /**
-     * Return an instance identifier trimmed at the first occurrence of a
-     * specific component type.
+     * Return an instance identifier trimmed at the first occurrence of a specific component type.
      *
+     * <p>
      * For example let's say an instance identifier was built like so,
      * <pre>
-     *      identifier = InstanceIdentifierBuilder.builder(Nodes.class).child(Node.class, new NodeKey(new NodeId("openflow:1")).build();
+     *      identifier = InstanceIdentifierBuilder.builder(Nodes.class).child(Node.class,
+     *                   new NodeKey(new NodeId("openflow:1")).build();
      * </pre>
      *
+     * <p>
      * And you wanted to obtain the Instance identifier which represented Nodes you would do it like so,
      *
+     * <p>
      * <pre>
      *      identifier.firstIdentifierOf(Nodes.class)
      * </pre>
@@ -204,16 +216,18 @@ public class InstanceIdentifier<T extends DataObject> implements Path<InstanceId
      * @return trimmed instance identifier, or null if the component type
      *         is not present.
      */
-    public final <I extends DataObject> InstanceIdentifier<I> firstIdentifierOf(final Class<I> type) {
-        int i = 1;
+    public final <I extends DataObject> @Nullable InstanceIdentifier<I> firstIdentifierOf(
+            final Class<@NonNull I> type) {
+        int count = 1;
         for (final PathArgument a : pathArguments) {
             if (type.equals(a.getType())) {
                 @SuppressWarnings("unchecked")
-                final InstanceIdentifier<I> ret = (InstanceIdentifier<I>) internalCreate(Iterables.limit(pathArguments, i));
+                final InstanceIdentifier<I> ret = (InstanceIdentifier<I>) internalCreate(
+                        Iterables.limit(pathArguments, count));
                 return ret;
             }
 
-            ++i;
+            ++count;
         }
 
         return null;
@@ -224,11 +238,11 @@ public class InstanceIdentifier<T extends DataObject> implements Path<InstanceId
      * an identifier.
      *
      * @param listItem component type
-     * @param listKey component key type
      * @return key associated with the component, or null if the component type
      *         is not present.
      */
-    public final <N extends Identifiable<K> & DataObject, K extends Identifier<N>> K firstKeyOf(final Class<N> listItem, final Class<K> listKey) {
+    public final <N extends Identifiable<K> & DataObject, K extends Identifier<N>> @Nullable K firstKeyOf(
+            final Class<@NonNull N> listItem) {
         for (final PathArgument i : pathArguments) {
             if (listItem.equals(i.getType())) {
                 @SuppressWarnings("unchecked")
@@ -242,27 +256,27 @@ public class InstanceIdentifier<T extends DataObject> implements Path<InstanceId
 
     /**
      * Check whether an identifier is contained in this identifier. This is a strict subtree check, which requires all
-     * PathArguments to match exactly, e.g.
-     *
+     * PathArguments to match exactly.
      *
+     * <p>
      * The contains method checks if the other identifier is fully contained within the current identifier. It does this
      * by looking at only the types of the path arguments and not by comparing the path arguments themselves.
      *
-     * To illustrate here is an example which explains the working of this API.
-     *
-     * Let's say you have two instance identifiers as follows,
-     *
+     * <p>
+     * To illustrate here is an example which explains the working of this API. Let's say you have two instance
+     * identifiers as follows:
+     * {@code
      * this = /nodes/node/openflow:1
      * other = /nodes/node/openflow:2
-     *
+     * }
      * then this.contains(other) will return false.
      *
-     * @param other
-     * @return
+     * @param other Potentially-container instance identifier
+     * @return True if the specified identifier is contained in this identifier.
      */
     @Override
     public final boolean contains(final InstanceIdentifier<? extends DataObject> other) {
-        Preconditions.checkNotNull(other, "other should not be null");
+        requireNonNull(other, "other should not be null");
 
         final Iterator<?> lit = pathArguments.iterator();
         final Iterator<?> oit = other.pathArguments.iterator();
@@ -289,7 +303,7 @@ public class InstanceIdentifier<T extends DataObject> implements Path<InstanceId
      * @return true if this identifier contains the other object
      */
     public final boolean containsWildcarded(final InstanceIdentifier<?> other) {
-        Preconditions.checkNotNull(other, "other should not be null");
+        requireNonNull(other, "other should not be null");
 
         final Iterator<PathArgument> lit = pathArguments.iterator();
         final Iterator<PathArgument> oit = other.pathArguments.iterator();
@@ -313,130 +327,239 @@ public class InstanceIdentifier<T extends DataObject> implements Path<InstanceId
         return true;
     }
 
+    private <N extends DataObject> @NonNull InstanceIdentifier<N> childIdentifier(final AbstractPathArgument<N> arg) {
+        return trustedCreate(arg, Iterables.concat(pathArguments, Collections.singleton(arg)),
+            HashCodeBuilder.nextHashCode(hash, arg), isWildcarded());
+    }
+
     /**
-     * Create a builder rooted at this key.
+     * Create an InstanceIdentifier for a child container. This method is a more efficient equivalent to
+     * {@code builder().child(container).build()}.
      *
-     * @return A builder instance
+     * @param container Container to append
+     * @param <N> Container type
+     * @return An InstanceIdentifier.
+     * @throws NullPointerException if {@code container} is null
      */
-    public InstanceIdentifierBuilder<T> builder() {
-        return new InstanceIdentifierBuilderImpl<T>(new Item<T>(targetType), pathArguments, hash, isWildcarded());
-    }
-
-    private InstanceIdentifier<?> childIdentifier(final PathArgument arg) {
-        return trustedCreate(arg, Iterables.concat(pathArguments, Collections.singleton(arg)), HashCodeBuilder.nextHashCode(hash, arg), isWildcarded());
+    public final <N extends ChildOf<? super T>> @NonNull InstanceIdentifier<N> child(
+            final Class<@NonNull N> container) {
+        return childIdentifier(Item.of(container));
     }
 
+    /**
+     * Create an InstanceIdentifier for a child list item. This method is a more efficient equivalent to
+     * {@code builder().child(listItem, listKey).build()}.
+     *
+     * @param listItem List to append
+     * @param listKey List key
+     * @param <N> List type
+     * @param <K> Key type
+     * @return An InstanceIdentifier.
+     * @throws NullPointerException if any argument is null
+     */
     @SuppressWarnings("unchecked")
-    public final <N extends ChildOf<? super T>> InstanceIdentifier<N> child(final Class<N> container) {
-        final PathArgument arg = new Item<>(container);
-        return (InstanceIdentifier<N>) childIdentifier(arg);
+    public final <N extends Identifiable<K> & ChildOf<? super T>, K extends Identifier<N>>
+            @NonNull KeyedInstanceIdentifier<N, K> child(final Class<@NonNull N> listItem, final K listKey) {
+        return (KeyedInstanceIdentifier<N, K>) childIdentifier(IdentifiableItem.of(listItem, listKey));
     }
 
-    @SuppressWarnings("unchecked")
-    public final <N extends Identifiable<K> & ChildOf<? super T>, K extends Identifier<N>> KeyedInstanceIdentifier<N, K> child(
-            final Class<N> listItem, final K listKey) {
-        final PathArgument arg = new IdentifiableItem<>(listItem, listKey);
-        return (KeyedInstanceIdentifier<N, K>) childIdentifier(arg);
+    /**
+     * Create an InstanceIdentifier for a child container. This method is a more efficient equivalent to
+     * {@code builder().child(caze, container).build()}.
+     *
+     * @param caze Choice case class
+     * @param container Container to append
+     * @param <C> Case type
+     * @param <N> Container type
+     * @return An InstanceIdentifier.
+     * @throws NullPointerException if any argument is null
+     */
+    // FIXME: add a proper caller
+    public final <C extends ChoiceIn<? super T> & DataObject, N extends ChildOf<? super C>>
+            @NonNull InstanceIdentifier<N> child(final Class<@NonNull C> caze, final Class<@NonNull N> container) {
+        return childIdentifier(Item.of(caze, container));
     }
 
+    /**
+     * Create an InstanceIdentifier for a child list item. This method is a more efficient equivalent to
+     * {@code builder().child(caze, listItem, listKey).build()}.
+     *
+     * @param caze Choice case class
+     * @param listItem List to append
+     * @param listKey List key
+     * @param <C> Case type
+     * @param <N> List type
+     * @param <K> Key type
+     * @return An InstanceIdentifier.
+     * @throws NullPointerException if any argument is null
+     */
+    // FIXME: add a proper caller
     @SuppressWarnings("unchecked")
-    public final <N extends DataObject & Augmentation<? super T>> InstanceIdentifier<N> augmentation(
-            final Class<N> container) {
-        final PathArgument arg = new Item<>(container);
-        return (InstanceIdentifier<N>) childIdentifier(arg);
+    public final <C extends ChoiceIn<? super T> & DataObject, K extends Identifier<N>,
+        N extends Identifiable<K> & ChildOf<? super C>> @NonNull KeyedInstanceIdentifier<N, K> child(
+                final Class<@NonNull C> caze, final Class<@NonNull N> listItem, final K listKey) {
+        return (KeyedInstanceIdentifier<N, K>) childIdentifier(IdentifiableItem.of(caze, listItem, listKey));
     }
 
-    @Deprecated
-    private List<PathArgument> legacyCache;
-
     /**
-     * @deprecated Use {@link #getPathArguments()} instead.
+     * Create an InstanceIdentifier for a child augmentation. This method is a more efficient equivalent to
+     * {@code builder().augmentation(container).build()}.
+     *
+     * @param container Container to append
+     * @param <N> Container type
+     * @return An InstanceIdentifier.
+     * @throws NullPointerException if {@code container} is null
      */
-    @Deprecated
-    public final List<PathArgument> getPath() {
-        if (legacyCache == null) {
-            legacyCache = ImmutableList.<PathArgument>copyOf(pathArguments);
-        }
-
-        return legacyCache;
+    public final <N extends DataObject & Augmentation<? super T>> @NonNull InstanceIdentifier<N> augmentation(
+            final Class<@NonNull N> container) {
+        return childIdentifier(Item.of(container));
     }
 
     /**
-     * Create a new InstanceIdentifierBuilder given a base InstanceIdentifier
-     *
-     * @param base
-     * @param <T>
-     * @return
+     * Create a builder rooted at this key.
      *
-     * @deprecated Use {@link #builder()} instead.
+     * @return A builder instance
      */
-    @Deprecated
-    public static <T extends DataObject> InstanceIdentifierBuilder<T> builder(final InstanceIdentifier<T> base) {
-        return base.builder();
+    // FIXME: rename this method to 'toBuilder()'
+    public @NonNull InstanceIdentifierBuilder<T> builder() {
+        return new InstanceIdentifierBuilderImpl<>(Item.of(targetType), pathArguments, hash, isWildcarded());
     }
 
     /**
-     * Create an InstanceIdentifierBuilder for a specific type of InstanceIdentifier as specified by container
+     * Create an InstanceIdentifierBuilder for a specific type of InstanceIdentifier as specified by container.
      *
-     * @param container
-     * @param <T>
-     * @return
+     * @param container Base container
+     * @param <T> Type of the container
+     * @return A new {@link InstanceIdentifierBuilder}
+     * @throws NullPointerException if {@code container} is null
      */
-    public static <T extends ChildOf<? extends DataRoot>> InstanceIdentifierBuilder<T> builder(final Class<T> container) {
-        return new InstanceIdentifierBuilderImpl<T>().addNode(container);
+    public static <T extends ChildOf<? extends DataRoot>> @NonNull InstanceIdentifierBuilder<T> builder(
+            final Class<T> container) {
+        return new InstanceIdentifierBuilderImpl<T>().addWildNode(Item.of(container));
     }
 
     /**
-     * Create an InstanceIdentifierBuilder for a specific type of InstanceIdentifier which represents an IdentifiableItem
-     *
-     * @param listItem
-     * @param listKey
-     * @param <N>
-     * @param <K>
-     * @return
+     * Create an InstanceIdentifierBuilder for a specific type of InstanceIdentifier as specified by container in
+     * a {@code grouping} used in the {@code case} statement.
+     *
+     * @param caze Choice case class
+     * @param container Base container
+     * @param <C> Case type
+     * @param <T> Type of the container
+     * @return A new {@link InstanceIdentifierBuilder}
+     * @throws NullPointerException if any argument is null
      */
-    public static <N extends Identifiable<K> & ChildOf<? extends DataRoot>, K extends Identifier<N>> InstanceIdentifierBuilder<N> builder(
-            final Class<N> listItem, final K listKey) {
-        return new InstanceIdentifierBuilderImpl<N>().addNode(listItem, listKey);
+    public static <C extends ChoiceIn<? extends DataRoot> & DataObject, T extends ChildOf<? super C>>
+            @NonNull InstanceIdentifierBuilder<T> builder(final Class<C> caze, final Class<T> container) {
+        return new InstanceIdentifierBuilderImpl<T>().addWildNode(Item.of(caze, container));
     }
 
     /**
-     * Create an instance identifier for a very specific object type. This method
-     * implements {@link #create(Iterable)} semantics, except it is used by internal
-     * callers, which have assured that the argument is an immutable Iterable.
-     *
+     * Create an InstanceIdentifierBuilder for a specific type of InstanceIdentifier which represents an
+     * {@link IdentifiableItem}.
+     *
+     * @param listItem list item class
+     * @param listKey key value
+     * @param <N> List type
+     * @param <K> List key
+     * @return A new {@link InstanceIdentifierBuilder}
+     * @throws NullPointerException if any argument is null
+     */
+    public static <N extends Identifiable<K> & ChildOf<? extends DataRoot>,
+            K extends Identifier<N>> @NonNull InstanceIdentifierBuilder<N> builder(final Class<N> listItem,
+                    final K listKey) {
+        return new InstanceIdentifierBuilderImpl<N>().addNode(IdentifiableItem.of(listItem, listKey));
+    }
+
+    /**
+     * Create an InstanceIdentifierBuilder for a specific type of InstanceIdentifier which represents an
+     * {@link IdentifiableItem} in a {@code grouping} used in the {@code case} statement.
+     *
+     * @param caze Choice case class
+     * @param listItem list item class
+     * @param listKey key value
+     * @param <C> Case type
+     * @param <N> List type
+     * @param <K> List key
+     * @return A new {@link InstanceIdentifierBuilder}
+     * @throws NullPointerException if any argument is null
+     */
+    public static <C extends ChoiceIn<? extends DataRoot> & DataObject,
+            N extends Identifiable<K> & ChildOf<? super C>, K extends Identifier<N>>
+            @NonNull InstanceIdentifierBuilder<N> builder(final Class<C> caze, final Class<N> listItem,
+                    final K listKey) {
+        return new InstanceIdentifierBuilderImpl<N>().addNode(IdentifiableItem.of(caze, listItem, listKey));
+    }
+
+    public static <R extends DataRoot & DataObject, T extends ChildOf<? super R>>
+            @NonNull InstanceIdentifierBuilder<T> builderOfInherited(final Class<R> root, final Class<T> container) {
+        // FIXME: we are losing root identity, hence namespaces may not work correctly
+        return new InstanceIdentifierBuilderImpl<T>().addWildNode(Item.of(container));
+    }
+
+    public static <R extends DataRoot & DataObject, C extends ChoiceIn<? super R> & DataObject,
+            T extends ChildOf<? super C>>
+            @NonNull InstanceIdentifierBuilder<T> builderOfInherited(final Class<R> root,
+                final Class<C> caze, final Class<T> container) {
+        // FIXME: we are losing root identity, hence namespaces may not work correctly
+        return new InstanceIdentifierBuilderImpl<T>().addWildNode(Item.of(caze, container));
+    }
+
+    public static <R extends DataRoot & DataObject, N extends Identifiable<K> & ChildOf<? super R>,
+            K extends Identifier<N>>
+            @NonNull InstanceIdentifierBuilder<N> builderOfInherited(final Class<R> root,
+                final Class<N> listItem, final K listKey) {
+        // FIXME: we are losing root identity, hence namespaces may not work correctly
+        return new InstanceIdentifierBuilderImpl<N>().addNode(IdentifiableItem.of(listItem, listKey));
+    }
+
+    public static <R extends DataRoot & DataObject, C extends ChoiceIn<? super R> & DataObject,
+            N extends Identifiable<K> & ChildOf<? super C>, K extends Identifier<N>>
+            @NonNull InstanceIdentifierBuilder<N> builderOfInherited(final Class<R> root,
+                final Class<C> caze, final Class<N> listItem, final K listKey) {
+        // FIXME: we are losing root identity, hence namespaces may not work correctly
+        return new InstanceIdentifierBuilderImpl<N>().addNode(IdentifiableItem.of(caze, listItem, listKey));
+    }
+
+    /**
+     * Create an instance identifier for a very specific object type. This method implements {@link #create(Iterable)}
+     * semantics, except it is used by internal callers, which have assured that the argument is an immutable Iterable.
      *
      * @param pathArguments The path to a specific node in the data tree
      * @return InstanceIdentifier instance
-     * @throws IllegalArgumentException if pathArguments is empty or
-     *         contains a null element.
+     * @throws IllegalArgumentException if pathArguments is empty or contains a null element.
+     * @throws NullPointerException if {@code pathArguments} is null
      */
-    private static InstanceIdentifier<?> internalCreate(final Iterable<PathArgument> pathArguments) {
-        final Iterator<? extends PathArgument> it = Preconditions.checkNotNull(pathArguments, "pathArguments may not be null").iterator();
+    private static @NonNull InstanceIdentifier<?> internalCreate(final Iterable<PathArgument> pathArguments) {
+        final var it = requireNonNull(pathArguments, "pathArguments may not be null").iterator();
+        checkArgument(it.hasNext(), "pathArguments may not be empty");
+
         final HashCodeBuilder<PathArgument> hashBuilder = new HashCodeBuilder<>();
         boolean wildcard = false;
-        PathArgument a = null;
+        PathArgument arg;
 
-        while (it.hasNext()) {
-            a = it.next();
-            Preconditions.checkArgument(a != null, "pathArguments may not contain null elements");
+        do {
+            arg = it.next();
+            // Non-null is implied by our callers
+            final var type = verifyNotNull(arg).getType();
+            checkArgument(ChildOf.class.isAssignableFrom(type) || Augmentation.class.isAssignableFrom(type),
+                "%s is not a valid path argument", type);
 
-            // TODO: sanity check ChildOf<>;
-            hashBuilder.addArgument(a);
+            hashBuilder.addArgument(arg);
 
-            if (Identifiable.class.isAssignableFrom(a.getType()) && !(a instanceof IdentifiableItem<?, ?>)) {
+            if (Identifiable.class.isAssignableFrom(type) && !(arg instanceof IdentifiableItem)) {
                 wildcard = true;
             }
-        }
-        Preconditions.checkArgument(a != null, "pathArguments may not be empty");
+        } while (it.hasNext());
 
-        return trustedCreate(a, pathArguments, hashBuilder.build(), wildcard);
+        return trustedCreate(arg, pathArguments, hashBuilder.build(), wildcard);
     }
 
     /**
      * Create an instance identifier for a very specific object type.
      *
-     * Example
+     * <p>
+     * Example:
      * <pre>
      *  List&lt;PathArgument&gt; path = Arrays.asList(new Item(Nodes.class))
      *  new InstanceIdentifier(path);
@@ -447,19 +570,21 @@ public class InstanceIdentifier<T extends DataObject> implements Path<InstanceId
      * @throws IllegalArgumentException if pathArguments is empty or
      *         contains a null element.
      */
-    public static InstanceIdentifier<?> create(final Iterable<? extends PathArgument> pathArguments) {
-        if (pathArguments instanceof ImmutableCollection<?>) {
+    // FIXME: rename to 'unsafeOf()'
+    public static @NonNull InstanceIdentifier<?> create(final Iterable<? extends PathArgument> pathArguments) {
+        if (pathArguments instanceof ImmutableCollection) {
             @SuppressWarnings("unchecked")
-            final Iterable<PathArgument> immutableArguments = (Iterable<PathArgument>) pathArguments;
+            final var immutableArguments = (ImmutableCollection<PathArgument>) pathArguments;
             return internalCreate(immutableArguments);
-        } else {
-            return internalCreate(ImmutableList.copyOf(pathArguments));
         }
+
+        return internalCreate(ImmutableList.copyOf(pathArguments));
     }
 
     /**
      * Create an instance identifier for a very specific object type.
      *
+     * <p>
      * For example
      * <pre>
      *      new InstanceIdentifier(Nodes.class)
@@ -469,9 +594,11 @@ public class InstanceIdentifier<T extends DataObject> implements Path<InstanceId
      * @param type The type of the object which this instance identifier represents
      * @return InstanceIdentifier instance
      */
+    // FIXME: considering removing in favor of always going through a builder
     @SuppressWarnings("unchecked")
-    public static <T extends DataObject> InstanceIdentifier<T> create(final Class<T> type) {
-        return (InstanceIdentifier<T>) create(Collections.<PathArgument> singletonList(new Item<>(type)));
+    public static <T extends ChildOf<? extends DataRoot>> @NonNull InstanceIdentifier<T> create(
+            final Class<@NonNull T> type) {
+        return (InstanceIdentifier<T>) internalCreate(ImmutableList.of(Item.of(type)));
     }
 
     /**
@@ -482,9 +609,11 @@ public class InstanceIdentifier<T extends DataObject> implements Path<InstanceId
      * @throws IllegalArgumentException if the supplied identifier type cannot have a key.
      * @throws NullPointerException if id is null.
      */
-    public static <N extends Identifiable<K> & DataObject, K extends Identifier<N>> K keyOf(final InstanceIdentifier<N> id) {
-        Preconditions.checkNotNull(id);
-        Preconditions.checkArgument(id instanceof KeyedInstanceIdentifier, "%s does not have a key", id);
+    // FIXME: reconsider naming and design of this method
+    public static <N extends Identifiable<K> & DataObject, K extends Identifier<N>> K keyOf(
+            final InstanceIdentifier<N> id) {
+        requireNonNull(id);
+        checkArgument(id instanceof KeyedInstanceIdentifier, "%s does not have a key", id);
 
         @SuppressWarnings("unchecked")
         final K ret = ((KeyedInstanceIdentifier<N, K>)id).getKey();
@@ -492,37 +621,53 @@ public class InstanceIdentifier<T extends DataObject> implements Path<InstanceId
     }
 
     @SuppressWarnings({ "unchecked", "rawtypes" })
-    static InstanceIdentifier<?> trustedCreate(final PathArgument arg, final Iterable<PathArgument> pathArguments, final int hash, boolean wildcarded) {
-        if (Identifiable.class.isAssignableFrom(arg.getType()) && !(wildcarded)) {
+    static <N extends DataObject> @NonNull InstanceIdentifier<N> trustedCreate(final PathArgument arg,
+            final Iterable<PathArgument> pathArguments, final int hash, boolean wildcarded) {
+        if (Identifiable.class.isAssignableFrom(arg.getType()) && !wildcarded) {
             Identifier<?> key = null;
-            if (arg instanceof IdentifiableItem<?, ?>) {
-                key = ((IdentifiableItem<?, ?>)arg).key;
+            if (arg instanceof IdentifiableItem) {
+                key = ((IdentifiableItem<?, ?>)arg).getKey();
             } else {
                 wildcarded = true;
             }
 
             return new KeyedInstanceIdentifier(arg.getType(), pathArguments, wildcarded, hash, key);
-        } else {
-            return new InstanceIdentifier(arg.getType(), pathArguments, wildcarded, hash);
         }
+
+        return new InstanceIdentifier(arg.getType(), pathArguments, wildcarded, hash);
     }
 
     /**
-     * Path argument of {@link InstanceIdentifier}.
-     * <p>
-     * Interface which implementations are used as path components of the
+     * Path argument of {@link InstanceIdentifier}. Interface which implementations are used as path components of the
      * path in overall data tree.
      */
     public interface PathArgument extends Comparable<PathArgument> {
-        Class<? extends DataObject> getType();
+        /**
+         * Return the data object type backing this PathArgument.
+         *
+         * @return Data object type.
+         */
+        @NonNull Class<? extends DataObject> getType();
+
+        /**
+         * Return an optional enclosing case type. This is used only when {@link #getType()} references a node defined
+         * in a {@code grouping} which is reference inside a {@code case} statement in order to safely reference the
+         * node.
+         *
+         * @return Optional case class.
+         */
+        default Optional<? extends Class<? extends DataObject>> getCaseType() {
+            return Optional.empty();
+        }
     }
 
-    private static abstract class AbstractPathArgument<T extends DataObject> implements PathArgument, Serializable {
+    private abstract static class AbstractPathArgument<T extends DataObject> implements PathArgument, Serializable {
         private static final long serialVersionUID = 1L;
-        private final Class<T> type;
 
-        protected AbstractPathArgument(final Class<T> type) {
-            this.type = Preconditions.checkNotNull(type, "Type may not be null.");
+        private final @NonNull Class<T> type;
+
+        AbstractPathArgument(final Class<T> type) {
+            this.type = requireNonNull(type, "Type may not be null.");
         }
 
         @Override
@@ -530,29 +675,44 @@ public class InstanceIdentifier<T extends DataObject> implements Path<InstanceId
             return type;
         }
 
+        Object getKey() {
+            return null;
+        }
+
         @Override
-        public int hashCode() {
-            return type.hashCode();
+        public final int hashCode() {
+            return Objects.hash(type, getCaseType(), getKey());
         }
 
         @Override
-        public boolean equals(final Object obj) {
+        public final boolean equals(final Object obj) {
             if (this == obj) {
                 return true;
             }
-            if (obj == null) {
-                return false;
-            }
-            if (getClass() != obj.getClass()) {
+            if (!(obj instanceof AbstractPathArgument)) {
                 return false;
             }
             final AbstractPathArgument<?> other = (AbstractPathArgument<?>) obj;
-            return type.equals(other.type);
+            return type.equals(other.type) && Objects.equals(getKey(), other.getKey())
+                    && getCaseType().equals(other.getCaseType());
         }
 
         @Override
-        public int compareTo(final PathArgument arg) {
-            return type.getCanonicalName().compareTo(arg.getType().getCanonicalName());
+        public final int compareTo(final PathArgument arg) {
+            final int cmp = compareClasses(type, arg.getType());
+            if (cmp != 0) {
+                return cmp;
+            }
+            final Optional<? extends Class<?>> caseType = getCaseType();
+            if (!caseType.isPresent()) {
+                return arg.getCaseType().isPresent() ? -1 : 1;
+            }
+            final Optional<? extends Class<?>> argCaseType = getCaseType();
+            return argCaseType.isPresent() ? compareClasses(caseType.get(), argCaseType.get()) : 1;
+        }
+
+        private static int compareClasses(final Class<?> first, final Class<?> second) {
+            return first.getCanonicalName().compareTo(second.getCanonicalName());
         }
     }
 
@@ -560,15 +720,43 @@ public class InstanceIdentifier<T extends DataObject> implements Path<InstanceId
      * An Item represents an object that probably is only one of it's kind. For example a Nodes object is only one of
      * a kind. In YANG terms this would probably represent a container.
      *
-     * @param <T>
+     * @param <T> Item type
      */
-    public static final class Item<T extends DataObject> extends AbstractPathArgument<T> {
+    public static class Item<T extends DataObject> extends AbstractPathArgument<T> {
         private static final long serialVersionUID = 1L;
 
-        public Item(final Class<T> type) {
+        Item(final Class<T> type) {
             super(type);
         }
 
+        /**
+         * Return a PathArgument instance backed by the specified class.
+         *
+         * @param type Backing class
+         * @param <T> Item type
+         * @return A new PathArgument
+         * @throws NullPointerException if {@code} is null.
+         */
+        public static <T extends DataObject> @NonNull Item<T> of(final Class<T> type) {
+            return new Item<>(type);
+        }
+
+        /**
+         * Return a PathArgument instance backed by the specified class, which in turn is defined in a {@code grouping}
+         * used in a corresponding {@code case} statement.
+         *
+         * @param caseType defining case class
+         * @param type Backing class
+         * @param <C> Case type
+         * @param <T> Item type
+         * @return A new PathArgument
+         * @throws NullPointerException if any argument is null.
+         */
+        public static <C extends ChoiceIn<?> & DataObject, T extends ChildOf<? super C>> @NonNull Item<T> of(
+                final Class<C> caseType, final Class<T> type) {
+            return new CaseItem<>(caseType, type);
+        }
+
         @Override
         public String toString() {
             return getType().getName();
@@ -582,27 +770,58 @@ public class InstanceIdentifier<T extends DataObject> implements Path<InstanceId
      * @param <I> An object that is identifiable by an identifier
      * @param <T> The identifier of the object
      */
-    public static final class IdentifiableItem<I extends Identifiable<T> & DataObject, T extends Identifier<I>> extends AbstractPathArgument<I> {
+    public static class IdentifiableItem<I extends Identifiable<T> & DataObject, T extends Identifier<I>>
+            extends AbstractPathArgument<I> {
         private static final long serialVersionUID = 1L;
-        private final T key;
 
-        public IdentifiableItem(final Class<I> type, final T key) {
+        private final @NonNull T key;
+
+        IdentifiableItem(final Class<I> type, final T key) {
             super(type);
-            this.key = Preconditions.checkNotNull(key, "Key may not be null.");
+            this.key = requireNonNull(key, "Key may not be null.");
         }
 
-        public T getKey() {
-            return this.key;
+        /**
+         * Return an IdentifiableItem instance backed by the specified class with specified key.
+         *
+         * @param type Backing class
+         * @param key Key
+         * @param <T> List type
+         * @param <I> Key type
+         * @return An IdentifiableItem
+         * @throws NullPointerException if any argument is null.
+         */
+        public static <T extends Identifiable<I> & DataObject, I extends Identifier<T>>
+                @NonNull IdentifiableItem<T, I> of(final Class<T> type, final I key) {
+            return new IdentifiableItem<>(type, key);
         }
 
-        @Override
-        public boolean equals(final Object obj) {
-            return super.equals(obj) && key.equals(((IdentifiableItem<?, ?>) obj).getKey());
+        /**
+         * Return an IdentifiableItem instance backed by the specified class with specified key. The class is in turn
+         * defined in a {@code grouping} used in a corresponding {@code case} statement.
+         *
+         * @param caseType defining case class
+         * @param type Backing class
+         * @param <C> Case type
+         * @param <T> List type
+         * @param <I> Key type
+         * @return A new PathArgument
+         * @throws NullPointerException if any argument is null.
+         */
+        public static <C extends ChoiceIn<?> & DataObject, T extends ChildOf<? super C> & Identifiable<I>,
+                I extends Identifier<T>> @NonNull IdentifiableItem<T, I> of(final Class<C> caseType,
+                        final Class<T> type, final I key) {
+            return new CaseIdentifiableItem<>(caseType, type, key);
         }
 
+        /**
+         * Return the data object type backing this PathArgument.
+         *
+         * @return Data object type.
+         */
         @Override
-        public int hashCode() {
-            return super.hashCode() * 31 + key.hashCode();
+        public final @NonNull T getKey() {
+            return key;
         }
 
         @Override
@@ -611,92 +830,131 @@ public class InstanceIdentifier<T extends DataObject> implements Path<InstanceId
         }
     }
 
+    private static final class CaseItem<C extends ChoiceIn<?> & DataObject, T extends ChildOf<? super C>>
+            extends Item<T> {
+        private static final long serialVersionUID = 1L;
+
+        private final Class<C> caseType;
 
-    public interface InstanceIdentifierBuilder<T extends DataObject> extends Builder<InstanceIdentifier<T>> {
+        CaseItem(final Class<C> caseType, final Class<T> type) {
+            super(type);
+            this.caseType = requireNonNull(caseType);
+        }
+
+        @Override
+        public Optional<Class<C>> getCaseType() {
+            return Optional.of(caseType);
+        }
+    }
+
+    private static final class CaseIdentifiableItem<C extends ChoiceIn<?> & DataObject,
+            T extends ChildOf<? super C> & Identifiable<K>, K extends Identifier<T>> extends IdentifiableItem<T, K> {
+        private static final long serialVersionUID = 1L;
+
+        private final Class<C> caseType;
+
+        CaseIdentifiableItem(final Class<C> caseType, final Class<T> type, final K key) {
+            super(type, key);
+            this.caseType = requireNonNull(caseType);
+        }
+
+        @Override
+        public Optional<Class<C>> getCaseType() {
+            return Optional.of(caseType);
+        }
+    }
+
+    // FIXME: rename to 'Builder'
+    // FIXME: introduce KeyedBuilder with specialized build() method
+    public interface InstanceIdentifierBuilder<T extends DataObject> {
         /**
-         * Append the specified container as a child of the current InstanceIdentifier referenced by the builder.
-         *
-         * This method should be used when you want to build an instance identifier by appending top-level
-         * elements
-         *
-         * Example,
+         * Append the specified container as a child of the current InstanceIdentifier referenced by the builder. This
+         * method should be used when you want to build an instance identifier by appending top-level elements, for
+         * example
          * <pre>
          *     InstanceIdentifier.builder().child(Nodes.class).build();
-         *
          * </pre>
          *
+         * <p>
          * NOTE :- The above example is only for illustration purposes InstanceIdentifier.builder() has been deprecated
          * and should not be used. Use InstanceIdentifier.builder(Nodes.class) instead
          *
-         * @param container
-         * @param <N>
-         * @return
+         * @param container Container to append
+         * @param <N> Container type
+         * @return this builder
+         * @throws NullPointerException if {@code container} is null
          */
-        <N extends ChildOf<? super T>> InstanceIdentifierBuilder<N> child(
-                Class<N> container);
+        <N extends ChildOf<? super T>> @NonNull InstanceIdentifierBuilder<N> child(Class<N> container);
 
         /**
-         * Append the specified listItem as a child of the current InstanceIdentifier referenced by the builder.
+         * Append the specified container as a child of the current InstanceIdentifier referenced by the builder. This
+         * method should be used when you want to build an instance identifier by appending a container node to the
+         * identifier and the {@code container} is defined in a {@code grouping} used in a {@code case} statement.
          *
-         * This method should be used when you want to build an instance identifier by appending a specific list element
-         * to the identifier
+         * @param caze Choice case class
+         * @param container Container to append
+         * @param <C> Case type
+         * @param <N> Container type
+         * @return this builder
+         * @throws NullPointerException if {@code container} is null
+         */
+        <C extends ChoiceIn<? super T> & DataObject, N extends ChildOf<? super C>>
+                @NonNull InstanceIdentifierBuilder<N> child(Class<C> caze, Class<N> container);
+
+        /**
+         * Append the specified listItem as a child of the current InstanceIdentifier referenced by the builder. This
+         * method should be used when you want to build an instance identifier by appending a specific list element to
+         * the identifier.
          *
-         * @param listItem
-         * @param listKey
-         * @param <N>
-         * @param <K>
-         * @return
+         * @param listItem List to append
+         * @param listKey List key
+         * @param <N> List type
+         * @param <K> Key type
+         * @return this builder
+         * @throws NullPointerException if any argument is null
          */
-        <N extends Identifiable<K> & ChildOf<? super T>, K extends Identifier<N>> InstanceIdentifierBuilder<N> child(
-                Class<N> listItem, K listKey);
+        <N extends Identifiable<K> & ChildOf<? super T>, K extends Identifier<N>>
+                @NonNull InstanceIdentifierBuilder<N> child(Class<@NonNull N> listItem, K listKey);
+
+        /**
+         * Append the specified listItem as a child of the current InstanceIdentifier referenced by the builder. This
+         * method should be used when you want to build an instance identifier by appending a specific list element to
+         * the identifier and the {@code list} is defined in a {@code grouping} used in a {@code case} statement.
+         *
+         * @param caze Choice case class
+         * @param listItem List to append
+         * @param listKey List key
+         * @param <C> Case type
+         * @param <N> List type
+         * @param <K> Key type
+         * @return this builder
+         * @throws NullPointerException if any argument is null
+         */
+        <C extends ChoiceIn<? super T> & DataObject, K extends Identifier<N>,
+                N extends Identifiable<K> & ChildOf<? super C>> @NonNull InstanceIdentifierBuilder<N> child(
+                        Class<C> caze, Class<N> listItem, K listKey);
 
         /**
          * Build an identifier which refers to a specific augmentation of the current InstanceIdentifier referenced by
-         * the builder
+         * the builder.
          *
-         * @param container
-         * @param <N>
-         * @return
+         * @param container augmentation class
+         * @param <N> augmentation type
+         * @return this builder
+         * @throws NullPointerException if {@code container} is null
          */
-        <N extends DataObject & Augmentation<? super T>> InstanceIdentifierBuilder<N> augmentation(
+        <N extends DataObject & Augmentation<? super T>> @NonNull InstanceIdentifierBuilder<N> augmentation(
                 Class<N> container);
 
         /**
          * Build the instance identifier.
          *
-         * @return
-         */
-        @Override
-        InstanceIdentifier<T> build();
-
-        /*
-         * @deprecated use #build()
+         * @return Resulting instance identifier.
          */
-        @Deprecated
-        InstanceIdentifier<T> toInstance();
-    }
-
-    private void writeObject(final java.io.ObjectOutputStream out) throws IOException {
-        out.defaultWriteObject();
-        out.writeInt(Iterables.size(pathArguments));
-        for (Object o : pathArguments) {
-            out.writeObject(o);
-        }
+        @NonNull InstanceIdentifier<T> build();
     }
 
-    private void readObject(final java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
-        in.defaultReadObject();
-
-        final int size = in.readInt();
-        final List<PathArgument> args = new ArrayList<>(size);
-        for (int i = 0; i < size; ++i) {
-            args.add((PathArgument) in.readObject());
-        }
-
-        try {
-            PATHARGUMENTS_FIELD.set(this, ImmutableList.copyOf(args));
-        } catch (IllegalArgumentException | IllegalAccessException e) {
-            throw new IOException(e);
-        }
+    private Object writeReplace() throws ObjectStreamException {
+        return new InstanceIdentifierV3<>(this);
     }
 }