2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.builder.impl;
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;
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;
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 boolean augmenting;
46 private AugmentationSchemaBuilder parentAugment;
47 private final Set<AugmentationSchemaBuilder> addedAugments = new HashSet<>();
48 private final List<SchemaNodeBuilder> refineBuilders = new ArrayList<>();
49 private final List<RefineHolder> refines = new ArrayList<>();
52 * Copies of target grouping child nodes.
54 private final Set<DataSchemaNodeBuilder> targetChildren = new HashSet<>();
57 * Copies of target grouping groupings.
59 private final Set<GroupingBuilder> targetGroupings = new HashSet<>();
62 * Copies of target grouping typedefs.
64 private final Set<TypeDefinitionBuilder> targetTypedefs = new HashSet<>();
67 * Copies of target grouping unknown nodes.
69 private final List<UnknownSchemaNodeBuilder> targetUnknownNodes = new ArrayList<>();
71 private final boolean isCopy;
72 private boolean dataCollected;
75 public boolean isCopy() {
80 public boolean isDataCollected() {
85 public void setDataCollected(boolean dataCollected) {
86 this.dataCollected = dataCollected;
89 public UsesNodeBuilderImpl(final String moduleName, final int line, final String groupingName) {
90 super(moduleName, line);
91 this.groupingName = groupingName;
95 public UsesNodeBuilderImpl(final String moduleName, final int line, final String groupingName, final boolean isCopy) {
96 super(moduleName, line);
97 this.groupingName = groupingName;
102 public UsesNode build() {
104 instance = new UsesNodeImpl(groupingPath);
105 instance.setAddedByUses(addedByUses);
108 final Set<AugmentationSchema> augments = new HashSet<>();
109 for (AugmentationSchemaBuilder builder : addedAugments) {
110 augments.add(builder.build());
112 instance.setAugmentations(augments);
115 final Map<SchemaPath, SchemaNode> refineNodes = new HashMap<>();
116 for (SchemaNodeBuilder refineBuilder : refineBuilders) {
117 SchemaNode refineNode = refineBuilder.build();
118 refineNodes.put(refineNode.getPath(), refineNode);
120 instance.setRefines(refineNodes);
123 List<UnknownSchemaNode> unknownNodes = new ArrayList<>();
124 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
125 unknownNodes.add(b.build());
127 instance.setUnknownSchemaNodes(unknownNodes);
136 public DataNodeContainerBuilder getParent() {
141 public void setParent(Builder parent) {
142 if (!(parent instanceof DataNodeContainerBuilder)) {
143 throw new YangParseException(moduleName, line,
144 "Parent of 'uses' has to be instance of DataNodeContainerBuilder, but was: '" + parent + "'.");
146 this.parent = (DataNodeContainerBuilder) parent;
150 public SchemaPath getGroupingPath() {
155 public GroupingDefinition getGroupingDefinition() {
156 return groupingDefinition;
160 public void setGroupingDefinition(GroupingDefinition groupingDefinition) {
161 this.groupingDefinition = groupingDefinition;
162 if (groupingDefinition != null) {
163 this.groupingPath = groupingDefinition.getPath();
168 public GroupingBuilder getGroupingBuilder() {
169 return groupingBuilder;
173 public void setGrouping(GroupingBuilder grouping) {
174 this.groupingBuilder = grouping;
175 if (groupingBuilder != null) {
176 this.groupingPath = groupingBuilder.getPath();
181 public String getGroupingPathAsString() {
186 public Set<AugmentationSchemaBuilder> getAugmentations() {
187 return addedAugments;
191 public void addAugment(final AugmentationSchemaBuilder augmentBuilder) {
192 addedAugments.add(augmentBuilder);
196 public boolean isAddedByUses() {
201 public void setAddedByUses(final boolean addedByUses) {
202 this.addedByUses = addedByUses;
206 public boolean isAugmenting() {
211 public void setAugmenting(boolean augmenting) {
212 this.augmenting = augmenting;
216 public AugmentationSchemaBuilder getParentAugment() {
217 return parentAugment;
221 public void setParentAugment(AugmentationSchemaBuilder augment) {
222 this.parentAugment = augment;
226 public List<SchemaNodeBuilder> getRefineNodes() {
227 return refineBuilders;
231 public void addRefineNode(DataSchemaNodeBuilder refineNode) {
232 refineBuilders.add(refineNode);
236 public List<RefineHolder> getRefines() {
241 public void addRefine(RefineHolder refine) {
246 public Set<DataSchemaNodeBuilder> getTargetChildren() {
247 return targetChildren;
251 public Set<GroupingBuilder> getTargetGroupings() {
252 return targetGroupings;
256 public Set<TypeDefinitionBuilder> getTargetTypedefs() {
257 return targetTypedefs;
261 public List<UnknownSchemaNodeBuilder> getTargetUnknownNodes() {
262 return targetUnknownNodes;
266 public int hashCode() {
267 final int prime = 31;
269 result = prime * result + ((groupingName == null) ? 0 : groupingName.hashCode());
270 result = prime * result + ((parent == null) ? 0 : parent.hashCode());
275 public boolean equals(Object obj) {
282 if (getClass() != obj.getClass()) {
285 UsesNodeBuilderImpl other = (UsesNodeBuilderImpl) obj;
286 if (groupingName == null) {
287 if (other.groupingName != null) {
290 } else if (!groupingName.equals(other.groupingName)) {
293 if (parent == null) {
294 if (other.parent != null) {
297 } else if (!parent.equals(other.parent)) {
304 public String toString() {
305 return "uses '" + groupingName + "'";
308 public final class UsesNodeImpl implements UsesNode {
309 private final SchemaPath groupingPath;
310 private Set<AugmentationSchema> augmentations = Collections.emptySet();
311 private boolean addedByUses;
312 private Map<SchemaPath, SchemaNode> refines = Collections.emptyMap();
313 private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
315 private UsesNodeImpl(final SchemaPath groupingPath) {
316 this.groupingPath = groupingPath;
320 public SchemaPath getGroupingPath() {
325 public Set<AugmentationSchema> getAugmentations() {
326 return augmentations;
329 private void setAugmentations(final Set<AugmentationSchema> augmentations) {
330 if (augmentations != null) {
331 this.augmentations = augmentations;
336 public boolean isAugmenting() {
341 public boolean isAddedByUses() {
345 private void setAddedByUses(final boolean addedByUses) {
346 this.addedByUses = addedByUses;
350 public Map<SchemaPath, SchemaNode> getRefines() {
354 private void setRefines(Map<SchemaPath, SchemaNode> refines) {
355 if (refines != null) {
356 this.refines = refines;
360 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
364 private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownSchemaNodes) {
365 if (unknownSchemaNodes != null) {
366 this.unknownNodes = unknownSchemaNodes;
370 public UsesNodeBuilder toBuilder() {
371 return UsesNodeBuilderImpl.this;
375 public int hashCode() {
376 final int prime = 31;
378 result = prime * result + ((groupingPath == null) ? 0 : groupingPath.hashCode());
379 result = prime * result + ((augmentations == null) ? 0 : augmentations.hashCode());
384 public boolean equals(Object obj) {
391 if (getClass() != obj.getClass()) {
394 final UsesNodeImpl other = (UsesNodeImpl) obj;
395 if (groupingPath == null) {
396 if (other.groupingPath != null) {
399 } else if (!groupingPath.equals(other.groupingPath)) {
402 if (augmentations == null) {
403 if (other.augmentations != null) {
406 } else if (!augmentations.equals(other.augmentations)) {
413 public String toString() {
414 StringBuilder sb = new StringBuilder(UsesNodeImpl.class.getSimpleName());
415 sb.append("[groupingPath=");
416 sb.append(groupingPath);
418 return sb.toString();