Make SchemaNodeIdentifier.Relative final
[yangtools.git] / yang / yang-model-api / src / main / java / org / opendaylight / yangtools / yang / model / api / stmt / SchemaNodeIdentifier.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.model.api.stmt;
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.UnmodifiableIterator;
16 import java.util.Arrays;
17 import java.util.Iterator;
18 import java.util.NoSuchElementException;
19 import java.util.Objects;
20 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
21 import org.opendaylight.yangtools.concepts.Immutable;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
24
25 /**
26  * Represents unique path to the every schema node inside the schema node identifier namespace.
27  */
28 public abstract class SchemaNodeIdentifier implements Immutable {
29
30     /**
31      * An absolute schema node identifier.
32      */
33     public static final class Absolute extends SchemaNodeIdentifier {
34         private Absolute(final SchemaNodeIdentifier parent, final QName qname) {
35             super(parent, qname);
36         }
37
38         @Override
39         public boolean isAbsolute() {
40             return true;
41         }
42
43         @Override
44         protected SchemaNodeIdentifier createInstance(final SchemaNodeIdentifier parent, final QName qname) {
45             return new Absolute(parent, Preconditions.checkNotNull(qname));
46         }
47     }
48
49     /**
50      * A relative schema node identifier.
51      */
52     public static final class Relative extends SchemaNodeIdentifier {
53         private Relative(final SchemaNodeIdentifier parent, final QName qname) {
54             super(parent, qname);
55         }
56
57         @Override
58         public boolean isAbsolute() {
59             return false;
60         }
61
62         @Override
63         protected SchemaNodeIdentifier createInstance(final SchemaNodeIdentifier parent, final QName qname) {
64             return new Relative(parent, Preconditions.checkNotNull(qname));
65         }
66     }
67
68     @SuppressWarnings("rawtypes")
69     private static final AtomicReferenceFieldUpdater<SchemaNodeIdentifier, ImmutableList> LEGACYPATH_UPDATER =
70             AtomicReferenceFieldUpdater.newUpdater(SchemaNodeIdentifier.class, ImmutableList.class, "legacyPath");
71     private static final AtomicReferenceFieldUpdater<SchemaNodeIdentifier, SchemaPath> SCHEMAPATH_UPDATER =
72             AtomicReferenceFieldUpdater.newUpdater(SchemaNodeIdentifier.class, SchemaPath.class, "schemaPath");
73     /**
74      * Shared instance of the conceptual root schema node.
75      */
76     public static final SchemaNodeIdentifier ROOT = new Absolute(null, null);
77
78     /**
79      * Shared instance of the "same" relative schema node.
80      */
81     public static final SchemaNodeIdentifier SAME = new Relative(null, null);
82
83     /**
84      * Parent path.
85      */
86     private final SchemaNodeIdentifier 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     /**
105      * Cached SchemaPath.
106      */
107     private volatile SchemaPath schemaPath;
108
109     protected SchemaNodeIdentifier(final SchemaNodeIdentifier parent, final QName 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     private ImmutableList<QName> getLegacyPath() {
122         ImmutableList<QName> ret = legacyPath;
123         if (ret == null) {
124             ret = ImmutableList.copyOf(getPathTowardsRoot()).reverse();
125             LEGACYPATH_UPDATER.lazySet(this, ret);
126         }
127
128         return ret;
129     }
130
131     /**
132      * Constructs new instance of this class with the concrete path.
133      *
134      * @param path
135      *            list of QName instances which specifies exact path to the
136      *            module node
137      * @param absolute
138      *            boolean value which specifies if the path is absolute or
139      *            relative
140      *
141      * @return A SchemaPath instance.
142      */
143     public static SchemaNodeIdentifier create(final Iterable<QName> path, final boolean absolute) {
144         final SchemaNodeIdentifier parent = absolute ? ROOT : SAME;
145         return parent.createChild(path);
146     }
147
148     /**
149      * Constructs new instance of this class with the concrete path.
150      *
151      * @param absolute
152      *            boolean value which specifies if the path is absolute or
153      *            relative
154      * @param path
155      *            one or more QName instances which specifies exact path to the
156      *            module node
157      *
158      * @return A SchemaPath instance.
159      */
160     public static SchemaNodeIdentifier create(final boolean absolute, final QName... path) {
161         return create(Arrays.asList(path), absolute);
162     }
163
164     /**
165      * Create a new instance.
166      *
167      * @param parent Parent schema node identifier
168      * @param qname next path element
169      * @return A new SchemaPath instance
170      */
171     protected abstract SchemaNodeIdentifier createInstance(SchemaNodeIdentifier parent, QName qname);
172
173     /**
174      * Create a child path based on concatenation of this path and a relative path.
175      *
176      * @param relative Relative path
177      * @return A new child path
178      */
179     public SchemaNodeIdentifier createChild(final Iterable<QName> relative) {
180         if (Iterables.isEmpty(relative)) {
181             return this;
182         }
183
184         SchemaNodeIdentifier parentNode = this;
185         for (QName qname : relative) {
186             parentNode = parentNode.createInstance(parentNode, qname);
187         }
188
189         return parentNode;
190     }
191
192     /**
193      * Create a child path based on concatenation of this path and a relative path.
194      *
195      * @param relative Relative SchemaPath
196      * @return A new child path
197      */
198     public SchemaNodeIdentifier createChild(final SchemaNodeIdentifier relative) {
199         Preconditions.checkArgument(!relative.isAbsolute(), "Child creation requires relative path");
200
201         SchemaNodeIdentifier parentNode = this;
202         for (QName qname : relative.getPathFromRoot()) {
203             parentNode = parentNode.createInstance(parentNode, qname);
204         }
205
206         return parentNode;
207     }
208
209     /**
210      * Create a child path based on concatenation of this path and additional
211      * path elements.
212      *
213      * @param elements Relative SchemaPath elements
214      * @return A new child path
215      */
216     public SchemaNodeIdentifier createChild(final QName... 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 SchemaPaths) to the node represented
223      * by this object.
224      *
225      * @return list of <code>qname</code> instances which represents
226      *         path from the root to the schema node.
227      */
228     public Iterable<QName> 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 SchemaPaths).
235      *
236      * @return list of <code>qname</code> instances which represents
237      *         path from the schema node towards the root.
238      */
239     public Iterable<QName> getPathTowardsRoot() {
240         return new Iterable<QName>() {
241             @Override
242             public Iterator<QName> iterator() {
243                 return new UnmodifiableIterator<QName>() {
244                     private SchemaNodeIdentifier current = SchemaNodeIdentifier.this;
245
246                     @Override
247                     public boolean hasNext() {
248                         return current.parent != null;
249                     }
250
251                     @Override
252                     public QName next() {
253                         if (current.parent != null) {
254                             final QName ret = current.qname;
255                             current = current.parent;
256                             return ret;
257                         } else {
258                             throw new NoSuchElementException("No more elements available");
259                         }
260                     }
261                 };
262             }
263         };
264     }
265
266     /**
267      * Returns the immediate parent SchemaPath.
268      *
269      * @return Parent path, null if this SchemaPath is already toplevel.
270      */
271     public SchemaNodeIdentifier 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 QName getLastComponent() {
281         return qname;
282     }
283
284     private SchemaPath createSchemaPath() {
285         final SchemaPath newPath;
286         if (parent == null) {
287             final SchemaPath parentPath = isAbsolute() ? SchemaPath.ROOT : SchemaPath.SAME;
288             newPath = qname == null ? parentPath : parentPath.createChild(qname);
289         } else {
290             newPath = parent.asSchemaPath().createChild(qname);
291         }
292
293         return SCHEMAPATH_UPDATER.compareAndSet(this, null, newPath) ? newPath : schemaPath;
294     }
295
296     /**
297      * Create the {@link SchemaPath} equivalent of this identifier.
298      *
299      * @return SchemaPath equivalent.
300      */
301     public final SchemaPath asSchemaPath() {
302         final SchemaPath ret = schemaPath;
303         return ret != null ? ret : createSchemaPath();
304     }
305
306     /**
307      * Describes whether schema node identifier is|isn't absolute.
308      *
309      * @return boolean value which is <code>true</code> if schema path is
310      *         absolute.
311      */
312     public abstract boolean isAbsolute();
313
314     @Override
315     public final int hashCode() {
316         return hash;
317     }
318
319     @Override
320     public boolean equals(final Object obj) {
321         if (this == obj) {
322             return true;
323         }
324         if (obj == null) {
325             return false;
326         }
327         if (getClass() != obj.getClass()) {
328             return false;
329         }
330         final SchemaNodeIdentifier other = (SchemaNodeIdentifier) obj;
331         return Objects.equals(qname, other.qname) && Objects.equals(parent, other.parent);
332     }
333
334     @Override
335     public final String toString() {
336         return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
337     }
338
339     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
340         return toStringHelper.add("path", getPathFromRoot());
341     }
342 }