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