2 * Copyright (c) 2014 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.data.api;
\r
10 import java.io.Serializable;
\r
11 import java.util.Collections;
\r
12 import java.util.HashMap;
\r
13 import java.util.List;
\r
14 import java.util.Map;
\r
16 import org.opendaylight.yangtools.concepts.Builder;
\r
17 import org.opendaylight.yangtools.concepts.Immutable;
\r
18 import org.opendaylight.yangtools.concepts.Path;
\r
19 import org.opendaylight.yangtools.yang.common.QName;
\r
21 import com.google.common.collect.ImmutableList;
\r
23 public class InstanceIdentifier implements Path<InstanceIdentifier>, Immutable, Serializable {
\r
25 private static final long serialVersionUID = 8467409862384206193L;
\r
26 private final List<PathArgument> path;
\r
28 private transient String to_string_cache = null;
\r
30 public List<PathArgument> getPath() {
\r
34 public InstanceIdentifier(final List<? extends PathArgument> path) {
\r
35 this.path =ImmutableList.copyOf(path);
\r
38 private InstanceIdentifier(NodeIdentifier nodeIdentifier) {
\r
39 this.path = ImmutableList.<PathArgument>of(nodeIdentifier);
\r
43 public int hashCode() {
\r
44 final int prime = 31;
\r
46 result = prime * result + ((path == null) ? 0 : path.hashCode());
\r
51 public boolean equals(Object obj) {
\r
56 if (getClass() != obj.getClass())
\r
58 InstanceIdentifier other = (InstanceIdentifier) obj;
\r
60 if (other.path != null)
\r
62 } else if (!path.equals(other.path))
\r
67 // Static factories & helpers
\r
69 public static InstanceIdentifier of(QName name) {
\r
70 return new InstanceIdentifier(new NodeIdentifier(name));
\r
73 static public InstanceIdentifierBuilder builder() {
\r
74 return new BuilderImpl();
\r
77 static public InstanceIdentifierBuilder builder(InstanceIdentifier origin) {
\r
78 return new BuilderImpl(origin.getPath());
\r
81 public interface PathArgument extends Immutable, Serializable {
\r
82 QName getNodeType();
\r
86 public interface InstanceIdentifierBuilder extends Builder<InstanceIdentifier> {
\r
87 InstanceIdentifierBuilder node(QName nodeType);
\r
89 InstanceIdentifierBuilder nodeWithKey(QName nodeType, Map<QName, Object> keyValues);
\r
91 InstanceIdentifierBuilder nodeWithKey(QName nodeType, QName key, Object value);
\r
94 InstanceIdentifier getIdentifier();
\r
97 public static final class NodeIdentifier implements PathArgument {
\r
102 private static final long serialVersionUID = -2255888212390871347L;
\r
104 private final QName nodeType;
\r
106 public NodeIdentifier(QName node) {
\r
107 this.nodeType = node;
\r
110 public QName getNodeType() {
\r
115 public int hashCode() {
\r
116 final int prime = 31;
\r
118 result = prime * result + ((nodeType == null) ? 0 : nodeType.hashCode());
\r
123 public boolean equals(Object obj) {
\r
128 if (getClass() != obj.getClass())
\r
130 NodeIdentifier other = (NodeIdentifier) obj;
\r
131 if (nodeType == null) {
\r
132 if (other.nodeType != null)
\r
134 } else if (!nodeType.equals(other.nodeType))
\r
140 public String toString() {
\r
141 return nodeType.toString();
\r
145 public static final class NodeIdentifierWithPredicates implements PathArgument {
\r
150 private static final long serialVersionUID = -4787195606494761540L;
\r
152 private final QName nodeType;
\r
153 private final Map<QName, Object> keyValues;
\r
155 public NodeIdentifierWithPredicates(QName node, Map<QName, Object> keyValues) {
\r
156 this.nodeType = node;
\r
157 this.keyValues = Collections.unmodifiableMap(new HashMap<QName, Object>(keyValues));
\r
160 public NodeIdentifierWithPredicates(QName node, QName key, Object value) {
\r
161 this.nodeType = node;
\r
162 this.keyValues = Collections.singletonMap(key, value);
\r
166 public QName getNodeType() {
\r
170 public Map<QName, Object> getKeyValues() {
\r
175 public int hashCode() {
\r
176 final int prime = 31;
\r
178 result = prime * result + ((keyValues == null) ? 0 : keyValues.hashCode());
\r
179 result = prime * result + ((nodeType == null) ? 0 : nodeType.hashCode());
\r
184 public boolean equals(Object obj) {
\r
189 if (getClass() != obj.getClass())
\r
191 NodeIdentifierWithPredicates other = (NodeIdentifierWithPredicates) obj;
\r
192 if (keyValues == null) {
\r
193 if (other.keyValues != null)
\r
195 } else if (!keyValues.equals(other.keyValues))
\r
197 if (nodeType == null) {
\r
198 if (other.nodeType != null)
\r
200 } else if (!nodeType.equals(other.nodeType))
\r
206 public String toString() {
\r
207 return nodeType + "[" + keyValues + "]";
\r
211 private static class BuilderImpl implements InstanceIdentifierBuilder {
\r
213 private final ImmutableList.Builder<PathArgument> path;
\r
215 public BuilderImpl() {
\r
216 path = ImmutableList.<PathArgument>builder();
\r
219 public BuilderImpl(List<? extends PathArgument> prefix) {
\r
220 path = ImmutableList.<PathArgument>builder();
\r
221 path.addAll(prefix);
\r
225 public InstanceIdentifierBuilder node(QName nodeType) {
\r
226 path.add(new NodeIdentifier(nodeType));
\r
231 public InstanceIdentifierBuilder nodeWithKey(QName nodeType, QName key, Object value) {
\r
232 path.add(new NodeIdentifierWithPredicates(nodeType, key, value));
\r
237 public InstanceIdentifierBuilder nodeWithKey(QName nodeType, Map<QName, Object> keyValues) {
\r
238 path.add(new NodeIdentifierWithPredicates(nodeType, keyValues));
\r
243 public InstanceIdentifier toInstance() {
\r
244 return new InstanceIdentifier(path.build());
\r
248 public InstanceIdentifier getIdentifier() {
\r
249 return toInstance();
\r
254 public boolean contains(final InstanceIdentifier other) {
\r
255 if (other == null) {
\r
256 throw new IllegalArgumentException("other should not be null");
\r
258 final int localSize = this.path.size();
\r
259 final List<PathArgument> otherPath = other.getPath();
\r
260 if (localSize > other.path.size()) {
\r
263 for (int i = 0; i < localSize; i++) {
\r
264 if (!path.get(i).equals(otherPath.get(i))) {
\r
272 public String toString() {
\r
273 if (to_string_cache != null) {
\r
274 return to_string_cache;
\r
276 StringBuilder builder = new StringBuilder();
\r
277 for (PathArgument argument : path) {
\r
278 builder.append("/");
\r
279 builder.append(argument.toString());
\r
281 to_string_cache = builder.toString();
\r
282 return to_string_cache;
\r
285 public static InstanceIdentifierBuilder builder(QName node) {
\r
286 return builder().node(node);
\r