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 java.util.Objects.requireNonNull;
13 import com.google.common.base.MoreObjects;
14 import com.google.common.base.MoreObjects.ToStringHelper;
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.NoSuchElementException;
20 import java.util.Objects;
21 import org.eclipse.jdt.annotation.NonNull;
22 import org.opendaylight.yangtools.concepts.Immutable;
23 import org.opendaylight.yangtools.yang.common.QName;
26 * Represents unique path to the every node inside the module.
28 public abstract class SchemaPath implements Immutable {
31 * An absolute SchemaPath.
33 private static final class AbsoluteSchemaPath extends SchemaPath {
34 private AbsoluteSchemaPath(final SchemaPath parent, final QName qname) {
39 public boolean isAbsolute() {
44 public AbsoluteSchemaPath createChild(final QName element) {
45 return new AbsoluteSchemaPath(this, requireNonNull(element));
50 * A relative SchemaPath.
52 private static final class RelativeSchemaPath extends SchemaPath {
53 private RelativeSchemaPath(final SchemaPath parent, final QName qname) {
58 public boolean isAbsolute() {
63 public RelativeSchemaPath createChild(final QName element) {
64 return new RelativeSchemaPath(this, requireNonNull(element));
69 * Shared instance of the conceptual root schema node.
71 public static final @NonNull SchemaPath ROOT = new AbsoluteSchemaPath(null, null);
74 * Shared instance of the "same" relative schema node.
76 public static final @NonNull SchemaPath SAME = new RelativeSchemaPath(null, null);
81 private final SchemaPath parent;
86 private final QName qname;
89 * Cached hash code. We can use this since we are immutable.
91 private final int hash;
93 SchemaPath(final SchemaPath parent, final QName qname) {
97 int tmp = Objects.hashCode(parent);
99 tmp = tmp * 31 + qname.hashCode();
106 * Constructs new instance of this class with the concrete path.
109 * list of QName instances which specifies exact path to the
112 * boolean value which specifies if the path is absolute or
115 * @return A SchemaPath instance.
117 public static @NonNull SchemaPath create(final Iterable<QName> path, final boolean absolute) {
118 return (absolute ? ROOT : SAME).createChild(path);
122 * Constructs new instance of this class with the concrete path.
125 * boolean value which specifies if the path is absolute or
128 * a single QName which specifies exact path to the
131 * @return A SchemaPath instance.
133 public static @NonNull SchemaPath create(final boolean absolute, final QName element) {
134 return (absolute ? ROOT : SAME).createChild(element);
138 * Constructs new instance of this class with the concrete path.
141 * boolean value which specifies if the path is absolute or
144 * one or more QName instances which specifies exact path to the
147 * @return A SchemaPath instance.
149 public static @NonNull SchemaPath create(final boolean absolute, final QName... path) {
150 return create(Arrays.asList(path), absolute);
154 * Create a child path based on concatenation of this path and a relative path.
156 * @param relative Relative path
157 * @return A new child path
159 public @NonNull SchemaPath createChild(final Iterable<QName> relative) {
160 if (Iterables.isEmpty(relative)) {
164 SchemaPath parentPath = this;
165 for (QName item : relative) {
166 parentPath = parentPath.createChild(item);
173 * Create a child path based on concatenation of this path and a relative path.
175 * @param relative Relative SchemaPath
176 * @return A new child path
178 public @NonNull SchemaPath createChild(final SchemaPath relative) {
179 checkArgument(!relative.isAbsolute(), "Child creation requires relative path");
180 return createChild(relative.getPathFromRoot());
184 * Create a child path based on concatenation of this path and an additional path element.
186 * @param element Relative SchemaPath elements
187 * @return A new child path
189 public abstract @NonNull SchemaPath createChild(QName element);
192 * Create a child path based on concatenation of this path and additional
195 * @param elements Relative SchemaPath elements
196 * @return A new child path
198 public @NonNull SchemaPath createChild(final QName... elements) {
199 return createChild(Arrays.asList(elements));
203 * Returns the list of nodes which need to be traversed to get from the
204 * starting point (root for absolute SchemaPaths) to the node represented
207 * @return list of <code>qname</code> instances which represents
208 * path from the root to the schema node.
210 public Iterable<QName> getPathFromRoot() {
212 return ImmutableList.of();
214 return parent == null ? ImmutableList.of(qname) : new PathFromRoot(this);
218 * Returns the list of nodes which need to be traversed to get from this
219 * node to the starting point (root for absolute SchemaPaths).
221 * @return list of <code>qname</code> instances which represents
222 * path from the schema node towards the root.
224 public Iterable<QName> getPathTowardsRoot() {
225 return () -> new UnmodifiableIterator<>() {
226 private SchemaPath current = SchemaPath.this;
229 public boolean hasNext() {
230 return current.parent != null;
234 public QName next() {
235 if (current.parent != null) {
236 final QName ret = current.qname;
237 current = current.parent;
241 throw new NoSuchElementException("No more elements available");
247 * Returns the immediate parent SchemaPath.
249 * @return Parent path, null if this SchemaPath is already toplevel.
251 public SchemaPath getParent() {
256 * Get the last component of this path.
258 * @return The last component of this path.
260 public final QName getLastComponent() {
265 * Describes whether schema path is|isn't absolute.
267 * @return boolean value which is <code>true</code> if schema path is
270 public abstract boolean isAbsolute();
273 public final int hashCode() {
278 public boolean equals(final Object obj) {
285 if (getClass() != obj.getClass()) {
288 final SchemaPath other = (SchemaPath) obj;
289 return Objects.equals(qname, other.qname) && Objects.equals(parent, other.parent);
293 public final String toString() {
294 return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
297 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
298 return toStringHelper.add("path", getPathFromRoot());