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