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