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