Add yang-data-api API evolution guidance
[yangtools.git] / yang / yang-data-api / src / main / java / org / opendaylight / yangtools / yang / data / api / YangInstanceIdentifier.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.api;
9
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;
13
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.ImmutableSet;
19 import com.google.common.collect.Iterables;
20 import com.google.common.collect.Sets;
21 import java.io.Serializable;
22 import java.lang.reflect.Array;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.Collection;
26 import java.util.Iterator;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Map.Entry;
30 import java.util.Objects;
31 import java.util.Optional;
32 import java.util.Set;
33 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
34 import javax.annotation.Nonnull;
35 import javax.annotation.Nullable;
36 import org.opendaylight.yangtools.concepts.Builder;
37 import org.opendaylight.yangtools.concepts.Immutable;
38 import org.opendaylight.yangtools.concepts.Path;
39 import org.opendaylight.yangtools.util.HashCodeBuilder;
40 import org.opendaylight.yangtools.util.ImmutableOffsetMap;
41 import org.opendaylight.yangtools.util.SharedSingletonMap;
42 import org.opendaylight.yangtools.yang.common.QName;
43 import org.opendaylight.yangtools.yang.common.QNameModule;
44 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
45
46 /**
47  * Unique identifier of a particular node instance in the data tree.
48  *
49  * <p>
50  * Java representation of YANG Built-in type <code>instance-identifier</code>,
51  * which conceptually is XPath expression minimized to uniquely identify element
52  * in data tree which conforms to constraints maintained by YANG Model,
53  * effectively this makes Instance Identifier a path to element in data tree.
54  *
55  * <p>
56  * Constraints put in YANG specification on instance-identifier allowed it to be
57  * effectively represented in Java and it's evaluation does not require
58  * full-blown XPath processor.
59  *
60  * <p>
61  * <h3>Path Arguments</h3>
62  * Path to the node represented in instance identifier consists of
63  * {@link PathArgument} which carries necessary information to uniquely identify
64  * node on particular level in the subtree.
65  *
66  * <ul>
67  * <li>{@link NodeIdentifier} - Identifier of node, which has cardinality
68  * <code>0..1</code> in particular subtree in data tree.</li>
69  * <li>{@link NodeIdentifierWithPredicates} - Identifier of node (list item),
70  * which has cardinality <code>0..n</code>.</li>
71  * <li>{@link NodeWithValue} - Identifier of instance <code>leaf</code> node or
72  * <code>leaf-list</code> node.</li>
73  * <li>{@link AugmentationIdentifier} - Identifier of instance of
74  * <code>augmentation</code> node.</li>
75  * </ul>
76  *
77  * @see <a href="http://tools.ietf.org/html/rfc6020#section-9.13">RFC6020</a>
78  */
79 // FIXME: 3.0.0: this concept needs to be moved to yang-common, as parser components need the ability to refer
80 //               to data nodes -- most notably XPath expressions and {@code default} statement arguments need to be able
81 //               to represent these.
82 public abstract class YangInstanceIdentifier implements Path<YangInstanceIdentifier>, Immutable, Serializable {
83     /**
84      * An empty {@link YangInstanceIdentifier}. It corresponds to the path of the conceptual
85      * root of the YANG namespace.
86      */
87     public static final YangInstanceIdentifier EMPTY = FixedYangInstanceIdentifier.EMPTY_INSTANCE;
88
89     private static final AtomicReferenceFieldUpdater<YangInstanceIdentifier, String> TOSTRINGCACHE_UPDATER =
90             AtomicReferenceFieldUpdater.newUpdater(YangInstanceIdentifier.class, String.class, "toStringCache");
91     private static final long serialVersionUID = 4L;
92
93     private final int hash;
94     private transient volatile String toStringCache = null;
95
96     // Package-private to prevent outside subclassing
97     YangInstanceIdentifier(final int hash) {
98         this.hash = hash;
99     }
100
101     @Nonnull abstract YangInstanceIdentifier createRelativeIdentifier(int skipFromRoot);
102
103     @Nonnull abstract Collection<PathArgument> tryPathArguments();
104
105     @Nonnull abstract Collection<PathArgument> tryReversePathArguments();
106
107     /**
108      * Check if this instance identifier has empty path arguments, e.g. it is
109      * empty and corresponds to {@link #EMPTY}.
110      *
111      * @return True if this instance identifier is empty, false otherwise.
112      */
113     public abstract boolean isEmpty();
114
115     /**
116      * Return an optimized version of this identifier, useful when the identifier
117      * will be used very frequently.
118      *
119      * @return A optimized equivalent instance.
120      */
121     @Beta
122     public abstract YangInstanceIdentifier toOptimized();
123
124     /**
125      * Return the conceptual parent {@link YangInstanceIdentifier}, which has
126      * one item less in {@link #getPathArguments()}.
127      *
128      * @return Parent {@link YangInstanceIdentifier}, or null if this object is {@link #EMPTY}.
129      */
130     @Nullable public abstract YangInstanceIdentifier getParent();
131
132     /**
133      * Return the ancestor {@link YangInstanceIdentifier} with a particular depth, e.g. number of path arguments.
134      *
135      * @param depth Ancestor depth
136      * @return Ancestor {@link YangInstanceIdentifier}
137      * @throws IllegalArgumentException if the specified depth is negative or is greater than the depth of this object.
138      */
139     @Nonnull public abstract YangInstanceIdentifier getAncestor(int depth);
140
141     /**
142      * Returns an ordered iteration of path arguments.
143      *
144      * @return Immutable iteration of path arguments.
145      */
146     public abstract List<PathArgument> getPathArguments();
147
148     /**
149      * Returns an iterable of path arguments in reverse order. This is useful
150      * when walking up a tree organized this way.
151      *
152      * @return Immutable iterable of path arguments in reverse order.
153      */
154     public abstract List<PathArgument> getReversePathArguments();
155
156     /**
157      * Returns the last PathArgument. This is equivalent of iterating
158      * to the last element of the iterable returned by {@link #getPathArguments()}.
159      *
160      * @return The last past argument, or null if there are no PathArguments.
161      */
162     public abstract PathArgument getLastPathArgument();
163
164     public static YangInstanceIdentifier create(final Iterable<? extends PathArgument> path) {
165         if (Iterables.isEmpty(path)) {
166             return EMPTY;
167         }
168
169         final HashCodeBuilder<PathArgument> hash = new HashCodeBuilder<>();
170         for (PathArgument a : path) {
171             hash.addArgument(a);
172         }
173
174         return FixedYangInstanceIdentifier.create(path, hash.build());
175     }
176
177     public static YangInstanceIdentifier create(final PathArgument... path) {
178         // We are forcing a copy, since we cannot trust the user
179         return create(Arrays.asList(path));
180     }
181
182     boolean pathArgumentsEqual(final YangInstanceIdentifier other) {
183         return Iterables.elementsEqual(getPathArguments(), other.getPathArguments());
184     }
185
186     @Override
187     public boolean equals(final Object obj) {
188         if (this == obj) {
189             return true;
190         }
191         if (!(obj instanceof YangInstanceIdentifier)) {
192             return false;
193         }
194         YangInstanceIdentifier other = (YangInstanceIdentifier) obj;
195         if (this.hashCode() != obj.hashCode()) {
196             return false;
197         }
198
199         return pathArgumentsEqual(other);
200     }
201
202     /**
203      * Constructs a new Instance Identifier with new {@link NodeIdentifier} added to the end of path arguments.
204      *
205      * @param name QName of {@link NodeIdentifier}
206      * @return Instance Identifier with additional path argument added to the end.
207      */
208     public final YangInstanceIdentifier node(final QName name) {
209         return node(new NodeIdentifier(name));
210     }
211
212     /**
213      * Constructs a new Instance Identifier with new {@link PathArgument} added to the end of path arguments.
214      *
215      * @param arg Path argument which should be added to the end
216      * @return Instance Identifier with additional path argument added to the end.
217      */
218     public final YangInstanceIdentifier node(final PathArgument arg) {
219         return new StackedYangInstanceIdentifier(this, arg, HashCodeBuilder.nextHashCode(hash, arg));
220     }
221
222     /**
223      * Get the relative path from an ancestor. This method attempts to perform
224      * the reverse of concatenating a base (ancestor) and a path.
225      *
226      * @param ancestor
227      *            Ancestor against which the relative path should be calculated
228      * @return This object's relative path from parent, or Optional.absent() if
229      *         the specified parent is not in fact an ancestor of this object.
230      */
231     public Optional<YangInstanceIdentifier> relativeTo(final YangInstanceIdentifier ancestor) {
232         if (this == ancestor) {
233             return Optional.of(EMPTY);
234         }
235         if (ancestor.isEmpty()) {
236             return Optional.of(this);
237         }
238
239         final Iterator<PathArgument> lit = getPathArguments().iterator();
240         final Iterator<PathArgument> oit = ancestor.getPathArguments().iterator();
241         int common = 0;
242
243         while (oit.hasNext()) {
244             // Ancestor is not really an ancestor
245             if (!lit.hasNext() || !lit.next().equals(oit.next())) {
246                 return Optional.empty();
247             }
248
249             ++common;
250         }
251
252         if (common == 0) {
253             return Optional.of(this);
254         }
255         if (!lit.hasNext()) {
256             return Optional.of(EMPTY);
257         }
258
259         return Optional.of(createRelativeIdentifier(common));
260     }
261
262     @Override
263     public final boolean contains(@Nonnull final YangInstanceIdentifier other) {
264         if (this == other) {
265             return true;
266         }
267
268         checkArgument(other != null, "other should not be null");
269         final Iterator<PathArgument> lit = getPathArguments().iterator();
270         final Iterator<PathArgument> oit = other.getPathArguments().iterator();
271
272         while (lit.hasNext()) {
273             if (!oit.hasNext()) {
274                 return false;
275             }
276
277             if (!lit.next().equals(oit.next())) {
278                 return false;
279             }
280         }
281
282         return true;
283     }
284
285     @Override
286     public final String toString() {
287         /*
288          * The toStringCache is safe, since the object contract requires
289          * immutability of the object and all objects referenced from this
290          * object.
291          * Used lists, maps are immutable. Path Arguments (elements) are also
292          * immutable, since the PathArgument contract requires immutability.
293          * The cache is thread-safe - if multiple computations occurs at the
294          * same time, cache will be overwritten with same result.
295          */
296         String ret = toStringCache;
297         if (ret == null) {
298             final StringBuilder builder = new StringBuilder("/");
299             PathArgument prev = null;
300             for (PathArgument argument : getPathArguments()) {
301                 if (prev != null) {
302                     builder.append('/');
303                 }
304                 builder.append(argument.toRelativeString(prev));
305                 prev = argument;
306             }
307
308             ret = builder.toString();
309             TOSTRINGCACHE_UPDATER.lazySet(this, ret);
310         }
311         return ret;
312     }
313
314     @Override
315     public final int hashCode() {
316         /*
317          * The caching is safe, since the object contract requires
318          * immutability of the object and all objects referenced from this
319          * object.
320          * Used lists, maps are immutable. Path Arguments (elements) are also
321          * immutable, since the PathArgument contract requires immutability.
322          */
323         return hash;
324     }
325
326     private static int hashCode(final Object value) {
327         if (value == null) {
328             return 0;
329         }
330
331         if (byte[].class.equals(value.getClass())) {
332             return Arrays.hashCode((byte[]) value);
333         }
334
335         if (value.getClass().isArray()) {
336             int hash = 0;
337             int length = Array.getLength(value);
338             for (int i = 0; i < length; i++) {
339                 hash += Objects.hashCode(Array.get(value, i));
340             }
341
342             return hash;
343         }
344
345         return Objects.hashCode(value);
346     }
347
348     // Static factories & helpers
349
350     /**
351      * Returns a new InstanceIdentifier with only one path argument of type {@link NodeIdentifier} with supplied
352      * QName.
353      *
354      * @param name QName of first node identifier
355      * @return Instance Identifier with only one path argument of type {@link NodeIdentifier}
356      */
357     public static YangInstanceIdentifier of(final QName name) {
358         return create(new NodeIdentifier(name));
359     }
360
361     /**
362      * Returns new builder for InstanceIdentifier with empty path arguments.
363      *
364      * @return new builder for InstanceIdentifier with empty path arguments.
365      */
366     public static InstanceIdentifierBuilder builder() {
367         return new YangInstanceIdentifierBuilder();
368     }
369
370     /**
371      * Returns new builder for InstanceIdentifier with path arguments copied from original instance identifier.
372      *
373      * @param origin InstanceIdentifier from which path arguments are copied.
374      * @return new builder for InstanceIdentifier with path arguments copied from original instance identifier.
375      */
376     public static InstanceIdentifierBuilder builder(final YangInstanceIdentifier origin) {
377         return new YangInstanceIdentifierBuilder(origin.getPathArguments(), origin.hashCode());
378     }
379
380     /**
381      * Path argument / component of InstanceIdentifier.
382      * Path argument uniquely identifies node in data tree on particular
383      * level.
384      *
385      * <p>
386      * This interface itself is used as common parent for actual
387      * path arguments types and should not be implemented by user code.
388      *
389      * <p>
390      * Path arguments SHOULD contain only minimum of information
391      * required to uniquely identify node on particular subtree level.
392      *
393      * <p>
394      * For actual path arguments types see:
395      * <ul>
396      * <li>{@link NodeIdentifier} - Identifier of container or leaf
397      * <li>{@link NodeIdentifierWithPredicates} - Identifier of list entries, which have key defined
398      * <li>{@link AugmentationIdentifier} - Identifier of augmentation
399      * <li>{@link NodeWithValue} - Identifier of leaf-list entry
400      * </ul>
401      */
402     public interface PathArgument extends Comparable<PathArgument>, Immutable, Serializable {
403         /**
404          * If applicable returns unique QName of data node as defined in YANG
405          * Schema.
406          *
407          * <p>
408          * This method may return null, if the corresponding schema node, does
409          * not have QName associated, such as in cases of augmentations.
410          *
411          * @return Node type
412          */
413         QName getNodeType();
414
415         /**
416          * Return the string representation of this object for use in context
417          * provided by a previous object. This method can be implemented in
418          * terms of {@link #toString()}, but implementations are encourage to
419          * reuse any context already emitted by the previous object.
420          *
421          * @param previous Previous path argument
422          * @return String representation
423          */
424         String toRelativeString(PathArgument previous);
425     }
426
427     private abstract static class AbstractPathArgument implements PathArgument {
428         private static final long serialVersionUID = -4546547994250849340L;
429         private final QName nodeType;
430         private transient int hashValue;
431         private transient volatile boolean hashGuard = false;
432
433         protected AbstractPathArgument(final QName nodeType) {
434             this.nodeType = requireNonNull(nodeType);
435         }
436
437         @Override
438         public final QName getNodeType() {
439             return nodeType;
440         }
441
442         @Override
443         @SuppressWarnings("checkstyle:parameterName")
444         public int compareTo(@Nonnull final PathArgument o) {
445             return nodeType.compareTo(o.getNodeType());
446         }
447
448         protected int hashCodeImpl() {
449             return 31 + getNodeType().hashCode();
450         }
451
452         @Override
453         public final int hashCode() {
454             if (!hashGuard) {
455                 hashValue = hashCodeImpl();
456                 hashGuard = true;
457             }
458
459             return hashValue;
460         }
461
462         @Override
463         public boolean equals(final Object obj) {
464             if (this == obj) {
465                 return true;
466             }
467             if (obj == null || this.getClass() != obj.getClass()) {
468                 return false;
469             }
470
471             return getNodeType().equals(((AbstractPathArgument)obj).getNodeType());
472         }
473
474         @Override
475         public String toString() {
476             return getNodeType().toString();
477         }
478
479         @Override
480         public String toRelativeString(final PathArgument previous) {
481             if (previous instanceof AbstractPathArgument) {
482                 final QNameModule mod = previous.getNodeType().getModule();
483                 if (getNodeType().getModule().equals(mod)) {
484                     return getNodeType().getLocalName();
485                 }
486             }
487
488             return getNodeType().toString();
489         }
490     }
491
492     /**
493      * Simple path argument identifying a {@link org.opendaylight.yangtools.yang.data.api.schema.ContainerNode} or
494      * {@link org.opendaylight.yangtools.yang.data.api.schema.LeafNode} leaf in particular subtree.
495      */
496     public static final class NodeIdentifier extends AbstractPathArgument {
497         private static final long serialVersionUID = -2255888212390871347L;
498         private static final LoadingCache<QName, NodeIdentifier> CACHE = CacheBuilder.newBuilder().weakValues()
499                 .build(new CacheLoader<QName, NodeIdentifier>() {
500                     @Override
501                     public NodeIdentifier load(@Nonnull final QName key) {
502                         return new NodeIdentifier(key);
503                     }
504                 });
505
506         public NodeIdentifier(final QName node) {
507             super(node);
508         }
509
510         /**
511          * Return a NodeIdentifier for a particular QName. Unlike the constructor, this factory method uses a global
512          * instance cache, resulting in object reuse for equal inputs.
513          *
514          * @param node Node's QName
515          * @return A {@link NodeIdentifier}
516          */
517         public static NodeIdentifier create(final QName node) {
518             return CACHE.getUnchecked(node);
519         }
520     }
521
522     /**
523      * Composite path argument identifying a {@link org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode} leaf
524      * overall data tree.
525      */
526     public static final class NodeIdentifierWithPredicates extends AbstractPathArgument {
527         private static final long serialVersionUID = -4787195606494761540L;
528
529         private final Map<QName, Object> keyValues;
530
531         public NodeIdentifierWithPredicates(final QName node, final Map<QName, Object> keyValues) {
532             super(node);
533             // Retains ImmutableMap for empty maps. For larger sizes uses a shared key set.
534             this.keyValues = ImmutableOffsetMap.unorderedCopyOf(keyValues);
535         }
536
537         public NodeIdentifierWithPredicates(final QName node, final QName key, final Object value) {
538             super(node);
539             this.keyValues = SharedSingletonMap.unorderedOf(key, value);
540         }
541
542         public Map<QName, Object> getKeyValues() {
543             return keyValues;
544         }
545
546         @Override
547         protected int hashCodeImpl() {
548             final int prime = 31;
549             int result = super.hashCodeImpl();
550             result = prime * result;
551
552             for (Entry<QName, Object> entry : keyValues.entrySet()) {
553                 result += Objects.hashCode(entry.getKey()) + YangInstanceIdentifier.hashCode(entry.getValue());
554             }
555             return result;
556         }
557
558         @Override
559         @SuppressWarnings("checkstyle:equalsHashCode")
560         public boolean equals(final Object obj) {
561             if (!super.equals(obj)) {
562                 return false;
563             }
564
565             final Map<QName, Object> otherKeyValues = ((NodeIdentifierWithPredicates) obj).keyValues;
566
567             // TODO: benchmark to see if just calling equals() on the two maps is not faster
568             if (keyValues == otherKeyValues) {
569                 return true;
570             }
571             if (keyValues.size() != otherKeyValues.size()) {
572                 return false;
573             }
574
575             for (Entry<QName, Object> entry : keyValues.entrySet()) {
576                 if (!otherKeyValues.containsKey(entry.getKey())
577                         || !Objects.deepEquals(entry.getValue(), otherKeyValues.get(entry.getKey()))) {
578
579                     return false;
580                 }
581             }
582
583             return true;
584         }
585
586         @Override
587         public String toString() {
588             return super.toString() + '[' + keyValues + ']';
589         }
590
591         @Override
592         public String toRelativeString(final PathArgument previous) {
593             return super.toRelativeString(previous) + '[' + keyValues + ']';
594         }
595     }
596
597     /**
598      * Simple path argument identifying a {@link LeafSetEntryNode} leaf
599      * overall data tree.
600      */
601     public static final class NodeWithValue<T> extends AbstractPathArgument {
602         private static final long serialVersionUID = -3637456085341738431L;
603
604         private final T value;
605
606         public NodeWithValue(final QName node, final T value) {
607             super(node);
608             this.value = value;
609         }
610
611         public T getValue() {
612             return value;
613         }
614
615         @Override
616         protected int hashCodeImpl() {
617             final int prime = 31;
618             int result = super.hashCodeImpl();
619             result = prime * result + YangInstanceIdentifier.hashCode(value);
620             return result;
621         }
622
623         @Override
624         @SuppressWarnings("checkstyle:equalsHashCode")
625         public boolean equals(final Object obj) {
626             if (!super.equals(obj)) {
627                 return false;
628             }
629             final NodeWithValue<?> other = (NodeWithValue<?>) obj;
630             return Objects.deepEquals(value, other.value);
631         }
632
633         @Override
634         public String toString() {
635             return super.toString() + '[' + value + ']';
636         }
637
638         @Override
639         public String toRelativeString(final PathArgument previous) {
640             return super.toRelativeString(previous) + '[' + value + ']';
641         }
642     }
643
644     /**
645      * Composite path argument identifying a {@link org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode}
646      * node in particular subtree.
647      *
648      * <p>
649      * Augmentation is uniquely identified by set of all possible child nodes.
650      * This is possible
651      * to identify instance of augmentation,
652      * since RFC6020 states that <code>augment</code> that augment
653      * statement must not add multiple nodes from same namespace
654      * / module to the target node.
655      *
656      * @see <a href="http://tools.ietf.org/html/rfc6020#section-7.15">RFC6020</a>
657      */
658     public static final class AugmentationIdentifier implements PathArgument {
659         private static final long serialVersionUID = -8122335594681936939L;
660         private final ImmutableSet<QName> childNames;
661
662         @Override
663         public QName getNodeType() {
664             // This should rather throw exception than return always null
665             throw new UnsupportedOperationException("Augmentation node has no QName");
666         }
667
668         /**
669          * Construct new augmentation identifier using supplied set of possible
670          * child nodes.
671          *
672          * @param childNames
673          *            Set of possible child nodes.
674          */
675         public AugmentationIdentifier(final Set<QName> childNames) {
676             this.childNames = ImmutableSet.copyOf(childNames);
677         }
678
679         /**
680          * Returns set of all possible child nodes.
681          *
682          * @return set of all possible child nodes.
683          */
684         public Set<QName> getPossibleChildNames() {
685             return childNames;
686         }
687
688         @Override
689         public String toString() {
690             return "AugmentationIdentifier{" + "childNames=" + childNames + '}';
691         }
692
693         @Override
694         public String toRelativeString(final PathArgument previous) {
695             return toString();
696         }
697
698         @Override
699         public boolean equals(final Object obj) {
700             if (this == obj) {
701                 return true;
702             }
703             if (!(obj instanceof AugmentationIdentifier)) {
704                 return false;
705             }
706
707             AugmentationIdentifier that = (AugmentationIdentifier) obj;
708             return childNames.equals(that.childNames);
709         }
710
711         @Override
712         public int hashCode() {
713             return childNames.hashCode();
714         }
715
716         @Override
717         @SuppressWarnings("checkstyle:parameterName")
718         public int compareTo(@Nonnull final PathArgument o) {
719             if (!(o instanceof AugmentationIdentifier)) {
720                 return -1;
721             }
722             AugmentationIdentifier other = (AugmentationIdentifier) o;
723             Set<QName> otherChildNames = other.getPossibleChildNames();
724             int thisSize = childNames.size();
725             int otherSize = otherChildNames.size();
726             if (thisSize == otherSize) {
727                 // Quick Set-based comparison
728                 if (childNames.equals(otherChildNames)) {
729                     return 0;
730                 }
731
732                 // We already know the sets are not equal, but have equal size, hence the sets differ in their elements,
733                 // but potentially share a common set of elements. The most consistent way of comparing them is using
734                 // total ordering defined by QName's compareTo. Hence convert both sets to lists ordered
735                 // by QName.compareTo() and decide on the first differing element.
736                 final List<QName> diff = new ArrayList<>(Sets.symmetricDifference(childNames, otherChildNames));
737                 verify(!diff.isEmpty(), "Augmentation identifiers %s and %s report no difference", this, o);
738                 diff.sort(QName::compareTo);
739                 return childNames.contains(diff.get(0)) ? -1 : 1;
740             } else if (thisSize < otherSize) {
741                 return 1;
742             } else {
743                 return -1;
744             }
745         }
746     }
747
748     /**
749      * Fluent Builder of Instance Identifier instances.
750      */
751     public interface InstanceIdentifierBuilder extends Builder<YangInstanceIdentifier> {
752         /**
753          * Adds a {@link PathArgument} to path arguments of resulting instance identifier.
754          *
755          * @param arg A {@link PathArgument} to be added
756          * @return this builder
757          */
758         InstanceIdentifierBuilder node(PathArgument arg);
759
760         /**
761          * Adds {@link NodeIdentifier} with supplied QName to path arguments of resulting instance identifier.
762          *
763          * @param nodeType QName of {@link NodeIdentifier} which will be added
764          * @return this builder
765          */
766         InstanceIdentifierBuilder node(QName nodeType);
767
768         /**
769          * Adds {@link NodeIdentifierWithPredicates} with supplied QName and key values to path arguments of resulting
770          * instance identifier.
771          *
772          * @param nodeType QName of {@link NodeIdentifierWithPredicates} which will be added
773          * @param keyValues Map of key components and their respective values for {@link NodeIdentifierWithPredicates}
774          * @return this builder
775          */
776         InstanceIdentifierBuilder nodeWithKey(QName nodeType, Map<QName, Object> keyValues);
777
778         /**
779          * Adds {@link NodeIdentifierWithPredicates} with supplied QName and key, value.
780          *
781          * @param nodeType QName of {@link NodeIdentifierWithPredicates} which will be added
782          * @param key QName of key which will be added
783          * @param value value of key which will be added
784          * @return this builder
785          */
786         InstanceIdentifierBuilder nodeWithKey(QName nodeType, QName key, Object value);
787
788         /**
789          * Adds a collection of {@link PathArgument}s to path arguments of resulting instance identifier.
790          *
791          * @param args {@link PathArgument}s to be added
792          * @return this builder
793          * @throws NullPointerException if any of the arguments is null
794          */
795         @Beta
796         InstanceIdentifierBuilder append(Collection<? extends PathArgument> args);
797
798         /**
799          * Adds a collection of {@link PathArgument}s to path arguments of resulting instance identifier.
800          *
801          * @param args {@link PathArgument}s to be added
802          * @return this builder
803          * @throws NullPointerException if any of the arguments is null
804          */
805         @Beta
806         default InstanceIdentifierBuilder append(final PathArgument... args) {
807             return append(Arrays.asList(args));
808         }
809
810         /**
811          * Builds an {@link YangInstanceIdentifier} with path arguments from this builder.
812          *
813          * @return {@link YangInstanceIdentifier}
814          */
815         @Override
816         YangInstanceIdentifier build();
817     }
818 }