Added getParent() method to DataSchemaNode and DataNodeContainer. Fixed Bugs.
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / 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.yangtools.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.yangtools.yang.model.api.ConstraintDefinition;
15 import org.opendaylight.yangtools.yang.model.api.MustDefinition;
16 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
17 import org.opendaylight.yangtools.yang.model.util.RevisionAwareXPathImpl;
18
19 public final class ConstraintsBuilder {
20     private static final int HASH_IF_BOOL_TRUE = 1231;
21     private static final int HASH_IF_BOOL_FALSE = 1237;
22
23     private final String moduleName;
24     private final int line;
25     private final ConstraintDefinitionImpl instance;
26     private final Set<MustDefinition> mustDefinitions;
27     private String whenCondition;
28     private boolean mandatory;
29     private Integer min;
30     private Integer max;
31
32     public ConstraintsBuilder(final String moduleName, final int line) {
33         this.moduleName = moduleName;
34         this.line = line;
35         instance = new ConstraintDefinitionImpl();
36         mustDefinitions = new HashSet<MustDefinition>();
37     }
38
39     ConstraintsBuilder(final ConstraintsBuilder b) {
40         this.moduleName = b.getModuleName();
41         this.line = b.getLine();
42         instance = new ConstraintDefinitionImpl();
43         mustDefinitions = new HashSet<MustDefinition>(b.getMustDefinitions());
44         whenCondition = b.getWhenCondition();
45         mandatory = b.isMandatory();
46         min = b.getMinElements();
47         max = b.getMaxElements();
48     }
49
50     public ConstraintDefinition build() {
51         RevisionAwareXPath whenStmt;
52         if (whenCondition == null) {
53             whenStmt = null;
54         } else {
55             whenStmt = new RevisionAwareXPathImpl(whenCondition, false);
56         }
57         instance.setWhenCondition(whenStmt);
58         instance.setMustConstraints(mustDefinitions);
59         instance.setMandatory(mandatory);
60         instance.setMinElements(min);
61         instance.setMaxElements(max);
62         return instance;
63     }
64
65     public String getModuleName() {
66         return moduleName;
67     }
68
69     public int getLine() {
70         return line;
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     private final class ConstraintDefinitionImpl implements ConstraintDefinition {
114         private RevisionAwareXPath whenCondition;
115         private Set<MustDefinition> mustConstraints;
116         private boolean mandatory;
117         private Integer minElements;
118         private Integer maxElements;
119
120         @Override
121         public RevisionAwareXPath getWhenCondition() {
122             return whenCondition;
123         }
124
125         private void setWhenCondition(RevisionAwareXPath whenCondition) {
126             this.whenCondition = whenCondition;
127         }
128
129         @Override
130         public Set<MustDefinition> getMustConstraints() {
131             if (mustConstraints == null) {
132                 return Collections.emptySet();
133             } else {
134                 return mustConstraints;
135             }
136         }
137
138         private void setMustConstraints(Set<MustDefinition> mustConstraints) {
139             if (mustConstraints != null) {
140                 this.mustConstraints = mustConstraints;
141             }
142         }
143
144         @Override
145         public boolean isMandatory() {
146             return mandatory;
147         }
148
149         private void setMandatory(boolean mandatory) {
150             this.mandatory = mandatory;
151         }
152
153         @Override
154         public Integer getMinElements() {
155             return minElements;
156         }
157
158         private void setMinElements(Integer minElements) {
159             this.minElements = minElements;
160         }
161
162         @Override
163         public Integer getMaxElements() {
164             return maxElements;
165         }
166
167         private void setMaxElements(Integer maxElements) {
168             this.maxElements = maxElements;
169         }
170
171         @Override
172         public int hashCode() {
173             final int prime = 31;
174             int result = 1;
175             result = prime * result + ((whenCondition == null) ? 0 : whenCondition.hashCode());
176             result = prime * result + ((mustConstraints == null) ? 0 : mustConstraints.hashCode());
177             result = prime * result + ((minElements == null) ? 0 : minElements.hashCode());
178             result = prime * result + ((maxElements == null) ? 0 : maxElements.hashCode());
179             result = prime * result + (mandatory ? HASH_IF_BOOL_TRUE : HASH_IF_BOOL_FALSE);
180             return result;
181         }
182
183         @Override
184         public boolean equals(Object obj) {
185             if (this == obj) {
186                 return true;
187             }
188             if (obj == null) {
189                 return false;
190             }
191             if (getClass() != obj.getClass()) {
192                 return false;
193             }
194             ConstraintDefinitionImpl other = (ConstraintDefinitionImpl) obj;
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(ConstraintDefinitionImpl.class.getSimpleName());
232             sb.append("[");
233             sb.append("whenCondition=" + whenCondition);
234             sb.append(", mustConstraints=" + mustConstraints);
235             sb.append(", mandatory=" + mandatory);
236             sb.append(", minElements=" + minElements);
237             sb.append(", maxElements=" + maxElements);
238             sb.append("]");
239             return sb.toString();
240         }
241     }
242
243 }