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.MustDefinition;
16 import org.opendaylight.controller.yang.model.api.RevisionAwareXPath;
17 import org.opendaylight.controller.yang.model.util.RevisionAwareXPathImpl;
18 import org.opendaylight.controller.yang.parser.builder.api.Builder;
20 public final class ConstraintsBuilder implements Builder {
21 private final ConstraintDefinitionImpl instance;
22 private final int line;
23 private final Set<MustDefinition> mustDefinitions;
24 private String whenCondition;
25 private boolean mandatory;
29 ConstraintsBuilder(final int line) {
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);
52 public int getLine() {
56 public Integer getMinElements() {
60 public void setMinElements(Integer minElements) {
61 this.min = minElements;
64 public Integer getMaxElements() {
68 public void setMaxElements(Integer maxElements) {
69 this.max = maxElements;
72 public Set<MustDefinition> getMustDefinitions() {
73 return mustDefinitions;
76 public void addMustDefinition(MustDefinition must) {
77 mustDefinitions.add(must);
80 public String getWhenCondition() {
84 public void addWhenCondition(String whenCondition) {
85 this.whenCondition = whenCondition;
88 public boolean isMandatory() {
92 public void setMandatory(boolean mandatory) {
93 this.mandatory = mandatory;
96 private final class ConstraintDefinitionImpl implements
97 ConstraintDefinition {
98 private RevisionAwareXPath whenCondition;
99 private Set<MustDefinition> mustConstraints;
100 private boolean mandatory;
101 private Integer minElements;
102 private Integer maxElements;
105 public RevisionAwareXPath getWhenCondition() {
106 return whenCondition;
109 private void setWhenCondition(RevisionAwareXPath whenCondition) {
110 this.whenCondition = whenCondition;
114 public Set<MustDefinition> getMustConstraints() {
115 if (mustConstraints == null) {
116 return Collections.emptySet();
118 return mustConstraints;
122 private void setMustConstraints(Set<MustDefinition> mustConstraints) {
123 if (mustConstraints != null) {
124 this.mustConstraints = mustConstraints;
129 public boolean isMandatory() {
133 private void setMandatory(boolean mandatory) {
134 this.mandatory = mandatory;
138 public Integer getMinElements() {
142 private void setMinElements(Integer minElements) {
143 this.minElements = minElements;
147 public Integer getMaxElements() {
151 private void setMaxElements(Integer maxElements) {
152 this.maxElements = maxElements;
156 public int hashCode() {
157 final int prime = 31;
159 result = prime * result
160 + ((whenCondition == null) ? 0 : whenCondition.hashCode());
163 + ((mustConstraints == null) ? 0 : mustConstraints
165 result = prime * result
166 + ((minElements == null) ? 0 : minElements.hashCode());
167 result = prime * result
168 + ((maxElements == null) ? 0 : maxElements.hashCode());
169 result = prime * result + (mandatory ? 1231 : 1237);
174 public boolean equals(Object obj) {
181 if (getClass() != obj.getClass()) {
184 ConstraintDefinitionImpl other = (ConstraintDefinitionImpl) obj;
185 if (whenCondition == null) {
186 if (other.whenCondition != null) {
189 } else if (!whenCondition.equals(other.whenCondition)) {
192 if (mustConstraints == null) {
193 if (other.mustConstraints != null) {
196 } else if (!mustConstraints.equals(other.mustConstraints)) {
199 if (mandatory != other.mandatory) {
202 if (minElements == null) {
203 if (other.minElements != null) {
206 } else if (!minElements.equals(other.minElements)) {
209 if (maxElements == null) {
210 if (other.maxElements != null) {
213 } else if (!maxElements.equals(other.maxElements)) {
220 public String toString() {
221 StringBuilder sb = new StringBuilder(
222 ConstraintDefinitionImpl.class.getSimpleName());
224 sb.append("whenCondition=" + whenCondition);
225 sb.append(", mustConstraints=" + mustConstraints);
226 sb.append(", mandatory=" + mandatory);
227 sb.append(", minElements=" + minElements);
228 sb.append(", maxElements=" + maxElements);
230 return sb.toString();