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 com.google.common.collect.UnmodifiableIterator;
16 import java.util.Arrays;
17 import java.util.Iterator;
18 import java.util.List;
19 import java.util.NoSuchElementException;
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 node inside the module.
27 public abstract class SchemaPath implements Immutable {
30 * An absolute SchemaPath.
32 private static final class AbsoluteSchemaPath extends SchemaPath {
33 private AbsoluteSchemaPath(final SchemaPath parent, final QName qname) {
38 public boolean isAbsolute() {
43 protected SchemaPath createInstance(final SchemaPath parent, final QName qname) {
44 return new AbsoluteSchemaPath(parent, qname);
49 * A relative SchemaPath.
51 private static final class RelativeSchemaPath extends SchemaPath {
52 private RelativeSchemaPath(final SchemaPath parent, final QName qname) {
57 public boolean isAbsolute() {
62 protected SchemaPath createInstance(final SchemaPath parent, final QName qname) {
63 return new RelativeSchemaPath(parent, qname);
67 @SuppressWarnings("rawtypes")
68 private static final AtomicReferenceFieldUpdater<SchemaPath, ImmutableList> LEGACYPATH_UPDATER =
69 AtomicReferenceFieldUpdater.newUpdater(SchemaPath.class, ImmutableList.class, "legacyPath");
72 * Shared instance of the conceptual root schema node.
74 public static final SchemaPath ROOT = new AbsoluteSchemaPath(null, null);
77 * Shared instance of the "same" relative schema node.
79 public static final SchemaPath SAME = new RelativeSchemaPath(null, null);
84 private final SchemaPath 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 protected SchemaPath(final SchemaPath parent, final QName qname) {
103 this.parent = parent;
106 int h = parent == null ? 0 : parent.hashCode();
108 h = h * 31 + qname.hashCode();
114 private ImmutableList<QName> getLegacyPath() {
115 ImmutableList<QName> ret = legacyPath;
117 ret = ImmutableList.copyOf(getPathTowardsRoot()).reverse();
118 LEGACYPATH_UPDATER.lazySet(this, ret);
125 * Returns the complete path to schema node.
127 * @return list of <code>QName</code> instances which represents complete
128 * path to schema node
130 * @deprecated Use {@link #getPathFromRoot()} instead.
133 public List<QName> getPath() {
134 return getLegacyPath();
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 SchemaPath create(final Iterable<QName> path, final boolean absolute) {
150 final SchemaPath 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 SchemaPath create(final boolean absolute, final QName... path) {
167 return create(Arrays.asList(path), absolute);
171 * Create a new instance.
173 * @param parent Parent SchemaPath
174 * @param qname next path element
175 * @return A new SchemaPath instance
177 protected abstract SchemaPath createInstance(SchemaPath 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 SchemaPath createChild(final Iterable<QName> relative) {
186 if (Iterables.isEmpty(relative)) {
190 SchemaPath parentPath = this;
191 for (QName qname : relative) {
192 parentPath = parentPath.createInstance(parentPath, 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 SchemaPath createChild(final SchemaPath relative) {
205 Preconditions.checkArgument(!relative.isAbsolute(), "Child creation requires relative path");
207 SchemaPath parentPath = this;
208 for (QName qname : relative.getPathFromRoot()) {
209 parentPath = parentPath.createInstance(parentPath, 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 SchemaPath 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 UnmodifiableIterator<QName>() {
250 private SchemaPath current = SchemaPath.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");
273 * Returns the immediate parent SchemaPath.
275 * @return Parent path, null if this SchemaPath is already toplevel.
277 public SchemaPath getParent() {
282 * Get the last component of this path.
284 * @return The last component of this path.
286 public final QName getLastComponent() {
291 * Describes whether schema path is|isn't absolute.
293 * @return boolean value which is <code>true</code> if schema path is
296 public abstract boolean isAbsolute();
299 public final int hashCode() {
304 public boolean equals(final Object obj) {
311 if (getClass() != obj.getClass()) {
314 final SchemaPath other = (SchemaPath) obj;
317 if (!qname.equals(other.qname)) {
321 if (other.qname != null) {
326 if (parent == null) {
327 return other.parent == null;
329 return parent.equals(other.parent);
333 public final String toString() {
334 return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
337 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
338 return toStringHelper.add("path", getPathFromRoot());