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;
12 import org.opendaylight.yangtools.yang.model.api.*;
13 import org.opendaylight.yangtools.yang.model.util.RevisionAwareXPathImpl;
15 public final class ConstraintsBuilder {
16 private static final int HASH_IF_BOOL_TRUE = 1231;
17 private static final int HASH_IF_BOOL_FALSE = 1237;
19 private final String moduleName;
20 private final int line;
21 private final ConstraintDefinitionImpl instance;
22 private final Set<MustDefinition> mustDefinitions;
23 private RevisionAwareXPath whenStmt;
24 private String whenCondition;
25 private boolean mandatory;
29 public ConstraintsBuilder(final String moduleName, final int line) {
30 this.moduleName = moduleName;
32 instance = new ConstraintDefinitionImpl();
33 mustDefinitions = new HashSet<MustDefinition>();
36 ConstraintsBuilder(final ConstraintsBuilder b) {
37 this.moduleName = b.getModuleName();
38 this.line = b.getLine();
39 instance = new ConstraintDefinitionImpl();
40 mustDefinitions = new HashSet<MustDefinition>(b.getMustDefinitions());
41 whenCondition = b.getWhenCondition();
42 mandatory = b.isMandatory();
43 min = b.getMinElements();
44 max = b.getMaxElements();
47 ConstraintsBuilder(final String moduleName, final int line, final ConstraintDefinition base) {
48 this.moduleName = moduleName;
50 instance = new ConstraintDefinitionImpl();
51 whenStmt = base.getWhenCondition();
52 mustDefinitions = new HashSet<MustDefinition>(base.getMustConstraints());
53 mandatory = base.isMandatory();
54 min = base.getMinElements();
55 max = base.getMaxElements();
58 public ConstraintDefinition build() {
59 if (whenStmt == null) {
60 if (whenCondition == null) {
63 whenStmt = new RevisionAwareXPathImpl(whenCondition, false);
66 instance.setWhenCondition(whenStmt);
67 instance.setMustConstraints(mustDefinitions);
68 instance.setMandatory(mandatory);
69 instance.setMinElements(min);
70 instance.setMaxElements(max);
74 public String getModuleName() {
78 public int getLine() {
82 public Integer getMinElements() {
86 public void setMinElements(Integer minElements) {
87 this.min = minElements;
90 public Integer getMaxElements() {
94 public void setMaxElements(Integer maxElements) {
95 this.max = maxElements;
98 public Set<MustDefinition> getMustDefinitions() {
99 return mustDefinitions;
102 public void addMustDefinition(MustDefinition must) {
103 mustDefinitions.add(must);
106 public String getWhenCondition() {
107 return whenCondition;
110 public void addWhenCondition(String whenCondition) {
111 this.whenCondition = whenCondition;
114 public boolean isMandatory() {
118 public void setMandatory(boolean mandatory) {
119 this.mandatory = mandatory;
122 private final class ConstraintDefinitionImpl implements ConstraintDefinition {
123 private RevisionAwareXPath whenCondition;
124 private Set<MustDefinition> mustConstraints;
125 private boolean mandatory;
126 private Integer minElements;
127 private Integer maxElements;
130 public RevisionAwareXPath getWhenCondition() {
131 return whenCondition;
134 private void setWhenCondition(RevisionAwareXPath whenCondition) {
135 this.whenCondition = whenCondition;
139 public Set<MustDefinition> getMustConstraints() {
140 if (mustConstraints == null) {
141 return Collections.emptySet();
143 return mustConstraints;
147 private void setMustConstraints(Set<MustDefinition> mustConstraints) {
148 if (mustConstraints != null) {
149 this.mustConstraints = mustConstraints;
154 public boolean isMandatory() {
158 private void setMandatory(boolean mandatory) {
159 this.mandatory = mandatory;
163 public Integer getMinElements() {
167 private void setMinElements(Integer minElements) {
168 this.minElements = minElements;
172 public Integer getMaxElements() {
176 private void setMaxElements(Integer maxElements) {
177 this.maxElements = maxElements;
181 public int hashCode() {
182 final int prime = 31;
184 result = prime * result + ((whenCondition == null) ? 0 : whenCondition.hashCode());
185 result = prime * result + ((mustConstraints == null) ? 0 : mustConstraints.hashCode());
186 result = prime * result + ((minElements == null) ? 0 : minElements.hashCode());
187 result = prime * result + ((maxElements == null) ? 0 : maxElements.hashCode());
188 result = prime * result + (mandatory ? HASH_IF_BOOL_TRUE : HASH_IF_BOOL_FALSE);
193 public boolean equals(Object obj) {
200 if (getClass() != obj.getClass()) {
203 ConstraintDefinitionImpl other = (ConstraintDefinitionImpl) obj;
204 if (whenCondition == null) {
205 if (other.whenCondition != null) {
208 } else if (!whenCondition.equals(other.whenCondition)) {
211 if (mustConstraints == null) {
212 if (other.mustConstraints != null) {
215 } else if (!mustConstraints.equals(other.mustConstraints)) {
218 if (mandatory != other.mandatory) {
221 if (minElements == null) {
222 if (other.minElements != null) {
225 } else if (!minElements.equals(other.minElements)) {
228 if (maxElements == null) {
229 if (other.maxElements != null) {
232 } else if (!maxElements.equals(other.maxElements)) {
239 public String toString() {
240 StringBuilder sb = new StringBuilder(ConstraintDefinitionImpl.class.getSimpleName());
242 sb.append("whenCondition=" + whenCondition);
243 sb.append(", mustConstraints=" + mustConstraints);
244 sb.append(", mandatory=" + mandatory);
245 sb.append(", minElements=" + minElements);
246 sb.append(", maxElements=" + maxElements);
248 return sb.toString();