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