Merge "Bug 1027: Decreased log level from INFO to DEBUG."
[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 java.net.URI;
11 import java.util.Date;
12 import java.util.HashSet;
13 import java.util.List;
14 import java.util.Set;
15
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.model.api.Status;
20 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
21 import org.opendaylight.yangtools.yang.parser.builder.api.Builder;
22 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
23 import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder;
24 import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
25 import org.opendaylight.yangtools.yang.parser.builder.api.UnknownSchemaNodeBuilder;
26 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainer;
27 import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainerBuilder;
28
29 import com.google.common.base.Preconditions;
30 import com.google.common.collect.ImmutableList;
31
32 public final class GroupingBuilderImpl extends AbstractDocumentedDataNodeContainerBuilder implements GroupingBuilder {
33     private GroupingDefinitionImpl instance;
34     // SchemaNode args
35     private SchemaPath schemaPath;
36     private String description;
37     private String reference;
38     private Status status = Status.CURRENT;
39     // DataSchemaNode args
40     private boolean addedByUses;
41
42     public GroupingBuilderImpl(final String moduleName, final int line, final QName qname, final SchemaPath path) {
43         super(moduleName, line, qname);
44         this.schemaPath = Preconditions.checkNotNull(path, "Schema Path must not be null");
45     }
46
47     public GroupingBuilderImpl(final String moduleName, final int line, final QName qname, final SchemaPath path,
48             final GroupingDefinition base) {
49         super(moduleName, line, base.getQName(), path, base);
50         schemaPath = path;
51
52         description = base.getDescription();
53         reference = base.getReference();
54         status = base.getStatus();
55         addedByUses = base.isAddedByUses();
56
57         URI ns = qname.getNamespace();
58         Date rev = qname.getRevision();
59         String pref = qname.getPrefix();
60         addedUnknownNodes.addAll(BuilderUtils.wrapUnknownNodes(moduleName, line, base.getUnknownSchemaNodes(), path,
61                 ns, rev, pref));
62     }
63
64     @Override
65     public GroupingDefinition build() {
66         if (instance != null) {
67             return instance;
68         }
69         buildChildren();
70         instance = new GroupingDefinitionImpl(qname, schemaPath, this);
71         instance.addedByUses = addedByUses;
72
73         // UNKNOWN NODES
74         for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
75             unknownNodes.add(b.build());
76         }
77         instance.unknownNodes = ImmutableList.copyOf(unknownNodes);
78
79         return instance;
80     }
81
82     @Override
83     public Set<DataSchemaNodeBuilder> instantiateChildNodes(final Builder newParent) {
84         final Set<DataSchemaNodeBuilder> nodes = new HashSet<>();
85         for (DataSchemaNodeBuilder node : getChildNodeBuilders()) {
86             DataSchemaNodeBuilder copy = CopyUtils.copy(node, newParent, true);
87             BuilderUtils.setNodeAddedByUses(copy);
88             nodes.add(copy);
89         }
90         return nodes;
91     }
92
93     @Override
94     public Set<TypeDefinitionBuilder> instantiateTypedefs(final Builder newParent) {
95         final Set<TypeDefinitionBuilder> nodes = new HashSet<>();
96         for (TypeDefinitionBuilder node : getTypeDefinitionBuilders()) {
97             TypeDefinitionBuilder copy = CopyUtils.copy(node, newParent, true);
98             nodes.add(copy);
99         }
100         return nodes;
101     }
102
103     @Override
104     public Set<GroupingBuilder> instantiateGroupings(final Builder newParent) {
105         final Set<GroupingBuilder> nodes = new HashSet<>();
106         for (GroupingBuilder node : getGroupingBuilders()) {
107             GroupingBuilder copy = CopyUtils.copy(node, newParent, true);
108             copy.setAddedByUses(true);
109             for (DataSchemaNodeBuilder childNode : copy.getChildNodeBuilders()) {
110                 BuilderUtils.setNodeAddedByUses(childNode);
111             }
112             nodes.add(copy);
113         }
114         return nodes;
115     }
116
117     @Override
118     public Set<UnknownSchemaNodeBuilder> instantiateUnknownNodes(final Builder newParent) {
119         final Set<UnknownSchemaNodeBuilder> nodes = new HashSet<>();
120         for (UnknownSchemaNodeBuilder node : addedUnknownNodes) {
121             UnknownSchemaNodeBuilderImpl copy = CopyUtils.copy(node, newParent, true);
122             copy.setAddedByUses(true);
123             nodes.add(copy);
124         }
125         return nodes;
126     }
127
128     @Override
129     public SchemaPath getPath() {
130         return schemaPath;
131     }
132
133     @Override
134     public void setPath(final SchemaPath path) {
135         this.schemaPath = path;
136     }
137
138     @Override
139     public boolean isAddedByUses() {
140         return addedByUses;
141     }
142
143     @Override
144     public void setAddedByUses(final boolean addedByUses) {
145         this.addedByUses = addedByUses;
146     }
147
148     @Override
149     public String toString() {
150         return "grouping " + qname.getLocalName();
151     }
152
153     @Override
154     public int hashCode() {
155         final int prime = 31;
156         int result = 1;
157         result = prime * result + ((getParent() == null) ? 0 : getParent().hashCode());
158         result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
159         return result;
160     }
161
162     @Override
163     public boolean equals(final Object obj) {
164         if (this == obj) {
165             return true;
166         }
167         if (obj == null) {
168             return false;
169         }
170         if (getClass() != obj.getClass()) {
171             return false;
172         }
173         if (!super.equals(obj)) {
174             return false;
175         }
176         final GroupingBuilderImpl other = (GroupingBuilderImpl) obj;
177         if (getParent() == null) {
178             if (other.getParent() != null) {
179                 return false;
180             }
181         } else if (!getParent().equals(other.getParent())) {
182             return false;
183         }
184         if (schemaPath == null) {
185             if (other.schemaPath != null) {
186                 return false;
187             }
188         } else if (!schemaPath.equals(other.schemaPath)) {
189             return false;
190         }
191         return true;
192     }
193
194     @Override
195     protected String getStatementName() {
196         return "grouping";
197     }
198
199     private static final class GroupingDefinitionImpl extends AbstractDocumentedDataNodeContainer implements
200             GroupingDefinition {
201         private final QName qname;
202         private final SchemaPath path;
203
204         private boolean addedByUses;
205         private ImmutableList<UnknownSchemaNode> unknownNodes;
206
207         private GroupingDefinitionImpl(final QName qname, final SchemaPath path, final GroupingBuilderImpl builder) {
208             super(builder);
209             this.qname = qname;
210             this.path = path;
211         }
212
213         @Override
214         public QName getQName() {
215             return qname;
216         }
217
218         @Override
219         public SchemaPath getPath() {
220             return path;
221         }
222
223         @Override
224         public boolean isAddedByUses() {
225             return addedByUses;
226         }
227
228         @Override
229         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
230             return unknownNodes;
231         }
232
233         @Override
234         public int hashCode() {
235             final int prime = 31;
236             int result = 1;
237             result = prime * result + ((qname == null) ? 0 : qname.hashCode());
238             result = prime * result + ((path == null) ? 0 : path.hashCode());
239             return result;
240         }
241
242         @Override
243         public boolean equals(final Object obj) {
244             if (this == obj) {
245                 return true;
246             }
247             if (obj == null) {
248                 return false;
249             }
250             if (getClass() != obj.getClass()) {
251                 return false;
252             }
253             final GroupingDefinitionImpl other = (GroupingDefinitionImpl) obj;
254             if (qname == null) {
255                 if (other.qname != null) {
256                     return false;
257                 }
258             } else if (!qname.equals(other.qname)) {
259                 return false;
260             }
261             if (path == null) {
262                 if (other.path != null) {
263                     return false;
264                 }
265             } else if (!path.equals(other.path)) {
266                 return false;
267             }
268             return true;
269         }
270
271         @Override
272         public String toString() {
273             StringBuilder sb = new StringBuilder(GroupingDefinitionImpl.class.getSimpleName());
274             sb.append("[");
275             sb.append("qname=" + qname);
276             sb.append("]");
277             return sb.toString();
278         }
279     }
280
281 }