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