package org.opendaylight.yangtools.yang.binding;
import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Verify.verify;
import static java.util.Objects.requireNonNull;
import com.google.common.base.MoreObjects;
import com.google.common.base.MoreObjects.ToStringHelper;
+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 java.util.Objects;
import java.util.Optional;
-import org.opendaylight.yangtools.concepts.Builder;
-import org.opendaylight.yangtools.concepts.Immutable;
-import org.opendaylight.yangtools.concepts.Path;
+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>
* 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 final transient 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 = requireNonNull(pathArguments);
*
* @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);
}
* @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())) {
* 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.
- *
- * @param listItem component 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) {
+ 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")
return true;
}
- private <N extends DataObject> InstanceIdentifier<N> childIdentifier(final AbstractPathArgument<N> arg) {
+ 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());
}
* @return An InstanceIdentifier.
* @throws NullPointerException if {@code container} is null
*/
- public final <N extends ChildOf<? super T>> InstanceIdentifier<N> child(final Class<N> container) {
+ public final <N extends ChildOf<? super T>> @NonNull InstanceIdentifier<N> child(
+ final Class<@NonNull N> container) {
return childIdentifier(Item.of(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) {
+ 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));
}
* @return An InstanceIdentifier.
* @throws NullPointerException if any argument is null
*/
- public final <C extends ChoiceIn<? super T> & DataObject, N extends ChildOf<? super C>> InstanceIdentifier<N> child(
- final Class<C> caze, final Class<N> 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(Item.of(caze, container));
}
* @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 Identifier<N>,
- N extends Identifiable<K> & ChildOf<? super C>> KeyedInstanceIdentifier<N, K> child(final Class<C> caze,
- final Class<N> listItem, final K listKey) {
+ 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));
}
* @return An InstanceIdentifier.
* @throws NullPointerException if {@code container} is null
*/
- public final <N extends DataObject & Augmentation<? super T>> InstanceIdentifier<N> augmentation(
- final Class<N> container) {
+ public final <N extends DataObject & Augmentation<? super T>> @NonNull InstanceIdentifier<N> augmentation(
+ final Class<@NonNull N> container) {
return childIdentifier(Item.of(container));
}
- @Deprecated
- private List<PathArgument> legacyCache;
-
- /**
- * Return the path as a list.
- *
- * @deprecated Use {@link #getPathArguments()} instead.
- */
- @Deprecated
- public final List<PathArgument> getPath() {
- if (legacyCache == null) {
- legacyCache = ImmutableList.<PathArgument>copyOf(pathArguments);
- }
-
- return legacyCache;
- }
-
/**
* Create a builder rooted at this key.
*
* @return A builder instance
*/
- public InstanceIdentifierBuilder<T> builder() {
+ public @NonNull InstanceIdentifierBuilder<T> builder() {
return new InstanceIdentifierBuilderImpl<>(Item.of(targetType), pathArguments, hash, isWildcarded());
}
* @return A new {@link InstanceIdentifierBuilder}
* @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 InstanceIdentifierBuilder<T> builder(
final Class<T> container) {
return new InstanceIdentifierBuilderImpl<T>().addWildNode(Item.of(container));
}
+ /**
+ * 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 <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 InstanceIdentifierBuilder for a specific type of InstanceIdentifier which represents an
* {@link IdentifiableItem}.
* @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) {
+ 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));
+ }
+
/**
* 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.
* @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) {
+ private static @NonNull 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<>();
* @throws IllegalArgumentException if pathArguments is empty or
* contains a null element.
*/
- public static InstanceIdentifier<?> create(final Iterable<? extends PathArgument> pathArguments) {
- if (pathArguments instanceof ImmutableCollection<?>) {
+ 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);
}
* @return InstanceIdentifier instance
*/
@SuppressWarnings("unchecked")
- public static <T extends DataObject> InstanceIdentifier<T> create(final Class<T> type) {
+ public static <T extends DataObject> @NonNull InstanceIdentifier<T> create(final Class<@NonNull T> type) {
return (InstanceIdentifier<T>) create(ImmutableList.of(Item.of(type)));
}
}
@SuppressWarnings({ "unchecked", "rawtypes" })
- static <N extends DataObject> InstanceIdentifier<N> trustedCreate(final PathArgument arg,
+ 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;
*
* @return Data object type.
*/
- Class<? extends DataObject> getType();
+ @NonNull Class<? extends DataObject> getType();
/**
* Return an optional enclosing case type. This is used only when {@link #getType()} references a node defined
private abstract static class AbstractPathArgument<T extends DataObject> implements PathArgument, Serializable {
private static final long serialVersionUID = 1L;
- private final Class<T> type;
+
+ private final @NonNull Class<T> type;
AbstractPathArgument(final Class<T> type) {
this.type = requireNonNull(type, "Type may not be null.");
public static class Item<T extends DataObject> extends AbstractPathArgument<T> {
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 new PathArgument
* @throws NullPointerException if {@code} is null.
*/
- public static <T extends DataObject> Item<T> of(final Class<T> type) {
+ public static <T extends DataObject> @NonNull Item<T> of(final Class<T> type) {
return new Item<>(type);
}
* @return A new PathArgument
* @throws NullPointerException if any argument is null.
*/
- public static <C extends ChoiceIn<?> & DataObject, T extends ChildOf<? super C>> Item<T> of(
+ 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);
}
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;
- /**
- * 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
* @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) {
+ 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);
}
* @throws NullPointerException if any argument is null.
*/
public static <C extends ChoiceIn<?> & DataObject, T extends ChildOf<? super C> & Identifiable<I>,
- I extends Identifier<T>> IdentifiableItem<T, I> of(final Class<C> caseType, final Class<T> type,
- final I key) {
+ 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 Data object type.
*/
@Override
- public final T getKey() {
+ public final @NonNull T getKey() {
return key;
}
}
}
- public interface InstanceIdentifierBuilder<T extends DataObject> extends Builder<InstanceIdentifier<T>> {
+ 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
*
* @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 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.
+ * 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.
*
* @param caze Choice case class
* @param container Container to append
* @return this builder
* @throws NullPointerException if {@code container} is null
*/
- <C extends ChoiceIn<? super T> & DataObject, N extends ChildOf<? super C>> InstanceIdentifierBuilder<N> child(
- Class<C> caze, Class<N> container);
+ <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
+ * 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);
+ <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.
+ * 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
* @throws NullPointerException if any argument is null
*/
<C extends ChoiceIn<? super T> & DataObject, K extends Identifier<N>,
- N extends Identifiable<K> & ChildOf<? super C>> InstanceIdentifierBuilder<N> child(Class<C> caze,
- Class<N> listItem, K listKey);
+ 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
* @return this builder
* @throws NullPointerException if {@code container} is null
*/
- <N extends DataObject & Augmentation<? super T>> InstanceIdentifierBuilder<N> augmentation(Class<N> container);
+ <N extends DataObject & Augmentation<? super T>> @NonNull InstanceIdentifierBuilder<N> augmentation(
+ Class<N> container);
/**
* Build the instance identifier.
*
* @return Resulting instance identifier.
*/
- @Override
- InstanceIdentifier<T> build();
- }
-
- 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);
}
}