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 com.google.common.base.MoreObjects;
11 import com.google.common.base.MoreObjects.ToStringHelper;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.ImmutableList;
14 import com.google.common.collect.Iterables;
15 import com.google.common.collect.Lists;
16 import com.google.common.collect.UnmodifiableIterator;
17 import java.util.ArrayList;
18 import java.util.Arrays;
19 import java.util.List;
20 import java.util.NoSuchElementException;
21 import java.util.Objects;
22 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
23 import org.opendaylight.yangtools.concepts.Immutable;
24 import org.opendaylight.yangtools.yang.common.QName;
27 * Represents unique path to the every node inside the module.
29 public abstract class SchemaPath implements Immutable {
32 * An absolute SchemaPath.
34 private static final class AbsoluteSchemaPath extends SchemaPath {
35 private AbsoluteSchemaPath(final SchemaPath parent, final QName qname) {
40 public boolean isAbsolute() {
45 protected SchemaPath createInstance(final SchemaPath parent, final QName qname) {
46 return new AbsoluteSchemaPath(parent, Preconditions.checkNotNull(qname));
51 * A relative SchemaPath.
53 private static final class RelativeSchemaPath extends SchemaPath {
54 private RelativeSchemaPath(final SchemaPath parent, final QName qname) {
59 public boolean isAbsolute() {
64 protected SchemaPath createInstance(final SchemaPath parent, final QName qname) {
65 return new RelativeSchemaPath(parent, Preconditions.checkNotNull(qname));
69 @SuppressWarnings("rawtypes")
70 private static final AtomicReferenceFieldUpdater<SchemaPath, ImmutableList> LEGACYPATH_UPDATER =
71 AtomicReferenceFieldUpdater.newUpdater(SchemaPath.class, ImmutableList.class, "legacyPath");
74 * Shared instance of the conceptual root schema node.
76 public static final SchemaPath ROOT = new AbsoluteSchemaPath(null, null);
79 * Shared instance of the "same" relative schema node.
81 public static final 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;
99 * Cached legacy path, filled-in when {@link #getPath()} or {@link #getPathTowardsRoot()}
102 private volatile ImmutableList<QName> legacyPath;
104 SchemaPath(final SchemaPath parent, final QName qname) {
105 this.parent = parent;
108 int tmp = Objects.hashCode(parent);
110 tmp = tmp * 31 + qname.hashCode();
116 private ImmutableList<QName> getLegacyPath() {
117 ImmutableList<QName> ret = legacyPath;
119 final List<QName> tmp = new ArrayList<>();
120 for (QName qname : getPathTowardsRoot()) {
123 ret = ImmutableList.copyOf(Lists.reverse(tmp));
124 LEGACYPATH_UPDATER.lazySet(this, ret);
131 * Returns the complete path to schema node.
133 * @return list of <code>QName</code> instances which represents complete
134 * path to schema node
136 * @deprecated Use {@link #getPathFromRoot()} instead.
139 public List<QName> getPath() {
140 return getLegacyPath();
144 * Constructs new instance of this class with the concrete path.
147 * list of QName instances which specifies exact path to the
150 * boolean value which specifies if the path is absolute or
153 * @return A SchemaPath instance.
155 public static SchemaPath create(final Iterable<QName> path, final boolean absolute) {
156 final SchemaPath parent = absolute ? ROOT : SAME;
157 return parent.createChild(path);
161 * Constructs new instance of this class with the concrete path.
164 * boolean value which specifies if the path is absolute or
167 * one or more QName instances which specifies exact path to the
170 * @return A SchemaPath instance.
172 public static SchemaPath create(final boolean absolute, final QName... path) {
173 return create(Arrays.asList(path), absolute);
177 * Create a new instance.
179 * @param parent Parent SchemaPath
180 * @param qname next path element
181 * @return A new SchemaPath instance
183 protected abstract SchemaPath createInstance(SchemaPath parent, QName qname);
186 * Create a child path based on concatenation of this path and a relative path.
188 * @param relative Relative path
189 * @return A new child path
191 public SchemaPath createChild(final Iterable<QName> relative) {
192 if (Iterables.isEmpty(relative)) {
196 SchemaPath parentPath = this;
197 for (QName qname : relative) {
198 parentPath = parentPath.createInstance(parentPath, qname);
205 * Create a child path based on concatenation of this path and a relative path.
207 * @param relative Relative SchemaPath
208 * @return A new child path
210 public SchemaPath createChild(final SchemaPath relative) {
211 Preconditions.checkArgument(!relative.isAbsolute(), "Child creation requires relative path");
213 SchemaPath parentPath = this;
214 for (QName qname : relative.getPathFromRoot()) {
215 parentPath = parentPath.createInstance(parentPath, qname);
222 * Create a child path based on concatenation of this path and additional
225 * @param elements Relative SchemaPath elements
226 * @return A new child path
228 public SchemaPath createChild(final QName... elements) {
229 return createChild(Arrays.asList(elements));
233 * Returns the list of nodes which need to be traversed to get from the
234 * starting point (root for absolute SchemaPaths) to the node represented
237 * @return list of <code>qname</code> instances which represents
238 * path from the root to the schema node.
240 public Iterable<QName> getPathFromRoot() {
241 return getLegacyPath();
245 * Returns the list of nodes which need to be traversed to get from this
246 * node to the starting point (root for absolute SchemaPaths).
248 * @return list of <code>qname</code> instances which represents
249 * path from the schema node towards the root.
251 public Iterable<QName> getPathTowardsRoot() {
252 return () -> new UnmodifiableIterator<QName>() {
253 private SchemaPath current = SchemaPath.this;
256 public boolean hasNext() {
257 return current.parent != null;
261 public QName next() {
262 if (current.parent != null) {
263 final QName ret = current.qname;
264 current = current.parent;
267 throw new NoSuchElementException("No more elements available");
274 * Returns the immediate parent SchemaPath.
276 * @return Parent path, null if this SchemaPath is already toplevel.
278 public SchemaPath getParent() {
283 * Get the last component of this path.
285 * @return The last component of this path.
287 public final QName getLastComponent() {
292 * Describes whether schema path is|isn't absolute.
294 * @return boolean value which is <code>true</code> if schema path is
297 public abstract boolean isAbsolute();
300 public final int hashCode() {
305 public boolean equals(final Object obj) {
312 if (getClass() != obj.getClass()) {
315 final SchemaPath other = (SchemaPath) obj;
316 return Objects.equals(qname, other.qname) && Objects.equals(parent, other.parent);
320 public final String toString() {
321 return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
324 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
325 return toStringHelper.add("path", getPathFromRoot());