2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.model.api.stmt;
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;
25 * Represents unique path to the every schema node inside the schema node identifier namespace.
27 public abstract class SchemaNodeIdentifier implements Immutable {
30 * An absolute schema node identifier.
32 public static final class Absolute extends SchemaNodeIdentifier {
33 private Absolute(final SchemaNodeIdentifier parent, final QName qname) {
38 public boolean isAbsolute() {
43 protected SchemaNodeIdentifier createInstance(final SchemaNodeIdentifier parent, final QName qname) {
44 return new Absolute(parent, Preconditions.checkNotNull(qname));
49 * A relative schema node identifier.
51 public static final class Relative extends SchemaNodeIdentifier {
52 private Relative(final SchemaNodeIdentifier parent, final QName qname) {
57 public boolean isAbsolute() {
62 protected SchemaNodeIdentifier createInstance(final SchemaNodeIdentifier parent, final QName qname) {
63 return new Relative(parent, Preconditions.checkNotNull(qname));
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");
73 * Shared instance of the conceptual root schema node.
75 public static final SchemaNodeIdentifier ROOT = new Absolute(null, null);
78 * Shared instance of the "same" relative schema node.
80 public static final SchemaNodeIdentifier SAME = new Relative(null, null);
85 private final SchemaNodeIdentifier parent;
90 private final QName qname;
93 * Cached hash code. We can use this since we are immutable.
95 private final int hash;
98 * Cached legacy path, filled-in when {@link #getPath()} or {@link #getPathTowardsRoot()}
101 private volatile ImmutableList<QName> legacyPath;
106 private volatile SchemaPath schemaPath;
108 protected SchemaNodeIdentifier(final SchemaNodeIdentifier parent, final QName qname) {
109 this.parent = parent;
112 int tmp = Objects.hashCode(parent);
114 tmp = tmp * 31 + qname.hashCode();
120 private ImmutableList<QName> getLegacyPath() {
121 ImmutableList<QName> ret = legacyPath;
123 ret = ImmutableList.copyOf(getPathTowardsRoot()).reverse();
124 LEGACYPATH_UPDATER.lazySet(this, ret);
131 * Constructs new instance of this class with the concrete path.
134 * list of QName instances which specifies exact path to the
137 * boolean value which specifies if the path is absolute or
140 * @return A SchemaNodeIdentifier instance.
142 public static SchemaNodeIdentifier create(final Iterable<QName> path, final boolean absolute) {
143 final SchemaNodeIdentifier parent = absolute ? ROOT : SAME;
144 return parent.createChild(path);
148 * Constructs new instance of this class with the concrete path.
151 * boolean value which specifies if the path is absolute or
154 * one or more QName instances which specifies exact path to the
157 * @return A SchemaPath instance.
159 public static SchemaNodeIdentifier create(final boolean absolute, final QName... path) {
160 return create(Arrays.asList(path), absolute);
164 * Create a new instance.
166 * @param parent Parent schema node identifier
167 * @param qname next path element
168 * @return A new SchemaPath instance
170 protected abstract SchemaNodeIdentifier createInstance(SchemaNodeIdentifier parent, QName qname);
173 * Create a child path based on concatenation of this path and a relative path.
175 * @param relative Relative path
176 * @return A new child path
178 public SchemaNodeIdentifier createChild(final Iterable<QName> relative) {
179 if (Iterables.isEmpty(relative)) {
183 SchemaNodeIdentifier parentNode = this;
184 for (QName qname : relative) {
185 parentNode = parentNode.createInstance(parentNode, qname);
192 * Create a child path based on concatenation of this path and a relative path.
194 * @param relative Relative SchemaPath
195 * @return A new child path
197 public SchemaNodeIdentifier createChild(final SchemaNodeIdentifier relative) {
198 Preconditions.checkArgument(!relative.isAbsolute(), "Child creation requires relative path");
200 SchemaNodeIdentifier parentNode = this;
201 for (QName qname : relative.getPathFromRoot()) {
202 parentNode = parentNode.createInstance(parentNode, qname);
209 * Create a child path based on concatenation of this path and additional
212 * @param elements Relative SchemaPath elements
213 * @return A new child path
215 public SchemaNodeIdentifier createChild(final QName... elements) {
216 return createChild(Arrays.asList(elements));
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
224 * @return list of <code>qname</code> instances which represents
225 * path from the root to the schema node.
227 public Iterable<QName> getPathFromRoot() {
228 return getLegacyPath();
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).
235 * @return list of <code>qname</code> instances which represents
236 * path from the schema node towards the root.
238 public Iterable<QName> getPathTowardsRoot() {
239 return () -> new UnmodifiableIterator<QName>() {
240 private SchemaNodeIdentifier current = SchemaNodeIdentifier.this;
243 public boolean hasNext() {
244 return current.parent != null;
248 public QName next() {
249 if (current.parent != null) {
250 final QName ret = current.qname;
251 current = current.parent;
254 throw new NoSuchElementException("No more elements available");
261 * Returns the immediate parent SchemaPath.
263 * @return Parent path, null if this SchemaPath is already toplevel.
265 public SchemaNodeIdentifier getParent() {
270 * Get the last component of this path.
272 * @return The last component of this path.
274 public final QName getLastComponent() {
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);
284 newPath = parent.asSchemaPath().createChild(qname);
287 return SCHEMAPATH_UPDATER.compareAndSet(this, null, newPath) ? newPath : schemaPath;
291 * Create the {@link SchemaPath} equivalent of this identifier.
293 * @return SchemaPath equivalent.
295 public final SchemaPath asSchemaPath() {
296 final SchemaPath ret = schemaPath;
297 return ret != null ? ret : createSchemaPath();
301 * Describes whether schema node identifier is|isn't absolute.
303 * @return boolean value which is <code>true</code> if schema path is
306 public abstract boolean isAbsolute();
309 public final int hashCode() {
314 public boolean equals(final Object obj) {
321 if (getClass() != obj.getClass()) {
324 final SchemaNodeIdentifier other = (SchemaNodeIdentifier) obj;
325 return Objects.equals(qname, other.qname) && Objects.equals(parent, other.parent);
329 public final String toString() {
330 return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
333 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
334 return toStringHelper.add("path", getPathFromRoot());