091bfc72337e8037a1d823f81f352990d00126ec
[controller.git] / opendaylight / sal / yang-prototype / code-generator / yang-model-parser-impl / src / main / java / org / opendaylight / controller / 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.controller.yang.parser.builder.api;
9
10 import java.util.Collections;
11 import java.util.HashSet;
12 import java.util.Set;
13
14 import org.opendaylight.controller.yang.common.QName;
15 import org.opendaylight.controller.yang.model.api.DataSchemaNode;
16 import org.opendaylight.controller.yang.model.api.GroupingDefinition;
17 import org.opendaylight.controller.yang.parser.util.YangParseException;
18
19 public abstract class AbstractDataNodeContainerBuilder implements DataNodeContainerBuilder {
20     protected final int line;
21     protected final QName qname;
22     protected Builder parent;
23
24     protected Set<DataSchemaNode> childNodes;
25     protected final Set<DataSchemaNodeBuilder> addedChildNodes = new HashSet<DataSchemaNodeBuilder>();
26
27     protected Set<GroupingDefinition> groupings;
28     protected final Set<GroupingBuilder> addedGroupings = new HashSet<GroupingBuilder>();
29
30     protected AbstractDataNodeContainerBuilder(final int line, final QName qname) {
31         this.line = line;
32         this.qname = qname;
33     }
34
35     @Override
36     public int getLine() {
37         return line;
38     }
39
40     @Override
41     public Builder getParent() {
42         return parent;
43     }
44
45     @Override
46     public void setParent(final Builder parent) {
47         this.parent = parent;
48     }
49
50     @Override
51     public QName getQName() {
52         return qname;
53     }
54
55     @Override
56     public Set<DataSchemaNode> getChildNodes() {
57         if (childNodes == null) {
58             return Collections.emptySet();
59         }
60         return childNodes;
61     }
62
63     public void setChildNodes(Set<DataSchemaNode> childNodes) {
64         this.childNodes = childNodes;
65     }
66
67     @Override
68     public Set<DataSchemaNodeBuilder> getChildNodeBuilders() {
69         return addedChildNodes;
70     }
71
72     @Override
73     public DataSchemaNodeBuilder getDataChildByName(final String name) {
74         for (DataSchemaNodeBuilder child : addedChildNodes) {
75             if (child.getQName().getLocalName().equals(name)) {
76                 return child;
77             }
78         }
79         return null;
80     }
81
82     @Override
83     public void addChildNode(DataSchemaNodeBuilder child) {
84         for (DataSchemaNodeBuilder childNode : addedChildNodes) {
85             if (childNode.getQName().getLocalName().equals(child.getQName().getLocalName())) {
86                 throw new YangParseException(child.getLine(), "Duplicate node found at line " + childNode.getLine());
87             }
88         }
89         addedChildNodes.add(child);
90     }
91
92     @Override
93     public Set<GroupingDefinition> getGroupings() {
94         if (groupings == null) {
95             return Collections.emptySet();
96         }
97         return groupings;
98     }
99
100     public void setGroupings(final Set<GroupingDefinition> groupings) {
101         this.groupings = groupings;
102     }
103
104     public Set<GroupingBuilder> getGroupingBuilders() {
105         return addedGroupings;
106     }
107
108     @Override
109     public void addGrouping(GroupingBuilder groupingBuilder) {
110         for (GroupingBuilder gb : addedGroupings) {
111             if (gb.getQName().getLocalName().equals(groupingBuilder.getQName().getLocalName())) {
112                 throw new YangParseException(groupingBuilder.getLine(), "Duplicate node found at line " + gb.getLine());
113             }
114         }
115         addedGroupings.add(groupingBuilder);
116     }
117
118 }