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.Lists;
18 import com.google.common.collect.UnmodifiableIterator;
19 import java.util.ArrayList;
20 import java.util.Arrays;
21 import java.util.List;
22 import java.util.NoSuchElementException;
23 import java.util.Objects;
24 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
25 import org.eclipse.jdt.annotation.NonNull;
26 import org.opendaylight.yangtools.concepts.Immutable;
27 import org.opendaylight.yangtools.yang.common.QName;
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));
72 @SuppressWarnings("rawtypes")
73 private static final AtomicReferenceFieldUpdater<SchemaPath, ImmutableList> LEGACYPATH_UPDATER =
74 AtomicReferenceFieldUpdater.newUpdater(SchemaPath.class, ImmutableList.class, "legacyPath");
77 * Shared instance of the conceptual root schema node.
79 public static final @NonNull SchemaPath ROOT = new AbsoluteSchemaPath(null, null);
82 * Shared instance of the "same" relative schema node.
84 public static final @NonNull SchemaPath SAME = new RelativeSchemaPath(null, null);
89 private final SchemaPath parent;
94 private final QName qname;
97 * Cached hash code. We can use this since we are immutable.
99 private final int hash;
102 * Cached legacy path, filled-in when {@link #getPath()} or {@link #getPathTowardsRoot()}
105 private volatile ImmutableList<QName> legacyPath;
107 SchemaPath(final SchemaPath parent, final QName qname) {
108 this.parent = parent;
111 int tmp = Objects.hashCode(parent);
113 tmp = tmp * 31 + qname.hashCode();
119 private ImmutableList<QName> getLegacyPath() {
120 ImmutableList<QName> ret = legacyPath;
122 final List<QName> tmp = new ArrayList<>();
123 for (QName item : getPathTowardsRoot()) {
126 ret = ImmutableList.copyOf(Lists.reverse(tmp));
127 LEGACYPATH_UPDATER.lazySet(this, ret);
134 * Returns the complete path to schema node.
136 * @return list of <code>QName</code> instances which represents complete
137 * path to schema node
139 * @deprecated Use {@link #getPathFromRoot()} instead.
142 public List<QName> getPath() {
143 return getLegacyPath();
147 * Constructs new instance of this class with the concrete path.
150 * list of QName instances which specifies exact path to the
153 * boolean value which specifies if the path is absolute or
156 * @return A SchemaPath instance.
158 public static @NonNull SchemaPath create(final Iterable<QName> path, final boolean absolute) {
159 return (absolute ? ROOT : SAME).createChild(path);
163 * Constructs new instance of this class with the concrete path.
166 * boolean value which specifies if the path is absolute or
169 * a single QName which specifies exact path to the
172 * @return A SchemaPath instance.
174 public static @NonNull SchemaPath create(final boolean absolute, final QName element) {
175 return (absolute ? ROOT : SAME).createChild(element);
179 * Constructs new instance of this class with the concrete path.
182 * boolean value which specifies if the path is absolute or
185 * one or more QName instances which specifies exact path to the
188 * @return A SchemaPath instance.
190 public static @NonNull SchemaPath create(final boolean absolute, final QName... path) {
191 return create(Arrays.asList(path), absolute);
195 * Create a child path based on concatenation of this path and a relative path.
197 * @param relative Relative path
198 * @return A new child path
200 public @NonNull SchemaPath createChild(final Iterable<QName> relative) {
201 if (Iterables.isEmpty(relative)) {
205 SchemaPath parentPath = this;
206 for (QName item : relative) {
207 parentPath = parentPath.createChild(item);
214 * Create a child path based on concatenation of this path and a relative path.
216 * @param relative Relative SchemaPath
217 * @return A new child path
219 public @NonNull SchemaPath createChild(final SchemaPath relative) {
220 checkArgument(!relative.isAbsolute(), "Child creation requires relative path");
221 return createChild(relative.getPathFromRoot());
225 * Create a child path based on concatenation of this path and an additional path element.
227 * @param element Relative SchemaPath elements
228 * @return A new child path
230 public abstract @NonNull SchemaPath createChild(QName element);
233 * Create a child path based on concatenation of this path and additional
236 * @param elements Relative SchemaPath elements
237 * @return A new child path
239 public @NonNull SchemaPath createChild(final QName... elements) {
240 return createChild(Arrays.asList(elements));
244 * Returns the list of nodes which need to be traversed to get from the
245 * starting point (root for absolute SchemaPaths) to the node represented
248 * @return list of <code>qname</code> instances which represents
249 * path from the root to the schema node.
251 public Iterable<QName> getPathFromRoot() {
252 return getLegacyPath();
256 * Returns the list of nodes which need to be traversed to get from this
257 * node to the starting point (root for absolute SchemaPaths).
259 * @return list of <code>qname</code> instances which represents
260 * path from the schema node towards the root.
262 public Iterable<QName> getPathTowardsRoot() {
263 return () -> new UnmodifiableIterator<QName>() {
264 private SchemaPath current = SchemaPath.this;
267 public boolean hasNext() {
268 return current.parent != null;
272 public QName next() {
273 if (current.parent != null) {
274 final QName ret = current.qname;
275 current = current.parent;
279 throw new NoSuchElementException("No more elements available");
285 * Returns the immediate parent SchemaPath.
287 * @return Parent path, null if this SchemaPath is already toplevel.
289 public SchemaPath getParent() {
294 * Get the last component of this path.
296 * @return The last component of this path.
298 public final QName getLastComponent() {
303 * Describes whether schema path is|isn't absolute.
305 * @return boolean value which is <code>true</code> if schema path is
308 public abstract boolean isAbsolute();
311 public final int hashCode() {
316 public boolean equals(final Object obj) {
323 if (getClass() != obj.getClass()) {
326 final SchemaPath other = (SchemaPath) obj;
327 return Objects.equals(qname, other.qname) && Objects.equals(parent, other.parent);
331 public final String toString() {
332 return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
335 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
336 return toStringHelper.add("path", getPathFromRoot());