Merge "Updated context references in yang-ext"
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / builder / impl / UsesNodeBuilderImpl.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.HashMap;
13 import java.util.HashSet;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.Set;
17
18 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
19 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
20 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
22 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.UsesNode;
24 import org.opendaylight.yangtools.yang.parser.builder.api.AbstractBuilder;
25 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
26 import org.opendaylight.yangtools.yang.parser.builder.api.Builder;
27 import org.opendaylight.yangtools.yang.parser.builder.api.DataNodeContainerBuilder;
28 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
29 import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder;
30 import org.opendaylight.yangtools.yang.parser.builder.api.SchemaNodeBuilder;
31 import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
32 import org.opendaylight.yangtools.yang.parser.builder.api.UsesNodeBuilder;
33 import org.opendaylight.yangtools.yang.parser.util.RefineHolder;
34 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
35
36 public final class UsesNodeBuilderImpl extends AbstractBuilder implements UsesNodeBuilder {
37     private boolean isBuilt;
38     private UsesNodeImpl instance;
39     private DataNodeContainerBuilder parent;
40     private final String groupingName;
41     private SchemaPath groupingPath;
42     private GroupingDefinition groupingDefinition;
43     private GroupingBuilder groupingBuilder;
44     private boolean addedByUses;
45     private final Set<AugmentationSchemaBuilder> addedAugments = new HashSet<AugmentationSchemaBuilder>();
46     private final List<SchemaNodeBuilder> refineBuilders = new ArrayList<SchemaNodeBuilder>();
47     private final List<RefineHolder> refines = new ArrayList<RefineHolder>();
48
49     private Set<DataSchemaNodeBuilder> targetChildren = new HashSet<>();
50     private Set<GroupingBuilder> targetGroupings = new HashSet<>();
51     private Set<TypeDefinitionBuilder> targetTypedefs = new HashSet<>();
52     private List<UnknownSchemaNodeBuilder> targetUnknownNodes = new ArrayList<>();
53
54     private final boolean isCopy;
55     private boolean dataCollected;
56
57     @Override
58     public boolean isCopy() {
59         return isCopy;
60     }
61
62     @Override
63     public boolean isDataCollected() {
64         return dataCollected;
65     }
66
67     @Override
68     public void setDataCollected(boolean dataCollected) {
69         this.dataCollected = dataCollected;
70     }
71
72     public UsesNodeBuilderImpl(final String moduleName, final int line, final String groupingName) {
73         super(moduleName, line);
74         this.groupingName = groupingName;
75         isCopy = false;
76     }
77
78     public UsesNodeBuilderImpl(final String moduleName, final int line, final String groupingName, final boolean isCopy) {
79         super(moduleName, line);
80         this.groupingName = groupingName;
81         this.isCopy = isCopy;
82     }
83
84     @Override
85     public UsesNode build() {
86         if (!isBuilt) {
87             instance = new UsesNodeImpl(groupingPath);
88             instance.setAddedByUses(addedByUses);
89
90             // AUGMENTATIONS
91             final Set<AugmentationSchema> augments = new HashSet<AugmentationSchema>();
92             for (AugmentationSchemaBuilder builder : addedAugments) {
93                 augments.add(builder.build());
94             }
95             instance.setAugmentations(augments);
96
97             // REFINES
98             final Map<SchemaPath, SchemaNode> refineNodes = new HashMap<SchemaPath, SchemaNode>();
99             for (SchemaNodeBuilder refineBuilder : refineBuilders) {
100                 SchemaNode refineNode = refineBuilder.build();
101                 refineNodes.put(refineNode.getPath(), refineNode);
102             }
103             instance.setRefines(refineNodes);
104
105             // UNKNOWN NODES
106             List<UnknownSchemaNode> unknownNodes = new ArrayList<UnknownSchemaNode>();
107             for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
108                 unknownNodes.add(b.build());
109             }
110             instance.setUnknownSchemaNodes(unknownNodes);
111
112             isBuilt = true;
113         }
114
115         return instance;
116     }
117
118     @Override
119     public DataNodeContainerBuilder getParent() {
120         return parent;
121     }
122
123     @Override
124     public void setParent(Builder parent) {
125         if (!(parent instanceof DataNodeContainerBuilder)) {
126             throw new YangParseException(moduleName, line,
127                     "Parent of 'uses' has to be instance of DataNodeContainerBuilder, but was: '" + parent + "'.");
128         }
129         this.parent = (DataNodeContainerBuilder) parent;
130     }
131
132     @Override
133     public SchemaPath getGroupingPath() {
134         return groupingPath;
135     }
136
137     @Override
138     public GroupingDefinition getGroupingDefinition() {
139         return groupingDefinition;
140     }
141
142     @Override
143     public void setGroupingDefinition(GroupingDefinition groupingDefinition) {
144         this.groupingDefinition = groupingDefinition;
145         if (groupingDefinition != null) {
146             this.groupingPath = groupingDefinition.getPath();
147         }
148     }
149
150     @Override
151     public GroupingBuilder getGroupingBuilder() {
152         return groupingBuilder;
153     }
154
155     @Override
156     public void setGrouping(GroupingBuilder grouping) {
157         this.groupingBuilder = grouping;
158         if (groupingBuilder != null) {
159             this.groupingPath = groupingBuilder.getPath();
160         }
161     }
162
163     @Override
164     public String getGroupingPathAsString() {
165         return groupingName;
166     }
167
168     @Override
169     public Set<AugmentationSchemaBuilder> getAugmentations() {
170         return addedAugments;
171     }
172
173     @Override
174     public void addAugment(final AugmentationSchemaBuilder augmentBuilder) {
175         addedAugments.add(augmentBuilder);
176     }
177
178     @Override
179     public boolean isAddedByUses() {
180         return addedByUses;
181     }
182
183     @Override
184     public void setAddedByUses(final boolean addedByUses) {
185         this.addedByUses = addedByUses;
186     }
187
188     @Override
189     public List<SchemaNodeBuilder> getRefineNodes() {
190         return refineBuilders;
191     }
192
193     @Override
194     public void addRefineNode(DataSchemaNodeBuilder refineNode) {
195         refineBuilders.add(refineNode);
196     }
197
198     @Override
199     public List<RefineHolder> getRefines() {
200         return refines;
201     }
202
203     @Override
204     public void addRefine(RefineHolder refine) {
205         refines.add(refine);
206     }
207
208     @Override
209     public Set<DataSchemaNodeBuilder> getTargetChildren() {
210         return targetChildren;
211     }
212
213     @Override
214     public void setTargetChildren(Set<DataSchemaNodeBuilder> targetChildren) {
215         this.targetChildren = targetChildren;
216     }
217
218     @Override
219     public Set<GroupingBuilder> getTargetGroupings() {
220         return targetGroupings;
221     }
222
223     @Override
224     public void setTargetGroupings(Set<GroupingBuilder> targetGroupings) {
225         this.targetGroupings = targetGroupings;
226     }
227
228     @Override
229     public Set<TypeDefinitionBuilder> getTargetTypedefs() {
230         return targetTypedefs;
231     }
232
233     @Override
234     public void setTargetTypedefs(Set<TypeDefinitionBuilder> targetTypedefs) {
235         this.targetTypedefs = targetTypedefs;
236     }
237
238     @Override
239     public List<UnknownSchemaNodeBuilder> getTargetUnknownNodes() {
240         return targetUnknownNodes;
241     }
242
243     @Override
244     public void setTargetUnknownNodes(List<UnknownSchemaNodeBuilder> targetUnknownNodes) {
245         this.targetUnknownNodes = targetUnknownNodes;
246     }
247
248     @Override
249     public int hashCode() {
250         final int prime = 31;
251         int result = 1;
252         result = prime * result + ((groupingName == null) ? 0 : groupingName.hashCode());
253         result = prime * result + ((parent == null) ? 0 : parent.hashCode());
254         result = prime * result + ((refines == null) ? 0 : refines.hashCode());
255         return result;
256     }
257
258     @Override
259     public boolean equals(Object obj) {
260         if (this == obj) {
261             return true;
262         }
263         if (obj == null) {
264             return false;
265         }
266         if (getClass() != obj.getClass()) {
267             return false;
268         }
269         UsesNodeBuilderImpl other = (UsesNodeBuilderImpl) obj;
270         if (groupingName == null) {
271             if (other.groupingName != null) {
272                 return false;
273             }
274         } else if (!groupingName.equals(other.groupingName)) {
275             return false;
276         }
277         if (parent == null) {
278             if (other.parent != null) {
279                 return false;
280             }
281         } else if (!parent.equals(other.parent)) {
282             return false;
283         }
284         if (refines == null) {
285             if (other.refines != null) {
286                 return false;
287             }
288         } else if (!refines.equals(other.refines)) {
289             return false;
290         }
291         return true;
292     }
293
294     @Override
295     public String toString() {
296         return "uses '" + groupingName + "'";
297     }
298
299     public final class UsesNodeImpl implements UsesNode {
300         private final SchemaPath groupingPath;
301         private Set<AugmentationSchema> augmentations = Collections.emptySet();
302         private boolean addedByUses;
303         private Map<SchemaPath, SchemaNode> refines = Collections.emptyMap();
304         private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
305
306         private UsesNodeImpl(final SchemaPath groupingPath) {
307             this.groupingPath = groupingPath;
308         }
309
310         @Override
311         public SchemaPath getGroupingPath() {
312             return groupingPath;
313         }
314
315         @Override
316         public Set<AugmentationSchema> getAugmentations() {
317             return augmentations;
318         }
319
320         private void setAugmentations(final Set<AugmentationSchema> augmentations) {
321             if (augmentations != null) {
322                 this.augmentations = augmentations;
323             }
324         }
325
326         @Override
327         public boolean isAugmenting() {
328             return false;
329         }
330
331         @Override
332         public boolean isAddedByUses() {
333             return addedByUses;
334         }
335
336         private void setAddedByUses(final boolean addedByUses) {
337             this.addedByUses = addedByUses;
338         }
339
340         @Override
341         public Map<SchemaPath, SchemaNode> getRefines() {
342             return refines;
343         }
344
345         private void setRefines(Map<SchemaPath, SchemaNode> refines) {
346             if (refines != null) {
347                 this.refines = refines;
348             }
349         }
350
351         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
352             return unknownNodes;
353         }
354
355         private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownSchemaNodes) {
356             if (unknownSchemaNodes != null) {
357                 this.unknownNodes = unknownSchemaNodes;
358             }
359         }
360
361         public UsesNodeBuilder toBuilder() {
362             return UsesNodeBuilderImpl.this;
363         }
364
365         @Override
366         public int hashCode() {
367             final int prime = 31;
368             int result = 1;
369             result = prime * result + ((groupingPath == null) ? 0 : groupingPath.hashCode());
370             result = prime * result + ((augmentations == null) ? 0 : augmentations.hashCode());
371             return result;
372         }
373
374         @Override
375         public boolean equals(Object obj) {
376             if (this == obj) {
377                 return true;
378             }
379             if (obj == null) {
380                 return false;
381             }
382             if (getClass() != obj.getClass()) {
383                 return false;
384             }
385             final UsesNodeImpl other = (UsesNodeImpl) obj;
386             if (groupingPath == null) {
387                 if (other.groupingPath != null) {
388                     return false;
389                 }
390             } else if (!groupingPath.equals(other.groupingPath)) {
391                 return false;
392             }
393             if (augmentations == null) {
394                 if (other.augmentations != null) {
395                     return false;
396                 }
397             } else if (!augmentations.equals(other.augmentations)) {
398                 return false;
399             }
400             return true;
401         }
402
403         @Override
404         public String toString() {
405             StringBuilder sb = new StringBuilder(UsesNodeImpl.class.getSimpleName());
406             sb.append("[groupingPath=");
407             sb.append(groupingPath);
408             sb.append("]");
409             return sb.toString();
410         }
411     }
412
413 }