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 static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
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;
27 * Represents unique path to the every schema node inside the schema node identifier namespace.
29 public abstract class SchemaNodeIdentifier implements Immutable {
32 * An absolute schema node identifier.
34 public static final class Absolute extends SchemaNodeIdentifier {
35 private Absolute(final SchemaNodeIdentifier parent, final QName qname) {
40 public boolean isAbsolute() {
45 protected SchemaNodeIdentifier createInstance(final SchemaNodeIdentifier parent, final QName qname) {
46 return new Absolute(parent, requireNonNull(qname));
51 * A relative schema node identifier.
53 public static final class Relative extends SchemaNodeIdentifier {
54 private Relative(final SchemaNodeIdentifier parent, final QName qname) {
59 public boolean isAbsolute() {
64 protected SchemaNodeIdentifier createInstance(final SchemaNodeIdentifier parent, final QName qname) {
65 return new Relative(parent, requireNonNull(qname));
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");
75 * Shared instance of the conceptual root schema node.
77 public static final SchemaNodeIdentifier ROOT = new Absolute(null, null);
80 * Shared instance of the "same" relative schema node.
82 public static final SchemaNodeIdentifier SAME = new Relative(null, null);
87 private final SchemaNodeIdentifier parent;
92 private final QName qname;
95 * Cached hash code. We can use this since we are immutable.
97 private final int hash;
100 * Cached legacy path, filled-in when {@link #getPath()} or {@link #getPathTowardsRoot()}
103 private volatile ImmutableList<QName> legacyPath;
108 private volatile SchemaPath schemaPath;
110 protected SchemaNodeIdentifier(final SchemaNodeIdentifier parent, final QName qname) {
111 this.parent = parent;
114 int tmp = Objects.hashCode(parent);
116 tmp = tmp * 31 + qname.hashCode();
122 private ImmutableList<QName> getLegacyPath() {
123 ImmutableList<QName> ret = legacyPath;
125 ret = ImmutableList.copyOf(getPathTowardsRoot()).reverse();
126 LEGACYPATH_UPDATER.lazySet(this, ret);
133 * Constructs new instance of this class with the concrete path.
136 * list of QName instances which specifies exact path to the
139 * boolean value which specifies if the path is absolute or
142 * @return A SchemaNodeIdentifier instance.
144 public static SchemaNodeIdentifier create(final Iterable<QName> path, final boolean absolute) {
145 final SchemaNodeIdentifier parent = absolute ? ROOT : SAME;
146 return parent.createChild(path);
150 * Constructs new instance of this class with the concrete path.
153 * boolean value which specifies if the path is absolute or
156 * one or more QName instances which specifies exact path to the
159 * @return A SchemaPath instance.
161 public static SchemaNodeIdentifier create(final boolean absolute, final QName... path) {
162 return create(Arrays.asList(path), absolute);
166 * Create a new instance.
168 * @param parent Parent schema node identifier
169 * @param qname next path element
170 * @return A new SchemaPath instance
172 protected abstract SchemaNodeIdentifier createInstance(SchemaNodeIdentifier parent, QName qname);
175 * Create a child path based on concatenation of this path and a relative path.
177 * @param relative Relative path
178 * @return A new child path
180 public SchemaNodeIdentifier createChild(final Iterable<QName> relative) {
181 if (Iterables.isEmpty(relative)) {
185 SchemaNodeIdentifier parentNode = this;
186 for (QName qname : relative) {
187 parentNode = parentNode.createInstance(parentNode, qname);
194 * Create a child path based on concatenation of this path and a relative path.
196 * @param relative Relative SchemaPath
197 * @return A new child path
199 public SchemaNodeIdentifier createChild(final SchemaNodeIdentifier relative) {
200 checkArgument(!relative.isAbsolute(), "Child creation requires relative path");
202 SchemaNodeIdentifier parentNode = this;
203 for (QName qname : relative.getPathFromRoot()) {
204 parentNode = parentNode.createInstance(parentNode, qname);
211 * Create a child path based on concatenation of this path and additional
214 * @param elements Relative SchemaPath elements
215 * @return A new child path
217 public SchemaNodeIdentifier createChild(final QName... elements) {
218 return createChild(Arrays.asList(elements));
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
226 * @return list of <code>qname</code> instances which represents
227 * path from the root to the schema node.
229 public Iterable<QName> getPathFromRoot() {
230 return getLegacyPath();
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).
237 * @return list of <code>qname</code> instances which represents
238 * path from the schema node towards the root.
240 public Iterable<QName> getPathTowardsRoot() {
241 return () -> new UnmodifiableIterator<QName>() {
242 private SchemaNodeIdentifier current = SchemaNodeIdentifier.this;
245 public boolean hasNext() {
246 return current.parent != null;
250 public QName next() {
251 if (current.parent != null) {
252 final QName ret = current.qname;
253 current = current.parent;
256 throw new NoSuchElementException("No more elements available");
263 * Returns the immediate parent SchemaPath.
265 * @return Parent path, null if this SchemaPath is already toplevel.
267 public SchemaNodeIdentifier getParent() {
272 * Get the last component of this path.
274 * @return The last component of this path.
276 public final QName getLastComponent() {
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);
286 newPath = parent.asSchemaPath().createChild(qname);
289 return SCHEMAPATH_UPDATER.compareAndSet(this, null, newPath) ? newPath : schemaPath;
293 * Create the {@link SchemaPath} equivalent of this identifier.
295 * @return SchemaPath equivalent.
297 public final SchemaPath asSchemaPath() {
298 final SchemaPath ret = schemaPath;
299 return ret != null ? ret : createSchemaPath();
303 * Describes whether schema node identifier is|isn't absolute.
305 * @return boolean value which is <code>true</code> if schema path is
308 public abstract boolean isAbsolute();
311 public final int hashCode() {
316 public boolean equals(final Object obj) {
323 if (getClass() != obj.getClass()) {
326 final SchemaNodeIdentifier other = (SchemaNodeIdentifier) obj;
327 return Objects.equals(qname, other.qname) && Objects.equals(parent, other.parent);
331 public final String toString() {
332 return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
335 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
336 return toStringHelper.add("path", getPathFromRoot());