2 * Copyright (c) 2014 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.data.api;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Verify.verify;
12 import static java.util.Objects.requireNonNull;
14 import com.google.common.annotations.Beta;
15 import com.google.common.cache.CacheBuilder;
16 import com.google.common.cache.CacheLoader;
17 import com.google.common.cache.LoadingCache;
18 import com.google.common.collect.ImmutableList;
19 import com.google.common.collect.ImmutableMap;
20 import com.google.common.collect.ImmutableSet;
21 import com.google.common.collect.Iterables;
22 import com.google.common.collect.Sets;
23 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
24 import java.io.Serializable;
25 import java.lang.reflect.Array;
26 import java.util.AbstractMap.SimpleImmutableEntry;
27 import java.util.ArrayList;
28 import java.util.Arrays;
29 import java.util.Collection;
30 import java.util.Deque;
31 import java.util.Iterator;
32 import java.util.List;
34 import java.util.Map.Entry;
35 import java.util.Objects;
36 import java.util.Optional;
38 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
39 import java.util.function.Function;
40 import org.eclipse.jdt.annotation.NonNull;
41 import org.eclipse.jdt.annotation.Nullable;
42 import org.opendaylight.yangtools.concepts.Builder;
43 import org.opendaylight.yangtools.concepts.Immutable;
44 import org.opendaylight.yangtools.concepts.Path;
45 import org.opendaylight.yangtools.util.HashCodeBuilder;
46 import org.opendaylight.yangtools.util.ImmutableOffsetMap;
47 import org.opendaylight.yangtools.util.SharedSingletonMap;
48 import org.opendaylight.yangtools.util.SingletonSet;
49 import org.opendaylight.yangtools.yang.common.QName;
50 import org.opendaylight.yangtools.yang.common.QNameModule;
51 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
54 * Unique identifier of a particular node instance in the data tree.
57 * Java representation of YANG Built-in type <code>instance-identifier</code>,
58 * which conceptually is XPath expression minimized to uniquely identify element
59 * in data tree which conforms to constraints maintained by YANG Model,
60 * effectively this makes Instance Identifier a path to element in data tree.
63 * Constraints put in YANG specification on instance-identifier allowed it to be
64 * effectively represented in Java and it's evaluation does not require
65 * full-blown XPath processor.
68 * <h3>Path Arguments</h3>
69 * Path to the node represented in instance identifier consists of
70 * {@link PathArgument} which carries necessary information to uniquely identify
71 * node on particular level in the subtree.
74 * <li>{@link NodeIdentifier} - Identifier of node, which has cardinality
75 * <code>0..1</code> in particular subtree in data tree.</li>
76 * <li>{@link NodeIdentifierWithPredicates} - Identifier of node (list item),
77 * which has cardinality <code>0..n</code>.</li>
78 * <li>{@link NodeWithValue} - Identifier of instance <code>leaf</code> node or
79 * <code>leaf-list</code> node.</li>
80 * <li>{@link AugmentationIdentifier} - Identifier of instance of
81 * <code>augmentation</code> node.</li>
84 * @see <a href="http://tools.ietf.org/html/rfc6020#section-9.13">RFC6020</a>
86 // FIXME: 4.0.0: this concept needs to be moved to yang-common, as parser components need the ability to refer
87 // to data nodes -- most notably XPath expressions and {@code default} statement arguments need to be able
88 // to represent these.
89 // FIXME: FixedYangInstanceIdentifier needs YangInstanceIdentifier initialized, but that includes initializing
90 // this field. Figure out a way out of this pickle.
91 @SuppressFBWarnings("IC_SUPERCLASS_USES_SUBCLASS_DURING_INITIALIZATION")
92 public abstract class YangInstanceIdentifier implements Path<YangInstanceIdentifier>, Immutable, Serializable {
94 * An empty {@link YangInstanceIdentifier}. It corresponds to the path of the conceptual root of the YANG namespace.
96 public static final @NonNull YangInstanceIdentifier EMPTY = FixedYangInstanceIdentifier.EMPTY_INSTANCE;
98 private static final AtomicReferenceFieldUpdater<YangInstanceIdentifier, String> TOSTRINGCACHE_UPDATER =
99 AtomicReferenceFieldUpdater.newUpdater(YangInstanceIdentifier.class, String.class, "toStringCache");
100 private static final long serialVersionUID = 4L;
102 private final int hash;
103 private transient volatile String toStringCache = null;
105 // Package-private to prevent outside subclassing
106 YangInstanceIdentifier(final int hash) {
110 abstract @NonNull YangInstanceIdentifier createRelativeIdentifier(int skipFromRoot);
112 abstract @Nullable Collection<PathArgument> tryPathArguments();
114 abstract @Nullable Collection<PathArgument> tryReversePathArguments();
117 * Check if this instance identifier has empty path arguments, e.g. it is
118 * empty and corresponds to {@link #EMPTY}.
120 * @return True if this instance identifier is empty, false otherwise.
122 public abstract boolean isEmpty();
125 * Return an optimized version of this identifier, useful when the identifier
126 * will be used very frequently.
128 * @return A optimized equivalent instance.
131 public abstract @NonNull YangInstanceIdentifier toOptimized();
134 * Return the conceptual parent {@link YangInstanceIdentifier}, which has
135 * one item less in {@link #getPathArguments()}.
137 * @return Parent {@link YangInstanceIdentifier}, or null if this object is {@link #EMPTY}.
139 public abstract @Nullable YangInstanceIdentifier getParent();
142 * Return the ancestor {@link YangInstanceIdentifier} with a particular depth, e.g. number of path arguments.
144 * @param depth Ancestor depth
145 * @return Ancestor {@link YangInstanceIdentifier}
146 * @throws IllegalArgumentException if the specified depth is negative or is greater than the depth of this object.
148 public abstract @NonNull YangInstanceIdentifier getAncestor(int depth);
151 * Returns an ordered iteration of path arguments.
153 * @return Immutable iteration of path arguments.
155 public abstract @NonNull List<PathArgument> getPathArguments();
158 * Returns an iterable of path arguments in reverse order. This is useful
159 * when walking up a tree organized this way.
161 * @return Immutable iterable of path arguments in reverse order.
163 public abstract @NonNull List<PathArgument> getReversePathArguments();
166 * Returns the last PathArgument. This is equivalent of iterating
167 * to the last element of the iterable returned by {@link #getPathArguments()}.
169 * @return The last past argument, or null if there are no PathArguments.
171 public abstract PathArgument getLastPathArgument();
173 public static @NonNull YangInstanceIdentifier create(final Iterable<? extends PathArgument> path) {
174 if (Iterables.isEmpty(path)) {
178 final HashCodeBuilder<PathArgument> hash = new HashCodeBuilder<>();
179 for (PathArgument a : path) {
183 return FixedYangInstanceIdentifier.create(path, hash.build());
186 public static @NonNull YangInstanceIdentifier create(final PathArgument... path) {
187 // We are forcing a copy, since we cannot trust the user
188 return create(Arrays.asList(path));
192 * Create a {@link YangInstanceIdentifier} by taking a snapshot of provided path and iterating it backwards.
194 * @param pathTowardsRoot Path towards root
195 * @return A {@link YangInstanceIdentifier} instance
196 * @throws NullPointerException if {@code pathTowardsRoot} or any of its members is null
198 public static @NonNull YangInstanceIdentifier createReverse(final Deque<PathArgument> pathTowardsRoot) {
199 final ImmutableList.Builder<PathArgument> builder = ImmutableList.builderWithExpectedSize(
200 pathTowardsRoot.size());
201 pathTowardsRoot.descendingIterator().forEachRemaining(builder::add);
202 return YangInstanceIdentifier.create(builder.build());
206 * Create a {@link YangInstanceIdentifier} by walking specified stack backwards and extracting path components
209 * @param stackTowardsRoot Stack towards root,
210 * @return A {@link YangInstanceIdentifier} instance
211 * @throws NullPointerException if {@code pathTowardsRoot} is null
213 public static <T> @NonNull YangInstanceIdentifier createReverse(final Deque<? extends T> stackTowardsRoot,
214 final Function<T, PathArgument> function) {
215 final ImmutableList.Builder<PathArgument> builder = ImmutableList.builderWithExpectedSize(
216 stackTowardsRoot.size());
217 final Iterator<? extends T> it = stackTowardsRoot.descendingIterator();
218 while (it.hasNext()) {
219 builder.add(function.apply(it.next()));
221 return YangInstanceIdentifier.create(builder.build());
224 boolean pathArgumentsEqual(final YangInstanceIdentifier other) {
225 return Iterables.elementsEqual(getPathArguments(), other.getPathArguments());
229 public boolean equals(final Object obj) {
233 if (!(obj instanceof YangInstanceIdentifier)) {
236 YangInstanceIdentifier other = (YangInstanceIdentifier) obj;
237 if (this.hashCode() != obj.hashCode()) {
241 return pathArgumentsEqual(other);
245 * Constructs a new Instance Identifier with new {@link NodeIdentifier} added to the end of path arguments.
247 * @param name QName of {@link NodeIdentifier}
248 * @return Instance Identifier with additional path argument added to the end.
250 public final @NonNull YangInstanceIdentifier node(final QName name) {
251 return node(new NodeIdentifier(name));
255 * Constructs a new Instance Identifier with new {@link PathArgument} added to the end of path arguments.
257 * @param arg Path argument which should be added to the end
258 * @return Instance Identifier with additional path argument added to the end.
260 public final @NonNull YangInstanceIdentifier node(final PathArgument arg) {
261 return new StackedYangInstanceIdentifier(this, arg, HashCodeBuilder.nextHashCode(hash, arg));
265 * Get the relative path from an ancestor. This method attempts to perform
266 * the reverse of concatenating a base (ancestor) and a path.
269 * Ancestor against which the relative path should be calculated
270 * @return This object's relative path from parent, or Optional.absent() if
271 * the specified parent is not in fact an ancestor of this object.
273 public Optional<YangInstanceIdentifier> relativeTo(final YangInstanceIdentifier ancestor) {
274 if (this == ancestor) {
275 return Optional.of(EMPTY);
277 if (ancestor.isEmpty()) {
278 return Optional.of(this);
281 final Iterator<PathArgument> lit = getPathArguments().iterator();
282 final Iterator<PathArgument> oit = ancestor.getPathArguments().iterator();
285 while (oit.hasNext()) {
286 // Ancestor is not really an ancestor
287 if (!lit.hasNext() || !lit.next().equals(oit.next())) {
288 return Optional.empty();
295 return Optional.of(this);
297 if (!lit.hasNext()) {
298 return Optional.of(EMPTY);
301 return Optional.of(createRelativeIdentifier(common));
305 public final boolean contains(final YangInstanceIdentifier other) {
310 checkArgument(other != null, "other should not be null");
311 final Iterator<PathArgument> lit = getPathArguments().iterator();
312 final Iterator<PathArgument> oit = other.getPathArguments().iterator();
314 while (lit.hasNext()) {
315 if (!oit.hasNext()) {
319 if (!lit.next().equals(oit.next())) {
328 public final String toString() {
330 * The toStringCache is safe, since the object contract requires
331 * immutability of the object and all objects referenced from this
333 * Used lists, maps are immutable. Path Arguments (elements) are also
334 * immutable, since the PathArgument contract requires immutability.
335 * The cache is thread-safe - if multiple computations occurs at the
336 * same time, cache will be overwritten with same result.
338 String ret = toStringCache;
340 final StringBuilder builder = new StringBuilder("/");
341 PathArgument prev = null;
342 for (PathArgument argument : getPathArguments()) {
346 builder.append(argument.toRelativeString(prev));
350 ret = builder.toString();
351 TOSTRINGCACHE_UPDATER.lazySet(this, ret);
357 public final int hashCode() {
359 * The caching is safe, since the object contract requires
360 * immutability of the object and all objects referenced from this
362 * Used lists, maps are immutable. Path Arguments (elements) are also
363 * immutable, since the PathArgument contract requires immutability.
368 @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
369 justification = "https://github.com/spotbugs/spotbugs/issues/811")
370 private static int hashCode(final Object value) {
375 if (byte[].class.equals(value.getClass())) {
376 return Arrays.hashCode((byte[]) value);
379 if (value.getClass().isArray()) {
381 int length = Array.getLength(value);
382 for (int i = 0; i < length; i++) {
383 hash += Objects.hashCode(Array.get(value, i));
389 return Objects.hashCode(value);
392 final Object writeReplace() {
393 return new YIDv1(this);
396 // Static factories & helpers
399 * Returns a new InstanceIdentifier with only one path argument of type {@link NodeIdentifier} with supplied
402 * @param name QName of first node identifier
403 * @return Instance Identifier with only one path argument of type {@link NodeIdentifier}
405 public static @NonNull YangInstanceIdentifier of(final QName name) {
406 return create(new NodeIdentifier(name));
410 * Returns new builder for InstanceIdentifier with empty path arguments.
412 * @return new builder for InstanceIdentifier with empty path arguments.
414 public static @NonNull InstanceIdentifierBuilder builder() {
415 return new YangInstanceIdentifierBuilder();
419 * Returns new builder for InstanceIdentifier with path arguments copied from original instance identifier.
421 * @param origin InstanceIdentifier from which path arguments are copied.
422 * @return new builder for InstanceIdentifier with path arguments copied from original instance identifier.
424 public static @NonNull InstanceIdentifierBuilder builder(final YangInstanceIdentifier origin) {
425 return new YangInstanceIdentifierBuilder(origin.getPathArguments(), origin.hashCode());
429 * Path argument / component of InstanceIdentifier.
430 * Path argument uniquely identifies node in data tree on particular
434 * This interface itself is used as common parent for actual
435 * path arguments types and should not be implemented by user code.
438 * Path arguments SHOULD contain only minimum of information
439 * required to uniquely identify node on particular subtree level.
442 * For actual path arguments types see:
444 * <li>{@link NodeIdentifier} - Identifier of container or leaf
445 * <li>{@link NodeIdentifierWithPredicates} - Identifier of list entries, which have key defined
446 * <li>{@link AugmentationIdentifier} - Identifier of augmentation
447 * <li>{@link NodeWithValue} - Identifier of leaf-list entry
450 public interface PathArgument extends Comparable<PathArgument>, Immutable, Serializable {
452 * Returns unique QName of data node as defined in YANG Schema, if available.
455 * @throws UnsupportedOperationException if node type is not applicable, for example in case of an augmentation.
457 @NonNull QName getNodeType();
460 * Return the string representation of this object for use in context
461 * provided by a previous object. This method can be implemented in
462 * terms of {@link #toString()}, but implementations are encourage to
463 * reuse any context already emitted by the previous object.
465 * @param previous Previous path argument
466 * @return String representation
468 @NonNull String toRelativeString(PathArgument previous);
471 private abstract static class AbstractPathArgument implements PathArgument {
472 private static final long serialVersionUID = -4546547994250849340L;
473 private final @NonNull QName nodeType;
474 private transient volatile int hashValue;
476 protected AbstractPathArgument(final QName nodeType) {
477 this.nodeType = requireNonNull(nodeType);
481 public final QName getNodeType() {
486 @SuppressWarnings("checkstyle:parameterName")
487 public int compareTo(final PathArgument o) {
488 return nodeType.compareTo(o.getNodeType());
491 protected int hashCodeImpl() {
492 return 31 + getNodeType().hashCode();
496 public final int hashCode() {
498 return (local = hashValue) != 0 ? local : (hashValue = hashCodeImpl());
502 public boolean equals(final Object obj) {
506 if (obj == null || this.getClass() != obj.getClass()) {
510 return getNodeType().equals(((AbstractPathArgument)obj).getNodeType());
514 public String toString() {
515 return getNodeType().toString();
519 public String toRelativeString(final PathArgument previous) {
520 if (previous instanceof AbstractPathArgument) {
521 final QNameModule mod = previous.getNodeType().getModule();
522 if (getNodeType().getModule().equals(mod)) {
523 return getNodeType().getLocalName();
527 return getNodeType().toString();
530 abstract Object writeReplace();
534 * Simple path argument identifying a {@link org.opendaylight.yangtools.yang.data.api.schema.ContainerNode} or
535 * {@link org.opendaylight.yangtools.yang.data.api.schema.LeafNode} leaf in particular subtree.
537 public static final class NodeIdentifier extends AbstractPathArgument {
538 private static final long serialVersionUID = -2255888212390871347L;
539 private static final LoadingCache<QName, NodeIdentifier> CACHE = CacheBuilder.newBuilder().weakValues()
540 .build(new CacheLoader<QName, NodeIdentifier>() {
542 public NodeIdentifier load(final QName key) {
543 return new NodeIdentifier(key);
547 public NodeIdentifier(final QName node) {
552 * Return a NodeIdentifier for a particular QName. Unlike the constructor, this factory method uses a global
553 * instance cache, resulting in object reuse for equal inputs.
555 * @param node Node's QName
556 * @return A {@link NodeIdentifier}
558 public static @NonNull NodeIdentifier create(final QName node) {
559 return CACHE.getUnchecked(node);
563 Object writeReplace() {
564 return new NIv1(this);
569 * Composite path argument identifying a {@link org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode} leaf
572 public abstract static class NodeIdentifierWithPredicates extends AbstractPathArgument {
573 private static final class Singleton extends NodeIdentifierWithPredicates {
574 private static final long serialVersionUID = 1L;
576 private final @NonNull QName key;
577 private final @NonNull Object value;
579 Singleton(final QName node, final QName key, final Object value) {
581 this.key = requireNonNull(key);
582 this.value = requireNonNull(value);
586 public SingletonSet<Entry<QName, Object>> entrySet() {
587 return SingletonSet.of(new SimpleImmutableEntry<>(key, value));
591 public SingletonSet<QName> keySet() {
592 return SingletonSet.of(key);
596 public SingletonSet<Object> values() {
597 return SingletonSet.of(value);
606 public ImmutableMap<QName, Object> asMap() {
607 return ImmutableMap.of(key, value);
611 boolean equalMapping(final NodeIdentifierWithPredicates other) {
612 final Singleton single = (Singleton) other;
613 return key.equals(single.key) && Objects.deepEquals(value, single.value);
617 Object keyValue(final QName qname) {
618 return key.equals(qname) ? value : null;
622 private static final class Regular extends NodeIdentifierWithPredicates {
623 private static final long serialVersionUID = 1L;
625 private final @NonNull Map<QName, Object> keyValues;
627 Regular(final QName node, final Map<QName, Object> keyValues) {
629 this.keyValues = requireNonNull(keyValues);
633 public Set<Entry<QName, Object>> entrySet() {
634 return keyValues.entrySet();
638 public Set<QName> keySet() {
639 return keyValues.keySet();
643 public Collection<Object> values() {
644 return keyValues.values();
649 return keyValues.size();
653 public Map<QName, Object> asMap() {
658 Object keyValue(final QName qname) {
659 return keyValues.get(qname);
663 boolean equalMapping(final NodeIdentifierWithPredicates other) {
664 final Map<QName, Object> otherKeyValues = ((Regular) other).keyValues;
665 // TODO: benchmark to see if just calling equals() on the two maps is not faster
666 if (keyValues == otherKeyValues) {
669 if (keyValues.size() != otherKeyValues.size()) {
673 for (Entry<QName, Object> entry : entrySet()) {
674 final Object otherValue = otherKeyValues.get(entry.getKey());
675 if (otherValue == null || !Objects.deepEquals(entry.getValue(), otherValue)) {
684 private static final long serialVersionUID = -4787195606494761540L;
686 NodeIdentifierWithPredicates(final QName node) {
690 public static @NonNull NodeIdentifierWithPredicates of(final QName node) {
691 return new Regular(node, ImmutableMap.of());
694 public static @NonNull NodeIdentifierWithPredicates of(final QName node, final QName key, final Object value) {
695 return new Singleton(node, key, value);
698 public static @NonNull NodeIdentifierWithPredicates of(final QName node, final Entry<QName, Object> entry) {
699 return of(node, entry.getKey(), entry.getValue());
702 public static @NonNull NodeIdentifierWithPredicates of(final QName node, final Map<QName, Object> keyValues) {
703 return keyValues.size() == 1 ? of(keyValues, node)
704 // Retains ImmutableMap for empty maps. For larger sizes uses a shared key set.
705 : new Regular(node, ImmutableOffsetMap.unorderedCopyOf(keyValues));
708 public static @NonNull NodeIdentifierWithPredicates of(final QName node,
709 final ImmutableOffsetMap<QName, Object> keyValues) {
710 return keyValues.size() == 1 ? of(keyValues, node) : new Regular(node, keyValues);
714 public static @NonNull NodeIdentifierWithPredicates of(final QName node,
715 final SharedSingletonMap<QName, Object> keyValues) {
716 return of(node, keyValues.getEntry());
719 private static @NonNull NodeIdentifierWithPredicates of(final Map<QName, Object> keyValues, final QName node) {
720 return of(node, keyValues.entrySet().iterator().next());
724 * Return the set of predicates keys and values. Keys are guaranteeed to be unique.
726 * @return Predicate set.
729 public abstract @NonNull Set<Entry<QName, Object>> entrySet();
732 * Return the predicate key in the iteration order of {@link #entrySet()}.
734 * @return Predicate values.
737 public abstract @NonNull Set<QName> keySet();
740 * Return the predicate values in the iteration order of {@link #entrySet()}.
742 * @return Predicate values.
745 public abstract @NonNull Collection<Object> values();
748 public final @Nullable Object getValue(final QName key) {
749 return keyValue(requireNonNull(key));
753 public final <T> @Nullable T getValue(final QName key, final Class<T> valueClass) {
754 return valueClass.cast(getValue(key));
758 * Return the number of predicates present.
760 * @return The number of predicates present.
763 public abstract int size();
766 * A Map-like view of this identifier's predicates. The view is expected to be stable and effectively-immutable.
768 * @return Map of predicates.
769 * @deprecated This method in a provisional one. It can be used in the code base, but users requiring it should
770 * contact <a href="mailto:yangtools-dev@lists.opendaylight.org">yangtools-dev</a> for migration
771 * guidelines. Callers are strongly encouraged to explore {@link #entrySet()}, {@link #size()},
772 * {@link #values()} and {@link #keySet()} as an alternative.
776 // FIXME: 4.0.0: evaluate the real usefulness of this. The problem here is Map.hashCode() and Map.equals(),
777 // which limits our options.
778 public abstract @NonNull Map<QName, Object> asMap();
781 protected final int hashCodeImpl() {
782 int result = 31 * super.hashCodeImpl();
783 for (Entry<QName, Object> entry : entrySet()) {
784 result += entry.getKey().hashCode() + YangInstanceIdentifier.hashCode(entry.getValue());
790 @SuppressWarnings("checkstyle:equalsHashCode")
791 public final boolean equals(final Object obj) {
792 return super.equals(obj) && equalMapping((NodeIdentifierWithPredicates) obj);
795 abstract boolean equalMapping(NodeIdentifierWithPredicates other);
797 abstract @Nullable Object keyValue(@NonNull QName qname);
800 public final String toString() {
801 return super.toString() + '[' + asMap() + ']';
805 public final String toRelativeString(final PathArgument previous) {
806 return super.toRelativeString(previous) + '[' + asMap() + ']';
810 final Object writeReplace() {
811 return new NIPv2(this);
816 * Simple path argument identifying a {@link LeafSetEntryNode} leaf
819 public static final class NodeWithValue<T> extends AbstractPathArgument {
820 private static final long serialVersionUID = -3637456085341738431L;
822 private final T value;
824 public NodeWithValue(final QName node, final T value) {
829 public T getValue() {
834 protected int hashCodeImpl() {
835 final int prime = 31;
836 int result = super.hashCodeImpl();
837 result = prime * result + YangInstanceIdentifier.hashCode(value);
842 @SuppressWarnings("checkstyle:equalsHashCode")
843 public boolean equals(final Object obj) {
844 if (!super.equals(obj)) {
847 final NodeWithValue<?> other = (NodeWithValue<?>) obj;
848 return Objects.deepEquals(value, other.value);
852 public String toString() {
853 return super.toString() + '[' + value + ']';
857 public String toRelativeString(final PathArgument previous) {
858 return super.toRelativeString(previous) + '[' + value + ']';
862 Object writeReplace() {
863 return new NIVv1(this);
868 * Composite path argument identifying a {@link org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode}
869 * node in particular subtree.
872 * Augmentation is uniquely identified by set of all possible child nodes.
874 * to identify instance of augmentation,
875 * since RFC6020 states that <code>augment</code> that augment
876 * statement must not add multiple nodes from same namespace
877 * / module to the target node.
879 * @see <a href="http://tools.ietf.org/html/rfc6020#section-7.15">RFC6020</a>
881 public static final class AugmentationIdentifier implements PathArgument {
882 private static final long serialVersionUID = -8122335594681936939L;
884 private static final LoadingCache<ImmutableSet<QName>, AugmentationIdentifier> CACHE = CacheBuilder.newBuilder()
885 .weakValues().build(new CacheLoader<ImmutableSet<QName>, AugmentationIdentifier>() {
887 public AugmentationIdentifier load(final ImmutableSet<QName> key) {
888 return new AugmentationIdentifier(key);
892 private final @NonNull ImmutableSet<QName> childNames;
895 public QName getNodeType() {
896 // This should rather throw exception than return always null
897 throw new UnsupportedOperationException("Augmentation node has no QName");
901 * Construct new augmentation identifier using supplied set of possible
905 * Set of possible child nodes.
907 public AugmentationIdentifier(final ImmutableSet<QName> childNames) {
908 this.childNames = requireNonNull(childNames);
912 * Construct new augmentation identifier using supplied set of possible
916 * Set of possible child nodes.
918 public AugmentationIdentifier(final Set<QName> childNames) {
919 this.childNames = ImmutableSet.copyOf(childNames);
923 * Return an AugmentationIdentifier for a particular set of QNames. Unlike the constructor, this factory method
924 * uses a global instance cache, resulting in object reuse for equal inputs.
926 * @param childNames Set of possible child nodes
927 * @return An {@link AugmentationIdentifier}
929 public static @NonNull AugmentationIdentifier create(final ImmutableSet<QName> childNames) {
930 return CACHE.getUnchecked(childNames);
934 * Return an AugmentationIdentifier for a particular set of QNames. Unlike the constructor, this factory method
935 * uses a global instance cache, resulting in object reuse for equal inputs.
937 * @param childNames Set of possible child nodes
938 * @return An {@link AugmentationIdentifier}
940 public static @NonNull AugmentationIdentifier create(final Set<QName> childNames) {
941 final AugmentationIdentifier existing = CACHE.getIfPresent(childNames);
942 return existing != null ? existing : create(ImmutableSet.copyOf(childNames));
946 * Returns set of all possible child nodes.
948 * @return set of all possible child nodes.
950 public @NonNull Set<QName> getPossibleChildNames() {
955 public String toString() {
956 return "AugmentationIdentifier{" + "childNames=" + childNames + '}';
960 public String toRelativeString(final PathArgument previous) {
965 public boolean equals(final Object obj) {
969 if (!(obj instanceof AugmentationIdentifier)) {
973 AugmentationIdentifier that = (AugmentationIdentifier) obj;
974 return childNames.equals(that.childNames);
978 public int hashCode() {
979 return childNames.hashCode();
983 @SuppressWarnings("checkstyle:parameterName")
984 public int compareTo(final PathArgument o) {
985 if (!(o instanceof AugmentationIdentifier)) {
988 AugmentationIdentifier other = (AugmentationIdentifier) o;
989 Set<QName> otherChildNames = other.getPossibleChildNames();
990 int thisSize = childNames.size();
991 int otherSize = otherChildNames.size();
992 if (thisSize == otherSize) {
993 // Quick Set-based comparison
994 if (childNames.equals(otherChildNames)) {
998 // We already know the sets are not equal, but have equal size, hence the sets differ in their elements,
999 // but potentially share a common set of elements. The most consistent way of comparing them is using
1000 // total ordering defined by QName's compareTo. Hence convert both sets to lists ordered
1001 // by QName.compareTo() and decide on the first differing element.
1002 final List<QName> diff = new ArrayList<>(Sets.symmetricDifference(childNames, otherChildNames));
1003 verify(!diff.isEmpty(), "Augmentation identifiers %s and %s report no difference", this, o);
1004 diff.sort(QName::compareTo);
1005 return childNames.contains(diff.get(0)) ? -1 : 1;
1006 } else if (thisSize < otherSize) {
1013 private Object writeReplace() {
1014 return new AIv1(this);
1019 * Fluent Builder of Instance Identifier instances.
1021 public interface InstanceIdentifierBuilder extends Builder<YangInstanceIdentifier> {
1023 * Adds a {@link PathArgument} to path arguments of resulting instance identifier.
1025 * @param arg A {@link PathArgument} to be added
1026 * @return this builder
1028 @NonNull InstanceIdentifierBuilder node(PathArgument arg);
1031 * Adds {@link NodeIdentifier} with supplied QName to path arguments of resulting instance identifier.
1033 * @param nodeType QName of {@link NodeIdentifier} which will be added
1034 * @return this builder
1036 @NonNull InstanceIdentifierBuilder node(QName nodeType);
1039 * Adds {@link NodeIdentifierWithPredicates} with supplied QName and key values to path arguments of resulting
1040 * instance identifier.
1042 * @param nodeType QName of {@link NodeIdentifierWithPredicates} which will be added
1043 * @param keyValues Map of key components and their respective values for {@link NodeIdentifierWithPredicates}
1044 * @return this builder
1046 @NonNull InstanceIdentifierBuilder nodeWithKey(QName nodeType, Map<QName, Object> keyValues);
1049 * Adds {@link NodeIdentifierWithPredicates} with supplied QName and key, value.
1051 * @param nodeType QName of {@link NodeIdentifierWithPredicates} which will be added
1052 * @param key QName of key which will be added
1053 * @param value value of key which will be added
1054 * @return this builder
1056 @NonNull InstanceIdentifierBuilder nodeWithKey(QName nodeType, QName key, Object value);
1059 * Adds a collection of {@link PathArgument}s to path arguments of resulting instance identifier.
1061 * @param args {@link PathArgument}s to be added
1062 * @return this builder
1063 * @throws NullPointerException if any of the arguments is null
1066 @NonNull InstanceIdentifierBuilder append(Collection<? extends PathArgument> args);
1069 * Adds a collection of {@link PathArgument}s to path arguments of resulting instance identifier.
1071 * @param args {@link PathArgument}s to be added
1072 * @return this builder
1073 * @throws NullPointerException if any of the arguments is null
1076 default @NonNull InstanceIdentifierBuilder append(final PathArgument... args) {
1077 return append(Arrays.asList(args));
1081 * Builds an {@link YangInstanceIdentifier} with path arguments from this builder.
1083 * @return {@link YangInstanceIdentifier}
1086 YangInstanceIdentifier build();