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.model.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.parser.builder.api.Builder;
19 import org.opendaylight.controller.yang.model.util.RevisionAwareXPathImpl;
21 public class ConstraintsBuilder implements Builder {
23 private final ConstraintDefinitionImpl instance;
24 private final Set<MustDefinition> mustDefinitions;
25 private String whenCondition;
26 private boolean mandatory;
30 ConstraintsBuilder() {
31 instance = new ConstraintDefinitionImpl();
32 mustDefinitions = new HashSet<MustDefinition>();
36 public ConstraintDefinition build() {
37 RevisionAwareXPath whenStmt;
38 if (whenCondition == null) {
41 whenStmt = new RevisionAwareXPathImpl(whenCondition, false);
43 instance.setWhenCondition(whenStmt);
44 instance.setMustConstraints(mustDefinitions);
45 instance.setMandatory(mandatory);
46 instance.setMinElements(min);
47 instance.setMaxElements(max);
51 public Integer getMinElements() {
55 public void setMinElements(Integer minElements) {
56 this.min = minElements;
59 public Integer getMaxElements() {
63 public void setMaxElements(Integer maxElements) {
64 this.max = maxElements;
67 public Set<MustDefinition> getMustDefinitions() {
68 return mustDefinitions;
71 public void addMustDefinition(MustDefinition must) {
72 mustDefinitions.add(must);
75 public String getWhenCondition() {
79 public void addWhenCondition(String whenCondition) {
80 this.whenCondition = whenCondition;
83 public boolean isMandatory() {
87 public void setMandatory(boolean mandatory) {
88 this.mandatory = mandatory;
91 private static class ConstraintDefinitionImpl implements
92 ConstraintDefinition {
93 private DataSchemaNode parent;
94 private RevisionAwareXPath whenCondition;
95 private Set<MustDefinition> mustConstraints;
96 private boolean mandatory;
97 private Integer minElements;
98 private Integer maxElements;
101 public DataSchemaNode getParent() {
106 public RevisionAwareXPath getWhenCondition() {
107 return whenCondition;
110 private void setWhenCondition(RevisionAwareXPath whenCondition) {
111 this.whenCondition = whenCondition;
115 public Set<MustDefinition> getMustConstraints() {
116 if (mustConstraints == null) {
117 return Collections.emptySet();
119 return mustConstraints;
123 private void setMustConstraints(Set<MustDefinition> mustConstraints) {
124 if (mustConstraints != null) {
125 this.mustConstraints = mustConstraints;
130 public boolean isMandatory() {
134 private void setMandatory(boolean mandatory) {
135 this.mandatory = mandatory;
139 public Integer getMinElements() {
143 private void setMinElements(Integer minElements) {
144 this.minElements = minElements;
148 public Integer getMaxElements() {
152 private void setMaxElements(Integer maxElements) {
153 this.maxElements = maxElements;
157 public int hashCode() {
158 final int prime = 31;
160 result = prime * result
161 + ((parent == null) ? 0 : parent.hashCode());
162 result = prime * result
163 + ((whenCondition == null) ? 0 : whenCondition.hashCode());
166 + ((mustConstraints == null) ? 0 : mustConstraints
168 result = prime * result
169 + ((minElements == null) ? 0 : minElements.hashCode());
170 result = prime * result
171 + ((maxElements == null) ? 0 : maxElements.hashCode());
172 result = prime * result + (mandatory ? 1231 : 1237);
177 public boolean equals(Object obj) {
184 if (getClass() != obj.getClass()) {
187 ConstraintDefinitionImpl other = (ConstraintDefinitionImpl) obj;
188 if (parent == null) {
189 if (other.parent != null) {
192 } else if (!parent.equals(other.parent)) {
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(
232 ConstraintDefinitionImpl.class.getSimpleName());
234 sb.append("parent=" + parent);
235 sb.append(", whenCondition=" + whenCondition);
236 sb.append(", mustConstraints=" + mustConstraints);
237 sb.append(", mandatory=" + mandatory);
238 sb.append(", minElements=" + minElements);
239 sb.append(", maxElements=" + maxElements);
241 return sb.toString();