BUG-579: memory improvements in parser's builders.
[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 final QName qname;
28
29     protected final Set<DataSchemaNode> childNodes = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
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     @Override
109     public Set<GroupingBuilder> getGroupingBuilders() {
110         return addedGroupings;
111     }
112
113     @Override
114     public void addGrouping(GroupingBuilder grouping) {
115         QName groupingName = grouping.getQName();
116         for (GroupingBuilder addedGrouping : addedGroupings) {
117             if (addedGrouping.getQName().equals(groupingName)) {
118                 throw new YangParseException(grouping.getModuleName(), grouping.getLine(), "Can not add '" + grouping
119                         + "': grouping with same name already declared in module '" + moduleName + "' at line "
120                         + addedGrouping.getLine());
121             }
122         }
123         addedGroupings.add(grouping);
124     }
125
126     @Override
127     public Set<TypeDefinition<?>> getTypeDefinitions() {
128         return typedefs;
129     }
130
131     public Set<UsesNode> getUsesNodes() {
132         return usesNodes;
133     }
134
135     @Override
136     public Set<UsesNodeBuilder> getUsesNodeBuilders() {
137         return addedUsesNodes;
138     }
139
140     @Override
141     public void addUsesNode(UsesNodeBuilder usesNode) {
142         addedUsesNodes.add(usesNode);
143     }
144
145     protected static DataSchemaNode getChildNode(Set<DataSchemaNode> childNodes, QName name) {
146         for (DataSchemaNode node : childNodes) {
147             if (node.getQName().equals(name)) {
148                 return node;
149             }
150         }
151         return null;
152     }
153
154     protected static DataSchemaNode getChildNode(Set<DataSchemaNode> childNodes, String name) {
155         for (DataSchemaNode node : childNodes) {
156             if (node.getQName().getLocalName().equals(name)) {
157                 return node;
158             }
159         }
160         return null;
161     }
162
163 }