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