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.Iterator;
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 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
26 * Represents unique path to the every schema node inside the schema node identifier namespace.
28 public abstract class SchemaNodeIdentifier implements Immutable {
31 * An absolute schema node identifier.
33 public static final class Absolute extends SchemaNodeIdentifier {
34 private Absolute(final SchemaNodeIdentifier parent, final QName qname) {
39 public boolean isAbsolute() {
44 protected SchemaNodeIdentifier createInstance(final SchemaNodeIdentifier parent, final QName qname) {
45 return new Absolute(parent, Preconditions.checkNotNull(qname));
50 * A relative schema node identifier.
52 public static final class Relative extends SchemaNodeIdentifier {
53 private Relative(final SchemaNodeIdentifier parent, final QName qname) {
58 public boolean isAbsolute() {
63 protected SchemaNodeIdentifier createInstance(final SchemaNodeIdentifier parent, final QName qname) {
64 return new Relative(parent, Preconditions.checkNotNull(qname));
68 @SuppressWarnings("rawtypes")
69 private static final AtomicReferenceFieldUpdater<SchemaNodeIdentifier, ImmutableList> LEGACYPATH_UPDATER =
70 AtomicReferenceFieldUpdater.newUpdater(SchemaNodeIdentifier.class, ImmutableList.class, "legacyPath");
71 private static final AtomicReferenceFieldUpdater<SchemaNodeIdentifier, SchemaPath> SCHEMAPATH_UPDATER =
72 AtomicReferenceFieldUpdater.newUpdater(SchemaNodeIdentifier.class, SchemaPath.class, "schemaPath");
74 * Shared instance of the conceptual root schema node.
76 public static final SchemaNodeIdentifier ROOT = new Absolute(null, null);
79 * Shared instance of the "same" relative schema node.
81 public static final SchemaNodeIdentifier SAME = new Relative(null, null);
86 private final SchemaNodeIdentifier parent;
91 private final QName qname;
94 * Cached hash code. We can use this since we are immutable.
96 private final int hash;
99 * Cached legacy path, filled-in when {@link #getPath()} or {@link #getPathTowardsRoot()}
102 private volatile ImmutableList<QName> legacyPath;
107 private volatile SchemaPath schemaPath;
109 protected SchemaNodeIdentifier(final SchemaNodeIdentifier parent, final QName qname) {
110 this.parent = parent;
113 int h = Objects.hashCode(parent);
115 h = h * 31 + qname.hashCode();
121 private ImmutableList<QName> getLegacyPath() {
122 ImmutableList<QName> ret = legacyPath;
124 ret = ImmutableList.copyOf(getPathTowardsRoot()).reverse();
125 LEGACYPATH_UPDATER.lazySet(this, ret);
132 * Constructs new instance of this class with the concrete path.
135 * list of QName instances which specifies exact path to the
138 * boolean value which specifies if the path is absolute or
141 * @return A SchemaNodeIdentifier instance.
143 public static SchemaNodeIdentifier create(final Iterable<QName> path, final boolean absolute) {
144 final SchemaNodeIdentifier parent = absolute ? ROOT : SAME;
145 return parent.createChild(path);
149 * Constructs new instance of this class with the concrete path.
152 * boolean value which specifies if the path is absolute or
155 * one or more QName instances which specifies exact path to the
158 * @return A SchemaPath instance.
160 public static SchemaNodeIdentifier create(final boolean absolute, final QName... path) {
161 return create(Arrays.asList(path), absolute);
165 * Create a new instance.
167 * @param parent Parent schema node identifier
168 * @param qname next path element
169 * @return A new SchemaPath instance
171 protected abstract SchemaNodeIdentifier createInstance(SchemaNodeIdentifier parent, QName qname);
174 * Create a child path based on concatenation of this path and a relative path.
176 * @param relative Relative path
177 * @return A new child path
179 public SchemaNodeIdentifier createChild(final Iterable<QName> relative) {
180 if (Iterables.isEmpty(relative)) {
184 SchemaNodeIdentifier parentNode = this;
185 for (QName qname : relative) {
186 parentNode = parentNode.createInstance(parentNode, qname);
193 * Create a child path based on concatenation of this path and a relative path.
195 * @param relative Relative SchemaPath
196 * @return A new child path
198 public SchemaNodeIdentifier createChild(final SchemaNodeIdentifier relative) {
199 Preconditions.checkArgument(!relative.isAbsolute(), "Child creation requires relative path");
201 SchemaNodeIdentifier parentNode = this;
202 for (QName qname : relative.getPathFromRoot()) {
203 parentNode = parentNode.createInstance(parentNode, qname);
210 * Create a child path based on concatenation of this path and additional
213 * @param elements Relative SchemaPath elements
214 * @return A new child path
216 public SchemaNodeIdentifier createChild(final QName... elements) {
217 return createChild(Arrays.asList(elements));
221 * Returns the list of nodes which need to be traversed to get from the
222 * starting point (root for absolute SchemaPaths) to the node represented
225 * @return list of <code>qname</code> instances which represents
226 * path from the root to the schema node.
228 public Iterable<QName> getPathFromRoot() {
229 return getLegacyPath();
233 * Returns the list of nodes which need to be traversed to get from this
234 * node to the starting point (root for absolute SchemaPaths).
236 * @return list of <code>qname</code> instances which represents
237 * path from the schema node towards the root.
239 public Iterable<QName> getPathTowardsRoot() {
240 return new Iterable<QName>() {
242 public Iterator<QName> iterator() {
243 return new UnmodifiableIterator<QName>() {
244 private SchemaNodeIdentifier current = SchemaNodeIdentifier.this;
247 public boolean hasNext() {
248 return current.parent != null;
252 public QName next() {
253 if (current.parent != null) {
254 final QName ret = current.qname;
255 current = current.parent;
258 throw new NoSuchElementException("No more elements available");
267 * Returns the immediate parent SchemaPath.
269 * @return Parent path, null if this SchemaPath is already toplevel.
271 public SchemaNodeIdentifier getParent() {
276 * Get the last component of this path.
278 * @return The last component of this path.
280 public final QName getLastComponent() {
284 private SchemaPath createSchemaPath() {
285 final SchemaPath newPath;
286 if (parent == null) {
287 final SchemaPath parentPath = isAbsolute() ? SchemaPath.ROOT : SchemaPath.SAME;
288 newPath = qname == null ? parentPath : parentPath.createChild(qname);
290 newPath = parent.asSchemaPath().createChild(qname);
293 return SCHEMAPATH_UPDATER.compareAndSet(this, null, newPath) ? newPath : schemaPath;
297 * Create the {@link SchemaPath} equivalent of this identifier.
299 * @return SchemaPath equivalent.
301 public final SchemaPath asSchemaPath() {
302 final SchemaPath ret = schemaPath;
303 return ret != null ? ret : createSchemaPath();
307 * Describes whether schema node identifier is|isn't absolute.
309 * @return boolean value which is <code>true</code> if schema path is
312 public abstract boolean isAbsolute();
315 public final int hashCode() {
320 public boolean equals(final Object obj) {
327 if (getClass() != obj.getClass()) {
330 final SchemaNodeIdentifier other = (SchemaNodeIdentifier) obj;
331 return Objects.equals(qname, other.qname) && Objects.equals(parent, other.parent);
335 public final String toString() {
336 return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
339 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
340 return toStringHelper.add("path", getPathFromRoot());