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 protected SchemaPath createInstance(final SchemaPath parent, final QName qname) {
48 return new AbsoluteSchemaPath(parent, requireNonNull(qname));
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 protected SchemaPath createInstance(final SchemaPath parent, final QName qname) {
67 return new RelativeSchemaPath(parent, requireNonNull(qname));
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 qname : 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 new instance.
181 * @param parent Parent SchemaPath
182 * @param qname next path element
183 * @return A new SchemaPath instance
185 protected abstract SchemaPath createInstance(SchemaPath parent, QName qname);
188 * Create a child path based on concatenation of this path and a relative path.
190 * @param relative Relative path
191 * @return A new child path
193 public SchemaPath createChild(final Iterable<QName> relative) {
194 if (Iterables.isEmpty(relative)) {
198 SchemaPath parentPath = this;
199 for (QName qname : relative) {
200 parentPath = parentPath.createInstance(parentPath, qname);
207 * Create a child path based on concatenation of this path and a relative path.
209 * @param relative Relative SchemaPath
210 * @return A new child path
212 public SchemaPath createChild(final SchemaPath relative) {
213 checkArgument(!relative.isAbsolute(), "Child creation requires relative path");
215 SchemaPath parentPath = this;
216 for (QName qname : relative.getPathFromRoot()) {
217 parentPath = parentPath.createInstance(parentPath, qname);
224 * Create a child path based on concatenation of this path and additional
227 * @param elements Relative SchemaPath elements
228 * @return A new child path
230 public SchemaPath createChild(final QName... elements) {
231 return createChild(Arrays.asList(elements));
235 * Returns the list of nodes which need to be traversed to get from the
236 * starting point (root for absolute SchemaPaths) to the node represented
239 * @return list of <code>qname</code> instances which represents
240 * path from the root to the schema node.
242 public Iterable<QName> getPathFromRoot() {
243 return getLegacyPath();
247 * Returns the list of nodes which need to be traversed to get from this
248 * node to the starting point (root for absolute SchemaPaths).
250 * @return list of <code>qname</code> instances which represents
251 * path from the schema node towards the root.
253 public Iterable<QName> getPathTowardsRoot() {
254 return () -> new UnmodifiableIterator<QName>() {
255 private SchemaPath current = SchemaPath.this;
258 public boolean hasNext() {
259 return current.parent != null;
263 public QName next() {
264 if (current.parent != null) {
265 final QName ret = current.qname;
266 current = current.parent;
269 throw new NoSuchElementException("No more elements available");
276 * Returns the immediate parent SchemaPath.
278 * @return Parent path, null if this SchemaPath is already toplevel.
280 public SchemaPath getParent() {
285 * Get the last component of this path.
287 * @return The last component of this path.
289 public final QName getLastComponent() {
294 * Describes whether schema path is|isn't absolute.
296 * @return boolean value which is <code>true</code> if schema path is
299 public abstract boolean isAbsolute();
302 public final int hashCode() {
307 public boolean equals(final Object obj) {
314 if (getClass() != obj.getClass()) {
317 final SchemaPath other = (SchemaPath) obj;
318 return Objects.equals(qname, other.qname) && Objects.equals(parent, other.parent);
322 public final String toString() {
323 return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
326 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
327 return toStringHelper.add("path", getPathFromRoot());