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;
19 import org.opendaylight.controller.yang.parser.util.YangParseException;
21 public final class ConstraintsBuilder implements Builder {
22 private final ConstraintDefinitionImpl instance;
23 private final int line;
24 private Builder parent;
25 private final Set<MustDefinition> mustDefinitions;
26 private String whenCondition;
27 private boolean mandatory;
31 ConstraintsBuilder(final int line) {
33 instance = new ConstraintDefinitionImpl();
34 mustDefinitions = new HashSet<MustDefinition>();
38 public ConstraintDefinition build() {
39 RevisionAwareXPath whenStmt;
40 if (whenCondition == null) {
43 whenStmt = new RevisionAwareXPathImpl(whenCondition, false);
45 instance.setWhenCondition(whenStmt);
46 instance.setMustConstraints(mustDefinitions);
47 instance.setMandatory(mandatory);
48 instance.setMinElements(min);
49 instance.setMaxElements(max);
54 public int getLine() {
59 public Builder getParent() {
64 public void setParent(final Builder parent) {
69 public void addUnknownSchemaNode(UnknownSchemaNodeBuilder unknownNode) {
70 throw new YangParseException(line, "Can not add unknown node to constraints.");
73 public Integer getMinElements() {
77 public void setMinElements(Integer minElements) {
78 this.min = minElements;
81 public Integer getMaxElements() {
85 public void setMaxElements(Integer maxElements) {
86 this.max = maxElements;
89 public Set<MustDefinition> getMustDefinitions() {
90 return mustDefinitions;
93 public void addMustDefinition(MustDefinition must) {
94 mustDefinitions.add(must);
97 public String getWhenCondition() {
101 public void addWhenCondition(String whenCondition) {
102 this.whenCondition = whenCondition;
105 public boolean isMandatory() {
109 public void setMandatory(boolean mandatory) {
110 this.mandatory = mandatory;
114 private final class ConstraintDefinitionImpl implements ConstraintDefinition {
115 private RevisionAwareXPath whenCondition;
116 private Set<MustDefinition> mustConstraints;
117 private boolean mandatory;
118 private Integer minElements;
119 private Integer maxElements;
122 public RevisionAwareXPath getWhenCondition() {
123 return whenCondition;
126 private void setWhenCondition(RevisionAwareXPath whenCondition) {
127 this.whenCondition = whenCondition;
131 public Set<MustDefinition> getMustConstraints() {
132 if (mustConstraints == null) {
133 return Collections.emptySet();
135 return mustConstraints;
139 private void setMustConstraints(Set<MustDefinition> mustConstraints) {
140 if (mustConstraints != null) {
141 this.mustConstraints = mustConstraints;
146 public boolean isMandatory() {
150 private void setMandatory(boolean mandatory) {
151 this.mandatory = mandatory;
155 public Integer getMinElements() {
159 private void setMinElements(Integer minElements) {
160 this.minElements = minElements;
164 public Integer getMaxElements() {
168 private void setMaxElements(Integer maxElements) {
169 this.maxElements = maxElements;
173 public int hashCode() {
174 final int prime = 31;
176 result = prime * result + ((whenCondition == null) ? 0 : whenCondition.hashCode());
177 result = prime * result + ((mustConstraints == null) ? 0 : mustConstraints.hashCode());
178 result = prime * result + ((minElements == null) ? 0 : minElements.hashCode());
179 result = prime * result + ((maxElements == null) ? 0 : maxElements.hashCode());
180 result = prime * result + (mandatory ? 1231 : 1237);
185 public boolean equals(Object obj) {
192 if (getClass() != obj.getClass()) {
195 ConstraintDefinitionImpl other = (ConstraintDefinitionImpl) obj;
196 if (whenCondition == null) {
197 if (other.whenCondition != null) {
200 } else if (!whenCondition.equals(other.whenCondition)) {
203 if (mustConstraints == null) {
204 if (other.mustConstraints != null) {
207 } else if (!mustConstraints.equals(other.mustConstraints)) {
210 if (mandatory != other.mandatory) {
213 if (minElements == null) {
214 if (other.minElements != null) {
217 } else if (!minElements.equals(other.minElements)) {
220 if (maxElements == null) {
221 if (other.maxElements != null) {
224 } else if (!maxElements.equals(other.maxElements)) {
231 public String toString() {
232 StringBuilder sb = new StringBuilder(ConstraintDefinitionImpl.class.getSimpleName());
234 sb.append("whenCondition=" + whenCondition);
235 sb.append(", mustConstraints=" + mustConstraints);
236 sb.append(", mandatory=" + mandatory);
237 sb.append(", minElements=" + minElements);
238 sb.append(", maxElements=" + maxElements);
240 return sb.toString();