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