1 package org.opendaylight.yangtools.yang.data.api;
\r
3 import java.io.Serializable;
\r
4 import java.util.ArrayList;
\r
5 import java.util.Collections;
\r
6 import java.util.HashMap;
\r
7 import java.util.List;
\r
8 import java.util.Map;
\r
10 import org.opendaylight.yangtools.concepts.Builder;
\r
11 import org.opendaylight.yangtools.concepts.Immutable;
\r
12 import org.opendaylight.yangtools.concepts.Path;
\r
13 import org.opendaylight.yangtools.yang.common.QName;
\r
15 public class InstanceIdentifier implements Path<InstanceIdentifier>, Immutable, Serializable {
\r
17 private static final long serialVersionUID = 8467409862384206193L;
\r
18 private final List<PathArgument> path;
\r
20 private transient String to_string_cache = null;
\r
22 public List<PathArgument> getPath() {
\r
26 public InstanceIdentifier(final List<? extends PathArgument> path) {
\r
27 this.path = Collections.unmodifiableList(new ArrayList<>(path));
\r
30 private InstanceIdentifier(NodeIdentifier nodeIdentifier) {
\r
31 this.path = Collections.<PathArgument> singletonList(nodeIdentifier);
\r
35 public int hashCode() {
\r
36 final int prime = 31;
\r
38 result = prime * result + ((path == null) ? 0 : path.hashCode());
\r
43 public boolean equals(Object obj) {
\r
48 if (getClass() != obj.getClass())
\r
50 InstanceIdentifier other = (InstanceIdentifier) obj;
\r
52 if (other.path != null)
\r
54 } else if (!path.equals(other.path))
\r
59 // Static factories & helpers
\r
61 public static InstanceIdentifier of(QName name) {
\r
62 return new InstanceIdentifier(new NodeIdentifier(name));
\r
65 static public InstanceIdentifierBuilder builder() {
\r
66 return new BuilderImpl();
\r
69 static public InstanceIdentifierBuilder builder(InstanceIdentifier origin) {
\r
70 return new BuilderImpl(origin.getPath());
\r
73 public interface PathArgument extends Immutable, Serializable {
\r
74 QName getNodeType();
\r
78 public interface InstanceIdentifierBuilder extends Builder<InstanceIdentifier> {
\r
79 InstanceIdentifierBuilder node(QName nodeType);
\r
81 InstanceIdentifierBuilder nodeWithKey(QName nodeType, Map<QName, Object> keyValues);
\r
83 InstanceIdentifierBuilder nodeWithKey(QName nodeType, QName key, Object value);
\r
86 InstanceIdentifier getIdentifier();
\r
89 public static final class NodeIdentifier implements PathArgument {
\r
94 private static final long serialVersionUID = -2255888212390871347L;
\r
96 private final QName nodeType;
\r
98 public NodeIdentifier(QName node) {
\r
99 this.nodeType = node;
\r
102 public QName getNodeType() {
\r
107 public int hashCode() {
\r
108 final int prime = 31;
\r
110 result = prime * result + ((nodeType == null) ? 0 : nodeType.hashCode());
\r
115 public boolean equals(Object obj) {
\r
120 if (getClass() != obj.getClass())
\r
122 NodeIdentifier other = (NodeIdentifier) obj;
\r
123 if (nodeType == null) {
\r
124 if (other.nodeType != null)
\r
126 } else if (!nodeType.equals(other.nodeType))
\r
132 public String toString() {
\r
133 return nodeType.toString();
\r
137 public static final class NodeIdentifierWithPredicates implements PathArgument {
\r
142 private static final long serialVersionUID = -4787195606494761540L;
\r
144 private final QName nodeType;
\r
145 private final Map<QName, Object> keyValues;
\r
147 public NodeIdentifierWithPredicates(QName node, Map<QName, Object> keyValues) {
\r
148 this.nodeType = node;
\r
149 this.keyValues = Collections.unmodifiableMap(new HashMap<QName, Object>(keyValues));
\r
152 public NodeIdentifierWithPredicates(QName node, QName key, Object value) {
\r
153 this.nodeType = node;
\r
154 this.keyValues = Collections.singletonMap(key, value);
\r
158 public QName getNodeType() {
\r
162 public Map<QName, Object> getKeyValues() {
\r
167 public int hashCode() {
\r
168 final int prime = 31;
\r
170 result = prime * result + ((keyValues == null) ? 0 : keyValues.hashCode());
\r
171 result = prime * result + ((nodeType == null) ? 0 : nodeType.hashCode());
\r
176 public boolean equals(Object obj) {
\r
181 if (getClass() != obj.getClass())
\r
183 NodeIdentifierWithPredicates other = (NodeIdentifierWithPredicates) obj;
\r
184 if (keyValues == null) {
\r
185 if (other.keyValues != null)
\r
187 } else if (!keyValues.equals(other.keyValues))
\r
189 if (nodeType == null) {
\r
190 if (other.nodeType != null)
\r
192 } else if (!nodeType.equals(other.nodeType))
\r
198 public String toString() {
\r
199 return nodeType + "[" + keyValues + "]";
\r
203 private static class BuilderImpl implements InstanceIdentifierBuilder {
\r
205 private final List<PathArgument> path = new ArrayList<>();
\r
207 public BuilderImpl() {
\r
211 public BuilderImpl(List<? extends PathArgument> prefix) {
\r
212 path.addAll(prefix);
\r
216 public InstanceIdentifierBuilder node(QName nodeType) {
\r
217 path.add(new NodeIdentifier(nodeType));
\r
222 public InstanceIdentifierBuilder nodeWithKey(QName nodeType, QName key, Object value) {
\r
223 path.add(new NodeIdentifierWithPredicates(nodeType, key, value));
\r
228 public InstanceIdentifierBuilder nodeWithKey(QName nodeType, Map<QName, Object> keyValues) {
\r
229 path.add(new NodeIdentifierWithPredicates(nodeType, keyValues));
\r
234 public InstanceIdentifier toInstance() {
\r
235 return new InstanceIdentifier(path);
\r
239 public InstanceIdentifier getIdentifier() {
\r
240 return toInstance();
\r
245 public boolean contains(final InstanceIdentifier other) {
\r
246 if (other == null) {
\r
247 throw new IllegalArgumentException("other should not be null");
\r
249 final int localSize = this.path.size();
\r
250 final List<PathArgument> otherPath = other.getPath();
\r
251 if (localSize > other.path.size()) {
\r
254 for (int i = 0; i < localSize; i++) {
\r
255 if (!path.get(i).equals(otherPath.get(i))) {
\r
263 public String toString() {
\r
264 if (to_string_cache != null) {
\r
265 return to_string_cache;
\r
267 StringBuilder builder = new StringBuilder();
\r
268 for (PathArgument argument : path) {
\r
269 builder.append("/");
\r
270 builder.append(argument.toString());
\r
272 to_string_cache = builder.toString();
\r
273 return to_string_cache;
\r