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;
12 import java.util.List;
15 import org.opendaylight.controller.yang.model.api.ConstraintDefinition;
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;
20 import org.opendaylight.controller.yang.parser.util.YangParseException;
22 public final class ConstraintsBuilder implements Builder {
23 private final ConstraintDefinitionImpl instance;
24 private final int line;
25 private Builder parent;
26 private final Set<MustDefinition> mustDefinitions;
27 private String whenCondition;
28 private boolean mandatory;
32 ConstraintsBuilder(final int line) {
34 instance = new ConstraintDefinitionImpl();
35 mustDefinitions = new HashSet<MustDefinition>();
39 public ConstraintDefinition build() {
40 RevisionAwareXPath whenStmt;
41 if (whenCondition == null) {
44 whenStmt = new RevisionAwareXPathImpl(whenCondition, false);
46 instance.setWhenCondition(whenStmt);
47 instance.setMustConstraints(mustDefinitions);
48 instance.setMandatory(mandatory);
49 instance.setMinElements(min);
50 instance.setMaxElements(max);
55 public int getLine() {
60 public Builder getParent() {
65 public void setParent(final Builder parent) {
70 public void addUnknownNodeBuilder(UnknownSchemaNodeBuilder unknownNode) {
71 throw new YangParseException(line, "Can not add unknown node to constraints.");
75 public List<UnknownSchemaNodeBuilder> getUnknownNodeBuilders() {
76 return Collections.emptyList();
79 public Integer getMinElements() {
83 public void setMinElements(Integer minElements) {
84 this.min = minElements;
87 public Integer getMaxElements() {
91 public void setMaxElements(Integer maxElements) {
92 this.max = maxElements;
95 public Set<MustDefinition> getMustDefinitions() {
96 return mustDefinitions;
99 public void addMustDefinition(MustDefinition must) {
100 mustDefinitions.add(must);
103 public String getWhenCondition() {
104 return whenCondition;
107 public void addWhenCondition(String whenCondition) {
108 this.whenCondition = whenCondition;
111 public boolean isMandatory() {
115 public void setMandatory(boolean mandatory) {
116 this.mandatory = mandatory;
120 private final class ConstraintDefinitionImpl implements ConstraintDefinition {
121 private RevisionAwareXPath whenCondition;
122 private Set<MustDefinition> mustConstraints;
123 private boolean mandatory;
124 private Integer minElements;
125 private Integer maxElements;
128 public RevisionAwareXPath getWhenCondition() {
129 return whenCondition;
132 private void setWhenCondition(RevisionAwareXPath whenCondition) {
133 this.whenCondition = whenCondition;
137 public Set<MustDefinition> getMustConstraints() {
138 if (mustConstraints == null) {
139 return Collections.emptySet();
141 return mustConstraints;
145 private void setMustConstraints(Set<MustDefinition> mustConstraints) {
146 if (mustConstraints != null) {
147 this.mustConstraints = mustConstraints;
152 public boolean isMandatory() {
156 private void setMandatory(boolean mandatory) {
157 this.mandatory = mandatory;
161 public Integer getMinElements() {
165 private void setMinElements(Integer minElements) {
166 this.minElements = minElements;
170 public Integer getMaxElements() {
174 private void setMaxElements(Integer maxElements) {
175 this.maxElements = maxElements;
179 public int hashCode() {
180 final int prime = 31;
182 result = prime * result + ((whenCondition == null) ? 0 : whenCondition.hashCode());
183 result = prime * result + ((mustConstraints == null) ? 0 : mustConstraints.hashCode());
184 result = prime * result + ((minElements == null) ? 0 : minElements.hashCode());
185 result = prime * result + ((maxElements == null) ? 0 : maxElements.hashCode());
186 result = prime * result + (mandatory ? 1231 : 1237);
191 public boolean equals(Object obj) {
198 if (getClass() != obj.getClass()) {
201 ConstraintDefinitionImpl other = (ConstraintDefinitionImpl) obj;
202 if (whenCondition == null) {
203 if (other.whenCondition != null) {
206 } else if (!whenCondition.equals(other.whenCondition)) {
209 if (mustConstraints == null) {
210 if (other.mustConstraints != null) {
213 } else if (!mustConstraints.equals(other.mustConstraints)) {
216 if (mandatory != other.mandatory) {
219 if (minElements == null) {
220 if (other.minElements != null) {
223 } else if (!minElements.equals(other.minElements)) {
226 if (maxElements == null) {
227 if (other.maxElements != null) {
230 } else if (!maxElements.equals(other.maxElements)) {
237 public String toString() {
238 StringBuilder sb = new StringBuilder(ConstraintDefinitionImpl.class.getSimpleName());
240 sb.append("whenCondition=" + whenCondition);
241 sb.append(", mustConstraints=" + mustConstraints);
242 sb.append(", mandatory=" + mandatory);
243 sb.append(", minElements=" + minElements);
244 sb.append(", maxElements=" + maxElements);
246 return sb.toString();