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.Collections;
15 import java.util.List;
16 import org.opendaylight.yangtools.concepts.Immutable;
17 import org.opendaylight.yangtools.util.HashCodeBuilder;
18 import org.opendaylight.yangtools.yang.common.QName;
22 * Represents unique path to the every node inside the module.
25 public class SchemaPath implements Immutable {
27 * Shared instance of the conceptual root schema node.
29 public static final SchemaPath ROOT = new SchemaPath(Collections.<QName>emptyList(), true, Boolean.TRUE.hashCode());
32 * Shared instance of the "same" relative schema node.
34 public static final SchemaPath SAME = new SchemaPath(Collections.<QName>emptyList(), false, Boolean.FALSE.hashCode());
37 * List of QName instances which represents complete path to the node.
39 private final Iterable<QName> path;
42 * Boolean value which represents type of schema path (relative or
45 private final boolean absolute;
48 * Cached hash code. We can use this since we are immutable.
50 private final int hash;
53 * Cached legacy path, filled-in when {@link #getPath()} or {@link #getPathTowardsRoot()}
56 private ImmutableList<QName> legacyPath;
59 * Constructs new instance of this class with the concrete path.
62 * list of QName instances which specifies exact path to the
65 * boolean value which specifies if the path is absolute or
68 * @deprecated Use {@link #create(Iterable, boolean)} instead.
71 public SchemaPath(final List<QName> path, final boolean absolute) {
72 this(ImmutableList.copyOf(path), absolute, Boolean.valueOf(absolute).hashCode());
75 private ImmutableList<QName> getLegacyPath() {
76 if (legacyPath == null) {
77 legacyPath = ImmutableList.copyOf(path);
84 * Returns the complete path to schema node.
86 * @return list of <code>QName</code> instances which represents complete
89 * @deprecated Use {@link #getPathFromRoot()} instead.
92 public List<QName> getPath() {
93 return getLegacyPath();
96 private SchemaPath(final Iterable<QName> path, final boolean absolute, final int hash) {
97 this.path = Preconditions.checkNotNull(path);
98 this.absolute = absolute;
103 * Constructs new instance of this class with the concrete path.
106 * list of QName instances which specifies exact path to the
109 * boolean value which specifies if the path is absolute or
112 * @return A SchemaPath instance.
114 public static SchemaPath create(final Iterable<QName> path, final boolean absolute) {
115 final SchemaPath parent = absolute ? ROOT : SAME;
116 return parent.createChild(path);
120 * Constructs new instance of this class with the concrete path.
123 * boolean value which specifies if the path is absolute or
126 * one or more QName instances which specifies exact path to the
129 * @return A SchemaPath instance.
131 public static SchemaPath create(final boolean absolute, final QName... path) {
132 return create(Arrays.asList(path), absolute);
135 private SchemaPath trustedCreateChild(final Iterable<QName> relative) {
136 if (Iterables.isEmpty(relative)) {
140 final HashCodeBuilder<QName> b = new HashCodeBuilder<>(hash);
141 for (QName p : relative) {
145 return new SchemaPath(Iterables.concat(path, relative), absolute, b.toInstance());
149 * Create a child path based on concatenation of this path and a relative path.
151 * @param relative Relative path
152 * @return A new child path
154 public SchemaPath createChild(final Iterable<QName> relative) {
155 if (Iterables.isEmpty(relative)) {
159 return trustedCreateChild(ImmutableList.copyOf(relative));
163 * Create a child path based on concatenation of this path and a relative path.
165 * @param relative Relative SchemaPath
166 * @return A new child path
168 public SchemaPath createChild(final SchemaPath relative) {
169 Preconditions.checkArgument(!relative.isAbsolute(), "Child creation requires relative path");
170 return trustedCreateChild(relative.path);
174 * Create a child path based on concatenation of this path and additional
177 * @param elements Relative SchemaPath elements
178 * @return A new child path
180 public SchemaPath createChild(final QName... elements) {
181 return createChild(Arrays.asList(elements));
185 * Returns the list of nodes which need to be traversed to get from the
186 * starting point (root for absolute SchemaPaths) to the node represented
189 * @return list of <code>qname</code> instances which represents
190 * path from the root to the schema node.
192 public Iterable<QName> getPathFromRoot() {
197 * Returns the list of nodes which need to be traversed to get from this
198 * node to the starting point (root for absolute SchemaPaths).
200 * @return list of <code>qname</code> instances which represents
201 * path from the schema node towards the root.
203 public Iterable<QName> getPathTowardsRoot() {
204 return getLegacyPath().reverse();
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();