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.UnmodifiableIterator;
16 import java.util.Arrays;
17 import java.util.Iterator;
18 import java.util.List;
19 import java.util.NoSuchElementException;
20 import java.util.Objects;
21 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
22 import org.opendaylight.yangtools.concepts.Immutable;
23 import org.opendaylight.yangtools.yang.common.QName;
26 * Represents unique path to the every node inside the module.
28 public abstract class SchemaPath implements Immutable {
31 * An absolute SchemaPath.
33 private static final class AbsoluteSchemaPath extends SchemaPath {
34 private AbsoluteSchemaPath(final SchemaPath parent, final QName qname) {
39 public boolean isAbsolute() {
44 protected SchemaPath createInstance(final SchemaPath parent, final QName qname) {
45 return new AbsoluteSchemaPath(parent, qname);
50 * A relative SchemaPath.
52 private static final class RelativeSchemaPath extends SchemaPath {
53 private RelativeSchemaPath(final SchemaPath parent, final QName qname) {
58 public boolean isAbsolute() {
63 protected SchemaPath createInstance(final SchemaPath parent, final QName qname) {
64 return new RelativeSchemaPath(parent, qname);
68 @SuppressWarnings("rawtypes")
69 private static final AtomicReferenceFieldUpdater<SchemaPath, ImmutableList> LEGACYPATH_UPDATER =
70 AtomicReferenceFieldUpdater.newUpdater(SchemaPath.class, ImmutableList.class, "legacyPath");
73 * Shared instance of the conceptual root schema node.
75 public static final SchemaPath ROOT = new AbsoluteSchemaPath(null, null);
78 * Shared instance of the "same" relative schema node.
80 public static final SchemaPath SAME = new RelativeSchemaPath(null, null);
85 private final SchemaPath parent;
90 private final QName qname;
93 * Cached hash code. We can use this since we are immutable.
95 private final int hash;
98 * Cached legacy path, filled-in when {@link #getPath()} or {@link #getPathTowardsRoot()}
101 private volatile ImmutableList<QName> legacyPath;
103 protected SchemaPath(final SchemaPath parent, final QName qname) {
104 this.parent = parent;
107 int h = Objects.hashCode(parent);
109 h = h * 31 + qname.hashCode();
115 private ImmutableList<QName> getLegacyPath() {
116 ImmutableList<QName> ret = legacyPath;
118 ret = ImmutableList.copyOf(getPathTowardsRoot()).reverse();
119 LEGACYPATH_UPDATER.lazySet(this, ret);
126 * Returns the complete path to schema node.
128 * @return list of <code>QName</code> instances which represents complete
129 * path to schema node
131 * @deprecated Use {@link #getPathFromRoot()} instead.
134 public List<QName> getPath() {
135 return getLegacyPath();
139 * Constructs new instance of this class with the concrete path.
142 * list of QName instances which specifies exact path to the
145 * boolean value which specifies if the path is absolute or
148 * @return A SchemaPath instance.
150 public static SchemaPath create(final Iterable<QName> path, final boolean absolute) {
151 final SchemaPath parent = absolute ? ROOT : SAME;
152 return parent.createChild(path);
156 * Constructs new instance of this class with the concrete path.
159 * boolean value which specifies if the path is absolute or
162 * one or more QName instances which specifies exact path to the
165 * @return A SchemaPath instance.
167 public static SchemaPath create(final boolean absolute, final QName... path) {
168 return create(Arrays.asList(path), absolute);
172 * Create a new instance.
174 * @param parent Parent SchemaPath
175 * @param qname next path element
176 * @return A new SchemaPath instance
178 protected abstract SchemaPath createInstance(SchemaPath parent, QName qname);
181 * Create a child path based on concatenation of this path and a relative path.
183 * @param relative Relative path
184 * @return A new child path
186 public SchemaPath createChild(final Iterable<QName> relative) {
187 if (Iterables.isEmpty(relative)) {
191 SchemaPath parentPath = this;
192 for (QName qname : relative) {
193 parentPath = parentPath.createInstance(parentPath, qname);
200 * Create a child path based on concatenation of this path and a relative path.
202 * @param relative Relative SchemaPath
203 * @return A new child path
205 public SchemaPath createChild(final SchemaPath relative) {
206 Preconditions.checkArgument(!relative.isAbsolute(), "Child creation requires relative path");
208 SchemaPath parentPath = this;
209 for (QName qname : relative.getPathFromRoot()) {
210 parentPath = parentPath.createInstance(parentPath, qname);
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 Iterable<QName>() {
249 public Iterator<QName> iterator() {
250 return new UnmodifiableIterator<QName>() {
251 private SchemaPath current = SchemaPath.this;
254 public boolean hasNext() {
255 return current.parent != null;
259 public QName next() {
260 if (current.parent != null) {
261 final QName ret = current.qname;
262 current = current.parent;
265 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());