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 java.util.Arrays;
16 import java.util.Iterator;
17 import java.util.List;
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;
25 * Represents unique path to the every schema node inside the schema node identifier
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, qname);
51 * A relative schema node identifier.
53 public static 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, qname);
69 @SuppressWarnings("rawtypes")
70 private static final AtomicReferenceFieldUpdater<SchemaNodeIdentifier, ImmutableList> LEGACYPATH_UPDATER =
71 AtomicReferenceFieldUpdater.newUpdater(SchemaNodeIdentifier.class, ImmutableList.class, "legacyPath");
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;
104 protected SchemaNodeIdentifier(final SchemaNodeIdentifier parent, final QName qname) {
105 this.parent = parent;
108 int h = Objects.hashCode(parent);
110 h = h * 31 + qname.hashCode();
116 private ImmutableList<QName> getLegacyPath() {
117 ImmutableList<QName> ret = legacyPath;
119 ret = ImmutableList.copyOf(getPathTowardsRoot()).reverse();
120 LEGACYPATH_UPDATER.lazySet(this, ret);
127 * Returns the complete path to schema node.
129 * @return list of <code>QName</code> instances which represents complete
130 * path to schema node
132 * @deprecated Use {@link #getPathFromRoot()} instead.
135 public List<QName> getPath() {
136 return getLegacyPath();
140 * Constructs new instance of this class with the concrete path.
143 * list of QName instances which specifies exact path to the
146 * boolean value which specifies if the path is absolute or
149 * @return A SchemaPath instance.
151 public static SchemaNodeIdentifier create(final Iterable<QName> path, final boolean absolute) {
152 final SchemaNodeIdentifier parent = absolute ? ROOT : SAME;
153 return parent.createChild(path);
157 * Constructs new instance of this class with the concrete path.
160 * boolean value which specifies if the path is absolute or
163 * one or more QName instances which specifies exact path to the
166 * @return A SchemaPath instance.
168 public static SchemaNodeIdentifier create(final boolean absolute, final QName... path) {
169 return create(Arrays.asList(path), absolute);
173 * Create a new instance.
175 * @param parent Parent schema node identifier
176 * @param qname next path element
177 * @return A new SchemaPath instance
179 protected abstract SchemaNodeIdentifier createInstance(SchemaNodeIdentifier parent, QName qname);
182 * Create a child path based on concatenation of this path and a relative path.
184 * @param relative Relative path
185 * @return A new child path
187 public SchemaNodeIdentifier createChild(final Iterable<QName> relative) {
188 if (Iterables.isEmpty(relative)) {
192 SchemaNodeIdentifier parentNode = this;
193 for (QName qname : relative) {
194 parentNode = parentNode.createInstance(parentNode, qname);
201 * Create a child path based on concatenation of this path and a relative path.
203 * @param relative Relative SchemaPath
204 * @return A new child path
206 public SchemaNodeIdentifier createChild(final SchemaNodeIdentifier relative) {
207 Preconditions.checkArgument(!relative.isAbsolute(), "Child creation requires relative path");
209 SchemaNodeIdentifier parentNode = this;
210 for (QName qname : relative.getPathFromRoot()) {
211 parentNode = parentNode.createInstance(parentNode, qname);
218 * Create a child path based on concatenation of this path and additional
221 * @param elements Relative SchemaPath elements
222 * @return A new child path
224 public SchemaNodeIdentifier createChild(final QName... elements) {
225 return createChild(Arrays.asList(elements));
229 * Returns the list of nodes which need to be traversed to get from the
230 * starting point (root for absolute SchemaPaths) to the node represented
233 * @return list of <code>qname</code> instances which represents
234 * path from the root to the schema node.
236 public Iterable<QName> getPathFromRoot() {
237 return getLegacyPath();
241 * Returns the list of nodes which need to be traversed to get from this
242 * node to the starting point (root for absolute SchemaPaths).
244 * @return list of <code>qname</code> instances which represents
245 * path from the schema node towards the root.
247 public Iterable<QName> getPathTowardsRoot() {
248 return new Iterable<QName>() {
250 public Iterator<QName> iterator() {
251 return new Iterator<QName>() {
252 private SchemaNodeIdentifier current = SchemaNodeIdentifier.this;
255 public boolean hasNext() {
256 return current.parent != null;
260 public QName next() {
261 if (current.parent != null) {
262 final QName ret = current.qname;
263 current = current.parent;
266 throw new NoSuchElementException("No more elements available");
271 public void remove() {
272 throw new UnsupportedOperationException("Component removal not supported");
280 * Returns the immediate parent SchemaPath.
282 * @return Parent path, null if this SchemaPath is already toplevel.
284 public SchemaNodeIdentifier getParent() {
289 * Get the last component of this path.
291 * @return The last component of this path.
293 public final QName getLastComponent() {
298 * Describes whether schema node identifier is|isn't absolute.
300 * @return boolean value which is <code>true</code> if schema path is
303 public abstract boolean isAbsolute();
306 public final int hashCode() {
311 public boolean equals(final Object obj) {
318 if (getClass() != obj.getClass()) {
321 final SchemaNodeIdentifier other = (SchemaNodeIdentifier) obj;
322 return Objects.equals(qname, other.qname) && Objects.equals(parent, other.parent);
326 public final String toString() {
327 return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
330 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
331 return toStringHelper.add("path", getPathFromRoot());