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.Preconditions;
11 import com.google.common.collect.ImmutableList;
12 import com.google.common.collect.Iterables;
14 import java.util.Arrays;
15 import java.util.Collections;
16 import java.util.List;
18 import org.opendaylight.yangtools.concepts.Immutable;
19 import org.opendaylight.yangtools.util.HashCodeBuilder;
20 import org.opendaylight.yangtools.yang.common.QName;
24 * Represents unique path to the every node inside the module.
27 public class SchemaPath implements Immutable {
29 * Shared instance of the conceptual root schema node.
31 public static final SchemaPath ROOT = new SchemaPath(Collections.<QName>emptyList(), true, Boolean.TRUE.hashCode());
34 * Shared instance of the "same" relative schema node.
36 public static final SchemaPath SAME = new SchemaPath(Collections.<QName>emptyList(), false, Boolean.FALSE.hashCode());
39 * List of QName instances which represents complete path to the node.
41 private final Iterable<QName> path;
44 * Boolean value which represents type of schema path (relative or
47 private final boolean absolute;
50 * Cached hash code. We can use this since we are immutable.
52 private final int hash;
55 * Cached legacy path, filled-in when {@link #getPath()} or {@link #getPathTowardsRoot()}
58 private ImmutableList<QName> legacyPath;
61 * Constructs new instance of this class with the concrete path.
64 * list of QName instances which specifies exact path to the
67 * boolean value which specifies if the path is absolute or
70 * @deprecated Use {@link #create(Iterable, boolean)} instead.
73 public SchemaPath(final List<QName> path, final boolean absolute) {
74 this(ImmutableList.copyOf(path), absolute, Boolean.valueOf(absolute).hashCode());
77 private ImmutableList<QName> getLegacyPath() {
78 if (legacyPath == null) {
79 legacyPath = ImmutableList.copyOf(path);
86 * Returns the complete path to schema node.
88 * @return list of <code>QName</code> instances which represents complete
91 * @deprecated Use {@link #getPathFromRoot()} instead.
94 public List<QName> getPath() {
95 return getLegacyPath();
98 private SchemaPath(final Iterable<QName> path, final boolean absolute, final int hash) {
99 this.path = Preconditions.checkNotNull(path);
100 this.absolute = absolute;
105 * Constructs new instance of this class with the concrete path.
108 * list of QName instances which specifies exact path to the
111 * boolean value which specifies if the path is absolute or
114 * @return A SchemaPath instance.
116 public static SchemaPath create(final Iterable<QName> path, final boolean absolute) {
117 final SchemaPath parent = absolute ? ROOT : SAME;
118 return parent.createChild(path);
122 * Constructs new instance of this class with the concrete path.
125 * boolean value which specifies if the path is absolute or
128 * one or more QName instances which specifies exact path to the
131 * @return A SchemaPath instance.
133 public static SchemaPath create(final boolean absolute, final QName... path) {
134 return create(Arrays.asList(path), absolute);
137 private SchemaPath trustedCreateChild(final Iterable<QName> relative) {
138 if (Iterables.isEmpty(relative)) {
142 final HashCodeBuilder<QName> b = new HashCodeBuilder<>(hash);
143 for (QName p : relative) {
147 return new SchemaPath(Iterables.concat(path, relative), absolute, b.toInstance());
151 * Create a child path based on concatenation of this path and a relative path.
153 * @param relative Relative path
154 * @return A new child path
156 public SchemaPath createChild(final Iterable<QName> relative) {
157 if (Iterables.isEmpty(relative)) {
161 return trustedCreateChild(ImmutableList.copyOf(relative));
165 * Create a child path based on concatenation of this path and a relative path.
167 * @param relative Relative SchemaPath
168 * @return A new child path
170 public SchemaPath createChild(final SchemaPath relative) {
171 Preconditions.checkArgument(!relative.isAbsolute(), "Child creation requires relative path");
172 return trustedCreateChild(relative.path);
176 * Create a child path based on concatenation of this path and additional
179 * @param elements Relative SchemaPath elements
180 * @return A new child path
182 public SchemaPath createChild(final QName... elements) {
183 return createChild(Arrays.asList(elements));
187 * Returns the list of nodes which need to be traversed to get from the
188 * starting point (root for absolute SchemaPaths) to the node represented
191 * @return list of <code>qname</code> instances which represents
192 * path from the root to the schema node.
194 public Iterable<QName> getPathFromRoot() {
199 * Returns the list of nodes which need to be traversed to get from this
200 * node to the starting point (root for absolute SchemaPaths).
202 * @return list of <code>qname</code> instances which represents
203 * path from the schema node towards the root.
205 public Iterable<QName> getPathTowardsRoot() {
206 return getLegacyPath().reverse();
210 * Returns the immediate parent SchemaPath.
212 * @return Parent path, null if this SchemaPath is already toplevel.
214 public SchemaPath getParent() {
215 final int size = Iterables.size(path);
217 final SchemaPath parent = isAbsolute() ? ROOT : SAME;
218 return parent.trustedCreateChild(Iterables.limit(path, size - 1));
225 * Describes whether schema path is|isn't absolute.
227 * @return boolean value which is <code>true</code> if schema path is
230 public boolean isAbsolute() {
235 public int hashCode() {
240 public boolean equals(final Object obj) {
247 if (getClass() != obj.getClass()) {
250 SchemaPath other = (SchemaPath) obj;
251 if (absolute != other.absolute) {
255 return Iterables.elementsEqual(path, other.path);
259 public String toString() {
260 StringBuilder builder = new StringBuilder();
261 builder.append("SchemaPath [path=");
262 builder.append(path);
263 builder.append(", absolute=");
264 builder.append(absolute);
266 return builder.toString();