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