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.Objects;
11 import com.google.common.base.Objects.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.concurrent.atomic.AtomicReferenceFieldUpdater;
20 import org.opendaylight.yangtools.concepts.Immutable;
21 import org.opendaylight.yangtools.yang.common.QName;
24 * Represents unique path to the every schema node inside the schema node identifier
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, qname);
50 * A relative schema node identifier.
52 public static 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, qname);
68 @SuppressWarnings("rawtypes")
69 private static final AtomicReferenceFieldUpdater<SchemaNodeIdentifier, ImmutableList> LEGACYPATH_UPDATER =
70 AtomicReferenceFieldUpdater.newUpdater(SchemaNodeIdentifier.class, ImmutableList.class, "legacyPath");
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;
103 protected SchemaNodeIdentifier(final SchemaNodeIdentifier parent, final QName qname) {
104 this.parent = parent;
107 int h = parent == null ? 0 : parent.hashCode();
109 h = h * 31 + qname.hashCode();
115 private ImmutableList<QName> getLegacyPath() {
116 ImmutableList<QName> ret = legacyPath;
118 ret = ImmutableList.copyOf(getPathTowardsRoot()).reverse();
119 LEGACYPATH_UPDATER.lazySet(this, ret);
126 * Returns the complete path to schema node.
128 * @return list of <code>QName</code> instances which represents complete
129 * path to schema node
131 * @deprecated Use {@link #getPathFromRoot()} instead.
134 public List<QName> getPath() {
135 return getLegacyPath();
139 * Constructs new instance of this class with the concrete path.
142 * list of QName instances which specifies exact path to the
145 * boolean value which specifies if the path is absolute or
148 * @return A SchemaPath instance.
150 public static SchemaNodeIdentifier create(final Iterable<QName> path, final boolean absolute) {
151 final SchemaNodeIdentifier parent = absolute ? ROOT : SAME;
152 return parent.createChild(path);
156 * Constructs new instance of this class with the concrete path.
159 * boolean value which specifies if the path is absolute or
162 * one or more QName instances which specifies exact path to the
165 * @return A SchemaPath instance.
167 public static SchemaNodeIdentifier create(final boolean absolute, final QName... path) {
168 return create(Arrays.asList(path), absolute);
172 * Create a new instance.
174 * @param parent Parent schema node identifier
175 * @param qname next path element
176 * @return A new SchemaPath instance
178 protected abstract SchemaNodeIdentifier createInstance(SchemaNodeIdentifier parent, QName qname);
181 * Create a child path based on concatenation of this path and a relative path.
183 * @param relative Relative path
184 * @return A new child path
186 public SchemaNodeIdentifier createChild(final Iterable<QName> relative) {
187 if (Iterables.isEmpty(relative)) {
191 SchemaNodeIdentifier parentNode = this;
192 for (QName qname : relative) {
193 parentNode = parentNode.createInstance(parentNode, qname);
200 * Create a child path based on concatenation of this path and a relative path.
202 * @param relative Relative SchemaPath
203 * @return A new child path
205 public SchemaNodeIdentifier createChild(final SchemaNodeIdentifier relative) {
206 Preconditions.checkArgument(!relative.isAbsolute(), "Child creation requires relative path");
208 SchemaNodeIdentifier parentNode = this;
209 for (QName qname : relative.getPathFromRoot()) {
210 parentNode = parentNode.createInstance(parentNode, qname);
217 * Create a child path based on concatenation of this path and additional
220 * @param elements Relative SchemaPath elements
221 * @return A new child path
223 public SchemaNodeIdentifier createChild(final QName... elements) {
224 return createChild(Arrays.asList(elements));
228 * Returns the list of nodes which need to be traversed to get from the
229 * starting point (root for absolute SchemaPaths) to the node represented
232 * @return list of <code>qname</code> instances which represents
233 * path from the root to the schema node.
235 public Iterable<QName> getPathFromRoot() {
236 return getLegacyPath();
240 * Returns the list of nodes which need to be traversed to get from this
241 * node to the starting point (root for absolute SchemaPaths).
243 * @return list of <code>qname</code> instances which represents
244 * path from the schema node towards the root.
246 public Iterable<QName> getPathTowardsRoot() {
247 return new Iterable<QName>() {
249 public Iterator<QName> iterator() {
250 return new Iterator<QName>() {
251 private SchemaNodeIdentifier current = SchemaNodeIdentifier.this;
254 public boolean hasNext() {
255 return current.parent != null;
259 public QName next() {
260 if (current.parent != null) {
261 final QName ret = current.qname;
262 current = current.parent;
265 throw new NoSuchElementException("No more elements available");
270 public void remove() {
271 throw new UnsupportedOperationException("Component removal not supported");
279 * Returns the immediate parent SchemaPath.
281 * @return Parent path, null if this SchemaPath is already toplevel.
283 public SchemaNodeIdentifier getParent() {
288 * Get the last component of this path.
290 * @return The last component of this path.
292 public final QName getLastComponent() {
297 * Describes whether schema node identifier is|isn't absolute.
299 * @return boolean value which is <code>true</code> if schema path is
302 public abstract boolean isAbsolute();
305 public final int hashCode() {
310 public boolean equals(final Object obj) {
317 if (getClass() != obj.getClass()) {
320 final SchemaNodeIdentifier other = (SchemaNodeIdentifier) obj;
323 if (!qname.equals(other.qname)) {
327 if (other.qname != null) {
332 if (parent == null) {
333 return other.parent == null;
335 return parent.equals(other.parent);
339 public final String toString() {
340 return addToStringAttributes(Objects.toStringHelper(this)).toString();
343 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
344 return toStringHelper.add("path", getPathFromRoot());