Fixed bugs in naming conventions of packages.
[controller.git] / opendaylight / sal / yang-prototype / code-generator / yang-model-parser-impl / src / main / java / org / opendaylight / controller / yang / model / parser / builder / impl / ConstraintsBuilder.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.model.parser.builder.impl;
9
10 import java.util.Collections;
11 import java.util.HashSet;
12 import java.util.Set;
13
14 import org.opendaylight.controller.yang.model.api.ConstraintDefinition;
15 import org.opendaylight.controller.yang.model.api.DataSchemaNode;
16 import org.opendaylight.controller.yang.model.api.MustDefinition;
17 import org.opendaylight.controller.yang.model.api.RevisionAwareXPath;
18 import org.opendaylight.controller.yang.model.parser.builder.api.Builder;
19 import org.opendaylight.controller.yang.model.util.RevisionAwareXPathImpl;
20
21 public class ConstraintsBuilder implements Builder {
22
23     private final ConstraintDefinitionImpl instance;
24     private final Set<MustDefinition> mustDefinitions;
25     private String whenCondition;
26
27     ConstraintsBuilder() {
28         instance = new ConstraintDefinitionImpl();
29         mustDefinitions = new HashSet<MustDefinition>();
30     }
31
32     @Override
33     public ConstraintDefinition build() {
34         RevisionAwareXPath whenStmt = new RevisionAwareXPathImpl(whenCondition,
35                 false);
36         instance.setWhenCondition(whenStmt);
37         instance.setMustConstraints(mustDefinitions);
38         return instance;
39     }
40
41     public void setMinElements(Integer minElements) {
42         instance.setMinElements(minElements);
43     }
44
45     public void setMaxElements(Integer maxElements) {
46         instance.setMaxElements(maxElements);
47     }
48
49     public void addMustDefinition(String mustStr, String description,
50             String reference) {
51         MustDefinition must = new MustDefinitionImpl(mustStr, description,
52                 reference);
53         mustDefinitions.add(must);
54     }
55
56     public void addWhenCondition(String whenCondition) {
57         this.whenCondition = whenCondition;
58     }
59
60     public void setMandatory(boolean mandatory) {
61         instance.setMandatory(mandatory);
62     }
63
64     private static class ConstraintDefinitionImpl implements
65             ConstraintDefinition {
66
67         private DataSchemaNode parent;
68         private RevisionAwareXPath whenCondition;
69         private Set<MustDefinition> mustConstraints;
70         private boolean mandatory;
71         private Integer minElements;
72         private Integer maxElements;
73
74         @Override
75         public DataSchemaNode getParent() {
76             return parent;
77         }
78
79         @Override
80         public RevisionAwareXPath getWhenCondition() {
81             return whenCondition;
82         }
83
84         private void setWhenCondition(RevisionAwareXPath whenCondition) {
85             this.whenCondition = whenCondition;
86         }
87
88         @Override
89         public Set<MustDefinition> getMustConstraints() {
90             if (mustConstraints == null) {
91                 return Collections.emptySet();
92             } else {
93                 return mustConstraints;
94             }
95         }
96
97         private void setMustConstraints(Set<MustDefinition> mustConstraints) {
98             if (mustConstraints != null) {
99                 this.mustConstraints = mustConstraints;
100             }
101         }
102
103         @Override
104         public boolean isMandatory() {
105             return mandatory;
106         }
107
108         private void setMandatory(boolean mandatory) {
109             this.mandatory = mandatory;
110         }
111
112         @Override
113         public Integer getMinElements() {
114             return minElements;
115         }
116
117         private void setMinElements(Integer minElements) {
118             this.minElements = minElements;
119         }
120
121         @Override
122         public Integer getMaxElements() {
123             return maxElements;
124         }
125
126         private void setMaxElements(Integer maxElements) {
127             this.maxElements = maxElements;
128         }
129
130         @Override
131         public int hashCode() {
132             final int prime = 31;
133             int result = 1;
134             result = prime * result
135                     + ((parent == null) ? 0 : parent.hashCode());
136             result = prime * result
137                     + ((whenCondition == null) ? 0 : whenCondition.hashCode());
138             result = prime
139                     * result
140                     + ((mustConstraints == null) ? 0 : mustConstraints
141                             .hashCode());
142             result = prime * result
143                     + ((minElements == null) ? 0 : minElements.hashCode());
144             result = prime * result
145                     + ((maxElements == null) ? 0 : maxElements.hashCode());
146             result = prime * result + (mandatory ? 1231 : 1237);
147             return result;
148         }
149
150         @Override
151         public boolean equals(Object obj) {
152             if (this == obj) {
153                 return true;
154             }
155             if (obj == null) {
156                 return false;
157             }
158             if (getClass() != obj.getClass()) {
159                 return false;
160             }
161             ConstraintDefinitionImpl other = (ConstraintDefinitionImpl) obj;
162             if (parent == null) {
163                 if (other.parent != null) {
164                     return false;
165                 }
166             } else if (!parent.equals(other.parent)) {
167                 return false;
168             }
169             if (whenCondition == null) {
170                 if (other.whenCondition != null) {
171                     return false;
172                 }
173             } else if (!whenCondition.equals(other.whenCondition)) {
174                 return false;
175             }
176             if (mustConstraints == null) {
177                 if (other.mustConstraints != null) {
178                     return false;
179                 }
180             } else if (!mustConstraints.equals(other.mustConstraints)) {
181                 return false;
182             }
183             if (mandatory != other.mandatory) {
184                 return false;
185             }
186             if (minElements == null) {
187                 if (other.minElements != null) {
188                     return false;
189                 }
190             } else if (!minElements.equals(other.minElements)) {
191                 return false;
192             }
193             if (maxElements == null) {
194                 if (other.maxElements != null) {
195                     return false;
196                 }
197             } else if (!maxElements.equals(other.maxElements)) {
198                 return false;
199             }
200             return true;
201         }
202
203         @Override
204         public String toString() {
205             StringBuilder sb = new StringBuilder(
206                     ConstraintDefinitionImpl.class.getSimpleName());
207             sb.append("[");
208             sb.append("parent=" + parent);
209             sb.append(", whenCondition=" + whenCondition);
210             sb.append(", mustConstraints=" + mustConstraints);
211             sb.append(", mandatory=" + mandatory);
212             sb.append(", minElements=" + minElements);
213             sb.append(", maxElements=" + maxElements);
214             sb.append("]");
215             return sb.toString();
216         }
217
218     }
219
220     private static class MustDefinitionImpl implements MustDefinition {
221
222         private final String mustStr;
223         private final String description;
224         private final String reference;
225
226         private MustDefinitionImpl(String mustStr, String description,
227                 String reference) {
228             this.mustStr = mustStr;
229             this.description = description;
230             this.reference = reference;
231         }
232
233         @Override
234         public String getDescription() {
235             return description;
236         }
237
238         @Override
239         public String getErrorAppTag() {
240             // TODO Auto-generated method stub
241             return null;
242         }
243
244         @Override
245         public String getErrorMessage() {
246             // TODO Auto-generated method stub
247             return null;
248         }
249
250         @Override
251         public String getReference() {
252             return reference;
253         }
254
255         @Override
256         public RevisionAwareXPath getXpath() {
257             // TODO Auto-generated method stub
258             return null;
259         }
260
261         @Override
262         public int hashCode() {
263             final int prime = 31;
264             int result = 1;
265             result = prime * result
266                     + ((mustStr == null) ? 0 : mustStr.hashCode());
267             result = prime * result
268                     + ((description == null) ? 0 : description.hashCode());
269             result = prime * result
270                     + ((reference == null) ? 0 : reference.hashCode());
271             return result;
272         }
273
274         @Override
275         public boolean equals(Object obj) {
276             if (this == obj) {
277                 return true;
278             }
279             if (obj == null) {
280                 return false;
281             }
282             if (getClass() != obj.getClass()) {
283                 return false;
284             }
285             MustDefinitionImpl other = (MustDefinitionImpl) obj;
286             if (mustStr == null) {
287                 if (other.mustStr != null) {
288                     return false;
289                 }
290             } else if (!mustStr.equals(other.mustStr)) {
291                 return false;
292             }
293             if (description == null) {
294                 if (other.description != null) {
295                     return false;
296                 }
297             } else if (!description.equals(other.description)) {
298                 return false;
299             }
300             if (reference == null) {
301                 if (other.reference != null) {
302                     return false;
303                 }
304             } else if (!reference.equals(other.reference)) {
305                 return false;
306             }
307             return true;
308         }
309
310         @Override
311         public String toString() {
312             return MustDefinitionImpl.class.getSimpleName() + "[mustStr="
313                     + mustStr + "]";
314         }
315     }
316
317 }