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