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.Objects;
11 import com.google.common.base.Objects.ToStringHelper;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.ImmutableList;
14 import com.google.common.collect.Iterables;
16 import java.util.Arrays;
17 import java.util.Collections;
18 import java.util.List;
20 import org.opendaylight.yangtools.concepts.Immutable;
21 import org.opendaylight.yangtools.util.HashCodeBuilder;
22 import org.opendaylight.yangtools.yang.common.QName;
25 * Represents unique path to the every node inside the module.
27 public abstract class SchemaPath implements Immutable {
29 * An absolute SchemaPath.
31 private static final class AbsoluteSchemaPath extends SchemaPath {
32 private AbsoluteSchemaPath(final Iterable<QName> path, final int hash) {
37 public boolean isAbsolute() {
42 protected SchemaPath createInstance(final Iterable<QName> path, final int hash) {
43 return new AbsoluteSchemaPath(path, hash);
48 * A relative SchemaPath.
50 private static final class RelativeSchemaPath extends SchemaPath {
51 private RelativeSchemaPath(final Iterable<QName> path, final int hash) {
56 public boolean isAbsolute() {
61 protected SchemaPath createInstance(final Iterable<QName> path, final int hash) {
62 return new RelativeSchemaPath(path, hash);
67 * Shared instance of the conceptual root schema node.
69 public static final SchemaPath ROOT = new AbsoluteSchemaPath(Collections.<QName>emptyList(), Boolean.TRUE.hashCode());
72 * Shared instance of the "same" relative schema node.
74 public static final SchemaPath SAME = new RelativeSchemaPath(Collections.<QName>emptyList(), Boolean.FALSE.hashCode());
77 * List of QName instances which represents complete path to the node.
79 private final Iterable<QName> path;
82 * Cached hash code. We can use this since we are immutable.
84 private final int hash;
87 * Cached legacy path, filled-in when {@link #getPath()} or {@link #getPathTowardsRoot()}
90 private ImmutableList<QName> legacyPath;
92 private ImmutableList<QName> getLegacyPath() {
93 if (legacyPath == null) {
94 legacyPath = ImmutableList.copyOf(path);
101 * Returns the complete path to schema node.
103 * @return list of <code>QName</code> instances which represents complete
104 * path to schema node
106 * @deprecated Use {@link #getPathFromRoot()} instead.
109 public List<QName> getPath() {
110 return getLegacyPath();
113 protected SchemaPath(final Iterable<QName> path, final int hash) {
114 this.path = Preconditions.checkNotNull(path);
119 * Constructs new instance of this class with the concrete path.
122 * list of QName instances which specifies exact path to the
125 * boolean value which specifies if the path is absolute or
128 * @return A SchemaPath instance.
130 public static SchemaPath create(final Iterable<QName> path, final boolean absolute) {
131 final SchemaPath parent = absolute ? ROOT : SAME;
132 return parent.createChild(path);
136 * Constructs new instance of this class with the concrete path.
139 * boolean value which specifies if the path is absolute or
142 * one or more QName instances which specifies exact path to the
145 * @return A SchemaPath instance.
147 public static SchemaPath create(final boolean absolute, final QName... path) {
148 return create(Arrays.asList(path), absolute);
152 * Create a new instance.
154 * @param path path from root
155 * @param hash intended hash code
156 * @return A new SchemaPath instance
158 protected abstract SchemaPath createInstance(Iterable<QName> path, int hash);
160 private SchemaPath trustedCreateChild(final Iterable<QName> relative) {
161 if (Iterables.isEmpty(relative)) {
165 final HashCodeBuilder<QName> b = new HashCodeBuilder<>(hash);
166 for (QName p : relative) {
170 return createInstance(Iterables.concat(path, relative), b.toInstance());
174 * Create a child path based on concatenation of this path and a relative path.
176 * @param relative Relative path
177 * @return A new child path
179 public SchemaPath createChild(final Iterable<QName> relative) {
180 if (Iterables.isEmpty(relative)) {
184 return trustedCreateChild(ImmutableList.copyOf(relative));
188 * Create a child path based on concatenation of this path and a relative path.
190 * @param relative Relative SchemaPath
191 * @return A new child path
193 public SchemaPath createChild(final SchemaPath relative) {
194 Preconditions.checkArgument(!relative.isAbsolute(), "Child creation requires relative path");
195 return trustedCreateChild(relative.path);
199 * Create a child path based on concatenation of this path and additional
202 * @param elements Relative SchemaPath elements
203 * @return A new child path
205 public SchemaPath createChild(final QName... elements) {
206 return createChild(Arrays.asList(elements));
210 * Returns the list of nodes which need to be traversed to get from the
211 * starting point (root for absolute SchemaPaths) to the node represented
214 * @return list of <code>qname</code> instances which represents
215 * path from the root to the schema node.
217 public Iterable<QName> getPathFromRoot() {
222 * Returns the list of nodes which need to be traversed to get from this
223 * node to the starting point (root for absolute SchemaPaths).
225 * @return list of <code>qname</code> instances which represents
226 * path from the schema node towards the root.
228 public Iterable<QName> getPathTowardsRoot() {
229 return getLegacyPath().reverse();
233 * Returns the immediate parent SchemaPath.
235 * @return Parent path, null if this SchemaPath is already toplevel.
237 public SchemaPath getParent() {
238 final int size = Iterables.size(path);
240 final SchemaPath parent = isAbsolute() ? ROOT : SAME;
241 return parent.trustedCreateChild(Iterables.limit(path, size - 1));
248 * Describes whether schema path is|isn't absolute.
250 * @return boolean value which is <code>true</code> if schema path is
253 public abstract boolean isAbsolute();
256 public final int hashCode() {
261 public boolean equals(final Object obj) {
268 if (getClass() != obj.getClass()) {
271 SchemaPath other = (SchemaPath) obj;
272 return Iterables.elementsEqual(path, other.path);
276 public final String toString() {
277 return addToStringAttributes(Objects.toStringHelper(this)).toString();
280 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
281 return toStringHelper.add("path", path);