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 {
30 * An absolute schema node identifier.
32 public static final class Absolute extends SchemaNodeIdentifier {
33 private Absolute(final SchemaNodeIdentifier parent, final QName qname) {
38 public boolean isAbsolute() {
43 protected SchemaNodeIdentifier createInstance(final SchemaNodeIdentifier parent, final QName qname) {
44 return new Absolute(parent, qname);
49 * A relative schema node identifier.
51 public static class Relative extends SchemaNodeIdentifier {
52 private Relative(final SchemaNodeIdentifier parent, final QName qname) {
57 public boolean isAbsolute() {
62 protected SchemaNodeIdentifier createInstance(final SchemaNodeIdentifier parent, final QName qname) {
63 return new Relative(parent, qname);
67 @SuppressWarnings("rawtypes")
68 private static final AtomicReferenceFieldUpdater<SchemaNodeIdentifier, ImmutableList> LEGACYPATH_UPDATER =
69 AtomicReferenceFieldUpdater.newUpdater(SchemaNodeIdentifier.class, ImmutableList.class, "legacyPath");
72 * Shared instance of the conceptual root schema node.
74 public static final SchemaNodeIdentifier ROOT = new Absolute(null, null);
77 * Shared instance of the "same" relative schema node.
79 public static final SchemaNodeIdentifier SAME = new Relative(null, null);
84 private final SchemaNodeIdentifier parent;
89 private final QName qname;
92 * Cached hash code. We can use this since we are immutable.
94 private final int hash;
97 * Cached legacy path, filled-in when {@link #getPath()} or {@link #getPathTowardsRoot()}
100 private volatile ImmutableList<QName> legacyPath;
102 private ImmutableList<QName> getLegacyPath() {
103 ImmutableList<QName> ret = legacyPath;
105 ret = ImmutableList.copyOf(getPathTowardsRoot()).reverse();
106 LEGACYPATH_UPDATER.lazySet(this, ret);
113 * Returns the complete path to schema node.
115 * @return list of <code>QName</code> instances which represents complete
116 * path to schema node
118 * @deprecated Use {@link #getPathFromRoot()} instead.
121 public List<QName> getPath() {
122 return getLegacyPath();
125 protected SchemaNodeIdentifier(final SchemaNodeIdentifier parent, final QName qname) {
126 this.parent = parent;
129 int h = parent == null ? 0 : parent.hashCode();
131 h = h * 31 + qname.hashCode();
138 * Constructs new instance of this class with the concrete path.
141 * list of QName instances which specifies exact path to the
144 * boolean value which specifies if the path is absolute or
147 * @return A SchemaPath instance.
149 public static SchemaNodeIdentifier create(final Iterable<QName> path, final boolean absolute) {
150 final SchemaNodeIdentifier parent = absolute ? ROOT : SAME;
151 return parent.createChild(path);
155 * Constructs new instance of this class with the concrete path.
158 * boolean value which specifies if the path is absolute or
161 * one or more QName instances which specifies exact path to the
164 * @return A SchemaPath instance.
166 public static SchemaNodeIdentifier create(final boolean absolute, final QName... path) {
167 return create(Arrays.asList(path), absolute);
171 * Create a new instance.
173 * @param parent Parent schema node identifier
174 * @param qname next path element
175 * @return A new SchemaPath instance
177 protected abstract SchemaNodeIdentifier createInstance(SchemaNodeIdentifier parent, QName qname);
180 * Create a child path based on concatenation of this path and a relative path.
182 * @param relative Relative path
183 * @return A new child path
185 public SchemaNodeIdentifier createChild(final Iterable<QName> relative) {
186 if (Iterables.isEmpty(relative)) {
190 SchemaNodeIdentifier parent = this;
191 for (QName qname : relative) {
192 parent = parent.createInstance(parent, qname);
199 * Create a child path based on concatenation of this path and a relative path.
201 * @param relative Relative SchemaPath
202 * @return A new child path
204 public SchemaNodeIdentifier createChild(final SchemaNodeIdentifier relative) {
205 Preconditions.checkArgument(!relative.isAbsolute(), "Child creation requires relative path");
207 SchemaNodeIdentifier parent = this;
208 for (QName qname : relative.getPathFromRoot()) {
209 parent = parent.createInstance(parent, qname);
216 * Create a child path based on concatenation of this path and additional
219 * @param elements Relative SchemaPath elements
220 * @return A new child path
222 public SchemaNodeIdentifier createChild(final QName... elements) {
223 return createChild(Arrays.asList(elements));
227 * Returns the list of nodes which need to be traversed to get from the
228 * starting point (root for absolute SchemaPaths) to the node represented
231 * @return list of <code>qname</code> instances which represents
232 * path from the root to the schema node.
234 public Iterable<QName> getPathFromRoot() {
235 return getLegacyPath();
239 * Returns the list of nodes which need to be traversed to get from this
240 * node to the starting point (root for absolute SchemaPaths).
242 * @return list of <code>qname</code> instances which represents
243 * path from the schema node towards the root.
245 public Iterable<QName> getPathTowardsRoot() {
246 return new Iterable<QName>() {
248 public Iterator<QName> iterator() {
249 return new Iterator<QName>() {
250 private SchemaNodeIdentifier current = SchemaNodeIdentifier.this;
253 public boolean hasNext() {
254 return current.parent != null;
258 public QName next() {
259 if (current.parent != null) {
260 final QName ret = current.qname;
261 current = current.parent;
264 throw new NoSuchElementException("No more elements available");
269 public void remove() {
270 throw new UnsupportedOperationException("Component removal not supported");
278 * Returns the immediate parent SchemaPath.
280 * @return Parent path, null if this SchemaPath is already toplevel.
282 public SchemaNodeIdentifier getParent() {
287 * Get the last component of this path.
289 * @return The last component of this path.
291 public final QName getLastComponent() {
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 boolean equals(final Object obj) {
316 if (getClass() != obj.getClass()) {
319 final SchemaNodeIdentifier other = (SchemaNodeIdentifier) obj;
322 if (!qname.equals(other.qname)) {
326 if (other.qname != null) {
331 if (parent == null) {
332 return other.parent == null;
334 return parent.equals(other.parent);
338 public final String toString() {
339 return addToStringAttributes(Objects.toStringHelper(this)).toString();
342 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
343 return toStringHelper.add("path", getPathFromRoot());