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.annotations.Beta;
import com.google.common.base.MoreObjects;
import com.google.common.base.MoreObjects.ToStringHelper;
-import com.google.common.collect.ImmutableCollection;
+import com.google.common.base.VerifyException;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import java.io.IOException;
+import java.io.NotSerializableException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+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 org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.Nullable;
+import org.opendaylight.yangtools.concepts.HierarchicalIdentifier;
import org.opendaylight.yangtools.util.HashCodeBuilder;
/**
*
* <p>
* For Example let's say you were trying to refer to a node in inventory which was modeled in YANG as follows,
+ * <pre>code{
+ * module opendaylight-inventory {
+ * ....
*
- * <p>
- * <pre>
- * module opendaylight-inventory {
- * ....
- *
- * container nodes {
- * list node {
- * key "id";
- * ext:context-instance "node-context";
+ * container nodes {
+ * list node {
+ * key "id";
+ * ext:context-instance "node-context";
*
- * uses node;
- * }
- * }
- *
- * }
- * </pre>
+ * uses node;
+ * }
+ * }
+ * }
+ * }</pre>
*
* <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();
+ * InstanceIdentifier.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 sealed class InstanceIdentifier<T extends DataObject>
+ implements HierarchicalIdentifier<InstanceIdentifier<? extends DataObject>>
+ permits KeyedInstanceIdentifier {
+ @java.io.Serial
+ 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 final transient Iterable<PathArgument> pathArguments;
- private final Class<T> targetType;
+ final Iterable<DataObjectStep<?>> 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,
+ InstanceIdentifier(final Class<T> type, final Iterable<DataObjectStep<?>> pathArguments, final boolean wildcarded,
final int hash) {
this.pathArguments = requireNonNull(pathArguments);
- this.targetType = requireNonNull(type);
+ targetType = requireNonNull(type);
this.wildcarded = wildcarded;
this.hash = hash;
}
*
* @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<DataObjectStep<?>> getPathArguments() {
return Iterables.unmodifiableIterable(pathArguments);
}
return false;
}
- final InstanceIdentifier<?> other = (InstanceIdentifier<?>) obj;
+ final var other = (InstanceIdentifier<?>) obj;
if (pathArguments == other.pathArguments) {
return true;
}
/*
- * We could now just go and compare the pathArguments, but that
- * can be potentially expensive. Let's try to avoid that by
- * checking various things that we have cached from pathArguments
- * and trying to prove the identifiers are *not* equal.
+ * We could now just go and compare the pathArguments, but that can be potentially expensive. Let's try to avoid
+ * that by checking various things that we have cached from pathArguments and trying to prove the identifiers
+ * are *not* equal.
*/
- if (hash != other.hash) {
- return false;
- }
- if (wildcarded != other.wildcarded) {
- return false;
- }
- if (targetType != other.targetType) {
- return false;
- }
- if (fastNonEqual(other)) {
- return false;
- }
-
- // Everything checks out so far, so we have to do a full equals
- return Iterables.elementsEqual(pathArguments, other.pathArguments);
+ return hash == other.hash && wildcarded == other.wildcarded && targetType == other.targetType
+ && keyEquals(other)
+ // Everything checks out so far, so we have to do a full equals
+ && Iterables.elementsEqual(pathArguments, other.pathArguments);
}
- /**
- * 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.
- */
- protected boolean fastNonEqual(final InstanceIdentifier<?> other) {
- return false;
+ boolean keyEquals(final InstanceIdentifier<?> other) {
+ return true;
}
@Override
* <p>
* For example let's say an instance identifier was built like so,
* <pre>
- * identifier = InstanceIdentifierBuilder.builder(Nodes.class).child(Node.class,
+ * identifier = InstanceIdentifier.builder(Nodes.class).child(Node.class,
* new NodeKey(new NodeId("openflow:1")).build();
* </pre>
*
* @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) {
+ 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())) {
+ for (var step : pathArguments) {
+ if (type.equals(step.type())) {
@SuppressWarnings("unchecked")
- final InstanceIdentifier<I> ret = (InstanceIdentifier<I>) internalCreate(
- Iterables.limit(pathArguments, count));
+ final var ret = (InstanceIdentifier<I>) internalCreate(Iterables.limit(pathArguments, count));
return ret;
}
return null;
}
- /**
- * Return the key associated with the first component of specified type in
- * 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.
- *
- * @deprecated Use {@link #firstKeyOf(Class)} instead.
- */
- @Deprecated
- public final <N extends Identifiable<K> & DataObject, K extends Identifier<N>> K firstKeyOf(final Class<N> listItem,
- final Class<K> listKey) {
- return firstKeyOf(listItem);
- }
-
/**
* Return the key associated with the first component of specified type in
* an identifier.
* @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) {
- for (final PathArgument i : pathArguments) {
- if (listItem.equals(i.getType())) {
+ public final <N extends KeyAware<K> & DataObject, K extends Key<N>> @Nullable K firstKeyOf(
+ final Class<@NonNull N> listItem) {
+ for (var step : pathArguments) {
+ if (step instanceof KeyStep<?, ?> keyPredicate && listItem.equals(step.type())) {
@SuppressWarnings("unchecked")
- final K ret = ((IdentifiableItem<N, K>)i).getKey();
+ final var ret = (K) keyPredicate.key();
return ret;
}
}
-
return null;
}
public final boolean contains(final InstanceIdentifier<? extends DataObject> other) {
requireNonNull(other, "other should not be null");
- final Iterator<?> lit = pathArguments.iterator();
- final Iterator<?> oit = other.pathArguments.iterator();
-
- while (lit.hasNext()) {
+ final var oit = other.pathArguments.iterator();
+ for (var step : pathArguments) {
if (!oit.hasNext()) {
return false;
}
-
- if (!lit.next().equals(oit.next())) {
+ if (!step.equals(oit.next())) {
return false;
}
}
-
return true;
}
public final boolean containsWildcarded(final InstanceIdentifier<?> other) {
requireNonNull(other, "other should not be null");
- final Iterator<PathArgument> lit = pathArguments.iterator();
- final Iterator<PathArgument> oit = other.pathArguments.iterator();
-
- while (lit.hasNext()) {
- if (!oit.hasNext()) {
+ final var otherSteps = other.pathArguments.iterator();
+ for (var step : pathArguments) {
+ if (!otherSteps.hasNext()) {
return false;
}
- final PathArgument la = lit.next();
- final PathArgument oa = oit.next();
-
- if (!la.getType().equals(oa.getType())) {
- return false;
- }
- if (la instanceof IdentifiableItem<?, ?> && oa instanceof IdentifiableItem<?, ?> && !la.equals(oa)) {
- return false;
+ final var otherStep = otherSteps.next();
+ if (step instanceof ExactDataObjectStep) {
+ if (!step.equals(otherStep)) {
+ return false;
+ }
+ } else if (step instanceof KeylessStep<?> keyless) {
+ if (!keyless.matches(otherStep)) {
+ return false;
+ }
+ } else {
+ throw new IllegalStateException("Unhandled step " + step);
}
}
return true;
}
- private <N extends DataObject> InstanceIdentifier<N> childIdentifier(final AbstractPathArgument<N> arg) {
+ private <N extends DataObject> @NonNull InstanceIdentifier<N> childIdentifier(final DataObjectStep<N> arg) {
return trustedCreate(arg, Iterables.concat(pathArguments, Collections.singleton(arg)),
- HashCodeBuilder.nextHashCode(hash, arg), isWildcarded());
+ HashCodeBuilder.nextHashCode(hash, arg), wildcarded);
}
/**
* @return An InstanceIdentifier.
* @throws NullPointerException if {@code container} is null
*/
- public final <N extends ChildOf<? super T>> InstanceIdentifier<N> child(final Class<N> container) {
- return childIdentifier(Item.of(container));
+ public final <N extends ChildOf<? super T>> @NonNull InstanceIdentifier<N> child(
+ final Class<@NonNull N> container) {
+ return childIdentifier(createStep(container));
}
/**
* @throws NullPointerException if any argument is null
*/
@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) {
- return (KeyedInstanceIdentifier<N, K>) childIdentifier(IdentifiableItem.of(listItem, listKey));
+ public final <N extends KeyAware<K> & ChildOf<? super T>, K extends Key<N>>
+ @NonNull KeyedInstanceIdentifier<N, K> child(final Class<@NonNull N> listItem, final K listKey) {
+ return (KeyedInstanceIdentifier<N, K>) childIdentifier(new KeyStep<>(listItem, listKey));
}
/**
- * Create an InstanceIdentifier for a child augmentation. This method is a more efficient equivalent to
- * {@code builder().augmentation(container).build()}.
+ * 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 {@code container} is null
+ * @throws NullPointerException if any argument is null
*/
- public final <N extends DataObject & Augmentation<? super T>> InstanceIdentifier<N> augmentation(
- final Class<N> container) {
- return childIdentifier(Item.of(container));
+ // 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(createStep(caze, container));
}
- @Deprecated
- private List<PathArgument> legacyCache;
+ /**
+ * 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 <C extends ChoiceIn<? super T> & DataObject, K extends Key<N>,
+ N extends KeyAware<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(new KeyStep<>(listItem, requireNonNull(caze), listKey));
+ }
/**
- * Return the path as a list.
+ * Create an InstanceIdentifier for a child augmentation. This method is a more efficient equivalent to
+ * {@code builder().augmentation(container).build()}.
*
- * @deprecated Use {@link #getPathArguments()} instead.
+ * @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);
- }
+ public final <N extends DataObject & Augmentation<? super T>> @NonNull InstanceIdentifier<N> augmentation(
+ final Class<@NonNull N> container) {
+ return childIdentifier(new NodeStep<>(container));
+ }
+
+ @java.io.Serial
+ Object writeReplace() throws ObjectStreamException {
+ return new IIv4<>(this);
+ }
- return legacyCache;
+ @java.io.Serial
+ private void readObject(final ObjectInputStream stream) throws IOException, ClassNotFoundException {
+ throwNSE();
+ }
+
+ @java.io.Serial
+ private void readObjectNoData() throws ObjectStreamException {
+ throwNSE();
+ }
+
+ @java.io.Serial
+ private void writeObject(final ObjectOutputStream stream) throws IOException {
+ throwNSE();
+ }
+
+ private void throwNSE() throws NotSerializableException {
+ throw new NotSerializableException(getClass().getName());
}
/**
*
* @return A builder instance
*/
- public InstanceIdentifierBuilder<T> builder() {
- return new InstanceIdentifierBuilderImpl<>(Item.of(targetType), pathArguments, hash, isWildcarded());
+ // FIXME: rename this method to 'toBuilder()'
+ public @NonNull Builder<T> builder() {
+ return new RegularBuilder<>(this);
}
/**
- * Create an InstanceIdentifierBuilder for a specific type of InstanceIdentifier as specified by container.
+ * Create a {@link Builder} for a specific type of InstanceIdentifier as specified by container.
*
* @param container Base container
* @param <T> Type of the container
- * @return A new {@link InstanceIdentifierBuilder}
+ * @return A new {@link Builder}
* @throws NullPointerException if {@code container} is null
*/
- public static <T extends ChildOf<? extends DataRoot>> InstanceIdentifierBuilder<T> builder(
+ public static <T extends ChildOf<? extends DataRoot>> @NonNull Builder<T> builder(
final Class<T> container) {
- return new InstanceIdentifierBuilderImpl<T>().addNode(container);
+ return new RegularBuilder<>(createStep(container));
+ }
+
+ /**
+ * Create a {@link Builder} 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 Builder}
+ * @throws NullPointerException if any argument is null
+ */
+ public static <C extends ChoiceIn<? extends DataRoot> & DataObject, T extends ChildOf<? super C>>
+ @NonNull Builder<T> builder(final Class<C> caze, final Class<T> container) {
+ return new RegularBuilder<>(createStep(caze, container));
}
/**
- * Create an InstanceIdentifierBuilder for a specific type of InstanceIdentifier which represents an
- * {@link IdentifiableItem}.
+ * Create a {@link Builder} 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}
+ * @return A new {@link Builder}
* @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 <N extends KeyAware<K> & ChildOf<? extends DataRoot>,
+ K extends Key<N>> @NonNull KeyedBuilder<N, K> builder(final Class<N> listItem,
+ final K listKey) {
+ return new KeyedBuilder<>(new KeyStep<>(listItem, listKey));
+ }
+
+ /**
+ * Create a {@link Builder} 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 Builder}
+ * @throws NullPointerException if any argument is null
+ */
+ public static <C extends ChoiceIn<? extends DataRoot> & DataObject,
+ N extends KeyAware<K> & ChildOf<? super C>, K extends Key<N>>
+ @NonNull KeyedBuilder<N, K> builder(final Class<C> caze, final Class<N> listItem,
+ final K listKey) {
+ return new KeyedBuilder<>(new KeyStep<>(listItem, requireNonNull(caze), listKey));
+ }
+
+ public static <R extends DataRoot & DataObject, T extends ChildOf<? super R>>
+ @NonNull Builder<T> builderOfInherited(final Class<R> root, final Class<T> container) {
+ // FIXME: we are losing root identity, hence namespaces may not work correctly
+ return new RegularBuilder<>(createStep(container));
+ }
+
+ public static <R extends DataRoot & DataObject, C extends ChoiceIn<? super R> & DataObject,
+ T extends ChildOf<? super C>>
+ @NonNull Builder<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 RegularBuilder<>(createStep(caze, container));
+ }
+
+ public static <R extends DataRoot & DataObject, N extends KeyAware<K> & ChildOf<? super R>,
+ K extends Key<N>>
+ @NonNull KeyedBuilder<N, K> 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 KeyedBuilder<>(new KeyStep<>(listItem, listKey));
+ }
+
+ public static <R extends DataRoot & DataObject, C extends ChoiceIn<? super R> & DataObject,
+ N extends KeyAware<K> & ChildOf<? super C>, K extends Key<N>>
+ @NonNull KeyedBuilder<N, K> 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 KeyedBuilder<>(new KeyStep<>(listItem, requireNonNull(caze), listKey));
+ }
+
+ @Beta
+ @SuppressWarnings({ "rawtypes", "unchecked" })
+ public static <T extends DataObject, C extends ChoiceIn<?> & DataObject> @NonNull DataObjectStep<T> createStep(
+ final Class<C> caze, final Class<T> type) {
+ return KeyAware.class.isAssignableFrom(type) ? new KeylessStep(type, caze) : new NodeStep<>(type, caze);
+ }
+
+ @Beta
+ public static <T extends DataObject> @NonNull DataObjectStep<T> createStep(final Class<T> type) {
+ return createStep(null, type);
}
/**
* @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 = requireNonNull(pathArguments, "pathArguments may not be null")
- .iterator();
- final HashCodeBuilder<PathArgument> hashBuilder = new HashCodeBuilder<>();
+ private static @NonNull InstanceIdentifier<?> internalCreate(final Iterable<DataObjectStep<?>> pathArguments) {
+ final var it = requireNonNull(pathArguments, "pathArguments may not be null").iterator();
+ checkArgument(it.hasNext(), "pathArguments may not be empty");
+
+ final var hashBuilder = new HashCodeBuilder<DataObjectStep<?>>();
boolean wildcard = false;
- PathArgument arg = null;
+ DataObjectStep<?> arg;
- while (it.hasNext()) {
+ do {
arg = it.next();
- checkArgument(arg != null, "pathArguments may not contain null elements");
+ // Non-null is implied by our callers
+ final var type = verifyNotNull(arg).type();
+ checkArgument(ChildOf.class.isAssignableFrom(type) || Augmentation.class.isAssignableFrom(type),
+ "%s is not a valid path argument", type);
- // TODO: sanity check ChildOf<>;
hashBuilder.addArgument(arg);
- if (Identifiable.class.isAssignableFrom(arg.getType()) && !(arg instanceof IdentifiableItem<?, ?>)) {
+ if (!(arg instanceof ExactDataObjectStep)) {
wildcard = true;
}
- }
- checkArgument(arg != null, "pathArguments may not be empty");
+ } while (it.hasNext());
return trustedCreate(arg, pathArguments, hashBuilder.build(), wildcard);
}
/**
- * Create an instance identifier for a very specific object type.
+ * Create an instance identifier for a sequence of {@link DataObjectStep} steps. The steps are required to be formed
+ * of classes extending either {@link ChildOf} or {@link Augmentation} contracts. This method does not check whether
+ * or not the sequence is structurally sound, for example that an {@link Augmentation} follows an
+ * {@link Augmentable} step. Furthermore the compile-time indicated generic type of the returned object does not
+ * necessarily match the contained state.
*
* <p>
- * Example:
- * <pre>
- * List<PathArgument> path = Arrays.asList(new Item(Nodes.class))
- * new InstanceIdentifier(path);
- * </pre>
+ * Failure to observe precautions to validate the list's contents may yield an object which mey be rejected at
+ * run-time or lead to undefined behaviour.
*
* @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 NullPointerException if {@code pathArguments} is, or contains an item which is, {@code null}
+ * @throws IllegalArgumentException if {@code pathArguments} is empty or contains an item which does not represent
+ * a valid addressing step.
*/
- public static InstanceIdentifier<?> create(final Iterable<? extends PathArgument> pathArguments) {
- if (pathArguments instanceof ImmutableCollection<?>) {
- @SuppressWarnings("unchecked")
- final Iterable<PathArgument> immutableArguments = (Iterable<PathArgument>) pathArguments;
- return internalCreate(immutableArguments);
- }
-
- return internalCreate(ImmutableList.copyOf(pathArguments));
+ @SuppressWarnings("unchecked")
+ public static <T extends DataObject> @NonNull InstanceIdentifier<T> unsafeOf(
+ final List<? extends DataObjectStep<?>> pathArguments) {
+ return (InstanceIdentifier<T>) internalCreate(ImmutableList.copyOf(pathArguments));
}
/**
* @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(ImmutableList.of(Item.of(type)));
+ public static <T extends ChildOf<? extends DataRoot>> @NonNull InstanceIdentifier<T> create(
+ final Class<@NonNull T> type) {
+ return (InstanceIdentifier<T>) internalCreate(ImmutableList.of(createStep(type)));
}
/**
* @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(
+ // FIXME: reconsider naming and design of this method
+ public static <N extends KeyAware<K> & DataObject, K extends Key<N>> K keyOf(
final InstanceIdentifier<N> id) {
requireNonNull(id);
checkArgument(id instanceof KeyedInstanceIdentifier, "%s does not have a key", id);
}
@SuppressWarnings({ "unchecked", "rawtypes" })
- static <N extends DataObject> 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;
- } else {
- wildcarded = true;
- }
-
- return new KeyedInstanceIdentifier(arg.getType(), pathArguments, wildcarded, hash, key);
+ static <N extends DataObject> @NonNull InstanceIdentifier<N> trustedCreate(final DataObjectStep<?> lastStep,
+ final Iterable<DataObjectStep<?>> pathArguments, final int hash, final boolean wildcarded) {
+ if (lastStep instanceof NodeStep) {
+ return new InstanceIdentifier(lastStep.type(), pathArguments, wildcarded, hash);
+ } else if (lastStep instanceof KeyStep<?, ?> predicate) {
+ return new KeyedInstanceIdentifier(predicate, pathArguments, wildcarded, hash);
+ } else if (lastStep instanceof KeylessStep) {
+ return new InstanceIdentifier(lastStep.type(), pathArguments, true, hash);
+ } else {
+ throw new IllegalStateException("Unhandled step " + lastStep);
}
-
- return new InstanceIdentifier(arg.getType(), pathArguments, wildcarded, hash);
}
- /**
- * 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> {
+ @Deprecated(since = "13.0.0", forRemoval = true)
+ private abstract static sealed class AbstractPathArgument<T extends DataObject>
+ implements Comparable<AbstractPathArgument<?>>, Serializable {
+ @java.io.Serial
+ private static final long serialVersionUID = 1L;
+
+ private final @NonNull Class<T> type;
+
+ AbstractPathArgument(final Class<T> type) {
+ this.type = requireNonNull(type, "Type may not be null.");
+ }
+
/**
* Return the data object type backing this PathArgument.
*
* @return Data object type.
*/
- Class<? extends DataObject> getType();
- }
+ final @NonNull Class<T> type() {
+ return type;
+ }
- private abstract static class AbstractPathArgument<T extends DataObject> implements PathArgument, Serializable {
- private static final long serialVersionUID = 1L;
- private final Class<T> type;
+ /**
+ * Return an optional enclosing case type. This is used only when {@link #type()} references a node defined
+ * in a {@code grouping} which is reference inside a {@code case} statement in order to safely reference the
+ * node.
+ *
+ * @return case class or {@code null}
+ */
+ Class<? extends DataObject> caseType() {
+ return null;
+ }
- protected AbstractPathArgument(final Class<T> type) {
- this.type = requireNonNull(type, "Type may not be null.");
+ @Nullable Object key() {
+ return null;
}
@Override
- public final Class<T> getType() {
- return type;
+ public final int hashCode() {
+ return Objects.hash(type, caseType(), key());
}
@Override
- public int hashCode() {
- return type.hashCode();
+ public final boolean equals(final Object obj) {
+ return this == obj || obj instanceof AbstractPathArgument<?> other && type.equals(other.type)
+ && Objects.equals(key(), other.key()) && Objects.equals(caseType(), other.caseType());
}
@Override
- public boolean equals(final Object obj) {
- if (this == obj) {
- return true;
+ public final int compareTo(final AbstractPathArgument<?> arg) {
+ final int cmp = compareClasses(type, arg.type());
+ if (cmp != 0) {
+ return cmp;
}
- if (obj == null) {
- return false;
+ final var caseType = caseType();
+ final var argCaseType = arg.caseType();
+ if (caseType == null) {
+ return argCaseType == null ? 1 : -1;
}
- if (getClass() != obj.getClass()) {
- return false;
- }
- final AbstractPathArgument<?> other = (AbstractPathArgument<?>) obj;
- return type.equals(other.type);
+ return argCaseType == null ? 1 : compareClasses(caseType, argCaseType);
}
- @Override
- public int compareTo(final PathArgument arg) {
- return type.getCanonicalName().compareTo(arg.getType().getCanonicalName());
+ private static int compareClasses(final Class<?> first, final Class<?> second) {
+ return first.getCanonicalName().compareTo(second.getCanonicalName());
+ }
+
+ @java.io.Serial
+ final Object readResolve() throws ObjectStreamException {
+ return toStep();
}
+
+ abstract DataObjectStep<?> toStep();
}
/**
*
* @param <T> Item type
*/
- public static final class Item<T extends DataObject> extends AbstractPathArgument<T> {
+ @Deprecated(since = "13.0.0", forRemoval = true)
+ private static sealed class Item<T extends DataObject> extends AbstractPathArgument<T> {
+ @java.io.Serial
private static final long serialVersionUID = 1L;
- /**
- * Construct an Item.
- *
- * @param type Backing class
- * @deprecated Use {@link #of(Class)} instead.
- */
- @Deprecated
- 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> Item<T> of(final Class<T> type) {
- return new Item<>(type);
+ @Override
+ @SuppressWarnings({ "rawtypes", "unchecked" })
+ final DataObjectStep<?> toStep() {
+ return createStep((Class) caseType(), type());
}
@Override
public String toString() {
- return getType().getName();
+ return type().getName();
}
}
* @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>>
+ @Deprecated(since = "13.0.0", forRemoval = true)
+ private static sealed class IdentifiableItem<I extends KeyAware<T> & DataObject, T extends Key<I>>
extends AbstractPathArgument<I> {
+ @java.io.Serial
private static final long serialVersionUID = 1L;
- private final T key;
- /**
- * Construct an Item.
- *
- * @param type Backing class
- * @param key key
- * @deprecated Use {@link #of(Class, Identifier)} instead.
- */
- @Deprecated
- 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 = requireNonNull(key, "Key may not be null.");
}
- /**
- * 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>> IdentifiableItem<T, I> of(
- final Class<T> type, final I key) {
- return new IdentifiableItem<>(type, key);
- }
-
/**
* Return the data object type backing this PathArgument.
*
* @return Data object type.
*/
- public T getKey() {
- return this.key;
+ @Override
+ final @NonNull T key() {
+ return key;
+ }
+
+ @Override
+ final KeyStep<?, ?> toStep() {
+ return new KeyStep<>(type(), caseType(), key);
}
@Override
- public boolean equals(final Object obj) {
- return super.equals(obj) && key.equals(((IdentifiableItem<?, ?>) obj).getKey());
+ public String toString() {
+ return type().getName() + "[key=" + key + "]";
+ }
+ }
+
+ @Deprecated(since = "13.0.0", forRemoval = true)
+ private static final class CaseItem<C extends ChoiceIn<?> & DataObject, T extends ChildOf<? super C>>
+ extends Item<T> {
+ @java.io.Serial
+ private static final long serialVersionUID = 1L;
+
+ private final Class<C> caseType;
+
+ CaseItem(final Class<C> caseType, final Class<T> type) {
+ super(type);
+ this.caseType = requireNonNull(caseType);
}
@Override
- public int hashCode() {
- return super.hashCode() * 31 + key.hashCode();
+ Class<C> caseType() {
+ return caseType;
+ }
+ }
+
+ @Deprecated(since = "13.0.0", forRemoval = true)
+ private static final class CaseIdentifiableItem<C extends ChoiceIn<?> & DataObject,
+ T extends ChildOf<? super C> & KeyAware<K>, K extends Key<T>> extends IdentifiableItem<T, K> {
+ @java.io.Serial
+ 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 String toString() {
- return getType().getName() + "[key=" + key + "]";
+ Class<C> caseType() {
+ return caseType;
}
}
- public interface InstanceIdentifierBuilder<T extends DataObject> extends Builder<InstanceIdentifier<T>> {
+ /**
+ * A builder of {@link InstanceIdentifier} objects.
+ *
+ * @param <T> Instance identifier target type
+ */
+ public abstract static sealed class Builder<T extends DataObject> {
+ private final ImmutableList.Builder<DataObjectStep<?>> pathBuilder;
+ private final HashCodeBuilder<DataObjectStep<?>> hashBuilder;
+ private final Iterable<? extends DataObjectStep<?>> basePath;
+
+ private boolean wildcard;
+
+ Builder(final Builder<?> prev, final DataObjectStep<?> item) {
+ pathBuilder = prev.pathBuilder;
+ hashBuilder = prev.hashBuilder;
+ basePath = prev.basePath;
+ wildcard = prev.wildcard;
+ appendItem(item);
+ }
+
+ Builder(final InstanceIdentifier<T> identifier) {
+ pathBuilder = ImmutableList.builder();
+ hashBuilder = new HashCodeBuilder<>(identifier.hashCode());
+ wildcard = identifier.isWildcarded();
+ basePath = identifier.pathArguments;
+ }
+
+ Builder(final DataObjectStep<?> item, final boolean wildcard) {
+ pathBuilder = ImmutableList.builder();
+ hashBuilder = new HashCodeBuilder<>();
+ basePath = null;
+ hashBuilder.addArgument(item);
+ pathBuilder.add(item);
+ this.wildcard = wildcard;
+ }
+
+ final boolean wildcard() {
+ return wildcard;
+ }
+
/**
- * 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
+ * Build an identifier which refers to a specific augmentation of the current InstanceIdentifier referenced by
+ * the builder.
*
- * Example,
+ * @param container augmentation class
+ * @param <N> augmentation type
+ * @return this builder
+ * @throws NullPointerException if {@code container} is null
+ */
+ public final <N extends DataObject & Augmentation<? super T>> Builder<N> augmentation(
+ final Class<N> container) {
+ return append(new NodeStep<>(container));
+ }
+
+ /**
+ * 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
*
* @return this builder
* @throws NullPointerException if {@code container} is null
*/
- <N extends ChildOf<? super T>> InstanceIdentifierBuilder<N> child(Class<N> container);
+ public final <N extends ChildOf<? super T>> Builder<N> child(final Class<N> container) {
+ return append(createStep(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
+ */
+ public final <C extends ChoiceIn<? super T> & DataObject, N extends ChildOf<? super C>> Builder<N> child(
+ final Class<C> caze, final Class<N> container) {
+ return append(createStep(caze, 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 List to append
* @param listKey List key
* @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);
+ public final <N extends KeyAware<K> & ChildOf<? super T>, K extends Key<N>> KeyedBuilder<N, K> child(
+ final Class<@NonNull N> listItem, final K listKey) {
+ return append(new KeyStep<>(listItem, listKey));
+ }
/**
- * Build an identifier which refers to a specific augmentation of the current InstanceIdentifier referenced by
- * the builder.
+ * 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 container augmentation class
- * @param <N> augmentation type
+ * @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 {@code container} is null
+ * @throws NullPointerException if any argument is null
*/
- <N extends DataObject & Augmentation<? super T>> InstanceIdentifierBuilder<N> augmentation(Class<N> container);
+ public final <C extends ChoiceIn<? super T> & DataObject, K extends Key<N>,
+ N extends KeyAware<K> & ChildOf<? super C>> KeyedBuilder<N, K> child(final Class<C> caze,
+ final Class<N> listItem, final K listKey) {
+ return append(new KeyStep<>(listItem, requireNonNull(caze), listKey));
+ }
/**
* Build the instance identifier.
*
- * @return Resulting instance identifier.
+ * @return Resulting {@link InstanceIdentifier}.
*/
+ public abstract @NonNull InstanceIdentifier<T> build();
+
@Override
- InstanceIdentifier<T> build();
+ public final int hashCode() {
+ return hashBuilder.build();
+ }
+
+ @Override
+ public final boolean equals(final Object obj) {
+ return this == obj || obj instanceof Builder<?> other
+ && wildcard == other.wildcard && hashCode() == other.hashCode()
+ && Iterables.elementsEqual(pathArguments(), other.pathArguments());
+ }
+
+ final Iterable<DataObjectStep<?>> pathArguments() {
+ final var args = pathBuilder.build();
+ return basePath == null ? args : Iterables.concat(basePath, args);
+ }
+
+ final void appendItem(final DataObjectStep<?> item) {
+ hashBuilder.addArgument(item);
+ pathBuilder.add(item);
+ if (!(item instanceof ExactDataObjectStep)) {
+ wildcard = true;
+ }
+ }
+
+ abstract <X extends DataObject> @NonNull RegularBuilder<X> append(DataObjectStep<X> step);
+
+ abstract <X extends DataObject & KeyAware<Y>, Y extends Key<X>> @NonNull KeyedBuilder<X, Y> append(
+ KeyStep<Y, X> step);
}
- private void writeObject(final java.io.ObjectOutputStream out) throws IOException {
- out.defaultWriteObject();
- out.writeInt(Iterables.size(pathArguments));
- for (Object o : pathArguments) {
- out.writeObject(o);
+ public static final class KeyedBuilder<T extends DataObject & KeyAware<K>, K extends Key<T>>
+ extends Builder<T> {
+ private @NonNull KeyStep<K, T> lastStep;
+
+ KeyedBuilder(final KeyStep<K, T> firstStep) {
+ super(firstStep, false);
+ lastStep = requireNonNull(firstStep);
+ }
+
+ KeyedBuilder(final KeyedInstanceIdentifier<T, K> identifier) {
+ super(identifier);
+ lastStep = identifier.lastStep();
+ }
+
+ private KeyedBuilder(final RegularBuilder<?> prev, final KeyStep<K, T> lastStep) {
+ super(prev, lastStep);
+ this.lastStep = requireNonNull(lastStep);
+ }
+
+ /**
+ * Build the instance identifier.
+ *
+ * @return Resulting {@link KeyedInstanceIdentifier}.
+ */
+ @Override
+ public @NonNull KeyedInstanceIdentifier<T, K> build() {
+ return new KeyedInstanceIdentifier<>(lastStep, pathArguments(), wildcard(), hashCode());
+ }
+
+ @Override
+ <X extends DataObject> @NonNull RegularBuilder<X> append(final DataObjectStep<X> step) {
+ return new RegularBuilder<>(this, step);
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ <X extends DataObject & KeyAware<Y>, Y extends Key<X>> KeyedBuilder<X, Y> append(final KeyStep<Y, X> step) {
+ appendItem(step);
+ lastStep = (KeyStep<K, T>) requireNonNull(step);
+ return (KeyedBuilder<X, Y>) this;
}
}
- private void readObject(final java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
- in.defaultReadObject();
+ private static final class RegularBuilder<T extends DataObject> extends Builder<T> {
+ private @NonNull Class<T> type;
+
+ RegularBuilder(final DataObjectStep<T> item) {
+ super(item, !(item instanceof ExactDataObjectStep));
+ type = item.type();
+ }
+
+ RegularBuilder(final InstanceIdentifier<T> identifier) {
+ super(identifier);
+ type = identifier.getTargetType();
+ }
+
+ private RegularBuilder(final KeyedBuilder<?, ?> prev, final DataObjectStep<T> item) {
+ super(prev, item);
+ type = item.type();
+ }
+
+ @Override
+ public InstanceIdentifier<T> build() {
+ return new InstanceIdentifier<>(type, pathArguments(), wildcard(), hashCode());
+ }
- final int size = in.readInt();
- final List<PathArgument> args = new ArrayList<>(size);
- for (int i = 0; i < size; ++i) {
- args.add((PathArgument) in.readObject());
+ @Override
+ @SuppressWarnings({ "rawtypes", "unchecked" })
+ <X extends DataObject> RegularBuilder<X> append(final DataObjectStep<X> step) {
+ appendItem(step);
+ type = (Class) step.type();
+ return (RegularBuilder<X>) this;
}
- try {
- PATHARGUMENTS_FIELD.set(this, ImmutableList.copyOf(args));
- } catch (IllegalArgumentException | IllegalAccessException e) {
- throw new IOException(e);
+ @Override
+ <X extends DataObject & KeyAware<Y>, Y extends Key<X>> KeyedBuilder<X, Y> append(
+ final KeyStep<Y, X> item) {
+ return new KeyedBuilder<>(this, item);
}
}
}