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<AugmentationSchemaBuilder>();
48 private final List<SchemaNodeBuilder> refineBuilders = new ArrayList<SchemaNodeBuilder>();
49 private final List<RefineHolder> refines = new ArrayList<RefineHolder>();
51 private final Set<DataSchemaNodeBuilder> targetChildren = new HashSet<>();
52 private final Set<GroupingBuilder> targetGroupings = new HashSet<>();
53 private final Set<TypeDefinitionBuilder> targetTypedefs = new HashSet<>();
54 private final List<UnknownSchemaNodeBuilder> targetUnknownNodes = new ArrayList<>();
56 private final boolean isCopy;
57 private boolean dataCollected;
60 public boolean isCopy() {
65 public boolean isDataCollected() {
70 public void setDataCollected(boolean dataCollected) {
71 this.dataCollected = dataCollected;
74 public UsesNodeBuilderImpl(final String moduleName, final int line, final String groupingName) {
75 super(moduleName, line);
76 this.groupingName = groupingName;
80 public UsesNodeBuilderImpl(final String moduleName, final int line, final String groupingName, final boolean isCopy) {
81 super(moduleName, line);
82 this.groupingName = groupingName;
87 public UsesNode build() {
89 instance = new UsesNodeImpl(groupingPath);
90 instance.setAddedByUses(addedByUses);
93 final Set<AugmentationSchema> augments = new HashSet<AugmentationSchema>();
94 for (AugmentationSchemaBuilder builder : addedAugments) {
95 augments.add(builder.build());
97 instance.setAugmentations(augments);
100 final Map<SchemaPath, SchemaNode> refineNodes = new HashMap<SchemaPath, SchemaNode>();
101 for (SchemaNodeBuilder refineBuilder : refineBuilders) {
102 SchemaNode refineNode = refineBuilder.build();
103 refineNodes.put(refineNode.getPath(), refineNode);
105 instance.setRefines(refineNodes);
108 List<UnknownSchemaNode> unknownNodes = new ArrayList<UnknownSchemaNode>();
109 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
110 unknownNodes.add(b.build());
112 instance.setUnknownSchemaNodes(unknownNodes);
121 public DataNodeContainerBuilder getParent() {
126 public void setParent(Builder parent) {
127 if (!(parent instanceof DataNodeContainerBuilder)) {
128 throw new YangParseException(moduleName, line,
129 "Parent of 'uses' has to be instance of DataNodeContainerBuilder, but was: '" + parent + "'.");
131 this.parent = (DataNodeContainerBuilder) parent;
135 public SchemaPath getGroupingPath() {
140 public GroupingDefinition getGroupingDefinition() {
141 return groupingDefinition;
145 public void setGroupingDefinition(GroupingDefinition groupingDefinition) {
146 this.groupingDefinition = groupingDefinition;
147 if (groupingDefinition != null) {
148 this.groupingPath = groupingDefinition.getPath();
153 public GroupingBuilder getGroupingBuilder() {
154 return groupingBuilder;
158 public void setGrouping(GroupingBuilder grouping) {
159 this.groupingBuilder = grouping;
160 if (groupingBuilder != null) {
161 this.groupingPath = groupingBuilder.getPath();
166 public String getGroupingPathAsString() {
171 public Set<AugmentationSchemaBuilder> getAugmentations() {
172 return addedAugments;
176 public void addAugment(final AugmentationSchemaBuilder augmentBuilder) {
177 addedAugments.add(augmentBuilder);
181 public boolean isAddedByUses() {
186 public void setAddedByUses(final boolean addedByUses) {
187 this.addedByUses = addedByUses;
191 public boolean isAugmenting() {
196 public void setAugmenting(boolean augmenting) {
197 this.augmenting = augmenting;
201 public AugmentationSchemaBuilder getParentAugment() {
202 return parentAugment;
206 public void setParentAugment(AugmentationSchemaBuilder augment) {
207 this.parentAugment = augment;
211 public List<SchemaNodeBuilder> getRefineNodes() {
212 return refineBuilders;
216 public void addRefineNode(DataSchemaNodeBuilder refineNode) {
217 refineBuilders.add(refineNode);
221 public List<RefineHolder> getRefines() {
226 public void addRefine(RefineHolder refine) {
231 public Set<DataSchemaNodeBuilder> getTargetChildren() {
232 return targetChildren;
236 public Set<GroupingBuilder> getTargetGroupings() {
237 return targetGroupings;
241 public Set<TypeDefinitionBuilder> getTargetTypedefs() {
242 return targetTypedefs;
246 public List<UnknownSchemaNodeBuilder> getTargetUnknownNodes() {
247 return targetUnknownNodes;
251 public int hashCode() {
252 final int prime = 31;
254 result = prime * result + ((groupingName == null) ? 0 : groupingName.hashCode());
255 result = prime * result + ((parent == null) ? 0 : parent.hashCode());
260 public boolean equals(Object obj) {
267 if (getClass() != obj.getClass()) {
270 UsesNodeBuilderImpl other = (UsesNodeBuilderImpl) obj;
271 if (groupingName == null) {
272 if (other.groupingName != null) {
275 } else if (!groupingName.equals(other.groupingName)) {
278 if (parent == null) {
279 if (other.parent != null) {
282 } else if (!parent.equals(other.parent)) {
289 public String toString() {
290 return "uses '" + groupingName + "'";
293 public final class UsesNodeImpl implements UsesNode {
294 private final SchemaPath groupingPath;
295 private Set<AugmentationSchema> augmentations = Collections.emptySet();
296 private boolean addedByUses;
297 private Map<SchemaPath, SchemaNode> refines = Collections.emptyMap();
298 private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
300 private UsesNodeImpl(final SchemaPath groupingPath) {
301 this.groupingPath = groupingPath;
305 public SchemaPath getGroupingPath() {
310 public Set<AugmentationSchema> getAugmentations() {
311 return augmentations;
314 private void setAugmentations(final Set<AugmentationSchema> augmentations) {
315 if (augmentations != null) {
316 this.augmentations = augmentations;
321 public boolean isAugmenting() {
326 public boolean isAddedByUses() {
330 private void setAddedByUses(final boolean addedByUses) {
331 this.addedByUses = addedByUses;
335 public Map<SchemaPath, SchemaNode> getRefines() {
339 private void setRefines(Map<SchemaPath, SchemaNode> refines) {
340 if (refines != null) {
341 this.refines = refines;
345 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
349 private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownSchemaNodes) {
350 if (unknownSchemaNodes != null) {
351 this.unknownNodes = unknownSchemaNodes;
355 public UsesNodeBuilder toBuilder() {
356 return UsesNodeBuilderImpl.this;
360 public int hashCode() {
361 final int prime = 31;
363 result = prime * result + ((groupingPath == null) ? 0 : groupingPath.hashCode());
364 result = prime * result + ((augmentations == null) ? 0 : augmentations.hashCode());
369 public boolean equals(Object obj) {
376 if (getClass() != obj.getClass()) {
379 final UsesNodeImpl other = (UsesNodeImpl) obj;
380 if (groupingPath == null) {
381 if (other.groupingPath != null) {
384 } else if (!groupingPath.equals(other.groupingPath)) {
387 if (augmentations == null) {
388 if (other.augmentations != null) {
391 } else if (!augmentations.equals(other.augmentations)) {
398 public String toString() {
399 StringBuilder sb = new StringBuilder(UsesNodeImpl.class.getSimpleName());
400 sb.append("[groupingPath=");
401 sb.append(groupingPath);
403 return sb.toString();