2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.builder.impl;
10 import com.google.common.collect.ImmutableSet;
11 import java.util.Collections;
12 import java.util.HashSet;
13 import java.util.Objects;
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.ConstraintsBuilder;
21 public final class ConstraintsBuilderImpl implements ConstraintsBuilder {
22 private static final ConstraintDefinitionImpl EMPTY_CONSTRAINT = new ConstraintDefinitionImpl();
23 private static final ConstraintDefinitionImpl EMPTY_MANDATORY_CONSTRAINT;
26 ConstraintDefinitionImpl c = new ConstraintDefinitionImpl();
29 EMPTY_MANDATORY_CONSTRAINT = c;
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;
42 public ConstraintsBuilderImpl(final String moduleName, final int line) {
43 this.moduleName = moduleName;
45 mustDefinitions = new HashSet<>();
48 ConstraintsBuilderImpl(final ConstraintsBuilder b) {
49 this.moduleName = b.getModuleName();
50 this.line = b.getLine();
51 mustDefinitions = new HashSet<>(b.getMustDefinitions());
52 whenCondition = b.getWhenCondition();
53 mandatory = b.isMandatory();
54 min = b.getMinElements();
55 max = b.getMaxElements();
58 ConstraintsBuilderImpl(final String moduleName, final int line, final ConstraintDefinition base) {
59 this.moduleName = moduleName;
61 whenStmt = base.getWhenCondition();
62 mustDefinitions = new HashSet<>(base.getMustConstraints());
63 mandatory = base.isMandatory();
64 min = base.getMinElements();
65 max = base.getMaxElements();
69 * @see org.opendaylight.yangtools.yang.parser.builder.impl.IConstraintsBuilder#build()
72 public ConstraintDefinition build() {
73 if (instance != null) {
77 if (whenStmt == null) {
78 if (whenCondition == null) {
81 whenStmt = new RevisionAwareXPathImpl(whenCondition, false);
85 ConstraintDefinitionImpl newInstance = new ConstraintDefinitionImpl();
86 newInstance.setWhenCondition(whenStmt);
87 newInstance.setMandatory(mandatory);
88 newInstance.setMinElements(min);
89 newInstance.setMaxElements(max);
91 if (!mustDefinitions.isEmpty()) {
92 newInstance.setMustConstraints(mustDefinitions);
94 if (EMPTY_CONSTRAINT.equals(newInstance)) {
95 newInstance = EMPTY_CONSTRAINT;
96 } else if (EMPTY_MANDATORY_CONSTRAINT.equals(newInstance)) {
97 newInstance = EMPTY_MANDATORY_CONSTRAINT;
100 instance = newInstance;
105 * @see org.opendaylight.yangtools.yang.parser.builder.impl.IConstraintsBuilder#getModuleName()
108 public String getModuleName() {
113 * @see org.opendaylight.yangtools.yang.parser.builder.impl.IConstraintsBuilder#getLine()
116 public int getLine() {
121 * @see org.opendaylight.yangtools.yang.parser.builder.impl.IConstraintsBuilder#getMinElements()
124 public Integer getMinElements() {
129 * @see org.opendaylight.yangtools.yang.parser.builder.impl.IConstraintsBuilder#setMinElements(java.lang.Integer)
132 public void setMinElements(final Integer minElements) {
133 this.min = minElements;
137 * @see org.opendaylight.yangtools.yang.parser.builder.impl.IConstraintsBuilder#getMaxElements()
140 public Integer getMaxElements() {
145 * @see org.opendaylight.yangtools.yang.parser.builder.impl.IConstraintsBuilder#setMaxElements(java.lang.Integer)
148 public void setMaxElements(final Integer maxElements) {
149 this.max = maxElements;
153 * @see org.opendaylight.yangtools.yang.parser.builder.impl.IConstraintsBuilder#getMustDefinitions()
156 public Set<MustDefinition> getMustDefinitions() {
157 return mustDefinitions;
161 * @see org.opendaylight.yangtools.yang.parser.builder.impl.IConstraintsBuilder#addMustDefinition(org.opendaylight.yangtools.yang.model.api.MustDefinition)
164 public void addMustDefinition(final MustDefinition must) {
165 mustDefinitions.add(must);
169 * @see org.opendaylight.yangtools.yang.parser.builder.impl.IConstraintsBuilder#getWhenCondition()
172 public String getWhenCondition() {
173 return whenCondition;
177 * @see org.opendaylight.yangtools.yang.parser.builder.impl.IConstraintsBuilder#addWhenCondition(java.lang.String)
180 public void addWhenCondition(final String whenCondition) {
181 this.whenCondition = whenCondition;
185 * @see org.opendaylight.yangtools.yang.parser.builder.impl.IConstraintsBuilder#isMandatory()
188 public boolean isMandatory() {
193 * @see org.opendaylight.yangtools.yang.parser.builder.impl.IConstraintsBuilder#setMandatory(boolean)
196 public void setMandatory(final boolean mandatory) {
197 this.mandatory = mandatory;
200 private static final class ConstraintDefinitionImpl implements ConstraintDefinition {
201 private RevisionAwareXPath whenCondition;
202 private Set<MustDefinition> mustConstraints = Collections.emptySet();
203 private Boolean mandatory = false;
204 private Integer minElements;
205 private Integer maxElements;
208 public RevisionAwareXPath getWhenCondition() {
209 return whenCondition;
212 private void setWhenCondition(final RevisionAwareXPath whenCondition) {
213 this.whenCondition = whenCondition;
217 public Set<MustDefinition> getMustConstraints() {
218 return mustConstraints;
221 private void setMustConstraints(final Set<MustDefinition> mustConstraints) {
222 if (mustConstraints != null) {
223 this.mustConstraints = ImmutableSet.copyOf(mustConstraints);
228 public boolean isMandatory() {
232 private void setMandatory(final boolean mandatory) {
233 this.mandatory = mandatory;
237 public Integer getMinElements() {
241 private void setMinElements(final Integer minElements) {
242 this.minElements = minElements;
246 public Integer getMaxElements() {
250 private void setMaxElements(final Integer maxElements) {
251 this.maxElements = maxElements;
255 public int hashCode() {
256 final int prime = 31;
258 result = prime * result + Objects.hashCode(whenCondition);
259 result = prime * result + Objects.hashCode(mustConstraints);
260 result = prime * result + Objects.hashCode(minElements);
261 result = prime * result + Objects.hashCode(maxElements);
262 result = prime * result + mandatory.hashCode();
267 public boolean equals(final Object obj) {
274 if (getClass() != obj.getClass()) {
277 ConstraintDefinitionImpl other = (ConstraintDefinitionImpl) obj;
278 if (whenCondition == null) {
279 if (other.whenCondition != null) {
282 } else if (!whenCondition.equals(other.whenCondition)) {
285 if (mustConstraints == null) {
286 if (other.mustConstraints != null) {
289 } else if (!mustConstraints.equals(other.mustConstraints)) {
292 if (!mandatory.equals(other.mandatory)) {
295 if (minElements == null) {
296 if (other.minElements != null) {
299 } else if (!minElements.equals(other.minElements)) {
302 if (maxElements == null) {
303 if (other.maxElements != null) {
306 } else if (!maxElements.equals(other.maxElements)) {
313 public String toString() {
314 StringBuilder sb = new StringBuilder(ConstraintDefinitionImpl.class.getSimpleName());
316 sb.append("whenCondition=").append(whenCondition);
317 sb.append(", mustConstraints=").append(mustConstraints);
318 sb.append(", mandatory=").append(mandatory);
319 sb.append(", minElements=").append(minElements);
320 sb.append(", maxElements=").append(maxElements);
322 return sb.toString();
327 * @deprecated Use #build() instead.
331 public ConstraintDefinition toInstance() {