Added more descriptive parsing exceptions.
[controller.git] / opendaylight / sal / yang-prototype / code-generator / yang-model-parser-impl / src / main / java / org / opendaylight / controller / yang / 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.parser.builder.impl;
9
10 import java.util.Collections;
11 import java.util.HashSet;
12 import java.util.List;
13 import java.util.Set;
14
15 import org.opendaylight.controller.yang.model.api.ConstraintDefinition;
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.util.RevisionAwareXPathImpl;
19 import org.opendaylight.controller.yang.parser.builder.api.AbstractBuilder;
20 import org.opendaylight.controller.yang.parser.util.YangParseException;
21
22 public final class ConstraintsBuilder extends AbstractBuilder {
23     private final ConstraintDefinitionImpl instance;
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 String moduleName, final int line) {
31         super(moduleName, 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 void addUnknownNodeBuilder(UnknownSchemaNodeBuilder unknownNode) {
54         throw new YangParseException(moduleName, line, "Can not add unknown node to constraints.");
55     }
56
57     @Override
58     public List<UnknownSchemaNodeBuilder> getUnknownNodeBuilders() {
59         return Collections.emptyList();
60     }
61
62     public Integer getMinElements() {
63         return min;
64     }
65
66     public void setMinElements(Integer minElements) {
67         this.min = minElements;
68     }
69
70     public Integer getMaxElements() {
71         return max;
72     }
73
74     public void setMaxElements(Integer maxElements) {
75         this.max = maxElements;
76     }
77
78     public Set<MustDefinition> getMustDefinitions() {
79         return mustDefinitions;
80     }
81
82     public void addMustDefinition(MustDefinition must) {
83         mustDefinitions.add(must);
84     }
85
86     public String getWhenCondition() {
87         return whenCondition;
88     }
89
90     public void addWhenCondition(String whenCondition) {
91         this.whenCondition = whenCondition;
92     }
93
94     public boolean isMandatory() {
95         return mandatory;
96     }
97
98     public void setMandatory(boolean mandatory) {
99         this.mandatory = mandatory;
100     }
101
102     private final class ConstraintDefinitionImpl implements ConstraintDefinition {
103         private RevisionAwareXPath whenCondition;
104         private Set<MustDefinition> mustConstraints;
105         private boolean mandatory;
106         private Integer minElements;
107         private Integer maxElements;
108
109         @Override
110         public RevisionAwareXPath getWhenCondition() {
111             return whenCondition;
112         }
113
114         private void setWhenCondition(RevisionAwareXPath whenCondition) {
115             this.whenCondition = whenCondition;
116         }
117
118         @Override
119         public Set<MustDefinition> getMustConstraints() {
120             if (mustConstraints == null) {
121                 return Collections.emptySet();
122             } else {
123                 return mustConstraints;
124             }
125         }
126
127         private void setMustConstraints(Set<MustDefinition> mustConstraints) {
128             if (mustConstraints != null) {
129                 this.mustConstraints = mustConstraints;
130             }
131         }
132
133         @Override
134         public boolean isMandatory() {
135             return mandatory;
136         }
137
138         private void setMandatory(boolean mandatory) {
139             this.mandatory = mandatory;
140         }
141
142         @Override
143         public Integer getMinElements() {
144             return minElements;
145         }
146
147         private void setMinElements(Integer minElements) {
148             this.minElements = minElements;
149         }
150
151         @Override
152         public Integer getMaxElements() {
153             return maxElements;
154         }
155
156         private void setMaxElements(Integer maxElements) {
157             this.maxElements = maxElements;
158         }
159
160         @Override
161         public int hashCode() {
162             final int prime = 31;
163             int result = 1;
164             result = prime * result + ((whenCondition == null) ? 0 : whenCondition.hashCode());
165             result = prime * result + ((mustConstraints == null) ? 0 : mustConstraints.hashCode());
166             result = prime * result + ((minElements == null) ? 0 : minElements.hashCode());
167             result = prime * result + ((maxElements == null) ? 0 : maxElements.hashCode());
168             result = prime * result + (mandatory ? 1231 : 1237);
169             return result;
170         }
171
172         @Override
173         public boolean equals(Object obj) {
174             if (this == obj) {
175                 return true;
176             }
177             if (obj == null) {
178                 return false;
179             }
180             if (getClass() != obj.getClass()) {
181                 return false;
182             }
183             ConstraintDefinitionImpl other = (ConstraintDefinitionImpl) obj;
184             if (whenCondition == null) {
185                 if (other.whenCondition != null) {
186                     return false;
187                 }
188             } else if (!whenCondition.equals(other.whenCondition)) {
189                 return false;
190             }
191             if (mustConstraints == null) {
192                 if (other.mustConstraints != null) {
193                     return false;
194                 }
195             } else if (!mustConstraints.equals(other.mustConstraints)) {
196                 return false;
197             }
198             if (mandatory != other.mandatory) {
199                 return false;
200             }
201             if (minElements == null) {
202                 if (other.minElements != null) {
203                     return false;
204                 }
205             } else if (!minElements.equals(other.minElements)) {
206                 return false;
207             }
208             if (maxElements == null) {
209                 if (other.maxElements != null) {
210                     return false;
211                 }
212             } else if (!maxElements.equals(other.maxElements)) {
213                 return false;
214             }
215             return true;
216         }
217
218         @Override
219         public String toString() {
220             StringBuilder sb = new StringBuilder(ConstraintDefinitionImpl.class.getSimpleName());
221             sb.append("[");
222             sb.append("whenCondition=" + whenCondition);
223             sb.append(", mustConstraints=" + mustConstraints);
224             sb.append(", mandatory=" + mandatory);
225             sb.append(", minElements=" + minElements);
226             sb.append(", maxElements=" + maxElements);
227             sb.append("]");
228             return sb.toString();
229         }
230     }
231
232 }