Merge "Bug 2561: Parser incorrectly allows usage of union in list"
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / builder / impl / GroupingBuilderImpl.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.impl;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.ImmutableList;
12 import java.util.ArrayList;
13 import java.util.HashSet;
14 import java.util.List;
15 import java.util.Set;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
18 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
19 import org.opendaylight.yangtools.yang.parser.builder.api.Builder;
20 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
21 import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder;
22 import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
23 import org.opendaylight.yangtools.yang.parser.builder.api.UnknownSchemaNodeBuilder;
24 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainerBuilder;
25
26 public final class GroupingBuilderImpl extends AbstractDocumentedDataNodeContainerBuilder implements GroupingBuilder {
27     private GroupingDefinitionImpl instance;
28     // SchemaNode args
29     private SchemaPath schemaPath;
30     // DataSchemaNode args
31     private boolean addedByUses;
32
33     public GroupingBuilderImpl(final String moduleName, final int line, final QName qname, final SchemaPath path) {
34         super(moduleName, line, qname);
35         this.schemaPath = Preconditions.checkNotNull(path, "Schema Path must not be null");
36     }
37
38     public GroupingBuilderImpl(final String moduleName, final int line, final QName qname, final SchemaPath path,
39             final GroupingDefinition base) {
40         super(moduleName, line, base.getQName(), Preconditions.checkNotNull(path, "Schema Path must not be null"), base);
41         schemaPath = path;
42         addedByUses = base.isAddedByUses();
43         addedUnknownNodes.addAll(BuilderUtils.wrapUnknownNodes(moduleName, line, base.getUnknownSchemaNodes(), path,
44                 qname));
45     }
46
47     @Override
48     public GroupingDefinition build() {
49         if (instance != null) {
50             return instance;
51         }
52         buildChildren();
53         instance = new GroupingDefinitionImpl(qname, schemaPath, this);
54         instance.addedByUses = addedByUses;
55
56         // UNKNOWN NODES
57         for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
58             unknownNodes.add(b.build());
59         }
60         instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
61
62         return instance;
63     }
64
65     @Override
66     public List<DataSchemaNodeBuilder> instantiateChildNodes(final Builder newParent) {
67         final List<DataSchemaNodeBuilder> nodes = new ArrayList<>();
68         for (DataSchemaNodeBuilder node : getChildNodeBuilders()) {
69             DataSchemaNodeBuilder copy = CopyUtils.copy(node, newParent, true);
70             BuilderUtils.setNodeAddedByUses(copy);
71             nodes.add(copy);
72         }
73         return nodes;
74     }
75
76     @Override
77     public Set<TypeDefinitionBuilder> instantiateTypedefs(final Builder newParent) {
78         final Set<TypeDefinitionBuilder> nodes = new HashSet<>();
79         for (TypeDefinitionBuilder node : getTypeDefinitionBuilders()) {
80             TypeDefinitionBuilder copy = CopyUtils.copy(node, newParent, true);
81             nodes.add(copy);
82         }
83         return nodes;
84     }
85
86     @Override
87     public Set<GroupingBuilder> instantiateGroupings(final Builder newParent) {
88         final Set<GroupingBuilder> nodes = new HashSet<>();
89         for (GroupingBuilder node : getGroupingBuilders()) {
90             GroupingBuilder copy = CopyUtils.copy(node, newParent, true);
91             copy.setAddedByUses(true);
92             for (DataSchemaNodeBuilder childNode : copy.getChildNodeBuilders()) {
93                 BuilderUtils.setNodeAddedByUses(childNode);
94             }
95             nodes.add(copy);
96         }
97         return nodes;
98     }
99
100     @Override
101     public Set<UnknownSchemaNodeBuilder> instantiateUnknownNodes(final Builder newParent) {
102         final Set<UnknownSchemaNodeBuilder> nodes = new HashSet<>();
103         for (UnknownSchemaNodeBuilder node : addedUnknownNodes) {
104             UnknownSchemaNodeBuilderImpl copy = CopyUtils.copy(node, newParent, true);
105             copy.setAddedByUses(true);
106             nodes.add(copy);
107         }
108         return nodes;
109     }
110
111     @Override
112     public SchemaPath getPath() {
113         return schemaPath;
114     }
115
116     @Override
117     public void setPath(final SchemaPath path) {
118         this.schemaPath = path;
119     }
120
121     @Override
122     public boolean isAddedByUses() {
123         return addedByUses;
124     }
125
126     @Override
127     public void setAddedByUses(final boolean addedByUses) {
128         this.addedByUses = addedByUses;
129     }
130
131     @Override
132     public String toString() {
133         return "grouping " + qname.getLocalName();
134     }
135
136     @Override
137     public int hashCode() {
138         final int prime = 31;
139         int result = 1;
140         result = prime * result + ((getParent() == null) ? 0 : getParent().hashCode());
141         result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
142         return result;
143     }
144
145     @Override
146     public boolean equals(final Object obj) {
147         if (this == obj) {
148             return true;
149         }
150         if (obj == null) {
151             return false;
152         }
153         if (getClass() != obj.getClass()) {
154             return false;
155         }
156         if (!super.equals(obj)) {
157             return false;
158         }
159         final GroupingBuilderImpl other = (GroupingBuilderImpl) obj;
160         if (getParent() == null) {
161             if (other.getParent() != null) {
162                 return false;
163             }
164         } else if (!getParent().equals(other.getParent())) {
165             return false;
166         }
167         if (schemaPath == null) {
168             if (other.schemaPath != null) {
169                 return false;
170             }
171         } else if (!schemaPath.equals(other.schemaPath)) {
172             return false;
173         }
174         return true;
175     }
176
177     @Override
178     protected String getStatementName() {
179         return "grouping";
180     }
181
182 }