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