Add missing copyright headers
[yangtools.git] / yang / yang-data-api / src / main / java / org / opendaylight / yangtools / yang / data / api / InstanceIdentifier.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.api;\r
9 \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
15 \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
20 \r
21 import com.google.common.collect.ImmutableList;\r
22 \r
23 public class InstanceIdentifier implements Path<InstanceIdentifier>, Immutable, Serializable {\r
24 \r
25     private static final long serialVersionUID = 8467409862384206193L;\r
26     private final List<PathArgument> path;\r
27 \r
28     private transient String to_string_cache = null;\r
29 \r
30     public List<PathArgument> getPath() {\r
31         return path;\r
32     }\r
33 \r
34     public InstanceIdentifier(final List<? extends PathArgument> path) {\r
35         this.path =ImmutableList.copyOf(path);\r
36     }\r
37 \r
38     private InstanceIdentifier(NodeIdentifier nodeIdentifier) {\r
39         this.path = ImmutableList.<PathArgument>of(nodeIdentifier);\r
40     }\r
41 \r
42     @Override\r
43     public int hashCode() {\r
44         final int prime = 31;\r
45         int result = 1;\r
46         result = prime * result + ((path == null) ? 0 : path.hashCode());\r
47         return result;\r
48     }\r
49 \r
50     @Override\r
51     public boolean equals(Object obj) {\r
52         if (this == obj)\r
53             return true;\r
54         if (obj == null)\r
55             return false;\r
56         if (getClass() != obj.getClass())\r
57             return false;\r
58         InstanceIdentifier other = (InstanceIdentifier) obj;\r
59         if (path == null) {\r
60             if (other.path != null)\r
61                 return false;\r
62         } else if (!path.equals(other.path))\r
63             return false;\r
64         return true;\r
65     }\r
66 \r
67     // Static factories & helpers\r
68 \r
69     public static InstanceIdentifier of(QName name) {\r
70         return new InstanceIdentifier(new NodeIdentifier(name));\r
71     }\r
72 \r
73     static public InstanceIdentifierBuilder builder() {\r
74         return new BuilderImpl();\r
75     }\r
76 \r
77     static public InstanceIdentifierBuilder builder(InstanceIdentifier origin) {\r
78         return new BuilderImpl(origin.getPath());\r
79     }\r
80 \r
81     public interface PathArgument extends Immutable, Serializable {\r
82         QName getNodeType();\r
83 \r
84     }\r
85 \r
86     public interface InstanceIdentifierBuilder extends Builder<InstanceIdentifier> {\r
87         InstanceIdentifierBuilder node(QName nodeType);\r
88 \r
89         InstanceIdentifierBuilder nodeWithKey(QName nodeType, Map<QName, Object> keyValues);\r
90 \r
91         InstanceIdentifierBuilder nodeWithKey(QName nodeType, QName key, Object value);\r
92 \r
93         @Deprecated\r
94         InstanceIdentifier getIdentifier();\r
95     }\r
96 \r
97     public static final class NodeIdentifier implements PathArgument {\r
98 \r
99         /**\r
100          * \r
101          */\r
102         private static final long serialVersionUID = -2255888212390871347L;\r
103 \r
104         private final QName nodeType;\r
105 \r
106         public NodeIdentifier(QName node) {\r
107             this.nodeType = node;\r
108         }\r
109 \r
110         public QName getNodeType() {\r
111             return nodeType;\r
112         }\r
113 \r
114         @Override\r
115         public int hashCode() {\r
116             final int prime = 31;\r
117             int result = 1;\r
118             result = prime * result + ((nodeType == null) ? 0 : nodeType.hashCode());\r
119             return result;\r
120         }\r
121 \r
122         @Override\r
123         public boolean equals(Object obj) {\r
124             if (this == obj)\r
125                 return true;\r
126             if (obj == null)\r
127                 return false;\r
128             if (getClass() != obj.getClass())\r
129                 return false;\r
130             NodeIdentifier other = (NodeIdentifier) obj;\r
131             if (nodeType == null) {\r
132                 if (other.nodeType != null)\r
133                     return false;\r
134             } else if (!nodeType.equals(other.nodeType))\r
135                 return false;\r
136             return true;\r
137         }\r
138 \r
139         @Override\r
140         public String toString() {\r
141             return nodeType.toString();\r
142         }\r
143     }\r
144 \r
145     public static final class NodeIdentifierWithPredicates implements PathArgument {\r
146 \r
147         /**\r
148          * \r
149          */\r
150         private static final long serialVersionUID = -4787195606494761540L;\r
151 \r
152         private final QName nodeType;\r
153         private final Map<QName, Object> keyValues;\r
154 \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
158         }\r
159 \r
160         public NodeIdentifierWithPredicates(QName node, QName key, Object value) {\r
161             this.nodeType = node;\r
162             this.keyValues = Collections.singletonMap(key, value);\r
163         }\r
164 \r
165         @Override\r
166         public QName getNodeType() {\r
167             return nodeType;\r
168         }\r
169 \r
170         public Map<QName, Object> getKeyValues() {\r
171             return keyValues;\r
172         }\r
173 \r
174         @Override\r
175         public int hashCode() {\r
176             final int prime = 31;\r
177             int result = 1;\r
178             result = prime * result + ((keyValues == null) ? 0 : keyValues.hashCode());\r
179             result = prime * result + ((nodeType == null) ? 0 : nodeType.hashCode());\r
180             return result;\r
181         }\r
182 \r
183         @Override\r
184         public boolean equals(Object obj) {\r
185             if (this == obj)\r
186                 return true;\r
187             if (obj == null)\r
188                 return false;\r
189             if (getClass() != obj.getClass())\r
190                 return false;\r
191             NodeIdentifierWithPredicates other = (NodeIdentifierWithPredicates) obj;\r
192             if (keyValues == null) {\r
193                 if (other.keyValues != null)\r
194                     return false;\r
195             } else if (!keyValues.equals(other.keyValues))\r
196                 return false;\r
197             if (nodeType == null) {\r
198                 if (other.nodeType != null)\r
199                     return false;\r
200             } else if (!nodeType.equals(other.nodeType))\r
201                 return false;\r
202             return true;\r
203         }\r
204 \r
205         @Override\r
206         public String toString() {\r
207             return nodeType + "[" + keyValues + "]";\r
208         }\r
209     }\r
210 \r
211     private static class BuilderImpl implements InstanceIdentifierBuilder {\r
212 \r
213         private final ImmutableList.Builder<PathArgument> path; \r
214 \r
215         public BuilderImpl() {\r
216             path = ImmutableList.<PathArgument>builder();\r
217         }\r
218 \r
219         public BuilderImpl(List<? extends PathArgument> prefix) {\r
220             path = ImmutableList.<PathArgument>builder();\r
221             path.addAll(prefix);\r
222         }\r
223 \r
224         @Override\r
225         public InstanceIdentifierBuilder node(QName nodeType) {\r
226             path.add(new NodeIdentifier(nodeType));\r
227             return this;\r
228         }\r
229 \r
230         @Override\r
231         public InstanceIdentifierBuilder nodeWithKey(QName nodeType, QName key, Object value) {\r
232             path.add(new NodeIdentifierWithPredicates(nodeType, key, value));\r
233             return this;\r
234         }\r
235 \r
236         @Override\r
237         public InstanceIdentifierBuilder nodeWithKey(QName nodeType, Map<QName, Object> keyValues) {\r
238             path.add(new NodeIdentifierWithPredicates(nodeType, keyValues));\r
239             return this;\r
240         }\r
241 \r
242         @Override\r
243         public InstanceIdentifier toInstance() {\r
244             return new InstanceIdentifier(path.build());\r
245         }\r
246 \r
247         @Override\r
248         public InstanceIdentifier getIdentifier() {\r
249             return toInstance();\r
250         }\r
251     }\r
252 \r
253     @Override\r
254     public boolean contains(final InstanceIdentifier other) {\r
255         if (other == null) {\r
256             throw new IllegalArgumentException("other should not be null");\r
257         }\r
258         final int localSize = this.path.size();\r
259         final List<PathArgument> otherPath = other.getPath();\r
260         if (localSize > other.path.size()) {\r
261             return false;\r
262         }\r
263         for (int i = 0; i < localSize; i++) {\r
264             if (!path.get(i).equals(otherPath.get(i))) {\r
265                 return false;\r
266             }\r
267         }\r
268         return true;\r
269     }\r
270 \r
271     @Override\r
272     public String toString() {\r
273         if (to_string_cache != null) {\r
274             return to_string_cache;\r
275         }\r
276         StringBuilder builder = new StringBuilder();\r
277         for (PathArgument argument : path) {\r
278             builder.append("/");\r
279             builder.append(argument.toString());\r
280         }\r
281         to_string_cache = builder.toString();\r
282         return to_string_cache;\r
283     }\r
284 \r
285     public static InstanceIdentifierBuilder builder(QName node) {\r
286         return builder().node(node);\r
287     }\r
288 }\r