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 // FIXME: 5.0.0: expose as a top-level construct (and use in APIs)
34 private static final class AbsoluteSchemaPath extends SchemaPath {
35 private AbsoluteSchemaPath(final SchemaPath parent, final QName qname) {
40 public boolean isAbsolute() {
45 public AbsoluteSchemaPath createChild(final QName element) {
46 return new AbsoluteSchemaPath(this, requireNonNull(element));
51 * A relative SchemaPath.
53 // FIXME: 5.0.0: expose as a top-level construct (and use in APIs)
54 private static final class RelativeSchemaPath extends SchemaPath {
55 private RelativeSchemaPath(final SchemaPath parent, final QName qname) {
60 public boolean isAbsolute() {
65 public RelativeSchemaPath createChild(final QName element) {
66 return new RelativeSchemaPath(this, requireNonNull(element));
71 * Shared instance of the conceptual root schema node.
73 public static final @NonNull SchemaPath ROOT = new AbsoluteSchemaPath(null, null);
76 * Shared instance of the "same" relative schema node.
78 public static final @NonNull SchemaPath SAME = new RelativeSchemaPath(null, null);
83 private final SchemaPath parent;
88 private final QName qname;
91 * Cached hash code. We can use this since we are immutable.
93 private final int hash;
95 SchemaPath(final SchemaPath parent, final QName qname) {
99 int tmp = Objects.hashCode(parent);
101 tmp = tmp * 31 + qname.hashCode();
108 * Constructs new instance of this class with the concrete path.
111 * list of QName instances which specifies exact path to the
114 * boolean value which specifies if the path is absolute or
117 * @return A SchemaPath instance.
119 public static @NonNull SchemaPath create(final Iterable<QName> path, final boolean absolute) {
120 return (absolute ? ROOT : SAME).createChild(path);
124 * Constructs new instance of this class with the concrete path.
127 * boolean value which specifies if the path is absolute or
130 * a single QName which specifies exact path to the
133 * @return A SchemaPath instance.
135 public static @NonNull SchemaPath create(final boolean absolute, final QName element) {
136 return (absolute ? ROOT : SAME).createChild(element);
140 * Constructs new instance of this class with the concrete path.
143 * boolean value which specifies if the path is absolute or
146 * one or more QName instances which specifies exact path to the
149 * @return A SchemaPath instance.
151 public static @NonNull SchemaPath create(final boolean absolute, final QName... path) {
152 return create(Arrays.asList(path), absolute);
156 * Create a child path based on concatenation of this path and a relative path.
158 * @param relative Relative path
159 * @return A new child path
161 public @NonNull SchemaPath createChild(final Iterable<QName> relative) {
162 if (Iterables.isEmpty(relative)) {
166 SchemaPath parentPath = this;
167 for (QName item : relative) {
168 parentPath = parentPath.createChild(item);
175 * Create a child path based on concatenation of this path and a relative path.
177 * @param relative Relative SchemaPath
178 * @return A new child path
180 public @NonNull SchemaPath createChild(final SchemaPath relative) {
181 checkArgument(!relative.isAbsolute(), "Child creation requires relative path");
182 return createChild(relative.getPathFromRoot());
186 * Create a child path based on concatenation of this path and an additional path element.
188 * @param element Relative SchemaPath elements
189 * @return A new child path
191 public abstract @NonNull SchemaPath createChild(QName element);
194 * Create a child path based on concatenation of this path and additional
197 * @param elements Relative SchemaPath elements
198 * @return A new child path
200 public @NonNull SchemaPath createChild(final QName... elements) {
201 return createChild(Arrays.asList(elements));
205 * Returns the list of nodes which need to be traversed to get from the
206 * starting point (root for absolute SchemaPaths) to the node represented
209 * @return list of <code>qname</code> instances which represents
210 * path from the root to the schema node.
212 public Iterable<QName> getPathFromRoot() {
214 return ImmutableList.of();
216 return parent == null ? ImmutableList.of(qname) : new PathFromRoot(this);
220 * Returns the list of nodes which need to be traversed to get from this
221 * node to the starting point (root for absolute SchemaPaths).
223 * @return list of <code>qname</code> instances which represents
224 * path from the schema node towards the root.
226 public Iterable<QName> getPathTowardsRoot() {
227 return () -> new UnmodifiableIterator<>() {
228 private SchemaPath current = SchemaPath.this;
231 public boolean hasNext() {
232 return current.parent != null;
236 public QName next() {
237 if (current.parent != null) {
238 final QName ret = current.qname;
239 current = current.parent;
243 throw new NoSuchElementException("No more elements available");
249 * Returns the immediate parent SchemaPath.
251 * @return Parent path, null if this SchemaPath is already toplevel.
253 public SchemaPath getParent() {
258 * Get the last component of this path.
260 * @return The last component of this path.
262 public final QName getLastComponent() {
267 * Describes whether schema path is|isn't absolute.
269 * @return boolean value which is <code>true</code> if schema path is
272 public abstract boolean isAbsolute();
275 public final int hashCode() {
280 public boolean equals(final Object obj) {
287 if (getClass() != obj.getClass()) {
290 final SchemaPath other = (SchemaPath) obj;
291 return Objects.equals(qname, other.qname) && Objects.equals(parent, other.parent);
295 public final String toString() {
296 return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
299 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
300 return toStringHelper.add("path", getPathFromRoot());