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;
60 private ImmutableList<QName> getLegacyPath() {
61 if (legacyPath == null) {
62 legacyPath = ImmutableList.copyOf(path);
69 * Returns the complete path to schema node.
71 * @return list of <code>QName</code> instances which represents complete
74 * @deprecated Use {@link #getPathFromRoot()} instead.
77 public List<QName> getPath() {
78 return getLegacyPath();
81 private SchemaPath(final Iterable<QName> path, final boolean absolute, final int hash) {
82 this.path = Preconditions.checkNotNull(path);
83 this.absolute = absolute;
88 * Constructs new instance of this class with the concrete path.
91 * list of QName instances which specifies exact path to the
94 * boolean value which specifies if the path is absolute or
97 * @return A SchemaPath instance.
99 public static SchemaPath create(final Iterable<QName> path, final boolean absolute) {
100 final SchemaPath parent = absolute ? ROOT : SAME;
101 return parent.createChild(path);
105 * Constructs new instance of this class with the concrete path.
108 * boolean value which specifies if the path is absolute or
111 * one or more QName instances which specifies exact path to the
114 * @return A SchemaPath instance.
116 public static SchemaPath create(final boolean absolute, final QName... path) {
117 return create(Arrays.asList(path), absolute);
120 private SchemaPath trustedCreateChild(final Iterable<QName> relative) {
121 if (Iterables.isEmpty(relative)) {
125 final HashCodeBuilder<QName> b = new HashCodeBuilder<>(hash);
126 for (QName p : relative) {
130 return new SchemaPath(Iterables.concat(path, relative), absolute, b.toInstance());
134 * Create a child path based on concatenation of this path and a relative path.
136 * @param relative Relative path
137 * @return A new child path
139 public SchemaPath createChild(final Iterable<QName> relative) {
140 if (Iterables.isEmpty(relative)) {
144 return trustedCreateChild(ImmutableList.copyOf(relative));
148 * Create a child path based on concatenation of this path and a relative path.
150 * @param relative Relative SchemaPath
151 * @return A new child path
153 public SchemaPath createChild(final SchemaPath relative) {
154 Preconditions.checkArgument(!relative.isAbsolute(), "Child creation requires relative path");
155 return trustedCreateChild(relative.path);
159 * Create a child path based on concatenation of this path and additional
162 * @param elements Relative SchemaPath elements
163 * @return A new child path
165 public SchemaPath createChild(final QName... elements) {
166 return createChild(Arrays.asList(elements));
170 * Returns the list of nodes which need to be traversed to get from the
171 * starting point (root for absolute SchemaPaths) to the node represented
174 * @return list of <code>qname</code> instances which represents
175 * path from the root to the schema node.
177 public Iterable<QName> getPathFromRoot() {
182 * Returns the list of nodes which need to be traversed to get from this
183 * node to the starting point (root for absolute SchemaPaths).
185 * @return list of <code>qname</code> instances which represents
186 * path from the schema node towards the root.
188 public Iterable<QName> getPathTowardsRoot() {
189 return getLegacyPath().reverse();
193 * Returns the immediate parent SchemaPath.
195 * @return Parent path, null if this SchemaPath is already toplevel.
197 public SchemaPath getParent() {
198 final int size = Iterables.size(path);
200 final SchemaPath parent = isAbsolute() ? ROOT : SAME;
201 return parent.trustedCreateChild(Iterables.limit(path, size - 1));
208 * Describes whether schema path is|isn't absolute.
210 * @return boolean value which is <code>true</code> if schema path is
213 public boolean isAbsolute() {
218 public int hashCode() {
223 public boolean equals(final Object obj) {
230 if (getClass() != obj.getClass()) {
233 SchemaPath other = (SchemaPath) obj;
234 if (absolute != other.absolute) {
238 return Iterables.elementsEqual(path, other.path);
242 public String toString() {
243 StringBuilder builder = new StringBuilder();
244 builder.append("SchemaPath [path=");
245 builder.append(path);
246 builder.append(", absolute=");
247 builder.append(absolute);
249 return builder.toString();