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