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, Preconditions.checkNotNull(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, Preconditions.checkNotNull(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;
104 * @deprecated This constructor will be hidden in a future release.
109 protected SchemaPath(final SchemaPath parent, final QName qname) {
110 this.parent = parent;
113 int h = Objects.hashCode(parent);
115 h = h * 31 + qname.hashCode();
121 private ImmutableList<QName> getLegacyPath() {
122 ImmutableList<QName> ret = legacyPath;
124 ret = ImmutableList.copyOf(getPathTowardsRoot()).reverse();
125 LEGACYPATH_UPDATER.lazySet(this, ret);
132 * Returns the complete path to schema node.
134 * @return list of <code>QName</code> instances which represents complete
135 * path to schema node
137 * @deprecated Use {@link #getPathFromRoot()} instead.
140 public List<QName> getPath() {
141 return getLegacyPath();
145 * Constructs new instance of this class with the concrete path.
148 * list of QName instances which specifies exact path to the
151 * boolean value which specifies if the path is absolute or
154 * @return A SchemaPath instance.
156 public static SchemaPath create(final Iterable<QName> path, final boolean absolute) {
157 final SchemaPath parent = absolute ? ROOT : SAME;
158 return parent.createChild(path);
162 * Constructs new instance of this class with the concrete path.
165 * boolean value which specifies if the path is absolute or
168 * one or more QName instances which specifies exact path to the
171 * @return A SchemaPath instance.
173 public static SchemaPath create(final boolean absolute, final QName... path) {
174 return create(Arrays.asList(path), absolute);
178 * Create a new instance.
180 * @param parent Parent SchemaPath
181 * @param qname next path element
182 * @return A new SchemaPath instance
184 protected abstract SchemaPath createInstance(SchemaPath parent, QName qname);
187 * Create a child path based on concatenation of this path and a relative path.
189 * @param relative Relative path
190 * @return A new child path
192 public SchemaPath createChild(final Iterable<QName> relative) {
193 if (Iterables.isEmpty(relative)) {
197 SchemaPath parentPath = this;
198 for (QName qname : relative) {
199 parentPath = parentPath.createInstance(parentPath, qname);
206 * Create a child path based on concatenation of this path and a relative path.
208 * @param relative Relative SchemaPath
209 * @return A new child path
211 public SchemaPath createChild(final SchemaPath relative) {
212 Preconditions.checkArgument(!relative.isAbsolute(), "Child creation requires relative path");
214 SchemaPath parentPath = this;
215 for (QName qname : relative.getPathFromRoot()) {
216 parentPath = parentPath.createInstance(parentPath, qname);
223 * Create a child path based on concatenation of this path and additional
226 * @param elements Relative SchemaPath elements
227 * @return A new child path
229 public SchemaPath createChild(final QName... elements) {
230 return createChild(Arrays.asList(elements));
234 * Returns the list of nodes which need to be traversed to get from the
235 * starting point (root for absolute SchemaPaths) to the node represented
238 * @return list of <code>qname</code> instances which represents
239 * path from the root to the schema node.
241 public Iterable<QName> getPathFromRoot() {
242 return getLegacyPath();
246 * Returns the list of nodes which need to be traversed to get from this
247 * node to the starting point (root for absolute SchemaPaths).
249 * @return list of <code>qname</code> instances which represents
250 * path from the schema node towards the root.
252 public Iterable<QName> getPathTowardsRoot() {
253 return new Iterable<QName>() {
255 public Iterator<QName> iterator() {
256 return new UnmodifiableIterator<QName>() {
257 private SchemaPath current = SchemaPath.this;
260 public boolean hasNext() {
261 return current.parent != null;
265 public QName next() {
266 if (current.parent != null) {
267 final QName ret = current.qname;
268 current = current.parent;
271 throw new NoSuchElementException("No more elements available");
280 * Returns the immediate parent SchemaPath.
282 * @return Parent path, null if this SchemaPath is already toplevel.
284 public SchemaPath getParent() {
289 * Get the last component of this path.
291 * @return The last component of this path.
293 public final QName getLastComponent() {
298 * Describes whether schema path is|isn't absolute.
300 * @return boolean value which is <code>true</code> if schema path is
303 public abstract boolean isAbsolute();
306 public final int hashCode() {
311 public boolean equals(final Object obj) {
318 if (getClass() != obj.getClass()) {
321 final SchemaPath other = (SchemaPath) obj;
322 return Objects.equals(qname, other.qname) && Objects.equals(parent, other.parent);
326 public final String toString() {
327 return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
330 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
331 return toStringHelper.add("path", getPathFromRoot());