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.VerifyException;
import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import java.util.Optional;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
-import org.opendaylight.yangtools.concepts.Builder;
-import org.opendaylight.yangtools.concepts.Immutable;
-import org.opendaylight.yangtools.concepts.Path;
+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 {
+public class InstanceIdentifier<T extends DataObject>
+ implements HierarchicalIdentifier<InstanceIdentifier<? extends DataObject>> {
private static final long serialVersionUID = 3L;
/*
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 trimmed instance identifier, or null if the component type
* is not present.
*/
- public final <I extends DataObject> @Nullable 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())) {
* is not present.
*/
public final <N extends Identifiable<K> & DataObject, K extends Identifier<N>> @Nullable K firstKeyOf(
- final Class<N> listItem) {
+ final Class<@NonNull N> listItem) {
for (final PathArgument i : pathArguments) {
if (listItem.equals(i.getType())) {
@SuppressWarnings("unchecked")
* @return An InstanceIdentifier.
* @throws NullPointerException if {@code container} is null
*/
- public final <N extends ChildOf<? super T>> @NonNull 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));
}
*/
@SuppressWarnings("unchecked")
public final <N extends Identifiable<K> & ChildOf<? super T>, K extends Identifier<N>>
- @NonNull KeyedInstanceIdentifier<N, K> child(final Class<N> listItem, final K listKey) {
+ @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
*/
+ // FIXME: add a proper caller
public final <C extends ChoiceIn<? super T> & DataObject, N extends ChildOf<? super C>>
- @NonNull InstanceIdentifier<N> child(final Class<C> caze, final Class<N> container) {
+ @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>> @NonNull KeyedInstanceIdentifier<N, K> child(
- final Class<C> caze, final Class<N> listItem, final K listKey) {
+ final Class<@NonNull C> caze, final Class<@NonNull N> listItem, final K listKey) {
return (KeyedInstanceIdentifier<N, K>) childIdentifier(IdentifiableItem.of(caze, listItem, listKey));
}
* @throws NullPointerException if {@code container} is null
*/
public final <N extends DataObject & Augmentation<? super T>> @NonNull InstanceIdentifier<N> augmentation(
- final Class<N> container) {
+ final Class<@NonNull N> container) {
return childIdentifier(Item.of(container));
}
*
* @return A builder instance
*/
+ // FIXME: rename this method to 'toBuilder()'
public @NonNull InstanceIdentifierBuilder<T> builder() {
return new InstanceIdentifierBuilderImpl<>(Item.of(targetType), pathArguments, hash, isWildcarded());
}
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.
* @throws NullPointerException if {@code pathArguments} is null
*/
private static @NonNull InstanceIdentifier<?> internalCreate(final Iterable<PathArgument> pathArguments) {
- final Iterator<? extends PathArgument> it = requireNonNull(pathArguments, "pathArguments may not be null")
- .iterator();
+ 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 arg = null;
+ PathArgument 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).getType();
+ 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 (Identifiable.class.isAssignableFrom(type) && !(arg instanceof IdentifiableItem)) {
wildcard = true;
}
- }
- checkArgument(arg != null, "pathArguments may not be empty");
+ } while (it.hasNext());
return trustedCreate(arg, pathArguments, hashBuilder.build(), wildcard);
}
* @throws IllegalArgumentException if pathArguments is empty or
* contains a null element.
*/
+ // FIXME: rename to 'unsafeOf()'
public static @NonNull InstanceIdentifier<?> create(final Iterable<? extends PathArgument> pathArguments) {
- if (pathArguments instanceof ImmutableCollection<?>) {
+ if (pathArguments instanceof ImmutableCollection) {
@SuppressWarnings("unchecked")
- final Iterable<PathArgument> immutableArguments = (Iterable<PathArgument>) pathArguments;
+ final var immutableArguments = (ImmutableCollection<PathArgument>) pathArguments;
return internalCreate(immutableArguments);
}
* @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> @NonNull 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(Item.of(type)));
}
/**
* @throws IllegalArgumentException if the supplied identifier type cannot have a key.
* @throws NullPointerException if id is null.
*/
+ // 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);
}
}
- public interface InstanceIdentifierBuilder<T extends DataObject> extends Builder<InstanceIdentifier<T>> {
+ // 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
*
<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
@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
* @throws NullPointerException if any argument is null
*/
<N extends Identifiable<K> & ChildOf<? super T>, K extends Identifier<N>>
- @NonNull InstanceIdentifierBuilder<N> child(Class<N> listItem, K listKey);
+ @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
*
* @return Resulting instance identifier.
*/
- @Override
- InstanceIdentifier<T> build();
+ @NonNull InstanceIdentifier<T> build();
}
private Object writeReplace() throws ObjectStreamException {