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;
16 import java.util.Arrays;
17 import java.util.Iterator;
18 import java.util.List;
19 import java.util.NoSuchElementException;
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 {
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);
67 * Shared instance of the conceptual root schema node.
69 public static final SchemaPath ROOT = new AbsoluteSchemaPath(null, null);
72 * Shared instance of the "same" relative schema node.
74 public static final SchemaPath SAME = new RelativeSchemaPath(null, null);
79 private final SchemaPath parent;
84 private final QName qname;
87 * Cached hash code. We can use this since we are immutable.
89 private final int hash;
92 * Cached legacy path, filled-in when {@link #getPath()} or {@link #getPathTowardsRoot()}
95 private ImmutableList<QName> legacyPath;
97 private ImmutableList<QName> getLegacyPath() {
98 if (legacyPath == null) {
99 legacyPath = ImmutableList.copyOf(getPathTowardsRoot()).reverse();
106 * Returns the complete path to schema node.
108 * @return list of <code>QName</code> instances which represents complete
109 * path to schema node
111 * @deprecated Use {@link #getPathFromRoot()} instead.
114 public List<QName> getPath() {
115 return getLegacyPath();
118 protected SchemaPath(final SchemaPath parent, final QName qname) {
119 this.parent = parent;
122 int h = parent == null ? 0 : parent.hashCode();
124 h = h * 31 + qname.hashCode();
131 * Constructs new instance of this class with the concrete path.
134 * list of QName instances which specifies exact path to the
137 * boolean value which specifies if the path is absolute or
140 * @return A SchemaPath instance.
142 public static SchemaPath create(final Iterable<QName> path, final boolean absolute) {
143 final SchemaPath parent = absolute ? ROOT : SAME;
144 return parent.createChild(path);
148 * Constructs new instance of this class with the concrete path.
151 * boolean value which specifies if the path is absolute or
154 * one or more QName instances which specifies exact path to the
157 * @return A SchemaPath instance.
159 public static SchemaPath create(final boolean absolute, final QName... path) {
160 return create(Arrays.asList(path), absolute);
164 * Create a new instance.
166 * @param path path from root
167 * @param hash intended hash code
168 * @return A new SchemaPath instance
170 protected abstract SchemaPath createInstance(SchemaPath parent, QName qname);
173 * Create a child path based on concatenation of this path and a relative path.
175 * @param relative Relative path
176 * @return A new child path
178 public SchemaPath createChild(final Iterable<QName> relative) {
179 if (Iterables.isEmpty(relative)) {
183 SchemaPath parent = this;
184 for (QName qname : relative) {
185 parent = parent.createInstance(parent, qname);
192 * Create a child path based on concatenation of this path and a relative path.
194 * @param relative Relative SchemaPath
195 * @return A new child path
197 public SchemaPath createChild(final SchemaPath relative) {
198 Preconditions.checkArgument(!relative.isAbsolute(), "Child creation requires relative path");
200 SchemaPath parent = this;
201 for (QName qname : relative.getPathFromRoot()) {
202 parent = parent.createInstance(parent, qname);
209 * Create a child path based on concatenation of this path and additional
212 * @param elements Relative SchemaPath elements
213 * @return A new child path
215 public SchemaPath createChild(final QName... elements) {
216 return createChild(Arrays.asList(elements));
220 * Returns the list of nodes which need to be traversed to get from the
221 * starting point (root for absolute SchemaPaths) to the node represented
224 * @return list of <code>qname</code> instances which represents
225 * path from the root to the schema node.
227 public Iterable<QName> getPathFromRoot() {
228 return getLegacyPath();
232 * Returns the list of nodes which need to be traversed to get from this
233 * node to the starting point (root for absolute SchemaPaths).
235 * @return list of <code>qname</code> instances which represents
236 * path from the schema node towards the root.
238 public Iterable<QName> getPathTowardsRoot() {
239 return new Iterable<QName>() {
241 public Iterator<QName> iterator() {
242 return new Iterator<QName>() {
243 private SchemaPath current = SchemaPath.this;
246 public boolean hasNext() {
247 return current.parent != null;
251 public QName next() {
252 if (current.parent != null) {
253 final QName ret = current.qname;
254 current = current.parent;
257 throw new NoSuchElementException("No more elements available");
262 public void remove() {
263 throw new UnsupportedOperationException("Component removal not supported");
271 * Returns the immediate parent SchemaPath.
273 * @return Parent path, null if this SchemaPath is already toplevel.
275 public SchemaPath getParent() {
280 * Describes whether schema path is|isn't absolute.
282 * @return boolean value which is <code>true</code> if schema path is
285 public abstract boolean isAbsolute();
288 public final int hashCode() {
293 public boolean equals(final Object obj) {
300 if (getClass() != obj.getClass()) {
303 final SchemaPath other = (SchemaPath) obj;
306 if (!qname.equals(other.qname)) {
310 if (other.qname != null) {
315 if (parent == null) {
316 return other.parent == null;
318 return parent.equals(other.parent);
322 public final String toString() {
323 return addToStringAttributes(Objects.toStringHelper(this)).toString();
326 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
327 return toStringHelper.add("path", getPathFromRoot());