798862b457df19dacefad5cca4a00a2571820dc6
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / builder / api / AbstractDataNodeContainerBuilder.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.api;
9
10 import java.util.Collections;
11 import java.util.HashSet;
12 import java.util.Set;
13 import java.util.TreeSet;
14
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
17 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
18 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
19 import org.opendaylight.yangtools.yang.model.api.UsesNode;
20 import org.opendaylight.yangtools.yang.parser.util.Comparators;
21 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
22
23 /**
24  * Basic implementation of DataNodeContainerBuilder.
25  */
26 public abstract class AbstractDataNodeContainerBuilder extends AbstractBuilder implements DataNodeContainerBuilder {
27     protected QName qname;
28
29     protected final Set<DataSchemaNode> childNodes = new HashSet<>();
30     protected final Set<DataSchemaNodeBuilder> addedChildNodes = new HashSet<>();
31
32     protected final Set<GroupingDefinition> groupings = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
33     protected final Set<GroupingBuilder> addedGroupings = new HashSet<>();
34
35     protected final Set<TypeDefinition<?>> typedefs = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
36     protected final Set<TypeDefinitionBuilder> addedTypedefs = new HashSet<>();
37
38     protected final Set<UsesNode> usesNodes = new HashSet<>();
39     protected final Set<UsesNodeBuilder> addedUsesNodes = new HashSet<>();
40
41     protected AbstractDataNodeContainerBuilder(final String moduleName, final int line, final QName qname) {
42         super(moduleName, line);
43         this.qname = qname;
44     }
45
46     @Override
47     public QName getQName() {
48         return qname;
49     }
50
51     public Set<DataSchemaNode> getChildNodes() {
52         return childNodes;
53     }
54
55     @Override
56     public Set<DataSchemaNodeBuilder> getChildNodeBuilders() {
57         return addedChildNodes;
58     }
59
60     @Override
61     public DataSchemaNodeBuilder getDataChildByName(final String name) {
62         for (DataSchemaNodeBuilder child : addedChildNodes) {
63             if (child.getQName().getLocalName().equals(name)) {
64                 return child;
65             }
66         }
67         return null;
68     }
69
70     @Override
71     public void addChildNode(DataSchemaNodeBuilder child) {
72         QName childName = child.getQName();
73         for (DataSchemaNodeBuilder addedChildNode : addedChildNodes) {
74             if (addedChildNode.getQName().equals(childName)) {
75                 throw new YangParseException(child.getModuleName(), child.getLine(), "Can not add '" + child + "' to '"
76                         + this + "' in module '" + moduleName + "': node with same name already declared at line "
77                         + addedChildNode.getLine());
78             }
79         }
80         addedChildNodes.add(child);
81     }
82
83     @Override
84     public void addChildNodeToContext(DataSchemaNodeBuilder child) {
85         addedChildNodes.add(child);
86     }
87
88     @Override
89     public void addChildNode(DataSchemaNode child) {
90         QName childName = child.getQName();
91         for (DataSchemaNode childNode : childNodes) {
92             if (childNode.getQName().equals(childName)) {
93                 throw new YangParseException(moduleName, line, "Can not add '" + child + "' to '" + this
94                         + "' in module '" + moduleName + "': node with same name already declared");
95             }
96         }
97         childNodes.add(child);
98     }
99
100     @Override
101     public Set<GroupingDefinition> getGroupings() {
102         if (groupings == null) {
103             return Collections.emptySet();
104         }
105         return groupings;
106     }
107
108     public Set<GroupingBuilder> getGroupingBuilders() {
109         return addedGroupings;
110     }
111
112     @Override
113     public void addGrouping(GroupingBuilder grouping) {
114         QName groupingName = grouping.getQName();
115         for (GroupingBuilder addedGrouping : addedGroupings) {
116             if (addedGrouping.getQName().equals(groupingName)) {
117                 throw new YangParseException(grouping.getModuleName(), grouping.getLine(), "Can not add '" + grouping
118                         + "': grouping with same name already declared in module '" + moduleName + "' at line "
119                         + addedGrouping.getLine());
120             }
121         }
122         addedGroupings.add(grouping);
123     }
124
125     @Override
126     public Set<TypeDefinition<?>> getTypeDefinitions() {
127         return typedefs;
128     }
129
130     public Set<UsesNode> getUsesNodes() {
131         return usesNodes;
132     }
133
134     @Override
135     public Set<UsesNodeBuilder> getUsesNodeBuilders() {
136         return addedUsesNodes;
137     }
138
139     @Override
140     public void addUsesNode(UsesNodeBuilder usesNode) {
141         addedUsesNodes.add(usesNode);
142     }
143
144     public DataSchemaNode getChildNode(Set<DataSchemaNode> childNodes, QName name) {
145         for (DataSchemaNode node : childNodes) {
146             if (node.getQName().equals(name)) {
147                 return node;
148             }
149         }
150         return null;
151     }
152
153     public DataSchemaNode getChildNode(Set<DataSchemaNode> childNodes, String name) {
154         for (DataSchemaNode node : childNodes) {
155             if (node.getQName().getLocalName().equals(name)) {
156                 return node;
157             }
158         }
159         return null;
160     }
161
162 }