ae0aae7612464da5ca6df433f08ea0bc799d426c
[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 =
66         AtomicReferenceFieldUpdater.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 hc = Objects.hashCode(parent);
114         if (qname != null) {
115             hc = hc * 31 + qname.hashCode();
116         }
117
118         hash = hc;
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(), "Child creation requires relative path");
199
200         LeafRefPath parent = this;
201         for (QNameWithPredicate qname : relative.getPathFromRoot()) {
202             parent = parent.createInstance(parent, qname);
203         }
204
205         return parent;
206     }
207
208     /**
209      * Create a child path based on concatenation of this path and additional
210      * path elements.
211      *
212      * @param elements
213      *            Relative LeafRefPath elements
214      * @return A new child path
215      */
216     public LeafRefPath createChild(final QNameWithPredicate... elements) {
217         return createChild(Arrays.asList(elements));
218     }
219
220     /**
221      * Returns the list of nodes which need to be traversed to get from the
222      * starting point (root for absolute LeafRefPaths) to the node represented
223      * by this object.
224      *
225      * @return list of <code>qname</code> instances which represents path from
226      *         the root to the schema node.
227      */
228     public Iterable<QNameWithPredicate> getPathFromRoot() {
229         return getLegacyPath();
230     }
231
232     /**
233      * Returns the list of nodes which need to be traversed to get from this
234      * node to the starting point (root for absolute LeafRefPaths).
235      *
236      * @return list of <code>qname</code> instances which represents path from
237      *         the schema node towards the root.
238      */
239     public Iterable<QNameWithPredicate> getPathTowardsRoot() {
240         return () -> new Iterator<QNameWithPredicate>() {
241             private LeafRefPath current = LeafRefPath.this;
242
243             @Override
244             public boolean hasNext() {
245                 return current.parent != null;
246             }
247
248             @Override
249             public QNameWithPredicate next() {
250                 if (current.parent == null) {
251                     throw new NoSuchElementException("No more elements available");
252                 }
253
254                 final QNameWithPredicate ret = current.qname;
255                 current = current.parent;
256                 return ret;
257             }
258
259             @Override
260             public void remove() {
261                 throw new UnsupportedOperationException("Component removal not supported");
262             }
263         };
264     }
265
266     /**
267      * Returns the immediate parent LeafRefPath.
268      *
269      * @return Parent path, null if this LeafRefPath is already toplevel.
270      */
271     public LeafRefPath getParent() {
272         return parent;
273     }
274
275     /**
276      * Get the last component of this path.
277      *
278      * @return The last component of this path.
279      */
280     public final QNameWithPredicate getLastComponent() {
281         return qname;
282     }
283
284     /**
285      * Describes whether schema path is|isn't absolute.
286      *
287      * @return boolean value which is <code>true</code> if schema path is
288      *         absolute.
289      */
290     public abstract boolean isAbsolute();
291
292     @Override
293     public final int hashCode() {
294         return hash;
295     }
296
297     @Override
298     public boolean equals(final Object obj) {
299         if (this == obj) {
300             return true;
301         }
302         if (obj == null) {
303             return false;
304         }
305         if (getClass() != obj.getClass()) {
306             return false;
307         }
308         final LeafRefPath other = (LeafRefPath) obj;
309         return Objects.equals(qname, other.qname) && Objects.equals(parent, other.parent);
310     }
311
312     @Override
313     public String toString() {
314         StringBuilder sb = new StringBuilder();
315
316         sb.append(isAbsolute() ? "Absolute path:" : "Relative path:");
317
318         for (QNameWithPredicate qname : getPathFromRoot()) {
319             sb.append('/').append(qname);
320         }
321
322         return sb.toString();
323     }
324
325     // @Override
326     // public final String toString() {
327     // return addToStringAttributes(Objects.toStringHelper(this)).toString();
328     // }
329     //
330     // protected ToStringHelper addToStringAttributes(final ToStringHelper
331     // toStringHelper) {
332     // return toStringHelper.add("path", getPathFromRoot());
333     // }
334
335 }