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.opendaylight.yangtools.concepts.Immutable;
26 import org.opendaylight.yangtools.yang.common.QName;
29 * Represents unique path to the every node inside the module.
31 public abstract class SchemaPath implements Immutable {
34 * An absolute SchemaPath.
36 private static final class AbsoluteSchemaPath extends SchemaPath {
37 private AbsoluteSchemaPath(final SchemaPath parent, final QName qname) {
42 public boolean isAbsolute() {
47 public AbsoluteSchemaPath createChild(final QName element) {
48 return new AbsoluteSchemaPath(this, requireNonNull(element));
53 * A relative SchemaPath.
55 private static final class RelativeSchemaPath extends SchemaPath {
56 private RelativeSchemaPath(final SchemaPath parent, final QName qname) {
61 public boolean isAbsolute() {
66 public RelativeSchemaPath createChild(final QName element) {
67 return new RelativeSchemaPath(this, requireNonNull(element));
71 @SuppressWarnings("rawtypes")
72 private static final AtomicReferenceFieldUpdater<SchemaPath, ImmutableList> LEGACYPATH_UPDATER =
73 AtomicReferenceFieldUpdater.newUpdater(SchemaPath.class, ImmutableList.class, "legacyPath");
76 * Shared instance of the conceptual root schema node.
78 public static final SchemaPath ROOT = new AbsoluteSchemaPath(null, null);
81 * Shared instance of the "same" relative schema node.
83 public static final SchemaPath SAME = new RelativeSchemaPath(null, null);
88 private final SchemaPath parent;
93 private final QName qname;
96 * Cached hash code. We can use this since we are immutable.
98 private final int hash;
101 * Cached legacy path, filled-in when {@link #getPath()} or {@link #getPathTowardsRoot()}
104 private volatile ImmutableList<QName> legacyPath;
106 SchemaPath(final SchemaPath parent, final QName qname) {
107 this.parent = parent;
110 int tmp = Objects.hashCode(parent);
112 tmp = tmp * 31 + qname.hashCode();
118 private ImmutableList<QName> getLegacyPath() {
119 ImmutableList<QName> ret = legacyPath;
121 final List<QName> tmp = new ArrayList<>();
122 for (QName item : getPathTowardsRoot()) {
125 ret = ImmutableList.copyOf(Lists.reverse(tmp));
126 LEGACYPATH_UPDATER.lazySet(this, ret);
133 * Returns the complete path to schema node.
135 * @return list of <code>QName</code> instances which represents complete
136 * path to schema node
138 * @deprecated Use {@link #getPathFromRoot()} instead.
141 public List<QName> getPath() {
142 return getLegacyPath();
146 * Constructs new instance of this class with the concrete path.
149 * list of QName instances which specifies exact path to the
152 * boolean value which specifies if the path is absolute or
155 * @return A SchemaPath instance.
157 public static SchemaPath create(final Iterable<QName> path, final boolean absolute) {
158 final SchemaPath parent = absolute ? ROOT : SAME;
159 return parent.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 * one or more QName instances which specifies exact path to the
172 * @return A SchemaPath instance.
174 public static SchemaPath create(final boolean absolute, final QName... path) {
175 return create(Arrays.asList(path), absolute);
179 * Create a child path based on concatenation of this path and a relative path.
181 * @param relative Relative path
182 * @return A new child path
184 public SchemaPath createChild(final Iterable<QName> relative) {
185 if (Iterables.isEmpty(relative)) {
189 SchemaPath parentPath = this;
190 for (QName item : relative) {
191 parentPath = parentPath.createChild(item);
198 * Create a child path based on concatenation of this path and a relative path.
200 * @param relative Relative SchemaPath
201 * @return A new child path
203 public SchemaPath createChild(final SchemaPath relative) {
204 checkArgument(!relative.isAbsolute(), "Child creation requires relative path");
205 return createChild(relative.getPathFromRoot());
209 * Create a child path based on concatenation of this path and an additional path element.
211 * @param element Relative SchemaPath elements
212 * @return A new child path
214 public abstract SchemaPath createChild(QName element);
217 * Create a child path based on concatenation of this path and additional
220 * @param elements Relative SchemaPath elements
221 * @return A new child path
223 public SchemaPath createChild(final QName... elements) {
224 return createChild(Arrays.asList(elements));
228 * Returns the list of nodes which need to be traversed to get from the
229 * starting point (root for absolute SchemaPaths) to the node represented
232 * @return list of <code>qname</code> instances which represents
233 * path from the root to the schema node.
235 public Iterable<QName> getPathFromRoot() {
236 return getLegacyPath();
240 * Returns the list of nodes which need to be traversed to get from this
241 * node to the starting point (root for absolute SchemaPaths).
243 * @return list of <code>qname</code> instances which represents
244 * path from the schema node towards the root.
246 public Iterable<QName> getPathTowardsRoot() {
247 return () -> new UnmodifiableIterator<QName>() {
248 private SchemaPath current = SchemaPath.this;
251 public boolean hasNext() {
252 return current.parent != null;
256 public QName next() {
257 if (current.parent != null) {
258 final QName ret = current.qname;
259 current = current.parent;
262 throw new NoSuchElementException("No more elements available");
269 * Returns the immediate parent SchemaPath.
271 * @return Parent path, null if this SchemaPath is already toplevel.
273 public SchemaPath getParent() {
278 * Get the last component of this path.
280 * @return The last component of this path.
282 public final QName getLastComponent() {
287 * Describes whether schema path is|isn't absolute.
289 * @return boolean value which is <code>true</code> if schema path is
292 public abstract boolean isAbsolute();
295 public final int hashCode() {
300 public boolean equals(final Object obj) {
307 if (getClass() != obj.getClass()) {
310 final SchemaPath other = (SchemaPath) obj;
311 return Objects.equals(qname, other.qname) && Objects.equals(parent, other.parent);
315 public final String toString() {
316 return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
319 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
320 return toStringHelper.add("path", getPathFromRoot());