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.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 node inside the module.
26 public abstract class SchemaPath implements Immutable {
28 * An absolute SchemaPath.
30 private static final class AbsoluteSchemaPath extends SchemaPath {
31 private AbsoluteSchemaPath(final SchemaPath parent, final QName qname) {
36 public boolean isAbsolute() {
41 protected SchemaPath createInstance(final SchemaPath parent, final QName qname) {
42 return new AbsoluteSchemaPath(parent, qname);
47 * A relative SchemaPath.
49 private static final class RelativeSchemaPath extends SchemaPath {
50 private RelativeSchemaPath(final SchemaPath parent, final QName qname) {
55 public boolean isAbsolute() {
60 protected SchemaPath createInstance(final SchemaPath parent, final QName qname) {
61 return new RelativeSchemaPath(parent, qname);
65 @SuppressWarnings("rawtypes")
66 private static final AtomicReferenceFieldUpdater<SchemaPath, ImmutableList> LEGACYPATH_UPDATER =
67 AtomicReferenceFieldUpdater.newUpdater(SchemaPath.class, ImmutableList.class, "legacyPath");
70 * Shared instance of the conceptual root schema node.
72 public static final SchemaPath ROOT = new AbsoluteSchemaPath(null, null);
75 * Shared instance of the "same" relative schema node.
77 public static final SchemaPath SAME = new RelativeSchemaPath(null, null);
82 private final SchemaPath parent;
87 private final QName qname;
90 * Cached hash code. We can use this since we are immutable.
92 private final int hash;
95 * Cached legacy path, filled-in when {@link #getPath()} or {@link #getPathTowardsRoot()}
98 private volatile ImmutableList<QName> legacyPath;
100 private ImmutableList<QName> getLegacyPath() {
101 ImmutableList<QName> ret = legacyPath;
103 ret = ImmutableList.copyOf(getPathTowardsRoot()).reverse();
104 LEGACYPATH_UPDATER.lazySet(this, ret);
111 * Returns the complete path to schema node.
113 * @return list of <code>QName</code> instances which represents complete
114 * path to schema node
116 * @deprecated Use {@link #getPathFromRoot()} instead.
119 public List<QName> getPath() {
120 return getLegacyPath();
123 protected SchemaPath(final SchemaPath parent, final QName qname) {
124 this.parent = parent;
127 int h = parent == null ? 0 : parent.hashCode();
129 h = h * 31 + qname.hashCode();
136 * Constructs new instance of this class with the concrete path.
139 * list of QName instances which specifies exact path to the
142 * boolean value which specifies if the path is absolute or
145 * @return A SchemaPath instance.
147 public static SchemaPath create(final Iterable<QName> path, final boolean absolute) {
148 final SchemaPath parent = absolute ? ROOT : SAME;
149 return parent.createChild(path);
153 * Constructs new instance of this class with the concrete path.
156 * boolean value which specifies if the path is absolute or
159 * one or more QName instances which specifies exact path to the
162 * @return A SchemaPath instance.
164 public static SchemaPath create(final boolean absolute, final QName... path) {
165 return create(Arrays.asList(path), absolute);
169 * Create a new instance.
171 * @param path path from root
172 * @param hash intended hash code
173 * @return A new SchemaPath instance
175 protected abstract SchemaPath createInstance(SchemaPath parent, QName qname);
178 * Create a child path based on concatenation of this path and a relative path.
180 * @param relative Relative path
181 * @return A new child path
183 public SchemaPath createChild(final Iterable<QName> relative) {
184 if (Iterables.isEmpty(relative)) {
188 SchemaPath parent = this;
189 for (QName qname : relative) {
190 parent = parent.createInstance(parent, qname);
197 * Create a child path based on concatenation of this path and a relative path.
199 * @param relative Relative SchemaPath
200 * @return A new child path
202 public SchemaPath createChild(final SchemaPath relative) {
203 Preconditions.checkArgument(!relative.isAbsolute(), "Child creation requires relative path");
205 SchemaPath parent = this;
206 for (QName qname : relative.getPathFromRoot()) {
207 parent = parent.createInstance(parent, qname);
214 * Create a child path based on concatenation of this path and additional
217 * @param elements Relative SchemaPath elements
218 * @return A new child path
220 public SchemaPath createChild(final QName... elements) {
221 return createChild(Arrays.asList(elements));
225 * Returns the list of nodes which need to be traversed to get from the
226 * starting point (root for absolute SchemaPaths) to the node represented
229 * @return list of <code>qname</code> instances which represents
230 * path from the root to the schema node.
232 public Iterable<QName> getPathFromRoot() {
233 return getLegacyPath();
237 * Returns the list of nodes which need to be traversed to get from this
238 * node to the starting point (root for absolute SchemaPaths).
240 * @return list of <code>qname</code> instances which represents
241 * path from the schema node towards the root.
243 public Iterable<QName> getPathTowardsRoot() {
244 return new Iterable<QName>() {
246 public Iterator<QName> iterator() {
247 return new Iterator<QName>() {
248 private SchemaPath current = SchemaPath.this;
251 public boolean hasNext() {
252 return current.parent != null;
256 public QName next() {
257 if (current.parent != null) {
258 final QName ret = current.qname;
259 current = current.parent;
262 throw new NoSuchElementException("No more elements available");
267 public void remove() {
268 throw new UnsupportedOperationException("Component removal not supported");
276 * Returns the immediate parent SchemaPath.
278 * @return Parent path, null if this SchemaPath is already toplevel.
280 public SchemaPath getParent() {
285 * Get the last component of this path.
287 * @return The last component of this path.
289 public final QName getLastComponent() {
294 * Describes whether schema path is|isn't absolute.
296 * @return boolean value which is <code>true</code> if schema path is
299 public abstract boolean isAbsolute();
302 public final int hashCode() {
307 public boolean equals(final Object obj) {
314 if (getClass() != obj.getClass()) {
317 final SchemaPath other = (SchemaPath) obj;
320 if (!qname.equals(other.qname)) {
324 if (other.qname != null) {
329 if (parent == null) {
330 return other.parent == null;
332 return parent.equals(other.parent);
336 public final String toString() {
337 return addToStringAttributes(Objects.toStringHelper(this)).toString();
340 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
341 return toStringHelper.add("path", getPathFromRoot());