Removed Equals/HashCodeBuilder for ARP/LLDPTLV
[controller.git] / opendaylight / sal / yang-prototype / code-generator / yang-model-parser-impl / src / main / java / org / opendaylight / controller / yang / parser / builder / impl / AugmentationSchemaBuilderImpl.java
1 /*
2  * Copyright (c) 2013 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.controller.yang.parser.builder.impl;
9
10 import java.util.Collections;
11 import java.util.HashMap;
12 import java.util.HashSet;
13 import java.util.Map;
14 import java.util.Set;
15
16 import org.opendaylight.controller.yang.common.QName;
17 import org.opendaylight.controller.yang.model.api.AugmentationSchema;
18 import org.opendaylight.controller.yang.model.api.DataSchemaNode;
19 import org.opendaylight.controller.yang.model.api.GroupingDefinition;
20 import org.opendaylight.controller.yang.model.api.RevisionAwareXPath;
21 import org.opendaylight.controller.yang.model.api.SchemaPath;
22 import org.opendaylight.controller.yang.model.api.Status;
23 import org.opendaylight.controller.yang.model.api.TypeDefinition;
24 import org.opendaylight.controller.yang.model.api.UsesNode;
25 import org.opendaylight.controller.yang.model.util.RevisionAwareXPathImpl;
26 import org.opendaylight.controller.yang.parser.builder.api.AugmentationSchemaBuilder;
27 import org.opendaylight.controller.yang.parser.builder.api.DataSchemaNodeBuilder;
28 import org.opendaylight.controller.yang.parser.builder.api.GroupingBuilder;
29 import org.opendaylight.controller.yang.parser.builder.api.TypeDefinitionBuilder;
30 import org.opendaylight.controller.yang.parser.builder.api.UsesNodeBuilder;
31 import org.opendaylight.controller.yang.parser.util.YangModelBuilderUtil;
32 import org.opendaylight.controller.yang.parser.util.YangParseException;
33
34 public final class AugmentationSchemaBuilderImpl implements AugmentationSchemaBuilder {
35     private boolean built;
36     private final AugmentationSchemaImpl instance;
37     private final int line;
38     private final String augmentTargetStr;
39     private SchemaPath augmentTarget;
40     private SchemaPath finalAugmentTarget;
41     private String whenCondition;
42     private final Set<DataSchemaNodeBuilder> childNodes = new HashSet<DataSchemaNodeBuilder>();
43     private final Set<GroupingBuilder> groupings = new HashSet<GroupingBuilder>();
44     private final Set<UsesNodeBuilder> usesNodes = new HashSet<UsesNodeBuilder>();
45     private boolean resolved;
46
47     AugmentationSchemaBuilderImpl(final String augmentTargetStr, final int line) {
48         this.augmentTargetStr = augmentTargetStr;
49         this.line = line;
50         final SchemaPath targetPath = YangModelBuilderUtil
51                 .parseAugmentPath(augmentTargetStr);
52         augmentTarget = targetPath;
53         instance = new AugmentationSchemaImpl(targetPath);
54     }
55
56     @Override
57     public int getLine() {
58         return line;
59     }
60
61     @Override
62     public void addChildNode(DataSchemaNodeBuilder childNode) {
63         childNodes.add(childNode);
64     }
65
66     @Override
67     public Set<DataSchemaNodeBuilder> getChildNodes() {
68         return childNodes;
69     }
70
71     @Override
72     public void addGrouping(GroupingBuilder grouping) {
73         groupings.add(grouping);
74     }
75
76     @Override
77     public void addUsesNode(UsesNodeBuilder usesBuilder) {
78         usesNodes.add(usesBuilder);
79     }
80
81     /**
82      * Always returns null.
83      */
84     @Override
85     public QName getQName() {
86         return null;
87     }
88
89     /**
90      * Always returns null.
91      */
92     @Override
93     public SchemaPath getPath() {
94         return null;
95     }
96
97     @Override
98     public AugmentationSchema build() {
99         if (!built) {
100             instance.setTargetPath(finalAugmentTarget);
101
102             RevisionAwareXPath whenStmt;
103             if (whenCondition == null) {
104                 whenStmt = null;
105             } else {
106                 whenStmt = new RevisionAwareXPathImpl(whenCondition, false);
107             }
108             instance.setWhenCondition(whenStmt);
109
110             // CHILD NODES
111             final Map<QName, DataSchemaNode> childs = new HashMap<QName, DataSchemaNode>();
112             for (DataSchemaNodeBuilder node : childNodes) {
113                 childs.put(node.getQName(), node.build());
114             }
115             instance.setChildNodes(childs);
116
117             // GROUPINGS
118             final Set<GroupingDefinition> groupingDefinitions = new HashSet<GroupingDefinition>();
119             for (GroupingBuilder builder : groupings) {
120                 groupingDefinitions.add(builder.build());
121             }
122             instance.setGroupings(groupingDefinitions);
123
124             // USES
125             final Set<UsesNode> usesNodeDefinitions = new HashSet<UsesNode>();
126             for (UsesNodeBuilder builder : usesNodes) {
127                 usesNodeDefinitions.add(builder.build());
128             }
129             instance.setUses(usesNodeDefinitions);
130
131             built = true;
132         }
133         return instance;
134     }
135
136     @Override
137     public boolean isResolved() {
138         return resolved;
139     }
140
141     @Override
142     public void setResolved(boolean resolved) {
143         this.resolved = resolved;
144     }
145
146     public String getWhenCondition() {
147         return whenCondition;
148     }
149
150     public void addWhenCondition(String whenCondition) {
151         this.whenCondition = whenCondition;
152     }
153
154     @Override
155     public void addTypedef(TypeDefinitionBuilder type) {
156         throw new YangParseException(line,
157                 "Augmentation can not contains type definitions");
158     }
159
160     @Override
161     public void setDescription(String description) {
162         instance.setDescription(description);
163     }
164
165     @Override
166     public void setReference(String reference) {
167         instance.setReference(reference);
168     }
169
170     @Override
171     public void setStatus(Status status) {
172         instance.setStatus(status);
173     }
174
175     @Override
176     public SchemaPath getTargetPath() {
177         return augmentTarget;
178     }
179
180     @Override
181     public void setTargetPath(SchemaPath path) {
182         this.finalAugmentTarget = path;
183     }
184
185     @Override
186     public String getTargetPathAsString() {
187         return augmentTargetStr;
188     }
189
190     @Override
191     public int hashCode() {
192         final int prime = 17;
193         int result = 1;
194         result = prime
195                 * result
196                 + ((augmentTargetStr == null) ? 0 : augmentTargetStr.hashCode());
197         result = prime * result
198                 + ((whenCondition == null) ? 0 : whenCondition.hashCode());
199         result = prime * result
200                 + ((childNodes == null) ? 0 : childNodes.hashCode());
201         return result;
202     }
203
204     @Override
205     public boolean equals(Object obj) {
206         if (this == obj) {
207             return true;
208         }
209         if (obj == null) {
210             return false;
211         }
212         if (getClass() != obj.getClass()) {
213             return false;
214         }
215         AugmentationSchemaBuilderImpl other = (AugmentationSchemaBuilderImpl) obj;
216         if (augmentTargetStr == null) {
217             if (other.augmentTargetStr != null) {
218                 return false;
219             }
220         } else if (!augmentTargetStr.equals(other.augmentTargetStr)) {
221             return false;
222         }
223         if (whenCondition == null) {
224             if (other.whenCondition != null) {
225                 return false;
226             }
227         } else if (!whenCondition.equals(other.whenCondition)) {
228             return false;
229         }
230         if (childNodes == null) {
231             if (other.childNodes != null) {
232                 return false;
233             }
234         } else if (!childNodes.equals(other.childNodes)) {
235             return false;
236         }
237         return true;
238     }
239
240     private final class AugmentationSchemaImpl implements AugmentationSchema {
241         private SchemaPath targetPath;
242         private RevisionAwareXPath whenCondition;
243         private Map<QName, DataSchemaNode> childNodes = Collections.emptyMap();
244         private Set<GroupingDefinition> groupings = Collections.emptySet();
245         private Set<UsesNode> uses = Collections.emptySet();
246
247         private String description;
248         private String reference;
249         private Status status;
250
251         private AugmentationSchemaImpl(SchemaPath targetPath) {
252             this.targetPath = targetPath;
253         }
254
255         @Override
256         public SchemaPath getTargetPath() {
257             return targetPath;
258         }
259
260         private void setTargetPath(SchemaPath path) {
261             this.targetPath = path;
262         }
263
264         @Override
265         public RevisionAwareXPath getWhenCondition() {
266             return whenCondition;
267         }
268
269         private void setWhenCondition(RevisionAwareXPath whenCondition) {
270             this.whenCondition = whenCondition;
271         }
272
273         @Override
274         public Set<DataSchemaNode> getChildNodes() {
275             return new HashSet<DataSchemaNode>(childNodes.values());
276         }
277
278         private void setChildNodes(Map<QName, DataSchemaNode> childNodes) {
279             if (childNodes != null) {
280                 this.childNodes = childNodes;
281             }
282         }
283
284         @Override
285         public Set<GroupingDefinition> getGroupings() {
286             return groupings;
287         }
288
289         private void setGroupings(Set<GroupingDefinition> groupings) {
290             if (groupings != null) {
291                 this.groupings = groupings;
292             }
293         }
294
295         @Override
296         public Set<UsesNode> getUses() {
297             return uses;
298         }
299
300         private void setUses(Set<UsesNode> uses) {
301             if (uses != null) {
302                 this.uses = uses;
303             }
304         }
305
306         /**
307          * Always returns an empty set, because augmentation can not contains
308          * type definitions.
309          */
310         @Override
311         public Set<TypeDefinition<?>> getTypeDefinitions() {
312             return Collections.emptySet();
313         }
314
315         @Override
316         public String getDescription() {
317             return description;
318         }
319
320         private void setDescription(String description) {
321             this.description = description;
322         }
323
324         @Override
325         public String getReference() {
326             return reference;
327         }
328
329         private void setReference(String reference) {
330             this.reference = reference;
331         }
332
333         @Override
334         public Status getStatus() {
335             return status;
336         }
337
338         private void setStatus(Status status) {
339             this.status = status;
340         }
341
342         @Override
343         public DataSchemaNode getDataChildByName(QName name) {
344             return childNodes.get(name);
345         }
346
347         @Override
348         public DataSchemaNode getDataChildByName(String name) {
349             DataSchemaNode result = null;
350             for (Map.Entry<QName, DataSchemaNode> entry : childNodes.entrySet()) {
351                 if (entry.getKey().getLocalName().equals(name)) {
352                     result = entry.getValue();
353                     break;
354                 }
355             }
356             return result;
357         }
358
359         @Override
360         public int hashCode() {
361             final int prime = 17;
362             int result = 1;
363             result = prime * result
364                     + ((targetPath == null) ? 0 : targetPath.hashCode());
365             result = prime * result
366                     + ((whenCondition == null) ? 0 : whenCondition.hashCode());
367             result = prime * result
368                     + ((childNodes == null) ? 0 : childNodes.hashCode());
369             return result;
370         }
371
372         @Override
373         public boolean equals(Object obj) {
374             if (this == obj) {
375                 return true;
376             }
377             if (obj == null) {
378                 return false;
379             }
380             if (getClass() != obj.getClass()) {
381                 return false;
382             }
383             AugmentationSchemaImpl other = (AugmentationSchemaImpl) obj;
384             if (targetPath == null) {
385                 if (other.targetPath != null) {
386                     return false;
387                 }
388             } else if (!targetPath.equals(other.targetPath)) {
389                 return false;
390             }
391             if (whenCondition == null) {
392                 if (other.whenCondition != null) {
393                     return false;
394                 }
395             } else if (!whenCondition.equals(other.whenCondition)) {
396                 return false;
397             }
398             if (childNodes == null) {
399                 if (other.childNodes != null) {
400                     return false;
401                 }
402             } else if (!childNodes.equals(other.childNodes)) {
403                 return false;
404             }
405             return true;
406         }
407
408         @Override
409         public String toString() {
410             StringBuilder sb = new StringBuilder(
411                     AugmentationSchemaImpl.class.getSimpleName());
412             sb.append("[");
413             sb.append("targetPath=" + targetPath);
414             sb.append(", childNodes=" + childNodes.values());
415             sb.append(", groupings=" + groupings);
416             sb.append(", uses=" + uses);
417             sb.append("]");
418             return sb.toString();
419         }
420     }
421
422 }