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 java.util.Collections;
11 import java.util.HashSet;
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;
19 import com.google.common.collect.ImmutableSet;
21 public final class 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 ConstraintsBuilder(final String moduleName, final int line) {
43 this.moduleName = moduleName;
45 mustDefinitions = new HashSet<MustDefinition>();
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();
58 ConstraintsBuilder(final String moduleName, final int line, final ConstraintDefinition base) {
59 this.moduleName = moduleName;
61 whenStmt = base.getWhenCondition();
62 mustDefinitions = new HashSet<MustDefinition>(base.getMustConstraints());
63 mandatory = base.isMandatory();
64 min = base.getMinElements();
65 max = base.getMaxElements();
68 public ConstraintDefinition build() {
69 if (instance != null) {
73 if (whenStmt == null) {
74 if (whenCondition == null) {
77 whenStmt = new RevisionAwareXPathImpl(whenCondition, false);
81 ConstraintDefinitionImpl newInstance = new ConstraintDefinitionImpl();
82 newInstance.setWhenCondition(whenStmt);
83 newInstance.setMandatory(mandatory);
84 newInstance.setMinElements(min);
85 newInstance.setMaxElements(max);
87 if (!mustDefinitions.isEmpty()) {
88 newInstance.setMustConstraints(mustDefinitions);
90 if (EMPTY_CONSTRAINT.equals(newInstance)) {
91 newInstance = EMPTY_CONSTRAINT;
92 } else if (EMPTY_MANDATORY_CONSTRAINT.equals(newInstance)) {
93 newInstance = EMPTY_MANDATORY_CONSTRAINT;
96 instance = newInstance;
100 public String getModuleName() {
104 public int getLine() {
108 public Integer getMinElements() {
112 public void setMinElements(final Integer minElements) {
113 this.min = minElements;
116 public Integer getMaxElements() {
120 public void setMaxElements(final Integer maxElements) {
121 this.max = maxElements;
124 public Set<MustDefinition> getMustDefinitions() {
125 return mustDefinitions;
128 public void addMustDefinition(final MustDefinition must) {
129 mustDefinitions.add(must);
132 public String getWhenCondition() {
133 return whenCondition;
136 public void addWhenCondition(final String whenCondition) {
137 this.whenCondition = whenCondition;
140 public boolean isMandatory() {
144 public void setMandatory(final boolean mandatory) {
145 this.mandatory = mandatory;
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;
156 public RevisionAwareXPath getWhenCondition() {
157 return whenCondition;
160 private void setWhenCondition(final RevisionAwareXPath whenCondition) {
161 this.whenCondition = whenCondition;
165 public Set<MustDefinition> getMustConstraints() {
166 return mustConstraints;
169 private void setMustConstraints(final Set<MustDefinition> mustConstraints) {
170 if (mustConstraints != null) {
171 this.mustConstraints = ImmutableSet.copyOf(mustConstraints);
176 public boolean isMandatory() {
180 private void setMandatory(final boolean mandatory) {
181 this.mandatory = mandatory;
185 public Integer getMinElements() {
189 private void setMinElements(final Integer minElements) {
190 this.minElements = minElements;
194 public Integer getMaxElements() {
198 private void setMaxElements(final Integer maxElements) {
199 this.maxElements = maxElements;
203 public int hashCode() {
204 final int prime = 31;
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();
215 public boolean equals(final Object obj) {
222 if (getClass() != obj.getClass()) {
225 ConstraintDefinitionImpl other = (ConstraintDefinitionImpl) obj;
226 if (whenCondition == null) {
227 if (other.whenCondition != null) {
230 } else if (!whenCondition.equals(other.whenCondition)) {
233 if (mustConstraints == null) {
234 if (other.mustConstraints != null) {
237 } else if (!mustConstraints.equals(other.mustConstraints)) {
240 if (mandatory != other.mandatory) {
243 if (minElements == null) {
244 if (other.minElements != null) {
247 } else if (!minElements.equals(other.minElements)) {
250 if (maxElements == null) {
251 if (other.maxElements != null) {
254 } else if (!maxElements.equals(other.maxElements)) {
261 public String toString() {
262 StringBuilder sb = new StringBuilder(ConstraintDefinitionImpl.class.getSimpleName());
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);
270 return sb.toString();