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