Minor code style improvements to eliminate eclipse warnings.
[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.UsesNodeBuilder;
32 import org.opendaylight.yangtools.yang.parser.util.Comparators;
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() {
60         if (!isBuilt) {
61             instance = new UsesNodeImpl(groupingPath);
62             instance.setAddedByUses(addedByUses);
63
64             // AUGMENTATIONS
65             for (AugmentationSchemaBuilder builder : addedAugments) {
66                 augments.add(builder.build());
67             }
68             instance.setAugmentations(augments);
69
70             // REFINES
71             final Map<SchemaPath, SchemaNode> refineNodes = new HashMap<>();
72             for (SchemaNodeBuilder refineBuilder : refineBuilders) {
73                 SchemaNode refineNode = refineBuilder.build();
74                 refineNodes.put(refineNode.getPath(), refineNode);
75             }
76             instance.setRefines(refineNodes);
77
78             // UNKNOWN NODES
79             for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
80                 unknownNodes.add(b.build());
81             }
82             Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
83             instance.addUnknownSchemaNodes(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     private static final class UsesNodeImpl implements UsesNode {
244         private final SchemaPath groupingPath;
245         private Set<AugmentationSchema> augmentations = Collections.emptySet();
246         private boolean addedByUses;
247         private Map<SchemaPath, SchemaNode> refines = Collections.emptyMap();
248         private final List<UnknownSchemaNode> unknownNodes = new ArrayList<>();
249
250         private UsesNodeImpl(final SchemaPath groupingPath) {
251             this.groupingPath = groupingPath;
252         }
253
254         @Override
255         public SchemaPath getGroupingPath() {
256             return groupingPath;
257         }
258
259         @Override
260         public Set<AugmentationSchema> getAugmentations() {
261             return augmentations;
262         }
263
264         private void setAugmentations(final Set<AugmentationSchema> augmentations) {
265             if (augmentations != null) {
266                 this.augmentations = augmentations;
267             }
268         }
269
270         @Override
271         public boolean isAugmenting() {
272             return false;
273         }
274
275         @Override
276         public boolean isAddedByUses() {
277             return addedByUses;
278         }
279
280         private void setAddedByUses(final boolean addedByUses) {
281             this.addedByUses = addedByUses;
282         }
283
284         @Override
285         public Map<SchemaPath, SchemaNode> getRefines() {
286             return refines;
287         }
288
289         private void setRefines(Map<SchemaPath, SchemaNode> refines) {
290             if (refines != null) {
291                 this.refines = refines;
292             }
293         }
294
295         private void addUnknownSchemaNodes(List<UnknownSchemaNode> unknownSchemaNodes) {
296             if (unknownSchemaNodes != null) {
297                 this.unknownNodes.addAll(unknownSchemaNodes);
298             }
299         }
300
301         @Override
302         public int hashCode() {
303             final int prime = 31;
304             int result = 1;
305             result = prime * result + ((groupingPath == null) ? 0 : groupingPath.hashCode());
306             result = prime * result + ((augmentations == null) ? 0 : augmentations.hashCode());
307             return result;
308         }
309
310         @Override
311         public boolean equals(Object obj) {
312             if (this == obj) {
313                 return true;
314             }
315             if (obj == null) {
316                 return false;
317             }
318             if (getClass() != obj.getClass()) {
319                 return false;
320             }
321             final UsesNodeImpl other = (UsesNodeImpl) obj;
322             if (groupingPath == null) {
323                 if (other.groupingPath != null) {
324                     return false;
325                 }
326             } else if (!groupingPath.equals(other.groupingPath)) {
327                 return false;
328             }
329             if (augmentations == null) {
330                 if (other.augmentations != null) {
331                     return false;
332                 }
333             } else if (!augmentations.equals(other.augmentations)) {
334                 return false;
335             }
336             return true;
337         }
338
339         @Override
340         public String toString() {
341             StringBuilder sb = new StringBuilder(UsesNodeImpl.class.getSimpleName());
342             sb.append("[groupingPath=");
343             sb.append(groupingPath);
344             sb.append("]");
345             return sb.toString();
346         }
347     }
348
349 }