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.model.util.RevisionAwareXPathImpl;
15 import org.opendaylight.controller.yang.model.api.ConstraintDefinition;
16 import org.opendaylight.controller.yang.model.api.DataSchemaNode;
17 import org.opendaylight.controller.yang.model.api.MustDefinition;
18 import org.opendaylight.controller.yang.model.api.RevisionAwareXPath;
19 import org.opendaylight.controller.yang.model.parser.builder.api.Builder;
21 public class ConstraintsBuilder implements Builder {
23 private final ConstraintDefinitionImpl instance;
24 private final Set<MustDefinition> mustDefinitions;
25 private String whenCondition;
27 ConstraintsBuilder() {
28 instance = new ConstraintDefinitionImpl();
29 mustDefinitions = new HashSet<MustDefinition>();
33 public ConstraintDefinition build() {
34 RevisionAwareXPath whenStmt = new RevisionAwareXPathImpl(whenCondition,
36 instance.setWhenCondition(whenStmt);
37 instance.setMustConstraints(mustDefinitions);
41 public void setMinElements(Integer minElements) {
42 instance.setMinElements(minElements);
45 public void setMaxElements(Integer maxElements) {
46 instance.setMaxElements(maxElements);
49 public void addMustDefinition(String mustStr, String description,
51 MustDefinition must = new MustDefinitionImpl(mustStr, description,
53 mustDefinitions.add(must);
56 public void addWhenCondition(String whenCondition) {
57 this.whenCondition = whenCondition;
60 public void setMandatory(boolean mandatory) {
61 instance.setMandatory(mandatory);
64 private static class ConstraintDefinitionImpl implements
65 ConstraintDefinition {
67 private DataSchemaNode parent;
68 private RevisionAwareXPath whenCondition;
69 private Set<MustDefinition> mustConstraints;
70 private boolean mandatory;
71 private Integer minElements;
72 private Integer maxElements;
75 public DataSchemaNode getParent() {
80 public RevisionAwareXPath getWhenCondition() {
84 private void setWhenCondition(RevisionAwareXPath whenCondition) {
85 this.whenCondition = whenCondition;
89 public Set<MustDefinition> getMustConstraints() {
90 if (mustConstraints == null) {
91 return Collections.emptySet();
93 return mustConstraints;
97 private void setMustConstraints(Set<MustDefinition> mustConstraints) {
98 if (mustConstraints != null) {
99 this.mustConstraints = mustConstraints;
104 public boolean isMandatory() {
108 private void setMandatory(boolean mandatory) {
109 this.mandatory = mandatory;
113 public Integer getMinElements() {
117 private void setMinElements(Integer minElements) {
118 this.minElements = minElements;
122 public Integer getMaxElements() {
126 private void setMaxElements(Integer maxElements) {
127 this.maxElements = maxElements;
131 public int hashCode() {
132 final int prime = 31;
134 result = prime * result
135 + ((parent == null) ? 0 : parent.hashCode());
136 result = prime * result
137 + ((whenCondition == null) ? 0 : whenCondition.hashCode());
140 + ((mustConstraints == null) ? 0 : mustConstraints
142 result = prime * result
143 + ((minElements == null) ? 0 : minElements.hashCode());
144 result = prime * result
145 + ((maxElements == null) ? 0 : maxElements.hashCode());
146 result = prime * result + (mandatory ? 1231 : 1237);
151 public boolean equals(Object obj) {
158 if (getClass() != obj.getClass()) {
161 ConstraintDefinitionImpl other = (ConstraintDefinitionImpl) obj;
162 if (parent == null) {
163 if (other.parent != null) {
166 } else if (!parent.equals(other.parent)) {
169 if (whenCondition == null) {
170 if (other.whenCondition != null) {
173 } else if (!whenCondition.equals(other.whenCondition)) {
176 if (mustConstraints == null) {
177 if (other.mustConstraints != null) {
180 } else if (!mustConstraints.equals(other.mustConstraints)) {
183 if (mandatory != other.mandatory) {
186 if (minElements == null) {
187 if (other.minElements != null) {
190 } else if (!minElements.equals(other.minElements)) {
193 if (maxElements == null) {
194 if (other.maxElements != null) {
197 } else if (!maxElements.equals(other.maxElements)) {
204 public String toString() {
205 StringBuilder sb = new StringBuilder(
206 ConstraintDefinitionImpl.class.getSimpleName());
208 sb.append("parent=" + parent);
209 sb.append(", whenCondition=" + whenCondition);
210 sb.append(", mustConstraints=" + mustConstraints);
211 sb.append(", mandatory=" + mandatory);
212 sb.append(", minElements=" + minElements);
213 sb.append(", maxElements=" + maxElements);
215 return sb.toString();
220 private static class MustDefinitionImpl implements MustDefinition {
222 private final String mustStr;
223 private final String description;
224 private final String reference;
226 private MustDefinitionImpl(String mustStr, String description,
228 this.mustStr = mustStr;
229 this.description = description;
230 this.reference = reference;
234 public String getDescription() {
239 public String getErrorAppTag() {
240 // TODO Auto-generated method stub
245 public String getErrorMessage() {
246 // TODO Auto-generated method stub
251 public String getReference() {
256 public RevisionAwareXPath getXpath() {
257 // TODO Auto-generated method stub
262 public int hashCode() {
263 final int prime = 31;
265 result = prime * result
266 + ((mustStr == null) ? 0 : mustStr.hashCode());
267 result = prime * result
268 + ((description == null) ? 0 : description.hashCode());
269 result = prime * result
270 + ((reference == null) ? 0 : reference.hashCode());
275 public boolean equals(Object obj) {
282 if (getClass() != obj.getClass()) {
285 MustDefinitionImpl other = (MustDefinitionImpl) obj;
286 if (mustStr == null) {
287 if (other.mustStr != null) {
290 } else if (!mustStr.equals(other.mustStr)) {
293 if (description == null) {
294 if (other.description != null) {
297 } else if (!description.equals(other.description)) {
300 if (reference == null) {
301 if (other.reference != null) {
304 } else if (!reference.equals(other.reference)) {
311 public String toString() {
312 return MustDefinitionImpl.class.getSimpleName() + "[mustStr="