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