X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=binding%2Fyang-binding%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang%2Fbinding%2FInstanceIdentifier.java;h=416ce36b3d132f2f9664ace6cb0293579ecc9b53;hb=refs%2Fchanges%2F62%2F100062%2F1;hp=246e63774a0624a5bb664e11b48d98cd3ee9871e;hpb=2573b50a5539aba4687150a91e6683520688d050;p=mdsal.git diff --git a/binding/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/InstanceIdentifier.java b/binding/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/InstanceIdentifier.java index 246e63774a..416ce36b3d 100644 --- a/binding/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/InstanceIdentifier.java +++ b/binding/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/InstanceIdentifier.java @@ -8,10 +8,12 @@ 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; @@ -21,9 +23,9 @@ import java.util.Collections; import java.util.Iterator; 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; /** @@ -57,8 +59,8 @@ import org.opendaylight.yangtools.util.HashCodeBuilder; *

* 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 implements Path>, - Immutable, Serializable { +public class InstanceIdentifier + implements HierarchicalIdentifier> { private static final long serialVersionUID = 3L; /* @@ -67,7 +69,7 @@ public class InstanceIdentifier implements Path pathArguments; - private final Class targetType; + private final @NonNull Class targetType; private final boolean wildcarded; private final int hash; @@ -84,16 +86,30 @@ public class InstanceIdentifier implements Path getTargetType() { + public final @NonNull Class 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 @NonNull InstanceIdentifier verifyTarget(final Class<@NonNull N> target) { + verify(target.equals(targetType), "Cannot adapt %s to %s", this, target); + return (InstanceIdentifier) 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 getPathArguments() { + public final @NonNull Iterable getPathArguments() { return Iterables.unmodifiableIterable(pathArguments); } @@ -199,7 +215,8 @@ public class InstanceIdentifier implements Path InstanceIdentifier firstIdentifierOf(final Class type) { + public final @Nullable InstanceIdentifier firstIdentifierOf( + final Class<@NonNull I> type) { int count = 1; for (final PathArgument a : pathArguments) { if (type.equals(a.getType())) { @@ -223,8 +240,8 @@ public class InstanceIdentifier implements Path & DataObject, K extends Identifier> K firstKeyOf( - final Class listItem) { + public final & DataObject, K extends Identifier> @Nullable K firstKeyOf( + final Class<@NonNull N> listItem) { for (final PathArgument i : pathArguments) { if (listItem.equals(i.getType())) { @SuppressWarnings("unchecked") @@ -309,7 +326,7 @@ public class InstanceIdentifier implements Path InstanceIdentifier childIdentifier(final AbstractPathArgument arg) { + private @NonNull InstanceIdentifier childIdentifier(final AbstractPathArgument arg) { return trustedCreate(arg, Iterables.concat(pathArguments, Collections.singleton(arg)), HashCodeBuilder.nextHashCode(hash, arg), isWildcarded()); } @@ -323,7 +340,8 @@ public class InstanceIdentifier implements Path> InstanceIdentifier child(final Class container) { + public final > @NonNull InstanceIdentifier child( + final Class<@NonNull N> container) { return childIdentifier(Item.of(container)); } @@ -339,8 +357,8 @@ public class InstanceIdentifier implements Path & ChildOf, K extends Identifier> KeyedInstanceIdentifier - child(final Class listItem, final K listKey) { + public final & ChildOf, K extends Identifier> + @NonNull KeyedInstanceIdentifier child(final Class<@NonNull N> listItem, final K listKey) { return (KeyedInstanceIdentifier) childIdentifier(IdentifiableItem.of(listItem, listKey)); } @@ -355,8 +373,9 @@ public class InstanceIdentifier implements Path & DataObject, N extends ChildOf> InstanceIdentifier child( - final Class caze, final Class container) { + // FIXME: add a proper caller + public final & DataObject, N extends ChildOf> + @NonNull InstanceIdentifier child(final Class<@NonNull C> caze, final Class<@NonNull N> container) { return childIdentifier(Item.of(caze, container)); } @@ -373,10 +392,11 @@ public class InstanceIdentifier implements Path & DataObject, K extends Identifier, - N extends Identifiable & ChildOf> KeyedInstanceIdentifier child(final Class caze, - final Class listItem, final K listKey) { + N extends Identifiable & ChildOf> @NonNull KeyedInstanceIdentifier child( + final Class<@NonNull C> caze, final Class<@NonNull N> listItem, final K listKey) { return (KeyedInstanceIdentifier) childIdentifier(IdentifiableItem.of(caze, listItem, listKey)); } @@ -389,8 +409,8 @@ public class InstanceIdentifier implements Path> InstanceIdentifier augmentation( - final Class container) { + public final > @NonNull InstanceIdentifier augmentation( + final Class<@NonNull N> container) { return childIdentifier(Item.of(container)); } @@ -399,7 +419,7 @@ public class InstanceIdentifier implements Path builder() { + public @NonNull InstanceIdentifierBuilder builder() { return new InstanceIdentifierBuilderImpl<>(Item.of(targetType), pathArguments, hash, isWildcarded()); } @@ -411,7 +431,7 @@ public class InstanceIdentifier implements Path> InstanceIdentifierBuilder builder( + public static > @NonNull InstanceIdentifierBuilder builder( final Class container) { return new InstanceIdentifierBuilderImpl().addWildNode(Item.of(container)); } @@ -428,7 +448,7 @@ public class InstanceIdentifier implements Path & DataObject, T extends ChildOf> - InstanceIdentifierBuilder builder(final Class caze, final Class container) { + @NonNull InstanceIdentifierBuilder builder(final Class caze, final Class container) { return new InstanceIdentifierBuilderImpl().addWildNode(Item.of(caze, container)); } @@ -444,7 +464,8 @@ public class InstanceIdentifier implements Path & ChildOf, - K extends Identifier> InstanceIdentifierBuilder builder(final Class listItem, final K listKey) { + K extends Identifier> @NonNull InstanceIdentifierBuilder builder(final Class listItem, + final K listKey) { return new InstanceIdentifierBuilderImpl().addNode(IdentifiableItem.of(listItem, listKey)); } @@ -463,7 +484,8 @@ public class InstanceIdentifier implements Path & DataObject, N extends Identifiable & ChildOf, K extends Identifier> - InstanceIdentifierBuilder builder(final Class caze, final Class listItem, final K listKey) { + @NonNull InstanceIdentifierBuilder builder(final Class caze, final Class listItem, + final K listKey) { return new InstanceIdentifierBuilderImpl().addNode(IdentifiableItem.of(caze, listItem, listKey)); } @@ -476,7 +498,7 @@ public class InstanceIdentifier implements Path internalCreate(final Iterable pathArguments) { + private static @NonNull InstanceIdentifier internalCreate(final Iterable pathArguments) { final Iterator it = requireNonNull(pathArguments, "pathArguments may not be null") .iterator(); final HashCodeBuilder hashBuilder = new HashCodeBuilder<>(); @@ -514,10 +536,10 @@ public class InstanceIdentifier implements Path create(final Iterable pathArguments) { - if (pathArguments instanceof ImmutableCollection) { + public static @NonNull InstanceIdentifier create(final Iterable pathArguments) { + if (pathArguments instanceof ImmutableCollection) { @SuppressWarnings("unchecked") - final Iterable immutableArguments = (Iterable) pathArguments; + final var immutableArguments = (ImmutableCollection) pathArguments; return internalCreate(immutableArguments); } @@ -538,7 +560,7 @@ public class InstanceIdentifier implements Path InstanceIdentifier create(final Class type) { + public static @NonNull InstanceIdentifier create(final Class<@NonNull T> type) { return (InstanceIdentifier) create(ImmutableList.of(Item.of(type))); } @@ -561,7 +583,7 @@ public class InstanceIdentifier implements Path InstanceIdentifier trustedCreate(final PathArgument arg, + static @NonNull InstanceIdentifier trustedCreate(final PathArgument arg, final Iterable pathArguments, final int hash, boolean wildcarded) { if (Identifiable.class.isAssignableFrom(arg.getType()) && !wildcarded) { Identifier key = null; @@ -587,7 +609,7 @@ public class InstanceIdentifier implements Path getType(); + @NonNull Class getType(); /** * Return an optional enclosing case type. This is used only when {@link #getType()} references a node defined @@ -603,7 +625,8 @@ public class InstanceIdentifier implements Path implements PathArgument, Serializable { private static final long serialVersionUID = 1L; - private final Class type; + + private final @NonNull Class type; AbstractPathArgument(final Class type) { this.type = requireNonNull(type, "Type may not be null."); @@ -676,7 +699,7 @@ public class InstanceIdentifier implements Path Item of(final Class type) { + public static @NonNull Item of(final Class type) { return new Item<>(type); } @@ -691,7 +714,7 @@ public class InstanceIdentifier implements Path & DataObject, T extends ChildOf> Item of( + public static & DataObject, T extends ChildOf> @NonNull Item of( final Class caseType, final Class type) { return new CaseItem<>(caseType, type); } @@ -712,7 +735,8 @@ public class InstanceIdentifier implements Path & DataObject, T extends Identifier> extends AbstractPathArgument { private static final long serialVersionUID = 1L; - private final T key; + + private final @NonNull T key; IdentifiableItem(final Class type, final T key) { super(type); @@ -729,8 +753,8 @@ public class InstanceIdentifier implements Path & DataObject, I extends Identifier> IdentifiableItem of( - final Class type, final I key) { + public static & DataObject, I extends Identifier> + @NonNull IdentifiableItem of(final Class type, final I key) { return new IdentifiableItem<>(type, key); } @@ -747,8 +771,8 @@ public class InstanceIdentifier implements Path & DataObject, T extends ChildOf & Identifiable, - I extends Identifier> IdentifiableItem of(final Class caseType, final Class type, - final I key) { + I extends Identifier> @NonNull IdentifiableItem of(final Class caseType, + final Class type, final I key) { return new CaseIdentifiableItem<>(caseType, type, key); } @@ -758,7 +782,7 @@ public class InstanceIdentifier implements Path implements Path extends Builder> { + public interface InstanceIdentifierBuilder { /** - * 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 *

          *     InstanceIdentifier.builder().child(Nodes.class).build();
          * 
* + *

* 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 * @@ -822,14 +844,12 @@ public class InstanceIdentifier implements Path> InstanceIdentifierBuilder child(Class container); + > @NonNull InstanceIdentifierBuilder child(Class 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 @@ -838,14 +858,13 @@ public class InstanceIdentifier implements Path & DataObject, N extends ChildOf> InstanceIdentifierBuilder child( - Class caze, Class container); + & DataObject, N extends ChildOf> + @NonNull InstanceIdentifierBuilder child(Class caze, Class 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 @@ -854,14 +873,13 @@ public class InstanceIdentifier implements Path & ChildOf, K extends Identifier> InstanceIdentifierBuilder child( - Class listItem, K listKey); + & ChildOf, K extends Identifier> + @NonNull InstanceIdentifierBuilder 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 @@ -873,8 +891,8 @@ public class InstanceIdentifier implements Path & DataObject, K extends Identifier, - N extends Identifiable & ChildOf> InstanceIdentifierBuilder child(Class caze, - Class listItem, K listKey); + N extends Identifiable & ChildOf> @NonNull InstanceIdentifierBuilder child( + Class caze, Class listItem, K listKey); /** * Build an identifier which refers to a specific augmentation of the current InstanceIdentifier referenced by @@ -885,15 +903,15 @@ public class InstanceIdentifier implements Path> InstanceIdentifierBuilder augmentation(Class container); + > @NonNull InstanceIdentifierBuilder augmentation( + Class container); /** * Build the instance identifier. * * @return Resulting instance identifier. */ - @Override - InstanceIdentifier build(); + @NonNull InstanceIdentifier build(); } private Object writeReplace() throws ObjectStreamException {