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 / ChoiceBuilder.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.ArrayList;
11 import java.util.Collections;
12 import java.util.HashSet;
13 import java.util.List;
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.ChoiceCaseNode;
19 import org.opendaylight.controller.yang.model.api.ChoiceNode;
20 import org.opendaylight.controller.yang.model.api.ConstraintDefinition;
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.UnknownSchemaNode;
24 import org.opendaylight.controller.yang.parser.builder.api.AugmentationSchemaBuilder;
25 import org.opendaylight.controller.yang.parser.builder.api.AugmentationTargetBuilder;
26 import org.opendaylight.controller.yang.parser.builder.api.ChildNodeBuilder;
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.YangParseException;
32
33 public final class ChoiceBuilder implements DataSchemaNodeBuilder,
34         ChildNodeBuilder, AugmentationTargetBuilder {
35     private boolean built;
36     private final ChoiceNodeImpl instance;
37     private final int line;
38     // SchemaNode args
39     private final QName qname;
40     private SchemaPath schemaPath;
41     private String description;
42     private String reference;
43     private Status status = Status.CURRENT;
44     private final List<UnknownSchemaNodeBuilder> addedUnknownNodes = new ArrayList<UnknownSchemaNodeBuilder>();
45     // DataSchemaNode args
46     private boolean augmenting;
47     private boolean configuration;
48     private final ConstraintsBuilder constraints;
49     // DataNodeContainer args
50     private final Set<TypeDefinitionBuilder> addedTypedefs = new HashSet<TypeDefinitionBuilder>();
51     private final Set<UsesNodeBuilder> addedUsesNodes = new HashSet<UsesNodeBuilder>();
52     // AugmentationTarget args
53     private final Set<AugmentationSchemaBuilder> addedAugmentations = new HashSet<AugmentationSchemaBuilder>();
54     // ChoiceNode args
55     private final Set<ChoiceCaseBuilder> cases = new HashSet<ChoiceCaseBuilder>();
56     private String defaultCase;
57
58     public ChoiceBuilder(final QName qname, final int line) {
59         this.qname = qname;
60         this.line = line;
61         instance = new ChoiceNodeImpl(qname);
62         constraints = new ConstraintsBuilder(line);
63     }
64
65     @Override
66     public ChoiceNode build() {
67         if (!built) {
68             instance.setPath(schemaPath);
69             instance.setDescription(description);
70             instance.setReference(reference);
71             instance.setStatus(status);
72             instance.setAugmenting(augmenting);
73             instance.setConfiguration(configuration);
74             instance.setConstraints(constraints.build());
75             instance.setDefaultCase(defaultCase);
76
77             // CASES
78             final Set<ChoiceCaseNode> choiceCases = new HashSet<ChoiceCaseNode>();
79             for (ChoiceCaseBuilder caseBuilder : cases) {
80                 choiceCases.add(caseBuilder.build());
81             }
82             instance.setCases(choiceCases);
83
84             // AUGMENTATIONS
85             final Set<AugmentationSchema> augmentations = new HashSet<AugmentationSchema>();
86             for (AugmentationSchemaBuilder builder : addedAugmentations) {
87                 augmentations.add(builder.build());
88             }
89             instance.setAvailableAugmentations(augmentations);
90
91             // UNKNOWN NODES
92             final List<UnknownSchemaNode> unknownNodes = new ArrayList<UnknownSchemaNode>();
93             for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
94                 unknownNodes.add(b.build());
95             }
96             instance.setUnknownSchemaNodes(unknownNodes);
97
98             built = true;
99         }
100         return instance;
101     }
102
103     @Override
104     public int getLine() {
105         return line;
106     }
107
108     public Set<ChoiceCaseBuilder> getCases() {
109         return cases;
110     }
111
112     @Override
113     public void addChildNode(DataSchemaNodeBuilder childNode) {
114         if (!(childNode instanceof ChoiceCaseBuilder)) {
115             ChoiceCaseBuilder caseBuilder = new ChoiceCaseBuilder(
116                     childNode.getQName(), childNode.getLine());
117             caseBuilder.addChildNode(childNode);
118             cases.add(caseBuilder);
119         } else {
120             cases.add((ChoiceCaseBuilder) childNode);
121         }
122     }
123
124     @Override
125     public QName getQName() {
126         return qname;
127     }
128
129     /**
130      * Choice can not contains grouping statements, so this method always
131      * returns an empty set.
132      *
133      * @return empty set
134      */
135     public Set<GroupingBuilder> getGroupings() {
136         return Collections.emptySet();
137     }
138
139     @Override
140     public void addGrouping(GroupingBuilder groupingBuilder) {
141         throw new YangParseException(line,
142                 "Can not add grouping to 'choice' node.");
143     }
144
145     public Set<TypeDefinitionBuilder> getTypedefs() {
146         return addedTypedefs;
147     }
148
149     @Override
150     public void addTypedef(final TypeDefinitionBuilder type) {
151         addedTypedefs.add(type);
152     }
153
154     public SchemaPath getPath() {
155         return schemaPath;
156     }
157
158     @Override
159     public void setPath(final SchemaPath schemaPath) {
160         this.schemaPath = schemaPath;
161     }
162
163     public String getDescription() {
164         return description;
165     }
166
167     @Override
168     public void setDescription(final String description) {
169         this.description = description;
170     }
171
172     public String getReference() {
173         return reference;
174     }
175
176     @Override
177     public void setReference(String reference) {
178         this.reference = reference;
179     }
180
181     public Status getStatus() {
182         return status;
183     }
184
185     @Override
186     public void setStatus(Status status) {
187         if (status != null) {
188             this.status = status;
189         }
190     }
191
192     public boolean isAugmenting() {
193         return augmenting;
194     }
195
196     @Override
197     public void setAugmenting(boolean augmenting) {
198         this.augmenting = augmenting;
199     }
200
201     public boolean isConfiguration() {
202         return configuration;
203     }
204
205     @Override
206     public void setConfiguration(boolean configuration) {
207         this.configuration = configuration;
208     }
209
210     @Override
211     public ConstraintsBuilder getConstraints() {
212         return constraints;
213     }
214
215     public Set<UsesNodeBuilder> getUsesNodes() {
216         return addedUsesNodes;
217     }
218
219     @Override
220     public void addUsesNode(UsesNodeBuilder usesNodeBuilder) {
221         addedUsesNodes.add(usesNodeBuilder);
222     }
223
224     public List<UnknownSchemaNodeBuilder> getUnknownNodes() {
225         return addedUnknownNodes;
226     }
227
228     public Set<AugmentationSchemaBuilder> getAugmentations() {
229         return addedAugmentations;
230     }
231
232     @Override
233     public void addAugmentation(AugmentationSchemaBuilder augment) {
234         addedAugmentations.add(augment);
235     }
236
237     @Override
238     public void addUnknownSchemaNode(UnknownSchemaNodeBuilder unknownNode) {
239         addedUnknownNodes.add(unknownNode);
240     }
241
242     public String getDefaultCase() {
243         return defaultCase;
244     }
245
246     public void setDefaultCase(String defaultCase) {
247         this.defaultCase = defaultCase;
248     }
249
250     @Override
251     public Set<DataSchemaNodeBuilder> getChildNodes() {
252         return new HashSet<DataSchemaNodeBuilder>(cases);
253     }
254
255     private final class ChoiceNodeImpl implements ChoiceNode {
256         private final QName qname;
257         private SchemaPath path;
258         private String description;
259         private String reference;
260         private Status status = Status.CURRENT;
261         private boolean augmenting;
262         private boolean configuration;
263         private ConstraintDefinition constraints;
264         private Set<ChoiceCaseNode> cases = Collections.emptySet();
265         private Set<AugmentationSchema> augmentations = Collections.emptySet();
266         private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
267         private String defaultCase;
268
269         private ChoiceNodeImpl(QName qname) {
270             this.qname = qname;
271         }
272
273         @Override
274         public QName getQName() {
275             return qname;
276         }
277
278         @Override
279         public SchemaPath getPath() {
280             return path;
281         }
282
283         private void setPath(SchemaPath path) {
284             this.path = path;
285         }
286
287         @Override
288         public String getDescription() {
289             return description;
290         }
291
292         private void setDescription(String description) {
293             this.description = description;
294         }
295
296         @Override
297         public String getReference() {
298             return reference;
299         }
300
301         private void setReference(String reference) {
302             this.reference = reference;
303         }
304
305         @Override
306         public Status getStatus() {
307             return status;
308         }
309
310         private void setStatus(Status status) {
311             if (status != null) {
312                 this.status = status;
313             }
314         }
315
316         @Override
317         public boolean isAugmenting() {
318             return augmenting;
319         }
320
321         private void setAugmenting(boolean augmenting) {
322             this.augmenting = augmenting;
323         }
324
325         @Override
326         public boolean isConfiguration() {
327             return configuration;
328         }
329
330         private void setConfiguration(boolean configuration) {
331             this.configuration = configuration;
332         }
333
334         @Override
335         public ConstraintDefinition getConstraints() {
336             return constraints;
337         }
338
339         private void setConstraints(ConstraintDefinition constraints) {
340             this.constraints = constraints;
341         }
342
343         @Override
344         public Set<AugmentationSchema> getAvailableAugmentations() {
345             return augmentations;
346         }
347
348         private void setAvailableAugmentations(
349                 Set<AugmentationSchema> availableAugmentations) {
350             if (availableAugmentations != null) {
351                 this.augmentations = availableAugmentations;
352             }
353         }
354
355         @Override
356         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
357             return unknownNodes;
358         }
359
360         private void setUnknownSchemaNodes(
361                 List<UnknownSchemaNode> unknownSchemaNodes) {
362             if (unknownSchemaNodes != null) {
363                 this.unknownNodes = unknownSchemaNodes;
364             }
365         }
366
367         @Override
368         public Set<ChoiceCaseNode> getCases() {
369             return cases;
370         }
371
372         private void setCases(Set<ChoiceCaseNode> cases) {
373             if (cases != null) {
374                 this.cases = cases;
375             }
376         }
377
378         public String getDefaultCase() {
379             return defaultCase;
380         }
381
382         private void setDefaultCase(String defaultCase) {
383             this.defaultCase = defaultCase;
384         }
385
386         @Override
387         public int hashCode() {
388             final int prime = 31;
389             int result = 1;
390             result = prime * result + ((qname == null) ? 0 : qname.hashCode());
391             result = prime * result + ((path == null) ? 0 : path.hashCode());
392             return result;
393         }
394
395         @Override
396         public boolean equals(Object obj) {
397             if (this == obj) {
398                 return true;
399             }
400             if (obj == null) {
401                 return false;
402             }
403             if (getClass() != obj.getClass()) {
404                 return false;
405             }
406             ChoiceNodeImpl other = (ChoiceNodeImpl) obj;
407             if (qname == null) {
408                 if (other.qname != null) {
409                     return false;
410                 }
411             } else if (!qname.equals(other.qname)) {
412                 return false;
413             }
414             if (path == null) {
415                 if (other.path != null) {
416                     return false;
417                 }
418             } else if (!path.equals(other.path)) {
419                 return false;
420             }
421             return true;
422         }
423
424         @Override
425         public String toString() {
426             StringBuilder sb = new StringBuilder(
427                     ChoiceNodeImpl.class.getSimpleName());
428             sb.append("[");
429             sb.append("qname=" + qname);
430             sb.append("]");
431             return sb.toString();
432         }
433     }
434
435 }