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.api;
10 import java.util.Collection;
11 import java.util.Collections;
12 import java.util.HashSet;
15 import java.util.TreeMap;
16 import java.util.TreeSet;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
21 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
22 import org.opendaylight.yangtools.yang.model.api.UsesNode;
23 import org.opendaylight.yangtools.yang.parser.util.Comparators;
24 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
27 * Basic implementation of DataNodeContainerBuilder.
29 public abstract class AbstractDataNodeContainerBuilder extends AbstractBuilder implements DataNodeContainerBuilder {
30 protected QName qname;
32 protected Map<QName, DataSchemaNode> childNodes = new TreeMap<QName, DataSchemaNode>(Comparators.QNAME_COMP);
33 protected final Set<DataSchemaNodeBuilder> addedChildNodes = new HashSet<DataSchemaNodeBuilder>();
35 protected Set<GroupingDefinition> groupings = new TreeSet<GroupingDefinition>(Comparators.SCHEMA_NODE_COMP);
36 protected final Set<GroupingBuilder> addedGroupings = new HashSet<GroupingBuilder>();
38 protected Set<TypeDefinition<?>> typedefs = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
39 protected final Set<TypeDefinitionBuilder> addedTypedefs = new HashSet<>();
41 protected Set<UsesNode> usesNodes = new HashSet<>();
42 protected final Set<UsesNodeBuilder> addedUsesNodes = new HashSet<>();
44 protected AbstractDataNodeContainerBuilder(final String moduleName, final int line, final QName qname) {
45 super(moduleName, line);
50 public QName getQName() {
55 public Collection<DataSchemaNode> getChildNodes() {
56 if (childNodes == null) {
57 return Collections.emptySet();
59 return childNodes.values();
63 public Set<DataSchemaNodeBuilder> getChildNodeBuilders() {
64 return addedChildNodes;
68 public DataSchemaNodeBuilder getDataChildByName(final String name) {
69 for (DataSchemaNodeBuilder child : addedChildNodes) {
70 if (child.getQName().getLocalName().equals(name)) {
78 public void addChildNode(DataSchemaNodeBuilder child) {
79 String childName = child.getQName().getLocalName();
80 for (DataSchemaNodeBuilder addedChildNode : addedChildNodes) {
81 if (addedChildNode.getQName().getLocalName().equals(childName)) {
82 throw new YangParseException(child.getModuleName(), child.getLine(), "Can not add '" + child + "' to '"
83 + this + "' in module '" + moduleName + "': node with same name already declared at line "
84 + addedChildNode.getLine());
87 addedChildNodes.add(child);
91 public void addChildNode(DataSchemaNode child) {
92 QName childName = child.getQName();
93 for (QName qname : childNodes.keySet()) {
94 if (qname.equals(childName)) {
95 throw new YangParseException(moduleName, line, "Can not add '" + child + "' to '" + this
96 + "' in module '" + moduleName + "': node with same name already declared");
99 childNodes.put(child.getQName(), child);
103 public Set<GroupingDefinition> getGroupings() {
104 if (groupings == null) {
105 return Collections.emptySet();
110 public void setGroupings(final Set<GroupingDefinition> groupings) {
111 this.groupings = groupings;
114 public Set<GroupingBuilder> getGroupingBuilders() {
115 return addedGroupings;
119 public void addGrouping(GroupingBuilder grouping) {
120 String groupingName = grouping.getQName().getLocalName();
121 for (GroupingBuilder addedGrouping : addedGroupings) {
122 if (addedGrouping.getQName().getLocalName().equals(groupingName)) {
123 throw new YangParseException(grouping.getModuleName(), grouping.getLine(), "Can not add '" + grouping
124 + "': grouping with same name already declared in module '" + moduleName + "' at line "
125 + addedGrouping.getLine());
128 addedGroupings.add(grouping);
132 public Set<UsesNodeBuilder> getUsesNodes() {
133 return addedUsesNodes;
136 public void setUsesnodes(final Set<UsesNode> usesNodes) {
137 this.usesNodes = usesNodes;
141 public void addUsesNode(UsesNodeBuilder usesNode) {
142 addedUsesNodes.add(usesNode);