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