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.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.TypeDefinitionBuilder;
33 import org.opendaylight.yangtools.yang.parser.builder.api.UsesNodeBuilder;
34 import org.opendaylight.yangtools.yang.parser.util.RefineHolder;
35 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
37 public final class UsesNodeBuilderImpl extends AbstractBuilder implements UsesNodeBuilder {
38 private boolean isBuilt;
39 private UsesNodeImpl instance;
40 private DataNodeContainerBuilder parentBuilder;
41 private final String groupingName;
42 private SchemaPath groupingPath;
43 private GroupingDefinition groupingDefinition;
44 private GroupingBuilder groupingBuilder;
45 private boolean addedByUses;
46 private boolean augmenting;
47 private AugmentationSchemaBuilder parentAugment;
48 private final Set<AugmentationSchema> augments = new HashSet<>();
49 private final Set<AugmentationSchemaBuilder> addedAugments = new HashSet<>();
50 private final List<SchemaNodeBuilder> refineBuilders = new ArrayList<>();
51 private final List<RefineHolder> refines = new ArrayList<>();
54 * Copies of target grouping child nodes.
56 private final Set<DataSchemaNodeBuilder> targetChildren = new HashSet<>();
59 * Copies of target grouping groupings.
61 private final Set<GroupingBuilder> targetGroupings = new HashSet<>();
64 * Copies of target grouping typedefs.
66 private final Set<TypeDefinitionBuilder> targetTypedefs = new HashSet<>();
69 * Copies of target grouping unknown nodes.
71 private final List<UnknownSchemaNodeBuilder> targetUnknownNodes = new ArrayList<>();
73 private final boolean isCopy;
74 private boolean dataCollected;
77 public boolean isCopy() {
82 public boolean isDataCollected() {
87 public void setDataCollected(boolean dataCollected) {
88 this.dataCollected = dataCollected;
91 public UsesNodeBuilderImpl(final String moduleName, final int line, final String groupingName) {
92 super(moduleName, line);
93 this.groupingName = groupingName;
97 public UsesNodeBuilderImpl(final String moduleName, final int line, final String groupingName, final boolean isCopy) {
98 super(moduleName, line);
99 this.groupingName = groupingName;
100 this.isCopy = isCopy;
104 public UsesNode build(YangNode parent) {
106 instance = new UsesNodeImpl(groupingPath);
107 instance.setAddedByUses(addedByUses);
108 instance.setParent(parent);
111 for (AugmentationSchemaBuilder builder : addedAugments) {
112 augments.add(builder.build(instance));
114 instance.setAugmentations(augments);
117 final Map<SchemaPath, SchemaNode> refineNodes = new HashMap<>();
118 for (SchemaNodeBuilder refineBuilder : refineBuilders) {
119 SchemaNode refineNode = refineBuilder.build(instance);
120 refineNodes.put(refineNode.getPath(), refineNode);
122 instance.setRefines(refineNodes);
125 for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
126 unknownNodes.add(b.build(instance));
128 instance.setUnknownSchemaNodes(unknownNodes);
137 public DataNodeContainerBuilder getParent() {
138 return parentBuilder;
142 public void setParent(Builder parent) {
143 if (!(parent instanceof DataNodeContainerBuilder)) {
144 throw new YangParseException(moduleName, line,
145 "Parent of 'uses' has to be instance of DataNodeContainerBuilder, but was: '" + parent + "'.");
147 this.parentBuilder = (DataNodeContainerBuilder) parent;
151 public SchemaPath getGroupingPath() {
156 public GroupingDefinition getGroupingDefinition() {
157 return groupingDefinition;
161 public void setGroupingDefinition(GroupingDefinition groupingDefinition) {
162 this.groupingDefinition = groupingDefinition;
163 if (groupingDefinition != null) {
164 this.groupingPath = groupingDefinition.getPath();
169 public GroupingBuilder getGroupingBuilder() {
170 return groupingBuilder;
174 public void setGrouping(GroupingBuilder grouping) {
175 this.groupingBuilder = grouping;
176 if (groupingBuilder != null) {
177 this.groupingPath = groupingBuilder.getPath();
182 public String getGroupingPathAsString() {
187 public Set<AugmentationSchemaBuilder> getAugmentations() {
188 return addedAugments;
192 public void addAugment(final AugmentationSchemaBuilder augmentBuilder) {
193 addedAugments.add(augmentBuilder);
197 public boolean isAddedByUses() {
202 public void setAddedByUses(final boolean addedByUses) {
203 this.addedByUses = addedByUses;
207 public boolean isAugmenting() {
212 public void setAugmenting(boolean augmenting) {
213 this.augmenting = augmenting;
217 public AugmentationSchemaBuilder getParentAugment() {
218 return parentAugment;
222 public void setParentAugment(AugmentationSchemaBuilder augment) {
223 this.parentAugment = augment;
227 public List<SchemaNodeBuilder> getRefineNodes() {
228 return refineBuilders;
232 public void addRefineNode(DataSchemaNodeBuilder refineNode) {
233 refineBuilders.add(refineNode);
237 public List<RefineHolder> getRefines() {
242 public void addRefine(RefineHolder refine) {
247 public Set<DataSchemaNodeBuilder> getTargetChildren() {
248 return targetChildren;
252 public Set<GroupingBuilder> getTargetGroupings() {
253 return targetGroupings;
257 public Set<TypeDefinitionBuilder> getTargetTypedefs() {
258 return targetTypedefs;
262 public List<UnknownSchemaNodeBuilder> getTargetUnknownNodes() {
263 return targetUnknownNodes;
267 public int hashCode() {
268 final int prime = 31;
270 result = prime * result + ((groupingName == null) ? 0 : groupingName.hashCode());
271 result = prime * result + ((parentBuilder == null) ? 0 : parentBuilder.hashCode());
276 public boolean equals(Object obj) {
283 if (getClass() != obj.getClass()) {
286 UsesNodeBuilderImpl other = (UsesNodeBuilderImpl) obj;
287 if (groupingName == null) {
288 if (other.groupingName != null) {
291 } else if (!groupingName.equals(other.groupingName)) {
294 if (parentBuilder == null) {
295 if (other.parentBuilder != null) {
298 } else if (!parentBuilder.equals(other.parentBuilder)) {
305 public String toString() {
306 return "uses '" + groupingName + "'";
309 public final class UsesNodeImpl implements UsesNode {
310 private YangNode parent;
311 private final SchemaPath groupingPath;
312 private Set<AugmentationSchema> augmentations = Collections.emptySet();
313 private boolean addedByUses;
314 private Map<SchemaPath, SchemaNode> refines = Collections.emptyMap();
315 private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
318 public YangNode getParent() {
322 private void setParent(YangNode parent) {
323 this.parent = parent;
326 private UsesNodeImpl(final SchemaPath groupingPath) {
327 this.groupingPath = groupingPath;
331 public SchemaPath getGroupingPath() {
336 public Set<AugmentationSchema> getAugmentations() {
337 return augmentations;
340 private void setAugmentations(final Set<AugmentationSchema> augmentations) {
341 if (augmentations != null) {
342 this.augmentations = augmentations;
347 public boolean isAugmenting() {
352 public boolean isAddedByUses() {
356 private void setAddedByUses(final boolean addedByUses) {
357 this.addedByUses = addedByUses;
361 public Map<SchemaPath, SchemaNode> getRefines() {
365 private void setRefines(Map<SchemaPath, SchemaNode> refines) {
366 if (refines != null) {
367 this.refines = refines;
371 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
375 private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownSchemaNodes) {
376 if (unknownSchemaNodes != null) {
377 this.unknownNodes = unknownSchemaNodes;
381 public UsesNodeBuilder toBuilder() {
382 return UsesNodeBuilderImpl.this;
386 public int hashCode() {
387 final int prime = 31;
389 result = prime * result + ((groupingPath == null) ? 0 : groupingPath.hashCode());
390 result = prime * result + ((augmentations == null) ? 0 : augmentations.hashCode());
395 public boolean equals(Object obj) {
402 if (getClass() != obj.getClass()) {
405 final UsesNodeImpl other = (UsesNodeImpl) obj;
406 if (groupingPath == null) {
407 if (other.groupingPath != null) {
410 } else if (!groupingPath.equals(other.groupingPath)) {
413 if (augmentations == null) {
414 if (other.augmentations != null) {
417 } else if (!augmentations.equals(other.augmentations)) {
424 public String toString() {
425 StringBuilder sb = new StringBuilder(UsesNodeImpl.class.getSimpleName());
426 sb.append("[groupingPath=");
427 sb.append(groupingPath);
429 return sb.toString();