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