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 public final class ConstraintsBuilder {
20 private static final int HASH_IF_BOOL_TRUE = 1231;
21 private static final int HASH_IF_BOOL_FALSE = 1237;
23 private final String moduleName;
24 private final int line;
25 private final ConstraintDefinitionImpl instance;
26 private final Set<MustDefinition> mustDefinitions;
27 private String whenCondition;
28 private boolean mandatory;
32 public ConstraintsBuilder(final String moduleName, final int line) {
33 this.moduleName = moduleName;
35 instance = new ConstraintDefinitionImpl();
36 mustDefinitions = new HashSet<MustDefinition>();
39 ConstraintsBuilder(final ConstraintsBuilder b) {
40 this.moduleName = b.getModuleName();
41 this.line = b.getLine();
42 instance = new ConstraintDefinitionImpl();
43 mustDefinitions = new HashSet<MustDefinition>(b.getMustDefinitions());
44 whenCondition = b.getWhenCondition();
45 mandatory = b.isMandatory();
46 min = b.getMinElements();
47 max = b.getMaxElements();
50 public ConstraintDefinition build() {
51 RevisionAwareXPath whenStmt;
52 if (whenCondition == null) {
55 whenStmt = new RevisionAwareXPathImpl(whenCondition, false);
57 instance.setWhenCondition(whenStmt);
58 instance.setMustConstraints(mustDefinitions);
59 instance.setMandatory(mandatory);
60 instance.setMinElements(min);
61 instance.setMaxElements(max);
65 public String getModuleName() {
69 public int getLine() {
73 public Integer getMinElements() {
77 public void setMinElements(Integer minElements) {
78 this.min = minElements;
81 public Integer getMaxElements() {
85 public void setMaxElements(Integer maxElements) {
86 this.max = maxElements;
89 public Set<MustDefinition> getMustDefinitions() {
90 return mustDefinitions;
93 public void addMustDefinition(MustDefinition must) {
94 mustDefinitions.add(must);
97 public String getWhenCondition() {
101 public void addWhenCondition(String whenCondition) {
102 this.whenCondition = whenCondition;
105 public boolean isMandatory() {
109 public void setMandatory(boolean mandatory) {
110 this.mandatory = mandatory;
113 private final class ConstraintDefinitionImpl implements ConstraintDefinition {
114 private RevisionAwareXPath whenCondition;
115 private Set<MustDefinition> mustConstraints;
116 private boolean mandatory;
117 private Integer minElements;
118 private Integer maxElements;
121 public RevisionAwareXPath getWhenCondition() {
122 return whenCondition;
125 private void setWhenCondition(RevisionAwareXPath whenCondition) {
126 this.whenCondition = whenCondition;
130 public Set<MustDefinition> getMustConstraints() {
131 if (mustConstraints == null) {
132 return Collections.emptySet();
134 return mustConstraints;
138 private void setMustConstraints(Set<MustDefinition> mustConstraints) {
139 if (mustConstraints != null) {
140 this.mustConstraints = mustConstraints;
145 public boolean isMandatory() {
149 private void setMandatory(boolean mandatory) {
150 this.mandatory = mandatory;
154 public Integer getMinElements() {
158 private void setMinElements(Integer minElements) {
159 this.minElements = minElements;
163 public Integer getMaxElements() {
167 private void setMaxElements(Integer maxElements) {
168 this.maxElements = maxElements;
172 public int hashCode() {
173 final int prime = 31;
175 result = prime * result + ((whenCondition == null) ? 0 : whenCondition.hashCode());
176 result = prime * result + ((mustConstraints == null) ? 0 : mustConstraints.hashCode());
177 result = prime * result + ((minElements == null) ? 0 : minElements.hashCode());
178 result = prime * result + ((maxElements == null) ? 0 : maxElements.hashCode());
179 result = prime * result + (mandatory ? HASH_IF_BOOL_TRUE : HASH_IF_BOOL_FALSE);
184 public boolean equals(Object obj) {
191 if (getClass() != obj.getClass()) {
194 ConstraintDefinitionImpl other = (ConstraintDefinitionImpl) obj;
195 if (whenCondition == null) {
196 if (other.whenCondition != null) {
199 } else if (!whenCondition.equals(other.whenCondition)) {
202 if (mustConstraints == null) {
203 if (other.mustConstraints != null) {
206 } else if (!mustConstraints.equals(other.mustConstraints)) {
209 if (mandatory != other.mandatory) {
212 if (minElements == null) {
213 if (other.minElements != null) {
216 } else if (!minElements.equals(other.minElements)) {
219 if (maxElements == null) {
220 if (other.maxElements != null) {
223 } else if (!maxElements.equals(other.maxElements)) {
230 public String toString() {
231 StringBuilder sb = new StringBuilder(ConstraintDefinitionImpl.class.getSimpleName());
233 sb.append("whenCondition=" + whenCondition);
234 sb.append(", mustConstraints=" + mustConstraints);
235 sb.append(", mandatory=" + mandatory);
236 sb.append(", minElements=" + minElements);
237 sb.append(", maxElements=" + maxElements);
239 return sb.toString();