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