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 java.util.Arrays;
11 import java.util.Collections;
12 import java.util.List;
14 import org.opendaylight.yangtools.yang.common.QName;
16 import com.google.common.base.Preconditions;
17 import com.google.common.collect.ImmutableList;
18 import com.google.common.collect.Iterables;
22 * Represents unique path to the every node inside the module.
25 public class SchemaPath {
27 * Shared instance of the conceptual root schema node.
29 public static final SchemaPath ROOT = SchemaPath.create(Collections.<QName>emptyList(), true);
32 * Shared instance of the "same" relative schema node.
34 public static final SchemaPath SAME = SchemaPath.create(Collections.<QName>emptyList(), false);
37 * List of QName instances which represents complete path to the node.
39 private final ImmutableList<QName> path;
42 * Boolean value which represents type of schema path (relative or
45 private final Boolean absolute;
48 * Constructs new instance of this class with the concrete path.
51 * list of QName instances which specifies exact path to the
54 * boolean value which specifies if the path is absolute or
57 * @deprecated Use {@link #create(Iterable, boolean)} instead.
60 public SchemaPath(final List<QName> path, final boolean absolute) {
61 this(ImmutableList.copyOf(path), absolute, null);
65 * Returns the complete path to schema node.
67 * @return list of <code>QName</code> instances which represents complete
70 * @deprecated Use {@link #getPathFromRoot()} instead.
73 public List<QName> getPath() {
77 private SchemaPath(final ImmutableList<QName> path, final boolean absolute, final Void dummy) {
78 this.path = Preconditions.checkNotNull(path);
79 this.absolute = absolute;
83 * Constructs new instance of this class with the concrete path.
86 * list of QName instances which specifies exact path to the
89 * boolean value which specifies if the path is absolute or
92 * @return A SchemaPath instance.
94 public static SchemaPath create(final Iterable<QName> path, final boolean absolute) {
95 if (Iterables.isEmpty(path)) {
96 return absolute ? ROOT : SAME;
98 return new SchemaPath(ImmutableList.copyOf(path), absolute, null);
103 * Constructs new instance of this class with the concrete path.
106 * boolean value which specifies if the path is absolute or
109 * one or more QName instances which specifies exact path to the
112 * @return A SchemaPath instance.
114 public static SchemaPath create(final boolean absolute, final QName... path) {
115 return create(Arrays.asList(path), absolute);
119 * Create a child path based on concatenation of this path and a relative path.
121 * @param relative Relative path
122 * @return A new child path
124 public SchemaPath createChild(final Iterable<QName> relative) {
125 if (Iterables.isEmpty(relative)) {
128 return create(Iterables.concat(path, relative), absolute);
132 * Create a child path based on concatenation of this path and a relative path.
134 * @param relative Relative SchemaPath
135 * @return A new child path
137 public SchemaPath createChild(final SchemaPath relative) {
138 Preconditions.checkArgument(!relative.isAbsolute(), "Child creation requires relative path");
139 return createChild(relative.path);
143 * Create a child path based on concatenation of this path and additional
146 * @param elements Relative SchemaPath elements
147 * @return A new child path
149 public SchemaPath createChild(final QName... elements) {
150 return createChild(Arrays.asList(elements));
154 * Returns the list of nodes which need to be traversed to get from the
155 * starting point (root for absolute SchemaPaths) to the node represented
158 * @return list of <code>qname</code> instances which represents
159 * path from the root to the schema node.
161 public Iterable<QName> getPathFromRoot() {
166 * Returns the list of nodes which need to be traversed to get from this
167 * node to the starting point (root for absolute SchemaPaths).
169 * @return list of <code>qname</code> instances which represents
170 * path from the schema node towards the root.
172 public Iterable<QName> getPathTowardsRoot() {
173 return path.reverse();
177 * Describes whether schema path is|isn't absolute.
179 * @return boolean value which is <code>true</code> if schema path is
182 public boolean isAbsolute() {
187 public int hashCode() {
188 final int prime = 31;
190 result = prime * result + absolute.hashCode();
191 for (Object o : path) {
192 result = prime * result + o.hashCode();
198 public boolean equals(final Object obj) {
205 if (getClass() != obj.getClass()) {
208 SchemaPath other = (SchemaPath) obj;
209 if (absolute != other.absolute) {
213 return Iterables.elementsEqual(path, other.path);
217 public String toString() {
218 StringBuilder builder = new StringBuilder();
219 builder.append("SchemaPath [path=");
220 builder.append(path);
221 builder.append(", absolute=");
222 builder.append(absolute);
224 return builder.toString();