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.base.MoreObjects.ToStringHelper;
16 import com.google.common.collect.ImmutableList;
17 import com.google.common.collect.Iterables;
18 import com.google.common.collect.UnmodifiableIterator;
19 import java.util.Arrays;
20 import java.util.List;
21 import java.util.NoSuchElementException;
22 import java.util.Objects;
23 import org.eclipse.jdt.annotation.NonNull;
24 import org.opendaylight.yangtools.concepts.Immutable;
25 import org.opendaylight.yangtools.yang.common.QName;
26 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
27 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
28 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Descendant;
31 * Represents unique path to the every node inside the module.
33 public abstract class SchemaPath implements Immutable {
36 * An absolute SchemaPath.
38 private static final class AbsoluteSchemaPath extends SchemaPath {
39 private AbsoluteSchemaPath(final SchemaPath parent, final QName qname) {
44 public boolean isAbsolute() {
49 public AbsoluteSchemaPath createChild(final QName element) {
50 return new AbsoluteSchemaPath(this, requireNonNull(element));
55 * A relative SchemaPath.
57 private static final class RelativeSchemaPath extends SchemaPath {
58 private RelativeSchemaPath(final SchemaPath parent, final QName qname) {
63 public boolean isAbsolute() {
68 public RelativeSchemaPath createChild(final QName element) {
69 return new RelativeSchemaPath(this, requireNonNull(element));
74 * Shared instance of the conceptual root schema node.
76 public static final @NonNull SchemaPath ROOT = new AbsoluteSchemaPath(null, null);
79 * Shared instance of the "same" relative schema node.
81 public static final @NonNull SchemaPath SAME = new RelativeSchemaPath(null, null);
86 private final SchemaPath parent;
91 private final QName qname;
94 * Cached hash code. We can use this since we are immutable.
96 private final int hash;
98 SchemaPath(final SchemaPath parent, final QName qname) {
102 int tmp = Objects.hashCode(parent);
104 tmp = tmp * 31 + qname.hashCode();
111 * Constructs new instance of this class with the concrete path.
114 * list of QName instances which specifies exact path to the
117 * boolean value which specifies if the path is absolute or
120 * @return A SchemaPath instance.
122 public static @NonNull SchemaPath create(final Iterable<QName> path, final boolean absolute) {
123 return (absolute ? ROOT : SAME).createChild(path);
127 * Constructs new instance of this class with the concrete path.
130 * boolean value which specifies if the path is absolute or
133 * a single QName which specifies exact path to the
136 * @return A SchemaPath instance.
138 public static @NonNull SchemaPath create(final boolean absolute, final QName element) {
139 return (absolute ? ROOT : SAME).createChild(element);
143 * Constructs new instance of this class with the concrete path.
146 * boolean value which specifies if the path is absolute or
149 * one or more QName instances which specifies exact path to the
152 * @return A SchemaPath instance.
154 public static @NonNull SchemaPath create(final boolean absolute, final QName... path) {
155 return create(Arrays.asList(path), absolute);
159 * Create a child path based on concatenation of this path and a relative path.
161 * @param relative Relative path
162 * @return A new child path
164 public @NonNull SchemaPath createChild(final Iterable<QName> relative) {
165 if (Iterables.isEmpty(relative)) {
169 SchemaPath parentPath = this;
170 for (QName item : relative) {
171 parentPath = parentPath.createChild(item);
178 * Create a child path based on concatenation of this path and a relative path.
180 * @param relative Relative SchemaPath
181 * @return A new child path
183 public @NonNull SchemaPath createChild(final SchemaPath relative) {
184 checkArgument(!relative.isAbsolute(), "Child creation requires relative path");
185 return createChild(relative.getPathFromRoot());
189 * Create a child path based on concatenation of this path and an additional path element.
191 * @param element Relative SchemaPath elements
192 * @return A new child path
194 public abstract @NonNull SchemaPath createChild(QName element);
197 * Create a child path based on concatenation of this path and additional
200 * @param elements Relative SchemaPath elements
201 * @return A new child path
203 public @NonNull SchemaPath createChild(final QName... elements) {
204 return createChild(Arrays.asList(elements));
208 * Returns the list of nodes which need to be traversed to get from the
209 * starting point (root for absolute SchemaPaths) to the node represented
212 * @return list of <code>qname</code> instances which represents
213 * path from the root to the schema node.
215 public List<QName> getPathFromRoot() {
217 return ImmutableList.of();
219 return parent == null ? ImmutableList.of(qname) : new PathFromRoot(this);
223 * Returns the list of nodes which need to be traversed to get from this
224 * node to the starting point (root for absolute SchemaPaths).
226 * @return list of <code>qname</code> instances which represents
227 * path from the schema node towards the root.
229 public Iterable<QName> getPathTowardsRoot() {
230 return () -> new UnmodifiableIterator<>() {
231 private SchemaPath current = SchemaPath.this;
234 public boolean hasNext() {
235 return current.parent != null;
239 public QName next() {
240 if (current.parent != null) {
241 final QName ret = current.qname;
242 current = current.parent;
246 throw new NoSuchElementException("No more elements available");
252 * Returns the immediate parent SchemaPath.
254 * @return Parent path, null if this SchemaPath is already toplevel.
256 public SchemaPath getParent() {
261 * Get the last component of this path.
263 * @return The last component of this path.
265 public final QName getLastComponent() {
270 * Describes whether schema path is|isn't absolute.
272 * @return boolean value which is <code>true</code> if schema path is
275 public abstract boolean isAbsolute();
278 * Return this path as a {@link SchemaNodeIdentifier}.
280 * @return A SchemaNodeIdentifier.
281 * @throws IllegalStateException if this path is empty
283 public final SchemaNodeIdentifier asSchemaNodeIdentifier() {
284 checkState(qname != null, "Cannot convert empty %s", this);
285 final List<QName> path = getPathFromRoot();
286 return isAbsolute() ? Absolute.of(path) : Descendant.of(path);
290 * Return this path as an {@link Absolute} SchemaNodeIdentifier.
292 * @return An SchemaNodeIdentifier.
293 * @throws IllegalStateException if this path is empty or is not absolute.
295 public final Absolute asAbsolute() {
296 final SchemaNodeIdentifier ret = asSchemaNodeIdentifier();
297 if (ret instanceof Absolute) {
298 return (Absolute) ret;
300 throw new IllegalStateException("Path " + this + " is relative");
304 * Return this path as an {@link Descendant} SchemaNodeIdentifier.
306 * @return An SchemaNodeIdentifier.
307 * @throws IllegalStateException if this path is empty or is not relative.
309 public final Descendant asDescendant() {
310 final SchemaNodeIdentifier ret = asSchemaNodeIdentifier();
311 if (ret instanceof Descendant) {
312 return (Descendant) ret;
314 throw new IllegalStateException("Path " + this + " is absolute");
318 public final int hashCode() {
323 public boolean equals(final Object obj) {
330 if (getClass() != obj.getClass()) {
333 final SchemaPath other = (SchemaPath) obj;
334 return Objects.equals(qname, other.qname) && Objects.equals(parent, other.parent);
338 public final String toString() {
339 return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
342 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
343 return toStringHelper.add("path", getPathFromRoot());