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 * @deprecated This path is not really unique, as it does not handle YANG namespace overlap correctly. There are two
33 * different replacements for this class:
35 * <li>{@link SchemaNodeIdentifier} for use in
36 * <a href="https://datatracker.ietf.org/doc/html/rfc7950#section-6.5">YANG schema addressing</a>
38 * <li>{@link EffectiveStatementInference} for use in contexts where the intent is to exchange pointer
39 * to a specific statement. Unlike SchemaPath, though, it does not require additional lookup in most
43 @Deprecated(since = "7.0.8")
44 public abstract class SchemaPath implements Immutable {
47 * An absolute SchemaPath.
49 private static final class AbsoluteSchemaPath extends SchemaPath {
50 private AbsoluteSchemaPath(final SchemaPath parent, final QName qname) {
55 public boolean isAbsolute() {
60 public AbsoluteSchemaPath createChild(final QName element) {
61 return new AbsoluteSchemaPath(this, requireNonNull(element));
66 * A relative SchemaPath.
68 private static final class RelativeSchemaPath extends SchemaPath {
69 private RelativeSchemaPath(final SchemaPath parent, final QName qname) {
74 public boolean isAbsolute() {
79 public RelativeSchemaPath createChild(final QName element) {
80 return new RelativeSchemaPath(this, requireNonNull(element));
85 * Shared instance of the conceptual root schema node.
87 public static final @NonNull SchemaPath ROOT = new AbsoluteSchemaPath(null, null);
90 * Shared instance of the "same" relative schema node.
92 public static final @NonNull SchemaPath SAME = new RelativeSchemaPath(null, null);
97 private final SchemaPath parent;
102 private final QName qname;
105 * Cached hash code. We can use this since we are immutable.
107 private final int hash;
109 SchemaPath(final SchemaPath parent, final QName qname) {
110 this.parent = parent;
113 int tmp = Objects.hashCode(parent);
115 tmp = tmp * 31 + qname.hashCode();
122 * Constructs new instance of this class with the concrete path.
125 * list of QName instances which specifies exact path to the
128 * boolean value which specifies if the path is absolute or
131 * @return A SchemaPath instance.
133 public static @NonNull SchemaPath create(final Iterable<QName> path, final boolean absolute) {
134 return (absolute ? ROOT : SAME).createChild(path);
138 * Constructs new instance of this class with the concrete path.
141 * boolean value which specifies if the path is absolute or
144 * a single QName which specifies exact path to the
147 * @return A SchemaPath instance.
149 public static @NonNull SchemaPath create(final boolean absolute, final QName element) {
150 return (absolute ? ROOT : SAME).createChild(element);
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 @NonNull SchemaPath create(final boolean absolute, final QName... path) {
166 return create(Arrays.asList(path), absolute);
170 * Create a child path based on concatenation of this path and a relative path.
172 * @param relative Relative path
173 * @return A new child path
175 public @NonNull SchemaPath createChild(final Iterable<QName> relative) {
176 if (Iterables.isEmpty(relative)) {
180 SchemaPath parentPath = this;
181 for (QName item : relative) {
182 parentPath = parentPath.createChild(item);
189 * Create a child path based on concatenation of this path and a relative path.
191 * @param relative Relative SchemaPath
192 * @return A new child path
194 public @NonNull SchemaPath createChild(final SchemaPath relative) {
195 checkArgument(!relative.isAbsolute(), "Child creation requires relative path");
196 return createChild(relative.getPathFromRoot());
200 * Create a child path based on concatenation of this path and an additional path element.
202 * @param element Relative SchemaPath elements
203 * @return A new child path
205 public abstract @NonNull SchemaPath createChild(QName element);
208 * Create a child path based on concatenation of this path and additional
211 * @param elements Relative SchemaPath elements
212 * @return A new child path
214 public @NonNull SchemaPath createChild(final QName... elements) {
215 return createChild(Arrays.asList(elements));
219 * Returns the list of nodes which need to be traversed to get from the
220 * starting point (root for absolute SchemaPaths) to the node represented
223 * @return list of <code>qname</code> instances which represents
224 * path from the root to the schema node.
226 public List<QName> getPathFromRoot() {
228 return ImmutableList.of();
230 return parent == null ? ImmutableList.of(qname) : new PathFromRoot(this);
234 * Returns the list of nodes which need to be traversed to get from this
235 * node to the starting point (root for absolute SchemaPaths).
237 * @return list of <code>qname</code> instances which represents
238 * path from the schema node towards the root.
240 public Iterable<QName> getPathTowardsRoot() {
241 return () -> new UnmodifiableIterator<>() {
242 private SchemaPath current = SchemaPath.this;
245 public boolean hasNext() {
246 return current.parent != null;
250 public QName next() {
251 if (current.parent != null) {
252 final QName ret = current.qname;
253 current = current.parent;
257 throw new NoSuchElementException("No more elements available");
263 * Returns the immediate parent SchemaPath.
265 * @return Parent path, null if this SchemaPath is already toplevel.
267 public SchemaPath getParent() {
272 * Get the last component of this path.
274 * @return The last component of this path.
276 public final QName getLastComponent() {
281 * Describes whether schema path is|isn't absolute.
283 * @return boolean value which is <code>true</code> if schema path is
286 public abstract boolean isAbsolute();
289 * Return this path as a {@link SchemaNodeIdentifier}.
291 * @return A SchemaNodeIdentifier.
292 * @throws IllegalStateException if this path is empty
294 public final SchemaNodeIdentifier asSchemaNodeIdentifier() {
295 checkState(qname != null, "Cannot convert empty %s", this);
296 final List<QName> path = getPathFromRoot();
297 return isAbsolute() ? Absolute.of(path) : Descendant.of(path);
301 * Return this path as an {@link Absolute} SchemaNodeIdentifier.
303 * @return An SchemaNodeIdentifier.
304 * @throws IllegalStateException if this path is empty or is not absolute.
306 public final Absolute asAbsolute() {
307 final SchemaNodeIdentifier ret = asSchemaNodeIdentifier();
308 if (ret instanceof Absolute) {
309 return (Absolute) ret;
311 throw new IllegalStateException("Path " + this + " is relative");
315 * Return this path as an {@link Descendant} SchemaNodeIdentifier.
317 * @return An SchemaNodeIdentifier.
318 * @throws IllegalStateException if this path is empty or is not relative.
320 public final Descendant asDescendant() {
321 final SchemaNodeIdentifier ret = asSchemaNodeIdentifier();
322 if (ret instanceof Descendant) {
323 return (Descendant) ret;
325 throw new IllegalStateException("Path " + this + " is absolute");
329 public final int hashCode() {
334 public boolean equals(final Object obj) {
341 if (getClass() != obj.getClass()) {
344 final SchemaPath other = (SchemaPath) obj;
345 return Objects.equals(qname, other.qname) && Objects.equals(parent, other.parent);
349 public final String toString() {
350 return MoreObjects.toStringHelper(this).add("path", getPathFromRoot()).toString();