Added line number to error messages.
[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     private final ConstraintDefinitionImpl instance;
23     private final int line;
24     private final Set<MustDefinition> mustDefinitions;
25     private String whenCondition;
26     private boolean mandatory;
27     private Integer min;
28     private Integer max;
29
30     ConstraintsBuilder(final int line) {
31         this.line = line;
32         instance = new ConstraintDefinitionImpl();
33         mustDefinitions = new HashSet<MustDefinition>();
34     }
35
36     @Override
37     public ConstraintDefinition build() {
38         RevisionAwareXPath whenStmt;
39         if (whenCondition == null) {
40             whenStmt = null;
41         } else {
42             whenStmt = new RevisionAwareXPathImpl(whenCondition, false);
43         }
44         instance.setWhenCondition(whenStmt);
45         instance.setMustConstraints(mustDefinitions);
46         instance.setMandatory(mandatory);
47         instance.setMinElements(min);
48         instance.setMaxElements(max);
49         return instance;
50     }
51
52     @Override
53     public int getLine() {
54         return line;
55     }
56
57     public Integer getMinElements() {
58         return min;
59     }
60
61     public void setMinElements(Integer minElements) {
62         this.min = minElements;
63     }
64
65     public Integer getMaxElements() {
66         return max;
67     }
68
69     public void setMaxElements(Integer maxElements) {
70         this.max = maxElements;
71     }
72
73     public Set<MustDefinition> getMustDefinitions() {
74         return mustDefinitions;
75     }
76
77     public void addMustDefinition(MustDefinition must) {
78         mustDefinitions.add(must);
79     }
80
81     public String getWhenCondition() {
82         return whenCondition;
83     }
84
85     public void addWhenCondition(String whenCondition) {
86         this.whenCondition = whenCondition;
87     }
88
89     public boolean isMandatory() {
90         return mandatory;
91     }
92
93     public void setMandatory(boolean mandatory) {
94         this.mandatory = mandatory;
95     }
96
97     private static class ConstraintDefinitionImpl implements
98             ConstraintDefinition {
99         private DataSchemaNode parent;
100         private RevisionAwareXPath whenCondition;
101         private Set<MustDefinition> mustConstraints;
102         private boolean mandatory;
103         private Integer minElements;
104         private Integer maxElements;
105
106         @Override
107         public DataSchemaNode getParent() {
108             return parent;
109         }
110
111         @Override
112         public RevisionAwareXPath getWhenCondition() {
113             return whenCondition;
114         }
115
116         private void setWhenCondition(RevisionAwareXPath whenCondition) {
117             this.whenCondition = whenCondition;
118         }
119
120         @Override
121         public Set<MustDefinition> getMustConstraints() {
122             if (mustConstraints == null) {
123                 return Collections.emptySet();
124             } else {
125                 return mustConstraints;
126             }
127         }
128
129         private void setMustConstraints(Set<MustDefinition> mustConstraints) {
130             if (mustConstraints != null) {
131                 this.mustConstraints = mustConstraints;
132             }
133         }
134
135         @Override
136         public boolean isMandatory() {
137             return mandatory;
138         }
139
140         private void setMandatory(boolean mandatory) {
141             this.mandatory = mandatory;
142         }
143
144         @Override
145         public Integer getMinElements() {
146             return minElements;
147         }
148
149         private void setMinElements(Integer minElements) {
150             this.minElements = minElements;
151         }
152
153         @Override
154         public Integer getMaxElements() {
155             return maxElements;
156         }
157
158         private void setMaxElements(Integer maxElements) {
159             this.maxElements = maxElements;
160         }
161
162         @Override
163         public int hashCode() {
164             final int prime = 31;
165             int result = 1;
166             result = prime * result
167                     + ((parent == null) ? 0 : parent.hashCode());
168             result = prime * result
169                     + ((whenCondition == null) ? 0 : whenCondition.hashCode());
170             result = prime
171                     * result
172                     + ((mustConstraints == null) ? 0 : mustConstraints
173                             .hashCode());
174             result = prime * result
175                     + ((minElements == null) ? 0 : minElements.hashCode());
176             result = prime * result
177                     + ((maxElements == null) ? 0 : maxElements.hashCode());
178             result = prime * result + (mandatory ? 1231 : 1237);
179             return result;
180         }
181
182         @Override
183         public boolean equals(Object obj) {
184             if (this == obj) {
185                 return true;
186             }
187             if (obj == null) {
188                 return false;
189             }
190             if (getClass() != obj.getClass()) {
191                 return false;
192             }
193             ConstraintDefinitionImpl other = (ConstraintDefinitionImpl) obj;
194             if (parent == null) {
195                 if (other.parent != null) {
196                     return false;
197                 }
198             } else if (!parent.equals(other.parent)) {
199                 return false;
200             }
201             if (whenCondition == null) {
202                 if (other.whenCondition != null) {
203                     return false;
204                 }
205             } else if (!whenCondition.equals(other.whenCondition)) {
206                 return false;
207             }
208             if (mustConstraints == null) {
209                 if (other.mustConstraints != null) {
210                     return false;
211                 }
212             } else if (!mustConstraints.equals(other.mustConstraints)) {
213                 return false;
214             }
215             if (mandatory != other.mandatory) {
216                 return false;
217             }
218             if (minElements == null) {
219                 if (other.minElements != null) {
220                     return false;
221                 }
222             } else if (!minElements.equals(other.minElements)) {
223                 return false;
224             }
225             if (maxElements == null) {
226                 if (other.maxElements != null) {
227                     return false;
228                 }
229             } else if (!maxElements.equals(other.maxElements)) {
230                 return false;
231             }
232             return true;
233         }
234
235         @Override
236         public String toString() {
237             StringBuilder sb = new StringBuilder(
238                     ConstraintDefinitionImpl.class.getSimpleName());
239             sb.append("[");
240             sb.append("parent=" + parent);
241             sb.append(", whenCondition=" + whenCondition);
242             sb.append(", mustConstraints=" + mustConstraints);
243             sb.append(", mandatory=" + mandatory);
244             sb.append(", minElements=" + minElements);
245             sb.append(", maxElements=" + maxElements);
246             sb.append("]");
247             return sb.toString();
248         }
249
250     }
251
252 }