Use Objects.equals() in yang-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 java.util.Arrays;
16 import java.util.Iterator;
17 import java.util.List;
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
24 /**
25  * Represents unique path to the every schema node inside the schema node identifier
26  * namespace.
27  *
28  */
29 public abstract class SchemaNodeIdentifier implements Immutable {
30
31     /**
32      * An absolute schema node identifier.
33      */
34     public static final class Absolute extends SchemaNodeIdentifier {
35         private Absolute(final SchemaNodeIdentifier 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 SchemaNodeIdentifier createInstance(final SchemaNodeIdentifier parent, final QName qname) {
46             return new Absolute(parent, qname);
47         }
48     }
49
50     /**
51      * A relative schema node identifier.
52      */
53     public static class Relative extends SchemaNodeIdentifier {
54         private Relative(final SchemaNodeIdentifier 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 SchemaNodeIdentifier createInstance(final SchemaNodeIdentifier parent, final QName qname) {
65             return new Relative(parent, qname);
66         }
67     }
68
69     @SuppressWarnings("rawtypes")
70     private static final AtomicReferenceFieldUpdater<SchemaNodeIdentifier, ImmutableList> LEGACYPATH_UPDATER =
71             AtomicReferenceFieldUpdater.newUpdater(SchemaNodeIdentifier.class, ImmutableList.class, "legacyPath");
72
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     protected SchemaNodeIdentifier(final SchemaNodeIdentifier parent, final QName qname) {
105         this.parent = parent;
106         this.qname = qname;
107
108         int h = Objects.hashCode(parent);
109         if (qname != null) {
110             h = h * 31 + qname.hashCode();
111         }
112
113         hash = h;
114     }
115
116     private ImmutableList<QName> getLegacyPath() {
117         ImmutableList<QName> ret = legacyPath;
118         if (ret == null) {
119             ret = ImmutableList.copyOf(getPathTowardsRoot()).reverse();
120             LEGACYPATH_UPDATER.lazySet(this, ret);
121         }
122
123         return ret;
124     }
125
126     /**
127      * Returns the complete path to schema node.
128      *
129      * @return list of <code>QName</code> instances which represents complete
130      *         path to schema node
131      *
132      * @deprecated Use {@link #getPathFromRoot()} instead.
133      */
134     @Deprecated
135     public List<QName> getPath() {
136         return getLegacyPath();
137     }
138
139     /**
140      * Constructs new instance of this class with the concrete path.
141      *
142      * @param path
143      *            list of QName instances which specifies exact path to the
144      *            module node
145      * @param absolute
146      *            boolean value which specifies if the path is absolute or
147      *            relative
148      *
149      * @return A SchemaPath instance.
150      */
151     public static SchemaNodeIdentifier create(final Iterable<QName> path, final boolean absolute) {
152         final SchemaNodeIdentifier parent = absolute ? ROOT : SAME;
153         return parent.createChild(path);
154     }
155
156     /**
157      * Constructs new instance of this class with the concrete path.
158      *
159      * @param absolute
160      *            boolean value which specifies if the path is absolute or
161      *            relative
162      * @param path
163      *            one or more QName instances which specifies exact path to the
164      *            module node
165      *
166      * @return A SchemaPath instance.
167      */
168     public static SchemaNodeIdentifier create(final boolean absolute, final QName... path) {
169         return create(Arrays.asList(path), absolute);
170     }
171
172     /**
173      * Create a new instance.
174      *
175      * @param parent Parent schema node identifier
176      * @param qname next path element
177      * @return A new SchemaPath instance
178      */
179     protected abstract SchemaNodeIdentifier createInstance(SchemaNodeIdentifier parent, QName qname);
180
181     /**
182      * Create a child path based on concatenation of this path and a relative path.
183      *
184      * @param relative Relative path
185      * @return A new child path
186      */
187     public SchemaNodeIdentifier createChild(final Iterable<QName> relative) {
188         if (Iterables.isEmpty(relative)) {
189             return this;
190         }
191
192         SchemaNodeIdentifier parentNode = this;
193         for (QName qname : relative) {
194             parentNode = parentNode.createInstance(parentNode, qname);
195         }
196
197         return parentNode;
198     }
199
200     /**
201      * Create a child path based on concatenation of this path and a relative path.
202      *
203      * @param relative Relative SchemaPath
204      * @return A new child path
205      */
206     public SchemaNodeIdentifier createChild(final SchemaNodeIdentifier relative) {
207         Preconditions.checkArgument(!relative.isAbsolute(), "Child creation requires relative path");
208
209         SchemaNodeIdentifier parentNode = this;
210         for (QName qname : relative.getPathFromRoot()) {
211             parentNode = parentNode.createInstance(parentNode, qname);
212         }
213
214         return parentNode;
215     }
216
217     /**
218      * Create a child path based on concatenation of this path and additional
219      * path elements.
220      *
221      * @param elements Relative SchemaPath elements
222      * @return A new child path
223      */
224     public SchemaNodeIdentifier createChild(final QName... elements) {
225         return createChild(Arrays.asList(elements));
226     }
227
228     /**
229      * Returns the list of nodes which need to be traversed to get from the
230      * starting point (root for absolute SchemaPaths) to the node represented
231      * by this object.
232      *
233      * @return list of <code>qname</code> instances which represents
234      *         path from the root to the schema node.
235      */
236     public Iterable<QName> getPathFromRoot() {
237         return getLegacyPath();
238     }
239
240     /**
241      * Returns the list of nodes which need to be traversed to get from this
242      * node to the starting point (root for absolute SchemaPaths).
243      *
244      * @return list of <code>qname</code> instances which represents
245      *         path from the schema node towards the root.
246      */
247     public Iterable<QName> getPathTowardsRoot() {
248         return new Iterable<QName>() {
249             @Override
250             public Iterator<QName> iterator() {
251                 return new Iterator<QName>() {
252                     private SchemaNodeIdentifier current = SchemaNodeIdentifier.this;
253
254                     @Override
255                     public boolean hasNext() {
256                         return current.parent != null;
257                     }
258
259                     @Override
260                     public QName next() {
261                         if (current.parent != null) {
262                             final QName ret = current.qname;
263                             current = current.parent;
264                             return ret;
265                         } else {
266                             throw new NoSuchElementException("No more elements available");
267                         }
268                     }
269
270                     @Override
271                     public void remove() {
272                         throw new UnsupportedOperationException("Component removal not supported");
273                     }
274                 };
275             }
276         };
277     }
278
279     /**
280      * Returns the immediate parent SchemaPath.
281      *
282      * @return Parent path, null if this SchemaPath is already toplevel.
283      */
284     public SchemaNodeIdentifier getParent() {
285         return parent;
286     }
287
288     /**
289      * Get the last component of this path.
290      *
291      * @return The last component of this path.
292      */
293     public final QName getLastComponent() {
294         return qname;
295     }
296
297     /**
298      * Describes whether schema node identifier is|isn't absolute.
299      *
300      * @return boolean value which is <code>true</code> if schema path is
301      *         absolute.
302      */
303     public abstract boolean isAbsolute();
304
305     @Override
306     public final int hashCode() {
307         return hash;
308     }
309
310     @Override
311     public boolean equals(final Object obj) {
312         if (this == obj) {
313             return true;
314         }
315         if (obj == null) {
316             return false;
317         }
318         if (getClass() != obj.getClass()) {
319             return false;
320         }
321         final SchemaNodeIdentifier other = (SchemaNodeIdentifier) obj;
322         return Objects.equals(qname, other.qname) && Objects.equals(parent, other.parent);
323     }
324
325     @Override
326     public final String toString() {
327         return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
328     }
329
330     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
331         return toStringHelper.add("path", getPathFromRoot());
332     }
333 }