Merge changes Iaac9f7e2,I9c336a30
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / builder / impl / AnyXmlBuilder.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.util.ArrayList;
11 import java.util.Collections;
12 import java.util.List;
13
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
16 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
17 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
18 import org.opendaylight.yangtools.yang.model.api.Status;
19 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
20 import org.opendaylight.yangtools.yang.parser.builder.api.AbstractSchemaNodeBuilder;
21 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
22 import org.opendaylight.yangtools.yang.parser.util.Comparators;
23
24 public final class AnyXmlBuilder extends AbstractSchemaNodeBuilder implements DataSchemaNodeBuilder {
25     private boolean built;
26     private final AnyXmlSchemaNodeImpl instance;
27     private final ConstraintsBuilder constraints;
28
29     public AnyXmlBuilder(final String moduleName, final int line, final QName qname, final SchemaPath path) {
30         super(moduleName, line, qname);
31         this.schemaPath = path;
32         instance = new AnyXmlSchemaNodeImpl(qname, schemaPath);
33         constraints = new ConstraintsBuilder(moduleName, line);
34     }
35
36     public AnyXmlBuilder(final String moduleName, final int line, final QName qname, final SchemaPath path,
37             final AnyXmlSchemaNode base) {
38         super(moduleName, line, qname);
39         this.schemaPath = path;
40         this.instance = new AnyXmlSchemaNodeImpl(qname, schemaPath);
41         constraints = new ConstraintsBuilder(moduleName, line, base.getConstraints());
42
43         instance.description = base.getDescription();
44         instance.reference = base.getReference();
45         instance.status = base.getStatus();
46         instance.augmenting = base.isAugmenting();
47         instance.addedByUses = base.isAddedByUses();
48         instance.configuration = base.isConfiguration();
49         instance.constraintsDef = base.getConstraints();
50         instance.unknownNodes.addAll(base.getUnknownSchemaNodes());
51     }
52
53     @Override
54     public AnyXmlSchemaNode build() {
55         if (!built) {
56             instance.setConstraints(constraints.build());
57
58             // UNKNOWN NODES
59             for (UnknownSchemaNodeBuilder b : addedUnknownNodes) {
60                 unknownNodes.add(b.build());
61             }
62             Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
63             instance.setUnknownSchemaNodes(unknownNodes);
64
65             built = true;
66         }
67         return instance;
68     }
69
70     @Override
71     public SchemaPath getPath() {
72         return instance.path;
73     }
74
75     @Override
76     public void setPath(SchemaPath path) {
77         instance.path = path;
78     }
79
80     @Override
81     public ConstraintsBuilder getConstraints() {
82         return constraints;
83     }
84
85     @Override
86     public String getDescription() {
87         return instance.description;
88     }
89
90     @Override
91     public void setDescription(final String description) {
92         instance.description = description;
93     }
94
95     @Override
96     public String getReference() {
97         return instance.reference;
98     }
99
100     @Override
101     public void setReference(final String reference) {
102         instance.reference = reference;
103     }
104
105     @Override
106     public Status getStatus() {
107         return instance.status;
108     }
109
110     @Override
111     public void setStatus(Status status) {
112         if (status != null) {
113             instance.status = status;
114         }
115     }
116
117     public List<UnknownSchemaNodeBuilder> getUnknownNodes() {
118         return addedUnknownNodes;
119     }
120
121     @Override
122     public boolean isAugmenting() {
123         return instance.augmenting;
124     }
125
126     @Override
127     public void setAugmenting(final boolean augmenting) {
128         instance.augmenting = augmenting;
129     }
130
131     @Override
132     public boolean isAddedByUses() {
133         return instance.addedByUses;
134     }
135
136     @Override
137     public void setAddedByUses(final boolean addedByUses) {
138         instance.addedByUses = addedByUses;
139     }
140
141     @Override
142     public boolean isConfiguration() {
143         return instance.configuration;
144     }
145
146     @Override
147     public void setConfiguration(final boolean configuration) {
148         instance.configuration = configuration;
149     }
150
151     @Override
152     public int hashCode() {
153         final int prime = 31;
154         int result = 1;
155         result = prime * result + ((schemaPath == null) ? 0 : schemaPath.hashCode());
156         return result;
157     }
158
159     @Override
160     public boolean equals(Object obj) {
161         if (this == obj) {
162             return true;
163         }
164         if (obj == null) {
165             return false;
166         }
167         if (getClass() != obj.getClass()) {
168             return false;
169         }
170         AnyXmlBuilder other = (AnyXmlBuilder) obj;
171         if (schemaPath == null) {
172             if (other.schemaPath != null) {
173                 return false;
174             }
175         } else if (!schemaPath.equals(other.schemaPath)) {
176             return false;
177         }
178         if (parentBuilder == null) {
179             if (other.parentBuilder != null) {
180                 return false;
181             }
182         } else if (!parentBuilder.equals(other.parentBuilder)) {
183             return false;
184         }
185         return true;
186     }
187
188     @Override
189     public String toString() {
190         return "anyxml " + qname.getLocalName();
191     }
192
193     private static final class AnyXmlSchemaNodeImpl implements AnyXmlSchemaNode {
194         private final QName qname;
195         private SchemaPath path;
196         private String description;
197         private String reference;
198         private Status status = Status.CURRENT;
199         private boolean configuration;
200         private ConstraintDefinition constraintsDef;
201         private boolean augmenting;
202         private boolean addedByUses;
203         private final List<UnknownSchemaNode> unknownNodes = new ArrayList<>();
204
205         private AnyXmlSchemaNodeImpl(final QName qname, final SchemaPath path) {
206             this.qname = qname;
207             this.path = path;
208         }
209
210         @Override
211         public QName getQName() {
212             return qname;
213         }
214
215         @Override
216         public SchemaPath getPath() {
217             return path;
218         }
219
220         @Override
221         public String getDescription() {
222             return description;
223         }
224
225         @Override
226         public String getReference() {
227             return reference;
228         }
229
230         @Override
231         public Status getStatus() {
232             return status;
233         }
234
235         @Override
236         public boolean isAugmenting() {
237             return augmenting;
238         }
239
240         @Override
241         public boolean isAddedByUses() {
242             return addedByUses;
243         }
244
245         @Override
246         public boolean isConfiguration() {
247             return configuration;
248         }
249
250         @Override
251         public ConstraintDefinition getConstraints() {
252             return constraintsDef;
253         }
254
255         private void setConstraints(ConstraintDefinition constraintsDef) {
256             this.constraintsDef = constraintsDef;
257         }
258
259         @Override
260         public List<UnknownSchemaNode> getUnknownSchemaNodes() {
261             return Collections.unmodifiableList(unknownNodes);
262         }
263
264         private void setUnknownSchemaNodes(List<UnknownSchemaNode> unknownNodes) {
265             if (unknownNodes != null) {
266                 this.unknownNodes.addAll(unknownNodes);
267             }
268         }
269
270         @Override
271         public int hashCode() {
272             final int prime = 31;
273             int result = 1;
274             result = prime * result + ((qname == null) ? 0 : qname.hashCode());
275             result = prime * result + ((path == null) ? 0 : path.hashCode());
276             return result;
277         }
278
279         @Override
280         public boolean equals(Object obj) {
281             if (this == obj) {
282                 return true;
283             }
284             if (obj == null) {
285                 return false;
286             }
287             if (getClass() != obj.getClass()) {
288                 return false;
289             }
290             AnyXmlSchemaNodeImpl other = (AnyXmlSchemaNodeImpl) obj;
291             if (qname == null) {
292                 if (other.qname != null) {
293                     return false;
294                 }
295             } else if (!qname.equals(other.qname)) {
296                 return false;
297             }
298             if (path == null) {
299                 if (other.path != null) {
300                     return false;
301                 }
302             } else if (!path.equals(other.path)) {
303                 return false;
304             }
305             return true;
306         }
307
308         @Override
309         public String toString() {
310             StringBuilder sb = new StringBuilder(AnyXmlSchemaNodeImpl.class.getSimpleName());
311             sb.append("[");
312             sb.append("qname=" + qname);
313             sb.append(", path=" + path);
314             sb.append("]");
315             return sb.toString();
316         }
317     }
318
319 }