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