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