ff8aa82a23f8c46e0d4d709b6bf538e230277a18
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / leafref / LeafRefPath.java
1 /**
2  * Copyright (c) 2015 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.impl.leafref;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.ImmutableList;
12 import com.google.common.collect.Iterables;
13 import java.util.Arrays;
14 import java.util.Iterator;
15 import java.util.NoSuchElementException;
16 import java.util.Objects;
17 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
18 import org.opendaylight.yangtools.concepts.Immutable;
19
20 public abstract class LeafRefPath implements Immutable {
21
22     /**
23      * An absolute LeafRefPath.
24      */
25     private static final class AbsoluteLeafRefPath extends LeafRefPath {
26         private AbsoluteLeafRefPath(final LeafRefPath parent,
27                 final QNameWithPredicate qname) {
28             super(parent, qname);
29         }
30
31         @Override
32         public boolean isAbsolute() {
33             return true;
34         }
35
36         @Override
37         protected LeafRefPath createInstance(final LeafRefPath parent,
38                 final QNameWithPredicate qname) {
39             return new AbsoluteLeafRefPath(parent, qname);
40         }
41     }
42
43     /**
44      * A relative LeafRefPath.
45      */
46     private static final class RelativeLeafRefPath extends LeafRefPath {
47         private RelativeLeafRefPath(final LeafRefPath parent,
48                 final QNameWithPredicate qname) {
49             super(parent, qname);
50         }
51
52         @Override
53         public boolean isAbsolute() {
54             return false;
55         }
56
57         @Override
58         protected LeafRefPath createInstance(final LeafRefPath parent,
59                 final QNameWithPredicate qname) {
60             return new RelativeLeafRefPath(parent, qname);
61         }
62     }
63
64     @SuppressWarnings("rawtypes")
65     private static final AtomicReferenceFieldUpdater<LeafRefPath, ImmutableList> LEGACYPATH_UPDATER = AtomicReferenceFieldUpdater
66             .newUpdater(LeafRefPath.class, ImmutableList.class, "legacyPath");
67
68     /**
69      * Shared instance of the conceptual root schema node.
70      */
71     public static final LeafRefPath ROOT = new AbsoluteLeafRefPath(null, null);
72
73     /**
74      * Shared instance of the "same" relative schema node.
75      */
76     public static final LeafRefPath SAME = new RelativeLeafRefPath(null, null);
77
78     /**
79      * Parent path.
80      */
81     private final LeafRefPath parent;
82
83     /**
84      * This component.
85      */
86     private final QNameWithPredicate qname;
87
88     /**
89      * Cached hash code. We can use this since we are immutable.
90      */
91     private final int hash;
92
93     /**
94      * Cached legacy path, filled-in when {@link #getPath()} or
95      * {@link #getPathTowardsRoot()} is invoked.
96      */
97     private volatile ImmutableList<QNameWithPredicate> legacyPath;
98
99     private ImmutableList<QNameWithPredicate> getLegacyPath() {
100         ImmutableList<QNameWithPredicate> ret = legacyPath;
101         if (ret == null) {
102             ret = ImmutableList.copyOf(getPathTowardsRoot()).reverse();
103             LEGACYPATH_UPDATER.lazySet(this, ret);
104         }
105
106         return ret;
107     }
108
109     protected LeafRefPath(final LeafRefPath parent, final QNameWithPredicate qname) {
110         this.parent = parent;
111         this.qname = qname;
112
113         int h = Objects.hashCode(parent);
114         if (qname != null) {
115             h = h * 31 + qname.hashCode();
116         }
117
118         hash = h;
119     }
120
121     /**
122      * Constructs new instance of this class with the concrete path.
123      *
124      * @param path
125      *            list of QNameWithPredicate instances which specifies exact
126      *            path to the module node
127      * @param absolute
128      *            boolean value which specifies if the path is absolute or
129      *            relative
130      *
131      * @return A LeafRefPath instance.
132      */
133     public static LeafRefPath create(final Iterable<QNameWithPredicate> path,
134             final boolean absolute) {
135         final LeafRefPath parent = absolute ? ROOT : SAME;
136         return parent.createChild(path);
137     }
138
139     /**
140      * Constructs new instance of this class with the concrete path.
141      *
142      * @param absolute
143      *            boolean value which specifies if the path is absolute or
144      *            relative
145      * @param path
146      *            one or more QNameWithPredicate instances which specifies exact
147      *            path to the module node
148      *
149      * @return A LeafRefPath instance.
150      */
151     public static LeafRefPath create(final boolean absolute,
152             final QNameWithPredicate... path) {
153         return create(Arrays.asList(path), absolute);
154     }
155
156     /**
157      * Create a new instance.
158      *
159      * @param parent
160      *            Parent LeafRefPath
161      * @param qname
162      *            next path element
163      * @return A new LeafRefPath instance
164      */
165     protected abstract LeafRefPath createInstance(LeafRefPath parent,
166             QNameWithPredicate qname);
167
168     /**
169      * Create a child path based on concatenation of this path and a relative
170      * path.
171      *
172      * @param relative
173      *            Relative path
174      * @return A new child path
175      */
176     public LeafRefPath createChild(final Iterable<QNameWithPredicate> relative) {
177         if (Iterables.isEmpty(relative)) {
178             return this;
179         }
180
181         LeafRefPath parent = this;
182         for (QNameWithPredicate qname : relative) {
183             parent = parent.createInstance(parent, qname);
184         }
185
186         return parent;
187     }
188
189     /**
190      * Create a child path based on concatenation of this path and a relative
191      * path.
192      *
193      * @param relative
194      *            Relative LeafRefPath
195      * @return A new child path
196      */
197     public LeafRefPath createChild(final LeafRefPath relative) {
198         Preconditions.checkArgument(!relative.isAbsolute(),
199                 "Child creation requires relative path");
200
201         LeafRefPath parent = this;
202         for (QNameWithPredicate qname : relative.getPathFromRoot()) {
203             parent = parent.createInstance(parent, qname);
204         }
205
206         return parent;
207     }
208
209     /**
210      * Create a child path based on concatenation of this path and additional
211      * path elements.
212      *
213      * @param elements
214      *            Relative LeafRefPath elements
215      * @return A new child path
216      */
217     public LeafRefPath createChild(final QNameWithPredicate... elements) {
218         return createChild(Arrays.asList(elements));
219     }
220
221     /**
222      * Returns the list of nodes which need to be traversed to get from the
223      * starting point (root for absolute LeafRefPaths) to the node represented
224      * by this object.
225      *
226      * @return list of <code>qname</code> instances which represents path from
227      *         the root to the schema node.
228      */
229     public Iterable<QNameWithPredicate> getPathFromRoot() {
230         return getLegacyPath();
231     }
232
233     /**
234      * Returns the list of nodes which need to be traversed to get from this
235      * node to the starting point (root for absolute LeafRefPaths).
236      *
237      * @return list of <code>qname</code> instances which represents path from
238      *         the schema node towards the root.
239      */
240     public Iterable<QNameWithPredicate> getPathTowardsRoot() {
241         return new Iterable<QNameWithPredicate>() {
242             @Override
243             public Iterator<QNameWithPredicate> iterator() {
244                 return new Iterator<QNameWithPredicate>() {
245                     private LeafRefPath current = LeafRefPath.this;
246
247                     @Override
248                     public boolean hasNext() {
249                         return current.parent != null;
250                     }
251
252                     @Override
253                     public QNameWithPredicate next() {
254                         if (current.parent != null) {
255                             final QNameWithPredicate ret = current.qname;
256                             current = current.parent;
257                             return ret;
258                         } else {
259                             throw new NoSuchElementException(
260                                     "No more elements available");
261                         }
262                     }
263
264                     @Override
265                     public void remove() {
266                         throw new UnsupportedOperationException(
267                                 "Component removal not supported");
268                     }
269                 };
270             }
271         };
272     }
273
274     /**
275      * Returns the immediate parent LeafRefPath.
276      *
277      * @return Parent path, null if this LeafRefPath is already toplevel.
278      */
279     public LeafRefPath getParent() {
280         return parent;
281     }
282
283     /**
284      * Get the last component of this path.
285      *
286      * @return The last component of this path.
287      */
288     public final QNameWithPredicate getLastComponent() {
289         return qname;
290     }
291
292     /**
293      * Describes whether schema path is|isn't absolute.
294      *
295      * @return boolean value which is <code>true</code> if schema path is
296      *         absolute.
297      */
298     public abstract boolean isAbsolute();
299
300     @Override
301     public final int hashCode() {
302         return hash;
303     }
304
305     @Override
306     public boolean equals(final Object obj) {
307         if (this == obj) {
308             return true;
309         }
310         if (obj == null) {
311             return false;
312         }
313         if (getClass() != obj.getClass()) {
314             return false;
315         }
316         final LeafRefPath other = (LeafRefPath) obj;
317         return Objects.equals(qname, other.qname) && Objects.equals(parent, other.parent);
318     }
319
320     @Override
321     public String toString() {
322         StringBuilder sb = new StringBuilder();
323
324         Iterable<QNameWithPredicate> pathFromRoot = this.getPathFromRoot();
325
326         sb.append(isAbsolute() ? "Absolute path:" : "Relative path:");
327
328         for (QNameWithPredicate qName : pathFromRoot) {
329             sb.append('/').append(qName);
330         }
331
332         return sb.toString();
333
334     }
335
336     // @Override
337     // public final String toString() {
338     // return addToStringAttributes(Objects.toStringHelper(this)).toString();
339     // }
340     //
341     // protected ToStringHelper addToStringAttributes(final ToStringHelper
342     // toStringHelper) {
343     // return toStringHelper.add("path", getPathFromRoot());
344     // }
345
346 }