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 // FIXME: 5.0.0: expose as a top-level construct (and use in APIs)
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 // FIXME: 5.0.0: expose as a top-level construct (and use in APIs)
58 private static final class RelativeSchemaPath extends SchemaPath {
59 private RelativeSchemaPath(final SchemaPath parent, final QName qname) {
64 public boolean isAbsolute() {
69 public RelativeSchemaPath createChild(final QName element) {
70 return new RelativeSchemaPath(this, requireNonNull(element));
74 @SuppressWarnings("rawtypes")
75 private static final AtomicReferenceFieldUpdater<SchemaPath, ImmutableList> LEGACYPATH_UPDATER =
76 AtomicReferenceFieldUpdater.newUpdater(SchemaPath.class, ImmutableList.class, "legacyPath");
79 * Shared instance of the conceptual root schema node.
81 public static final @NonNull SchemaPath ROOT = new AbsoluteSchemaPath(null, null);
84 * Shared instance of the "same" relative schema node.
86 public static final @NonNull SchemaPath SAME = new RelativeSchemaPath(null, null);
91 private final SchemaPath parent;
96 private final QName qname;
99 * Cached hash code. We can use this since we are immutable.
101 private final int hash;
104 * Cached legacy path, filled-in when {@link #getPath()} or {@link #getPathTowardsRoot()}
107 private volatile ImmutableList<QName> legacyPath;
109 SchemaPath(final SchemaPath parent, final QName qname) {
110 this.parent = parent;
113 int tmp = Objects.hashCode(parent);
115 tmp = tmp * 31 + qname.hashCode();
121 private ImmutableList<QName> getLegacyPath() {
122 ImmutableList<QName> ret = legacyPath;
124 final List<QName> tmp = new ArrayList<>();
125 for (QName item : getPathTowardsRoot()) {
128 ret = ImmutableList.copyOf(Lists.reverse(tmp));
129 LEGACYPATH_UPDATER.lazySet(this, ret);
136 * Returns the complete path to schema node.
138 * @return list of <code>QName</code> instances which represents complete
139 * path to schema node
141 * @deprecated Use {@link #getPathFromRoot()} instead.
143 @Deprecated(forRemoval = true)
144 public List<QName> getPath() {
145 return getLegacyPath();
149 * Constructs new instance of this class with the concrete path.
152 * list of QName instances which specifies exact path to the
155 * boolean value which specifies if the path is absolute or
158 * @return A SchemaPath instance.
160 public static @NonNull SchemaPath create(final Iterable<QName> path, final boolean absolute) {
161 return (absolute ? ROOT : SAME).createChild(path);
165 * Constructs new instance of this class with the concrete path.
168 * boolean value which specifies if the path is absolute or
171 * a single QName which specifies exact path to the
174 * @return A SchemaPath instance.
176 public static @NonNull SchemaPath create(final boolean absolute, final QName element) {
177 return (absolute ? ROOT : SAME).createChild(element);
181 * Constructs new instance of this class with the concrete path.
184 * boolean value which specifies if the path is absolute or
187 * one or more QName instances which specifies exact path to the
190 * @return A SchemaPath instance.
192 public static @NonNull SchemaPath create(final boolean absolute, final QName... path) {
193 return create(Arrays.asList(path), absolute);
197 * Create a child path based on concatenation of this path and a relative path.
199 * @param relative Relative path
200 * @return A new child path
202 public @NonNull SchemaPath createChild(final Iterable<QName> relative) {
203 if (Iterables.isEmpty(relative)) {
207 SchemaPath parentPath = this;
208 for (QName item : relative) {
209 parentPath = parentPath.createChild(item);
216 * Create a child path based on concatenation of this path and a relative path.
218 * @param relative Relative SchemaPath
219 * @return A new child path
221 public @NonNull SchemaPath createChild(final SchemaPath relative) {
222 checkArgument(!relative.isAbsolute(), "Child creation requires relative path");
223 return createChild(relative.getPathFromRoot());
227 * Create a child path based on concatenation of this path and an additional path element.
229 * @param element Relative SchemaPath elements
230 * @return A new child path
232 public abstract @NonNull SchemaPath createChild(QName element);
235 * Create a child path based on concatenation of this path and additional
238 * @param elements Relative SchemaPath elements
239 * @return A new child path
241 public @NonNull SchemaPath createChild(final QName... elements) {
242 return createChild(Arrays.asList(elements));
246 * Returns the list of nodes which need to be traversed to get from the
247 * starting point (root for absolute SchemaPaths) to the node represented
250 * @return list of <code>qname</code> instances which represents
251 * path from the root to the schema node.
253 public Iterable<QName> getPathFromRoot() {
254 return getLegacyPath();
258 * Returns the list of nodes which need to be traversed to get from this
259 * node to the starting point (root for absolute SchemaPaths).
261 * @return list of <code>qname</code> instances which represents
262 * path from the schema node towards the root.
264 public Iterable<QName> getPathTowardsRoot() {
265 return () -> new UnmodifiableIterator<>() {
266 private SchemaPath current = SchemaPath.this;
269 public boolean hasNext() {
270 return current.parent != null;
274 public QName next() {
275 if (current.parent != null) {
276 final QName ret = current.qname;
277 current = current.parent;
281 throw new NoSuchElementException("No more elements available");
287 * Returns the immediate parent SchemaPath.
289 * @return Parent path, null if this SchemaPath is already toplevel.
291 public SchemaPath getParent() {
296 * Get the last component of this path.
298 * @return The last component of this path.
300 public final QName getLastComponent() {
305 * Describes whether schema path is|isn't absolute.
307 * @return boolean value which is <code>true</code> if schema path is
310 public abstract boolean isAbsolute();
313 public final int hashCode() {
318 public boolean equals(final Object obj) {
325 if (getClass() != obj.getClass()) {
328 final SchemaPath other = (SchemaPath) obj;
329 return Objects.equals(qname, other.qname) && Objects.equals(parent, other.parent);
333 public final String toString() {
334 return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
337 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
338 return toStringHelper.add("path", getPathFromRoot());