2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.binding;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Verify.verify;
12 import static com.google.common.base.Verify.verifyNotNull;
13 import static java.util.Objects.requireNonNull;
15 import com.google.common.base.MoreObjects;
16 import com.google.common.base.MoreObjects.ToStringHelper;
17 import com.google.common.base.VerifyException;
18 import com.google.common.collect.ImmutableCollection;
19 import com.google.common.collect.ImmutableList;
20 import com.google.common.collect.Iterables;
21 import java.io.ObjectStreamException;
22 import java.io.Serializable;
23 import java.util.Collections;
24 import java.util.Iterator;
25 import java.util.Objects;
26 import java.util.Optional;
27 import org.eclipse.jdt.annotation.NonNull;
28 import org.eclipse.jdt.annotation.Nullable;
29 import org.opendaylight.yangtools.concepts.HierarchicalIdentifier;
30 import org.opendaylight.yangtools.util.HashCodeBuilder;
33 * This instance identifier uniquely identifies a specific DataObject in the data tree modeled by YANG.
36 * For Example let's say you were trying to refer to a node in inventory which was modeled in YANG as follows,
40 * module opendaylight-inventory {
46 * ext:context-instance "node-context";
56 * You can create an instance identifier as follows to get to a node with id "openflow:1": {@code
57 * InstanceIdentifierBuilder.builder(Nodes.class).child(Node.class, new NodeKey(new NodeId("openflow:1")).build();
61 * This would be the same as using a path like so, "/nodes/node/openflow:1" to refer to the openflow:1 node
63 public class InstanceIdentifier<T extends DataObject>
64 implements HierarchicalIdentifier<InstanceIdentifier<? extends DataObject>> {
65 private static final long serialVersionUID = 3L;
68 * Protected to differentiate internal and external access. Internal access is required never to modify
69 * the contents. References passed to outside entities have to be wrapped in an unmodifiable view.
71 final Iterable<PathArgument> pathArguments;
73 private final @NonNull Class<T> targetType;
74 private final boolean wildcarded;
75 private final int hash;
77 InstanceIdentifier(final Class<T> type, final Iterable<PathArgument> pathArguments, final boolean wildcarded,
79 this.pathArguments = requireNonNull(pathArguments);
80 this.targetType = requireNonNull(type);
81 this.wildcarded = wildcarded;
86 * Return the type of data which this InstanceIdentifier identifies.
90 public final @NonNull Class<T> getTargetType() {
95 * Perform a safe target type adaptation of this instance identifier to target type. This method is useful when
96 * dealing with type-squashed instances.
98 * @return Path argument with target type
99 * @throws VerifyException if this instance identifier cannot be adapted to target type
100 * @throws NullPointerException if {@code target} is null
102 @SuppressWarnings("unchecked")
103 public final <N extends DataObject> @NonNull InstanceIdentifier<N> verifyTarget(final Class<@NonNull N> target) {
104 verify(target.equals(targetType), "Cannot adapt %s to %s", this, target);
105 return (InstanceIdentifier<N>) this;
109 * Return the path argument chain which makes up this instance identifier.
111 * @return Path argument chain. Immutable and does not contain nulls.
113 public final @NonNull Iterable<PathArgument> getPathArguments() {
114 return Iterables.unmodifiableIterable(pathArguments);
118 * Check whether an instance identifier contains any wildcards. A wildcard is an path argument which has a null key.
120 * @return true if any of the path arguments has a null key.
122 public final boolean isWildcarded() {
127 public final int hashCode() {
132 public final boolean equals(final Object obj) {
139 if (getClass() != obj.getClass()) {
143 final InstanceIdentifier<?> other = (InstanceIdentifier<?>) obj;
144 if (pathArguments == other.pathArguments) {
149 * We could now just go and compare the pathArguments, but that
150 * can be potentially expensive. Let's try to avoid that by
151 * checking various things that we have cached from pathArguments
152 * and trying to prove the identifiers are *not* equal.
154 if (hash != other.hash) {
157 if (wildcarded != other.wildcarded) {
160 if (targetType != other.targetType) {
163 if (fastNonEqual(other)) {
167 // Everything checks out so far, so we have to do a full equals
168 return Iterables.elementsEqual(pathArguments, other.pathArguments);
172 * Perform class-specific fast checks for non-equality. This allows subclasses to avoid iterating over the
173 * pathArguments by performing quick checks on their specific fields.
175 * @param other The other identifier, guaranteed to be the same class
176 * @return true if the other identifier cannot be equal to this one.
178 protected boolean fastNonEqual(final InstanceIdentifier<?> other) {
183 public final String toString() {
184 return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
188 * Add class-specific toString attributes.
190 * @param toStringHelper ToStringHelper instance
191 * @return ToStringHelper instance which was passed in
193 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
194 return toStringHelper.add("targetType", targetType).add("path", Iterables.toString(pathArguments));
198 * Return an instance identifier trimmed at the first occurrence of a specific component type.
201 * For example let's say an instance identifier was built like so,
203 * identifier = InstanceIdentifierBuilder.builder(Nodes.class).child(Node.class,
204 * new NodeKey(new NodeId("openflow:1")).build();
208 * And you wanted to obtain the Instance identifier which represented Nodes you would do it like so,
212 * identifier.firstIdentifierOf(Nodes.class)
215 * @param type component type
216 * @return trimmed instance identifier, or null if the component type
219 public final <I extends DataObject> @Nullable InstanceIdentifier<I> firstIdentifierOf(
220 final Class<@NonNull I> type) {
222 for (final PathArgument a : pathArguments) {
223 if (type.equals(a.getType())) {
224 @SuppressWarnings("unchecked")
225 final InstanceIdentifier<I> ret = (InstanceIdentifier<I>) internalCreate(
226 Iterables.limit(pathArguments, count));
237 * Return the key associated with the first component of specified type in
240 * @param listItem component type
241 * @return key associated with the component, or null if the component type
244 public final <N extends Identifiable<K> & DataObject, K extends Identifier<N>> @Nullable K firstKeyOf(
245 final Class<@NonNull N> listItem) {
246 for (final PathArgument i : pathArguments) {
247 if (listItem.equals(i.getType())) {
248 @SuppressWarnings("unchecked")
249 final K ret = ((IdentifiableItem<N, K>)i).getKey();
258 * Check whether an identifier is contained in this identifier. This is a strict subtree check, which requires all
259 * PathArguments to match exactly.
262 * The contains method checks if the other identifier is fully contained within the current identifier. It does this
263 * by looking at only the types of the path arguments and not by comparing the path arguments themselves.
266 * To illustrate here is an example which explains the working of this API. Let's say you have two instance
267 * identifiers as follows:
269 * this = /nodes/node/openflow:1
270 * other = /nodes/node/openflow:2
272 * then this.contains(other) will return false.
274 * @param other Potentially-container instance identifier
275 * @return True if the specified identifier is contained in this identifier.
278 public final boolean contains(final InstanceIdentifier<? extends DataObject> other) {
279 requireNonNull(other, "other should not be null");
281 final Iterator<?> lit = pathArguments.iterator();
282 final Iterator<?> oit = other.pathArguments.iterator();
284 while (lit.hasNext()) {
285 if (!oit.hasNext()) {
289 if (!lit.next().equals(oit.next())) {
298 * Check whether this instance identifier contains the other identifier after wildcard expansion. This is similar
299 * to {@link #contains(InstanceIdentifier)}, with the exception that a wildcards are assumed to match the their
300 * non-wildcarded PathArgument counterpart.
302 * @param other Identifier which should be checked for inclusion.
303 * @return true if this identifier contains the other object
305 public final boolean containsWildcarded(final InstanceIdentifier<?> other) {
306 requireNonNull(other, "other should not be null");
308 final Iterator<PathArgument> lit = pathArguments.iterator();
309 final Iterator<PathArgument> oit = other.pathArguments.iterator();
311 while (lit.hasNext()) {
312 if (!oit.hasNext()) {
316 final PathArgument la = lit.next();
317 final PathArgument oa = oit.next();
319 if (!la.getType().equals(oa.getType())) {
322 if (la instanceof IdentifiableItem<?, ?> && oa instanceof IdentifiableItem<?, ?> && !la.equals(oa)) {
330 private <N extends DataObject> @NonNull InstanceIdentifier<N> childIdentifier(final AbstractPathArgument<N> arg) {
331 return trustedCreate(arg, Iterables.concat(pathArguments, Collections.singleton(arg)),
332 HashCodeBuilder.nextHashCode(hash, arg), isWildcarded());
336 * Create an InstanceIdentifier for a child container. This method is a more efficient equivalent to
337 * {@code builder().child(container).build()}.
339 * @param container Container to append
340 * @param <N> Container type
341 * @return An InstanceIdentifier.
342 * @throws NullPointerException if {@code container} is null
344 public final <N extends ChildOf<? super T>> @NonNull InstanceIdentifier<N> child(
345 final Class<@NonNull N> container) {
346 return childIdentifier(Item.of(container));
350 * Create an InstanceIdentifier for a child list item. This method is a more efficient equivalent to
351 * {@code builder().child(listItem, listKey).build()}.
353 * @param listItem List to append
354 * @param listKey List key
355 * @param <N> List type
356 * @param <K> Key type
357 * @return An InstanceIdentifier.
358 * @throws NullPointerException if any argument is null
360 @SuppressWarnings("unchecked")
361 public final <N extends Identifiable<K> & ChildOf<? super T>, K extends Identifier<N>>
362 @NonNull KeyedInstanceIdentifier<N, K> child(final Class<@NonNull N> listItem, final K listKey) {
363 return (KeyedInstanceIdentifier<N, K>) childIdentifier(IdentifiableItem.of(listItem, listKey));
367 * Create an InstanceIdentifier for a child container. This method is a more efficient equivalent to
368 * {@code builder().child(caze, container).build()}.
370 * @param caze Choice case class
371 * @param container Container to append
372 * @param <C> Case type
373 * @param <N> Container type
374 * @return An InstanceIdentifier.
375 * @throws NullPointerException if any argument is null
377 // FIXME: add a proper caller
378 public final <C extends ChoiceIn<? super T> & DataObject, N extends ChildOf<? super C>>
379 @NonNull InstanceIdentifier<N> child(final Class<@NonNull C> caze, final Class<@NonNull N> container) {
380 return childIdentifier(Item.of(caze, container));
384 * Create an InstanceIdentifier for a child list item. This method is a more efficient equivalent to
385 * {@code builder().child(caze, listItem, listKey).build()}.
387 * @param caze Choice case class
388 * @param listItem List to append
389 * @param listKey List key
390 * @param <C> Case type
391 * @param <N> List type
392 * @param <K> Key type
393 * @return An InstanceIdentifier.
394 * @throws NullPointerException if any argument is null
396 // FIXME: add a proper caller
397 @SuppressWarnings("unchecked")
398 public final <C extends ChoiceIn<? super T> & DataObject, K extends Identifier<N>,
399 N extends Identifiable<K> & ChildOf<? super C>> @NonNull KeyedInstanceIdentifier<N, K> child(
400 final Class<@NonNull C> caze, final Class<@NonNull N> listItem, final K listKey) {
401 return (KeyedInstanceIdentifier<N, K>) childIdentifier(IdentifiableItem.of(caze, listItem, listKey));
405 * Create an InstanceIdentifier for a child augmentation. This method is a more efficient equivalent to
406 * {@code builder().augmentation(container).build()}.
408 * @param container Container to append
409 * @param <N> Container type
410 * @return An InstanceIdentifier.
411 * @throws NullPointerException if {@code container} is null
413 public final <N extends DataObject & Augmentation<? super T>> @NonNull InstanceIdentifier<N> augmentation(
414 final Class<@NonNull N> container) {
415 return childIdentifier(Item.of(container));
419 * Create a builder rooted at this key.
421 * @return A builder instance
423 // FIXME: rename this method to 'toBuilder()'
424 public @NonNull InstanceIdentifierBuilder<T> builder() {
425 return new InstanceIdentifierBuilderImpl<>(Item.of(targetType), pathArguments, hash, isWildcarded());
429 * Create an InstanceIdentifierBuilder for a specific type of InstanceIdentifier as specified by container.
431 * @param container Base container
432 * @param <T> Type of the container
433 * @return A new {@link InstanceIdentifierBuilder}
434 * @throws NullPointerException if {@code container} is null
436 public static <T extends ChildOf<? extends DataRoot>> @NonNull InstanceIdentifierBuilder<T> builder(
437 final Class<T> container) {
438 return new InstanceIdentifierBuilderImpl<T>().addWildNode(Item.of(container));
442 * Create an InstanceIdentifierBuilder for a specific type of InstanceIdentifier as specified by container in
443 * a {@code grouping} used in the {@code case} statement.
445 * @param caze Choice case class
446 * @param container Base container
447 * @param <C> Case type
448 * @param <T> Type of the container
449 * @return A new {@link InstanceIdentifierBuilder}
450 * @throws NullPointerException if any argument is null
452 public static <C extends ChoiceIn<? extends DataRoot> & DataObject, T extends ChildOf<? super C>>
453 @NonNull InstanceIdentifierBuilder<T> builder(final Class<C> caze, final Class<T> container) {
454 return new InstanceIdentifierBuilderImpl<T>().addWildNode(Item.of(caze, container));
458 * Create an InstanceIdentifierBuilder for a specific type of InstanceIdentifier which represents an
459 * {@link IdentifiableItem}.
461 * @param listItem list item class
462 * @param listKey key value
463 * @param <N> List type
464 * @param <K> List key
465 * @return A new {@link InstanceIdentifierBuilder}
466 * @throws NullPointerException if any argument is null
468 public static <N extends Identifiable<K> & ChildOf<? extends DataRoot>,
469 K extends Identifier<N>> @NonNull InstanceIdentifierBuilder<N> builder(final Class<N> listItem,
471 return new InstanceIdentifierBuilderImpl<N>().addNode(IdentifiableItem.of(listItem, listKey));
475 * Create an InstanceIdentifierBuilder for a specific type of InstanceIdentifier which represents an
476 * {@link IdentifiableItem} in a {@code grouping} used in the {@code case} statement.
478 * @param caze Choice case class
479 * @param listItem list item class
480 * @param listKey key value
481 * @param <C> Case type
482 * @param <N> List type
483 * @param <K> List key
484 * @return A new {@link InstanceIdentifierBuilder}
485 * @throws NullPointerException if any argument is null
487 public static <C extends ChoiceIn<? extends DataRoot> & DataObject,
488 N extends Identifiable<K> & ChildOf<? super C>, K extends Identifier<N>>
489 @NonNull InstanceIdentifierBuilder<N> builder(final Class<C> caze, final Class<N> listItem,
491 return new InstanceIdentifierBuilderImpl<N>().addNode(IdentifiableItem.of(caze, listItem, listKey));
494 public static <R extends DataRoot & DataObject, T extends ChildOf<? super R>>
495 @NonNull InstanceIdentifierBuilder<T> builderOfInherited(final Class<R> root, final Class<T> container) {
496 // FIXME: we are losing root identity, hence namespaces may not work correctly
497 return new InstanceIdentifierBuilderImpl<T>().addWildNode(Item.of(container));
500 public static <R extends DataRoot & DataObject, C extends ChoiceIn<? super R> & DataObject,
501 T extends ChildOf<? super C>>
502 @NonNull InstanceIdentifierBuilder<T> builderOfInherited(final Class<R> root,
503 final Class<C> caze, final Class<T> container) {
504 // FIXME: we are losing root identity, hence namespaces may not work correctly
505 return new InstanceIdentifierBuilderImpl<T>().addWildNode(Item.of(caze, container));
508 public static <R extends DataRoot & DataObject, N extends Identifiable<K> & ChildOf<? super R>,
509 K extends Identifier<N>>
510 @NonNull InstanceIdentifierBuilder<N> builderOfInherited(final Class<R> root,
511 final Class<N> listItem, final K listKey) {
512 // FIXME: we are losing root identity, hence namespaces may not work correctly
513 return new InstanceIdentifierBuilderImpl<N>().addNode(IdentifiableItem.of(listItem, listKey));
516 public static <R extends DataRoot & DataObject, C extends ChoiceIn<? super R> & DataObject,
517 N extends Identifiable<K> & ChildOf<? super C>, K extends Identifier<N>>
518 @NonNull InstanceIdentifierBuilder<N> builderOfInherited(final Class<R> root,
519 final Class<C> caze, final Class<N> listItem, final K listKey) {
520 // FIXME: we are losing root identity, hence namespaces may not work correctly
521 return new InstanceIdentifierBuilderImpl<N>().addNode(IdentifiableItem.of(caze, listItem, listKey));
525 * Create an instance identifier for a very specific object type. This method implements {@link #create(Iterable)}
526 * semantics, except it is used by internal callers, which have assured that the argument is an immutable Iterable.
528 * @param pathArguments The path to a specific node in the data tree
529 * @return InstanceIdentifier instance
530 * @throws IllegalArgumentException if pathArguments is empty or contains a null element.
531 * @throws NullPointerException if {@code pathArguments} is null
533 private static @NonNull InstanceIdentifier<?> internalCreate(final Iterable<PathArgument> pathArguments) {
534 final var it = requireNonNull(pathArguments, "pathArguments may not be null").iterator();
535 checkArgument(it.hasNext(), "pathArguments may not be empty");
537 final HashCodeBuilder<PathArgument> hashBuilder = new HashCodeBuilder<>();
538 boolean wildcard = false;
543 // Non-null is implied by our callers
544 final var type = verifyNotNull(arg).getType();
545 checkArgument(ChildOf.class.isAssignableFrom(type) || Augmentation.class.isAssignableFrom(type),
546 "%s is not a valid path argument", type);
548 hashBuilder.addArgument(arg);
550 if (Identifiable.class.isAssignableFrom(type) && !(arg instanceof IdentifiableItem)) {
553 } while (it.hasNext());
555 return trustedCreate(arg, pathArguments, hashBuilder.build(), wildcard);
559 * Create an instance identifier for a very specific object type.
564 * List<PathArgument> path = Arrays.asList(new Item(Nodes.class))
565 * new InstanceIdentifier(path);
568 * @param pathArguments The path to a specific node in the data tree
569 * @return InstanceIdentifier instance
570 * @throws IllegalArgumentException if pathArguments is empty or
571 * contains a null element.
573 // FIXME: rename to 'unsafeOf()'
574 public static @NonNull InstanceIdentifier<?> create(final Iterable<? extends PathArgument> pathArguments) {
575 if (pathArguments instanceof ImmutableCollection) {
576 @SuppressWarnings("unchecked")
577 final var immutableArguments = (ImmutableCollection<PathArgument>) pathArguments;
578 return internalCreate(immutableArguments);
581 return internalCreate(ImmutableList.copyOf(pathArguments));
585 * Create an instance identifier for a very specific object type.
590 * new InstanceIdentifier(Nodes.class)
592 * would create an InstanceIdentifier for an object of type Nodes
594 * @param type The type of the object which this instance identifier represents
595 * @return InstanceIdentifier instance
597 // FIXME: considering removing in favor of always going through a builder
598 @SuppressWarnings("unchecked")
599 public static <T extends ChildOf<? extends DataRoot>> @NonNull InstanceIdentifier<T> create(
600 final Class<@NonNull T> type) {
601 return (InstanceIdentifier<T>) internalCreate(ImmutableList.of(Item.of(type)));
605 * Return the key associated with the last component of the specified identifier.
607 * @param id instance identifier
608 * @return key associated with the last component
609 * @throws IllegalArgumentException if the supplied identifier type cannot have a key.
610 * @throws NullPointerException if id is null.
612 // FIXME: reconsider naming and design of this method
613 public static <N extends Identifiable<K> & DataObject, K extends Identifier<N>> K keyOf(
614 final InstanceIdentifier<N> id) {
616 checkArgument(id instanceof KeyedInstanceIdentifier, "%s does not have a key", id);
618 @SuppressWarnings("unchecked")
619 final K ret = ((KeyedInstanceIdentifier<N, K>)id).getKey();
623 @SuppressWarnings({ "unchecked", "rawtypes" })
624 static <N extends DataObject> @NonNull InstanceIdentifier<N> trustedCreate(final PathArgument arg,
625 final Iterable<PathArgument> pathArguments, final int hash, boolean wildcarded) {
626 if (Identifiable.class.isAssignableFrom(arg.getType()) && !wildcarded) {
627 Identifier<?> key = null;
628 if (arg instanceof IdentifiableItem) {
629 key = ((IdentifiableItem<?, ?>)arg).getKey();
634 return new KeyedInstanceIdentifier(arg.getType(), pathArguments, wildcarded, hash, key);
637 return new InstanceIdentifier(arg.getType(), pathArguments, wildcarded, hash);
641 * Path argument of {@link InstanceIdentifier}. Interface which implementations are used as path components of the
642 * path in overall data tree.
644 public interface PathArgument extends Comparable<PathArgument> {
646 * Return the data object type backing this PathArgument.
648 * @return Data object type.
650 @NonNull Class<? extends DataObject> getType();
653 * Return an optional enclosing case type. This is used only when {@link #getType()} references a node defined
654 * in a {@code grouping} which is reference inside a {@code case} statement in order to safely reference the
657 * @return Optional case class.
659 default Optional<? extends Class<? extends DataObject>> getCaseType() {
660 return Optional.empty();
664 private abstract static class AbstractPathArgument<T extends DataObject> implements PathArgument, Serializable {
665 private static final long serialVersionUID = 1L;
667 private final @NonNull Class<T> type;
669 AbstractPathArgument(final Class<T> type) {
670 this.type = requireNonNull(type, "Type may not be null.");
674 public final Class<T> getType() {
683 public final int hashCode() {
684 return Objects.hash(type, getCaseType(), getKey());
688 public final boolean equals(final Object obj) {
692 if (!(obj instanceof AbstractPathArgument)) {
695 final AbstractPathArgument<?> other = (AbstractPathArgument<?>) obj;
696 return type.equals(other.type) && Objects.equals(getKey(), other.getKey())
697 && getCaseType().equals(other.getCaseType());
701 public final int compareTo(final PathArgument arg) {
702 final int cmp = compareClasses(type, arg.getType());
706 final Optional<? extends Class<?>> caseType = getCaseType();
707 if (!caseType.isPresent()) {
708 return arg.getCaseType().isPresent() ? -1 : 1;
710 final Optional<? extends Class<?>> argCaseType = getCaseType();
711 return argCaseType.isPresent() ? compareClasses(caseType.get(), argCaseType.get()) : 1;
714 private static int compareClasses(final Class<?> first, final Class<?> second) {
715 return first.getCanonicalName().compareTo(second.getCanonicalName());
720 * An Item represents an object that probably is only one of it's kind. For example a Nodes object is only one of
721 * a kind. In YANG terms this would probably represent a container.
723 * @param <T> Item type
725 public static class Item<T extends DataObject> extends AbstractPathArgument<T> {
726 private static final long serialVersionUID = 1L;
728 Item(final Class<T> type) {
733 * Return a PathArgument instance backed by the specified class.
735 * @param type Backing class
736 * @param <T> Item type
737 * @return A new PathArgument
738 * @throws NullPointerException if {@code} is null.
740 public static <T extends DataObject> @NonNull Item<T> of(final Class<T> type) {
741 return new Item<>(type);
745 * Return a PathArgument instance backed by the specified class, which in turn is defined in a {@code grouping}
746 * used in a corresponding {@code case} statement.
748 * @param caseType defining case class
749 * @param type Backing class
750 * @param <C> Case type
751 * @param <T> Item type
752 * @return A new PathArgument
753 * @throws NullPointerException if any argument is null.
755 public static <C extends ChoiceIn<?> & DataObject, T extends ChildOf<? super C>> @NonNull Item<T> of(
756 final Class<C> caseType, final Class<T> type) {
757 return new CaseItem<>(caseType, type);
761 public String toString() {
762 return getType().getName();
767 * An IdentifiableItem represents a object that is usually present in a collection and can be identified uniquely
768 * by a key. In YANG terms this would probably represent an item in a list.
770 * @param <I> An object that is identifiable by an identifier
771 * @param <T> The identifier of the object
773 public static class IdentifiableItem<I extends Identifiable<T> & DataObject, T extends Identifier<I>>
774 extends AbstractPathArgument<I> {
775 private static final long serialVersionUID = 1L;
777 private final @NonNull T key;
779 IdentifiableItem(final Class<I> type, final T key) {
781 this.key = requireNonNull(key, "Key may not be null.");
785 * Return an IdentifiableItem instance backed by the specified class with specified key.
787 * @param type Backing class
789 * @param <T> List type
790 * @param <I> Key type
791 * @return An IdentifiableItem
792 * @throws NullPointerException if any argument is null.
794 public static <T extends Identifiable<I> & DataObject, I extends Identifier<T>>
795 @NonNull IdentifiableItem<T, I> of(final Class<T> type, final I key) {
796 return new IdentifiableItem<>(type, key);
800 * Return an IdentifiableItem instance backed by the specified class with specified key. The class is in turn
801 * defined in a {@code grouping} used in a corresponding {@code case} statement.
803 * @param caseType defining case class
804 * @param type Backing class
805 * @param <C> Case type
806 * @param <T> List type
807 * @param <I> Key type
808 * @return A new PathArgument
809 * @throws NullPointerException if any argument is null.
811 public static <C extends ChoiceIn<?> & DataObject, T extends ChildOf<? super C> & Identifiable<I>,
812 I extends Identifier<T>> @NonNull IdentifiableItem<T, I> of(final Class<C> caseType,
813 final Class<T> type, final I key) {
814 return new CaseIdentifiableItem<>(caseType, type, key);
818 * Return the data object type backing this PathArgument.
820 * @return Data object type.
823 public final @NonNull T getKey() {
828 public String toString() {
829 return getType().getName() + "[key=" + key + "]";
833 private static final class CaseItem<C extends ChoiceIn<?> & DataObject, T extends ChildOf<? super C>>
835 private static final long serialVersionUID = 1L;
837 private final Class<C> caseType;
839 CaseItem(final Class<C> caseType, final Class<T> type) {
841 this.caseType = requireNonNull(caseType);
845 public Optional<Class<C>> getCaseType() {
846 return Optional.of(caseType);
850 private static final class CaseIdentifiableItem<C extends ChoiceIn<?> & DataObject,
851 T extends ChildOf<? super C> & Identifiable<K>, K extends Identifier<T>> extends IdentifiableItem<T, K> {
852 private static final long serialVersionUID = 1L;
854 private final Class<C> caseType;
856 CaseIdentifiableItem(final Class<C> caseType, final Class<T> type, final K key) {
858 this.caseType = requireNonNull(caseType);
862 public Optional<Class<C>> getCaseType() {
863 return Optional.of(caseType);
867 // FIXME: rename to 'Builder'
868 // FIXME: introduce KeyedBuilder with specialized build() method
869 public interface InstanceIdentifierBuilder<T extends DataObject> {
871 * Append the specified container as a child of the current InstanceIdentifier referenced by the builder. This
872 * method should be used when you want to build an instance identifier by appending top-level elements, for
875 * InstanceIdentifier.builder().child(Nodes.class).build();
879 * NOTE :- The above example is only for illustration purposes InstanceIdentifier.builder() has been deprecated
880 * and should not be used. Use InstanceIdentifier.builder(Nodes.class) instead
882 * @param container Container to append
883 * @param <N> Container type
884 * @return this builder
885 * @throws NullPointerException if {@code container} is null
887 <N extends ChildOf<? super T>> @NonNull InstanceIdentifierBuilder<N> child(Class<N> container);
890 * Append the specified container as a child of the current InstanceIdentifier referenced by the builder. This
891 * method should be used when you want to build an instance identifier by appending a container node to the
892 * identifier and the {@code container} is defined in a {@code grouping} used in a {@code case} statement.
894 * @param caze Choice case class
895 * @param container Container to append
896 * @param <C> Case type
897 * @param <N> Container type
898 * @return this builder
899 * @throws NullPointerException if {@code container} is null
901 <C extends ChoiceIn<? super T> & DataObject, N extends ChildOf<? super C>>
902 @NonNull InstanceIdentifierBuilder<N> child(Class<C> caze, Class<N> container);
905 * Append the specified listItem as a child of the current InstanceIdentifier referenced by the builder. This
906 * method should be used when you want to build an instance identifier by appending a specific list element to
909 * @param listItem List to append
910 * @param listKey List key
911 * @param <N> List type
912 * @param <K> Key type
913 * @return this builder
914 * @throws NullPointerException if any argument is null
916 <N extends Identifiable<K> & ChildOf<? super T>, K extends Identifier<N>>
917 @NonNull InstanceIdentifierBuilder<N> child(Class<@NonNull N> listItem, K listKey);
920 * Append the specified listItem as a child of the current InstanceIdentifier referenced by the builder. This
921 * method should be used when you want to build an instance identifier by appending a specific list element to
922 * the identifier and the {@code list} is defined in a {@code grouping} used in a {@code case} statement.
924 * @param caze Choice case class
925 * @param listItem List to append
926 * @param listKey List key
927 * @param <C> Case type
928 * @param <N> List type
929 * @param <K> Key type
930 * @return this builder
931 * @throws NullPointerException if any argument is null
933 <C extends ChoiceIn<? super T> & DataObject, K extends Identifier<N>,
934 N extends Identifiable<K> & ChildOf<? super C>> @NonNull InstanceIdentifierBuilder<N> child(
935 Class<C> caze, Class<N> listItem, K listKey);
938 * Build an identifier which refers to a specific augmentation of the current InstanceIdentifier referenced by
941 * @param container augmentation class
942 * @param <N> augmentation type
943 * @return this builder
944 * @throws NullPointerException if {@code container} is null
946 <N extends DataObject & Augmentation<? super T>> @NonNull InstanceIdentifierBuilder<N> augmentation(
950 * Build the instance identifier.
952 * @return Resulting instance identifier.
954 @NonNull InstanceIdentifier<T> build();
957 private Object writeReplace() throws ObjectStreamException {
958 return new InstanceIdentifierV3<>(this);