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 public Absolute createChild(final QName element) {
46 return new Absolute(this, requireNonNull(element));
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 public Relative createChild(final QName element) {
65 return new Relative(this, requireNonNull(element));
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 Absolute ROOT = new Absolute(null, null);
80 * Shared instance of the "same" relative schema node.
82 public static final Relative 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 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 child path based on concatenation of this path and a relative path.
168 * @param relative Relative path
169 * @return A new child path
171 public SchemaNodeIdentifier createChild(final Iterable<QName> relative) {
172 if (Iterables.isEmpty(relative)) {
176 SchemaNodeIdentifier parentNode = this;
177 for (QName qname : relative) {
178 parentNode = parentNode.createChild(qname);
185 * Create a child path based on concatenation of this path and a relative path.
187 * @param relative Relative SchemaPath
188 * @return A new child path
190 public SchemaNodeIdentifier createChild(final SchemaNodeIdentifier relative) {
191 checkArgument(!relative.isAbsolute(), "Child creation requires relative path");
192 return createChild(relative.getPathFromRoot());
196 * Create a child path based on concatenation of this path and an additional path element.
198 * @param element Next SchemaPath element
199 * @return A new child path
201 public abstract SchemaNodeIdentifier createChild(QName element);
204 * Create a child path based on concatenation of this path and additional
207 * @param elements Relative SchemaPath elements
208 * @return A new child path
210 public SchemaNodeIdentifier createChild(final QName... elements) {
211 return createChild(Arrays.asList(elements));
215 * Returns the list of nodes which need to be traversed to get from the
216 * starting point (root for absolute SchemaPaths) to the node represented
219 * @return list of <code>qname</code> instances which represents
220 * path from the root to the schema node.
222 public Iterable<QName> getPathFromRoot() {
223 return getLegacyPath();
227 * Returns the list of nodes which need to be traversed to get from this
228 * node to the starting point (root for absolute SchemaPaths).
230 * @return list of <code>qname</code> instances which represents
231 * path from the schema node towards the root.
233 public Iterable<QName> getPathTowardsRoot() {
234 return () -> new UnmodifiableIterator<QName>() {
235 private SchemaNodeIdentifier current = SchemaNodeIdentifier.this;
238 public boolean hasNext() {
239 return current.parent != null;
243 public QName next() {
244 if (current.parent != null) {
245 final QName ret = current.qname;
246 current = current.parent;
249 throw new NoSuchElementException("No more elements available");
256 * Returns the immediate parent SchemaPath.
258 * @return Parent path, null if this SchemaPath is already toplevel.
260 public SchemaNodeIdentifier getParent() {
265 * Get the last component of this path.
267 * @return The last component of this path.
269 public final QName getLastComponent() {
273 private SchemaPath createSchemaPath() {
274 final SchemaPath newPath;
275 if (parent == null) {
276 final SchemaPath parentPath = isAbsolute() ? SchemaPath.ROOT : SchemaPath.SAME;
277 newPath = qname == null ? parentPath : parentPath.createChild(qname);
279 newPath = parent.asSchemaPath().createChild(qname);
282 return SCHEMAPATH_UPDATER.compareAndSet(this, null, newPath) ? newPath : schemaPath;
286 * Create the {@link SchemaPath} equivalent of this identifier.
288 * @return SchemaPath equivalent.
290 public final SchemaPath asSchemaPath() {
291 final SchemaPath ret = schemaPath;
292 return ret != null ? ret : createSchemaPath();
296 * Describes whether schema node identifier is|isn't absolute.
298 * @return boolean value which is <code>true</code> if schema path is
301 public abstract boolean isAbsolute();
304 public final int hashCode() {
309 public final boolean equals(final Object obj) {
316 if (getClass() != obj.getClass()) {
319 final SchemaNodeIdentifier other = (SchemaNodeIdentifier) obj;
320 return Objects.equals(qname, other.qname) && Objects.equals(parent, other.parent);
324 public final String toString() {
325 return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
328 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
329 return toStringHelper.add("path", getPathFromRoot());