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