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;
13 import java.util.Arrays;
14 import java.util.List;
15 import org.opendaylight.yangtools.yang.common.QName;
19 * Represents unique path to the every node inside the module.
22 public class SchemaPath {
24 * Shared instance of the conceptual root schema node.
26 public static final SchemaPath ROOT = new SchemaPath(ImmutableList.<QName> of(), true, null);
29 * Shared instance of the "same" relative schema node.
31 public static final SchemaPath SAME = new SchemaPath(ImmutableList.<QName> of(), false, null);
34 * List of QName instances which represents complete path to the node.
36 private final ImmutableList<QName> path;
39 * Boolean value which represents type of schema path (relative or
42 private final Boolean absolute;
45 * Constructs new instance of this class with the concrete path.
48 * list of QName instances which specifies exact path to the
51 * boolean value which specifies if the path is absolute or
54 * @deprecated Use {@link #create(Iterable, boolean)} instead.
57 public SchemaPath(final List<QName> path, final boolean absolute) {
58 this(ImmutableList.copyOf(path), absolute, null);
62 * Returns the complete path to schema node.
64 * @return list of <code>QName</code> instances which represents complete
67 * @deprecated Use {@link #getPathFromRoot()} instead.
70 public List<QName> getPath() {
74 private SchemaPath(final ImmutableList<QName> path, final boolean absolute, final Void dummy) {
75 this.path = Preconditions.checkNotNull(path);
76 this.absolute = absolute;
80 * Constructs new instance of this class with the concrete path.
83 * list of QName instances which specifies exact path to the
86 * boolean value which specifies if the path is absolute or
89 * @return A SchemaPath instance.
91 public static SchemaPath create(final Iterable<QName> path, final boolean absolute) {
92 if (Iterables.isEmpty(path)) {
93 return absolute ? ROOT : SAME;
95 return new SchemaPath(ImmutableList.copyOf(path), absolute, null);
100 * Constructs new instance of this class with the concrete path.
103 * boolean value which specifies if the path is absolute or
106 * one or more QName instances which specifies exact path to the
109 * @return A SchemaPath instance.
111 public static SchemaPath create(final boolean absolute, final QName... path) {
112 return create(Arrays.asList(path), absolute);
116 * Create a child path based on concatenation of this path and a relative path.
118 * @param relative Relative path
119 * @return A new child path
121 public SchemaPath createChild(final Iterable<QName> relative) {
122 if (Iterables.isEmpty(relative)) {
125 return create(Iterables.concat(path, relative), absolute);
129 * Create a child path based on concatenation of this path and a relative path.
131 * @param relative Relative SchemaPath
132 * @return A new child path
134 public SchemaPath createChild(final SchemaPath relative) {
135 Preconditions.checkArgument(!relative.isAbsolute(), "Child creation requires relative path");
136 return createChild(relative.path);
140 * Create a child path based on concatenation of this path and additional
143 * @param elements Relative SchemaPath elements
144 * @return A new child path
146 public SchemaPath createChild(final QName... elements) {
147 return createChild(Arrays.asList(elements));
151 * Returns the list of nodes which need to be traversed to get from the
152 * starting point (root for absolute SchemaPaths) to the node represented
155 * @return list of <code>qname</code> instances which represents
156 * path from the root to the schema node.
158 public Iterable<QName> getPathFromRoot() {
163 * Returns the list of nodes which need to be traversed to get from this
164 * node to the starting point (root for absolute SchemaPaths).
166 * @return list of <code>qname</code> instances which represents
167 * path from the schema node towards the root.
169 public Iterable<QName> getPathTowardsRoot() {
170 return path.reverse();
174 * Describes whether schema path is|isn't absolute.
176 * @return boolean value which is <code>true</code> if schema path is
179 public boolean isAbsolute() {
184 public int hashCode() {
185 final int prime = 31;
187 result = prime * result + absolute.hashCode();
189 // TODO: Temporary fix for Bug 1076 - hash computation
190 // Which adds same behaviour as using List.hashCode().
192 for (Object o : path) {
193 pathHash = prime * pathHash + o.hashCode();
195 result = prime * result + pathHash;
200 public boolean equals(final Object obj) {
207 if (getClass() != obj.getClass()) {
210 SchemaPath other = (SchemaPath) obj;
211 if (absolute != other.absolute) {
215 return Iterables.elementsEqual(path, other.path);
219 public String toString() {
220 StringBuilder builder = new StringBuilder();
221 builder.append("SchemaPath [path=");
222 builder.append(path);
223 builder.append(", absolute=");
224 builder.append(absolute);
226 return builder.toString();