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