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 static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Preconditions.checkState;
12 import static java.util.Objects.requireNonNull;
14 import com.google.common.base.MoreObjects;
15 import com.google.common.collect.ImmutableList;
16 import com.google.common.collect.Iterables;
17 import com.google.common.collect.UnmodifiableIterator;
18 import java.util.Arrays;
19 import java.util.List;
20 import java.util.NoSuchElementException;
21 import java.util.Objects;
22 import org.eclipse.jdt.annotation.NonNull;
23 import org.opendaylight.yangtools.concepts.Immutable;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
26 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
27 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Descendant;
30 * Represents unique path to the every node inside the module.
32 public abstract class SchemaPath implements Immutable {
35 * An absolute SchemaPath.
37 private static final class AbsoluteSchemaPath extends SchemaPath {
38 private AbsoluteSchemaPath(final SchemaPath parent, final QName qname) {
43 public boolean isAbsolute() {
48 public AbsoluteSchemaPath createChild(final QName element) {
49 return new AbsoluteSchemaPath(this, requireNonNull(element));
54 * A relative SchemaPath.
56 private static final class RelativeSchemaPath extends SchemaPath {
57 private RelativeSchemaPath(final SchemaPath parent, final QName qname) {
62 public boolean isAbsolute() {
67 public RelativeSchemaPath createChild(final QName element) {
68 return new RelativeSchemaPath(this, requireNonNull(element));
73 * Shared instance of the conceptual root schema node.
75 public static final @NonNull SchemaPath ROOT = new AbsoluteSchemaPath(null, null);
78 * Shared instance of the "same" relative schema node.
80 public static final @NonNull SchemaPath SAME = new RelativeSchemaPath(null, null);
85 private final SchemaPath parent;
90 private final QName qname;
93 * Cached hash code. We can use this since we are immutable.
95 private final int hash;
97 SchemaPath(final SchemaPath parent, final QName qname) {
101 int tmp = Objects.hashCode(parent);
103 tmp = tmp * 31 + qname.hashCode();
110 * Constructs new instance of this class with the concrete path.
113 * list of QName instances which specifies exact path to the
116 * boolean value which specifies if the path is absolute or
119 * @return A SchemaPath instance.
121 public static @NonNull SchemaPath create(final Iterable<QName> path, final boolean absolute) {
122 return (absolute ? ROOT : SAME).createChild(path);
126 * Constructs new instance of this class with the concrete path.
129 * boolean value which specifies if the path is absolute or
132 * a single QName which specifies exact path to the
135 * @return A SchemaPath instance.
137 public static @NonNull SchemaPath create(final boolean absolute, final QName element) {
138 return (absolute ? ROOT : SAME).createChild(element);
142 * Constructs new instance of this class with the concrete path.
145 * boolean value which specifies if the path is absolute or
148 * one or more QName instances which specifies exact path to the
151 * @return A SchemaPath instance.
153 public static @NonNull SchemaPath create(final boolean absolute, final QName... path) {
154 return create(Arrays.asList(path), absolute);
158 * Create a child path based on concatenation of this path and a relative path.
160 * @param relative Relative path
161 * @return A new child path
163 public @NonNull SchemaPath createChild(final Iterable<QName> relative) {
164 if (Iterables.isEmpty(relative)) {
168 SchemaPath parentPath = this;
169 for (QName item : relative) {
170 parentPath = parentPath.createChild(item);
177 * Create a child path based on concatenation of this path and a relative path.
179 * @param relative Relative SchemaPath
180 * @return A new child path
182 public @NonNull SchemaPath createChild(final SchemaPath relative) {
183 checkArgument(!relative.isAbsolute(), "Child creation requires relative path");
184 return createChild(relative.getPathFromRoot());
188 * Create a child path based on concatenation of this path and an additional path element.
190 * @param element Relative SchemaPath elements
191 * @return A new child path
193 public abstract @NonNull SchemaPath createChild(QName element);
196 * Create a child path based on concatenation of this path and additional
199 * @param elements Relative SchemaPath elements
200 * @return A new child path
202 public @NonNull SchemaPath createChild(final QName... elements) {
203 return createChild(Arrays.asList(elements));
207 * Returns the list of nodes which need to be traversed to get from the
208 * starting point (root for absolute SchemaPaths) to the node represented
211 * @return list of <code>qname</code> instances which represents
212 * path from the root to the schema node.
214 public List<QName> getPathFromRoot() {
216 return ImmutableList.of();
218 return parent == null ? ImmutableList.of(qname) : new PathFromRoot(this);
222 * Returns the list of nodes which need to be traversed to get from this
223 * node to the starting point (root for absolute SchemaPaths).
225 * @return list of <code>qname</code> instances which represents
226 * path from the schema node towards the root.
228 public Iterable<QName> getPathTowardsRoot() {
229 return () -> new UnmodifiableIterator<>() {
230 private SchemaPath current = SchemaPath.this;
233 public boolean hasNext() {
234 return current.parent != null;
238 public QName next() {
239 if (current.parent != null) {
240 final QName ret = current.qname;
241 current = current.parent;
245 throw new NoSuchElementException("No more elements available");
251 * Returns the immediate parent SchemaPath.
253 * @return Parent path, null if this SchemaPath is already toplevel.
255 public SchemaPath getParent() {
260 * Get the last component of this path.
262 * @return The last component of this path.
264 public final QName getLastComponent() {
269 * Describes whether schema path is|isn't absolute.
271 * @return boolean value which is <code>true</code> if schema path is
274 public abstract boolean isAbsolute();
277 * Return this path as a {@link SchemaNodeIdentifier}.
279 * @return A SchemaNodeIdentifier.
280 * @throws IllegalStateException if this path is empty
282 public final SchemaNodeIdentifier asSchemaNodeIdentifier() {
283 checkState(qname != null, "Cannot convert empty %s", this);
284 final List<QName> path = getPathFromRoot();
285 return isAbsolute() ? Absolute.of(path) : Descendant.of(path);
289 * Return this path as an {@link Absolute} SchemaNodeIdentifier.
291 * @return An SchemaNodeIdentifier.
292 * @throws IllegalStateException if this path is empty or is not absolute.
294 public final Absolute asAbsolute() {
295 final SchemaNodeIdentifier ret = asSchemaNodeIdentifier();
296 if (ret instanceof Absolute) {
297 return (Absolute) ret;
299 throw new IllegalStateException("Path " + this + " is relative");
303 * Return this path as an {@link Descendant} SchemaNodeIdentifier.
305 * @return An SchemaNodeIdentifier.
306 * @throws IllegalStateException if this path is empty or is not relative.
308 public final Descendant asDescendant() {
309 final SchemaNodeIdentifier ret = asSchemaNodeIdentifier();
310 if (ret instanceof Descendant) {
311 return (Descendant) ret;
313 throw new IllegalStateException("Path " + this + " is absolute");
317 public final int hashCode() {
322 public boolean equals(final Object obj) {
329 if (getClass() != obj.getClass()) {
332 final SchemaPath other = (SchemaPath) obj;
333 return Objects.equals(qname, other.qname) && Objects.equals(parent, other.parent);
337 public final String toString() {
338 return MoreObjects.toStringHelper(this).add("path", getPathFromRoot()).toString();