2 * Copyright (c) 2013 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;
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.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 node inside the module.
26 public abstract class SchemaPath implements Immutable {
29 * An absolute SchemaPath.
31 private static final class AbsoluteSchemaPath extends SchemaPath {
32 private AbsoluteSchemaPath(final SchemaPath parent, final QName qname) {
37 public boolean isAbsolute() {
42 protected SchemaPath createInstance(final SchemaPath parent, final QName qname) {
43 return new AbsoluteSchemaPath(parent, qname);
48 * A relative SchemaPath.
50 private static final class RelativeSchemaPath extends SchemaPath {
51 private RelativeSchemaPath(final SchemaPath parent, final QName qname) {
56 public boolean isAbsolute() {
61 protected SchemaPath createInstance(final SchemaPath parent, final QName qname) {
62 return new RelativeSchemaPath(parent, qname);
66 @SuppressWarnings("rawtypes")
67 private static final AtomicReferenceFieldUpdater<SchemaPath, ImmutableList> LEGACYPATH_UPDATER =
68 AtomicReferenceFieldUpdater.newUpdater(SchemaPath.class, ImmutableList.class, "legacyPath");
71 * Shared instance of the conceptual root schema node.
73 public static final SchemaPath ROOT = new AbsoluteSchemaPath(null, null);
76 * Shared instance of the "same" relative schema node.
78 public static final SchemaPath SAME = new RelativeSchemaPath(null, null);
83 private final SchemaPath parent;
88 private final QName qname;
91 * Cached hash code. We can use this since we are immutable.
93 private final int hash;
96 * Cached legacy path, filled-in when {@link #getPath()} or {@link #getPathTowardsRoot()}
99 private volatile ImmutableList<QName> legacyPath;
101 protected SchemaPath(final SchemaPath parent, final QName qname) {
102 this.parent = parent;
105 int h = parent == null ? 0 : parent.hashCode();
107 h = h * 31 + qname.hashCode();
113 private ImmutableList<QName> getLegacyPath() {
114 ImmutableList<QName> ret = legacyPath;
116 ret = ImmutableList.copyOf(getPathTowardsRoot()).reverse();
117 LEGACYPATH_UPDATER.lazySet(this, ret);
124 * Returns the complete path to schema node.
126 * @return list of <code>QName</code> instances which represents complete
127 * path to schema node
129 * @deprecated Use {@link #getPathFromRoot()} instead.
132 public List<QName> getPath() {
133 return getLegacyPath();
137 * Constructs new instance of this class with the concrete path.
140 * list of QName instances which specifies exact path to the
143 * boolean value which specifies if the path is absolute or
146 * @return A SchemaPath instance.
148 public static SchemaPath create(final Iterable<QName> path, final boolean absolute) {
149 final SchemaPath parent = absolute ? ROOT : SAME;
150 return parent.createChild(path);
154 * Constructs new instance of this class with the concrete path.
157 * boolean value which specifies if the path is absolute or
160 * one or more QName instances which specifies exact path to the
163 * @return A SchemaPath instance.
165 public static SchemaPath create(final boolean absolute, final QName... path) {
166 return create(Arrays.asList(path), absolute);
170 * Create a new instance.
172 * @param parent Parent SchemaPath
173 * @param qname next path element
174 * @return A new SchemaPath instance
176 protected abstract SchemaPath createInstance(SchemaPath parent, QName qname);
179 * Create a child path based on concatenation of this path and a relative path.
181 * @param relative Relative path
182 * @return A new child path
184 public SchemaPath createChild(final Iterable<QName> relative) {
185 if (Iterables.isEmpty(relative)) {
189 SchemaPath parentPath = this;
190 for (QName qname : relative) {
191 parentPath = parentPath.createInstance(parentPath, qname);
198 * Create a child path based on concatenation of this path and a relative path.
200 * @param relative Relative SchemaPath
201 * @return A new child path
203 public SchemaPath createChild(final SchemaPath relative) {
204 Preconditions.checkArgument(!relative.isAbsolute(), "Child creation requires relative path");
206 SchemaPath parentPath = this;
207 for (QName qname : relative.getPathFromRoot()) {
208 parentPath = parentPath.createInstance(parentPath, qname);
215 * Create a child path based on concatenation of this path and additional
218 * @param elements Relative SchemaPath elements
219 * @return A new child path
221 public SchemaPath createChild(final QName... elements) {
222 return createChild(Arrays.asList(elements));
226 * Returns the list of nodes which need to be traversed to get from the
227 * starting point (root for absolute SchemaPaths) to the node represented
230 * @return list of <code>qname</code> instances which represents
231 * path from the root to the schema node.
233 public Iterable<QName> getPathFromRoot() {
234 return getLegacyPath();
238 * Returns the list of nodes which need to be traversed to get from this
239 * node to the starting point (root for absolute SchemaPaths).
241 * @return list of <code>qname</code> instances which represents
242 * path from the schema node towards the root.
244 public Iterable<QName> getPathTowardsRoot() {
245 return new Iterable<QName>() {
247 public Iterator<QName> iterator() {
248 return new Iterator<QName>() {
249 private SchemaPath current = SchemaPath.this;
252 public boolean hasNext() {
253 return current.parent != null;
257 public QName next() {
258 if (current.parent != null) {
259 final QName ret = current.qname;
260 current = current.parent;
263 throw new NoSuchElementException("No more elements available");
268 public void remove() {
269 throw new UnsupportedOperationException("Component removal not supported");
277 * Returns the immediate parent SchemaPath.
279 * @return Parent path, null if this SchemaPath is already toplevel.
281 public SchemaPath getParent() {
286 * Get the last component of this path.
288 * @return The last component of this path.
290 public final QName getLastComponent() {
295 * Describes whether schema path is|isn't absolute.
297 * @return boolean value which is <code>true</code> if schema path is
300 public abstract boolean isAbsolute();
303 public final int hashCode() {
308 public boolean equals(final Object obj) {
315 if (getClass() != obj.getClass()) {
318 final SchemaPath other = (SchemaPath) obj;
321 if (!qname.equals(other.qname)) {
325 if (other.qname != null) {
330 if (parent == null) {
331 return other.parent == null;
333 return parent.equals(other.parent);
337 public final String toString() {
338 return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
341 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
342 return toStringHelper.add("path", getPathFromRoot());