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.controller.yang.parser.builder.impl;
10 import java.util.Collections;
11 import java.util.HashSet;
14 import org.opendaylight.controller.yang.model.api.ConstraintDefinition;
15 import org.opendaylight.controller.yang.model.api.DataSchemaNode;
16 import org.opendaylight.controller.yang.model.api.MustDefinition;
17 import org.opendaylight.controller.yang.model.api.RevisionAwareXPath;
18 import org.opendaylight.controller.yang.model.util.RevisionAwareXPathImpl;
19 import org.opendaylight.controller.yang.parser.builder.api.Builder;
21 public class ConstraintsBuilder implements Builder {
22 private final ConstraintDefinitionImpl instance;
23 private final int line;
24 private final Set<MustDefinition> mustDefinitions;
25 private String whenCondition;
26 private boolean mandatory;
30 ConstraintsBuilder(final int line) {
32 instance = new ConstraintDefinitionImpl();
33 mustDefinitions = new HashSet<MustDefinition>();
37 public ConstraintDefinition build() {
38 RevisionAwareXPath whenStmt;
39 if (whenCondition == null) {
42 whenStmt = new RevisionAwareXPathImpl(whenCondition, false);
44 instance.setWhenCondition(whenStmt);
45 instance.setMustConstraints(mustDefinitions);
46 instance.setMandatory(mandatory);
47 instance.setMinElements(min);
48 instance.setMaxElements(max);
53 public int getLine() {
57 public Integer getMinElements() {
61 public void setMinElements(Integer minElements) {
62 this.min = minElements;
65 public Integer getMaxElements() {
69 public void setMaxElements(Integer maxElements) {
70 this.max = maxElements;
73 public Set<MustDefinition> getMustDefinitions() {
74 return mustDefinitions;
77 public void addMustDefinition(MustDefinition must) {
78 mustDefinitions.add(must);
81 public String getWhenCondition() {
85 public void addWhenCondition(String whenCondition) {
86 this.whenCondition = whenCondition;
89 public boolean isMandatory() {
93 public void setMandatory(boolean mandatory) {
94 this.mandatory = mandatory;
97 private static class ConstraintDefinitionImpl implements
98 ConstraintDefinition {
99 private DataSchemaNode parent;
100 private RevisionAwareXPath whenCondition;
101 private Set<MustDefinition> mustConstraints;
102 private boolean mandatory;
103 private Integer minElements;
104 private Integer maxElements;
107 public DataSchemaNode getParent() {
112 public RevisionAwareXPath getWhenCondition() {
113 return whenCondition;
116 private void setWhenCondition(RevisionAwareXPath whenCondition) {
117 this.whenCondition = whenCondition;
121 public Set<MustDefinition> getMustConstraints() {
122 if (mustConstraints == null) {
123 return Collections.emptySet();
125 return mustConstraints;
129 private void setMustConstraints(Set<MustDefinition> mustConstraints) {
130 if (mustConstraints != null) {
131 this.mustConstraints = mustConstraints;
136 public boolean isMandatory() {
140 private void setMandatory(boolean mandatory) {
141 this.mandatory = mandatory;
145 public Integer getMinElements() {
149 private void setMinElements(Integer minElements) {
150 this.minElements = minElements;
154 public Integer getMaxElements() {
158 private void setMaxElements(Integer maxElements) {
159 this.maxElements = maxElements;
163 public int hashCode() {
164 final int prime = 31;
166 result = prime * result
167 + ((parent == null) ? 0 : parent.hashCode());
168 result = prime * result
169 + ((whenCondition == null) ? 0 : whenCondition.hashCode());
172 + ((mustConstraints == null) ? 0 : mustConstraints
174 result = prime * result
175 + ((minElements == null) ? 0 : minElements.hashCode());
176 result = prime * result
177 + ((maxElements == null) ? 0 : maxElements.hashCode());
178 result = prime * result + (mandatory ? 1231 : 1237);
183 public boolean equals(Object obj) {
190 if (getClass() != obj.getClass()) {
193 ConstraintDefinitionImpl other = (ConstraintDefinitionImpl) obj;
194 if (parent == null) {
195 if (other.parent != null) {
198 } else if (!parent.equals(other.parent)) {
201 if (whenCondition == null) {
202 if (other.whenCondition != null) {
205 } else if (!whenCondition.equals(other.whenCondition)) {
208 if (mustConstraints == null) {
209 if (other.mustConstraints != null) {
212 } else if (!mustConstraints.equals(other.mustConstraints)) {
215 if (mandatory != other.mandatory) {
218 if (minElements == null) {
219 if (other.minElements != null) {
222 } else if (!minElements.equals(other.minElements)) {
225 if (maxElements == null) {
226 if (other.maxElements != null) {
229 } else if (!maxElements.equals(other.maxElements)) {
236 public String toString() {
237 StringBuilder sb = new StringBuilder(
238 ConstraintDefinitionImpl.class.getSimpleName());
240 sb.append("parent=" + parent);
241 sb.append(", whenCondition=" + whenCondition);
242 sb.append(", mustConstraints=" + mustConstraints);
243 sb.append(", mandatory=" + mandatory);
244 sb.append(", minElements=" + minElements);
245 sb.append(", maxElements=" + maxElements);
247 return sb.toString();