2 * Copyright (c) 2015 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.yangtools.yang.parser.stmt.rfc6020.effective;
10 import com.google.common.collect.ImmutableSet;
11 import java.util.Collection;
12 import java.util.Objects;
14 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
15 import org.opendaylight.yangtools.yang.model.api.MustDefinition;
16 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
18 public class EffectiveConstraintDefinitionImpl implements ConstraintDefinition {
19 private final RevisionAwareXPath whenCondition;
20 private final Set<MustDefinition> mustConstraints;
21 private final Boolean mandatory;
22 private final Integer minElements;
23 private final Integer maxElements;
25 public EffectiveConstraintDefinitionImpl(EffectiveStatementBase<?, ?> parent) {
27 MandatoryEffectiveStatementImpl firstMandatoryStmt = parent
28 .firstEffective(MandatoryEffectiveStatementImpl.class);
29 this.mandatory = (firstMandatoryStmt == null) ? false : firstMandatoryStmt.argument();
31 WhenEffectiveStatementImpl firstWhenStmt = parent.firstEffective(WhenEffectiveStatementImpl.class);
32 this.whenCondition = (firstWhenStmt == null) ? null : firstWhenStmt.argument();
34 MinElementsEffectiveStatementImpl firstMinElementsStmt = parent
35 .firstEffective(MinElementsEffectiveStatementImpl.class);
36 this.minElements = (firstMinElementsStmt == null) ? 0 : firstMinElementsStmt.argument();
38 MaxElementsEffectiveStatementImpl firstMaxElementsStmt = parent
39 .firstEffective(MaxElementsEffectiveStatementImpl.class);
40 String maxElementsArg = (firstMaxElementsStmt == null) ? "unbounded" : firstMaxElementsStmt.argument();
41 if (maxElementsArg.equals("unbounded")) {
42 this.maxElements = Integer.MAX_VALUE;
44 this.maxElements = Integer.valueOf(maxElementsArg);
47 Collection<MustDefinition> mustSubstatements = parent.allSubstatementsOfType(MustDefinition.class);
48 this.mustConstraints = ImmutableSet.copyOf(mustSubstatements);
52 public RevisionAwareXPath getWhenCondition() {
57 public Set<MustDefinition> getMustConstraints() {
58 return mustConstraints;
62 public boolean isMandatory() {
67 public Integer getMinElements() {
72 public Integer getMaxElements() {
77 public int hashCode() {
80 result = prime * result + Objects.hashCode(whenCondition);
81 result = prime * result + Objects.hashCode(mustConstraints);
82 result = prime * result + Objects.hashCode(minElements);
83 result = prime * result + Objects.hashCode(maxElements);
84 result = prime * result + mandatory.hashCode();
89 public boolean equals(final Object obj) {
96 if (getClass() != obj.getClass()) {
99 EffectiveConstraintDefinitionImpl other = (EffectiveConstraintDefinitionImpl) obj;
100 if (whenCondition == null) {
101 if (other.whenCondition != null) {
104 } else if (!whenCondition.equals(other.whenCondition)) {
107 if (mustConstraints == null) {
108 if (other.mustConstraints != null) {
111 } else if (!mustConstraints.equals(other.mustConstraints)) {
114 if (!mandatory.equals(other.mandatory)) {
117 if (minElements == null) {
118 if (other.minElements != null) {
121 } else if (!minElements.equals(other.minElements)) {
124 if (maxElements == null) {
125 if (other.maxElements != null) {
128 } else if (!maxElements.equals(other.maxElements)) {
135 public String toString() {
136 StringBuilder sb = new StringBuilder(
137 EffectiveConstraintDefinitionImpl.class.getSimpleName());
139 sb.append("whenCondition=").append(whenCondition);
140 sb.append(", mustConstraints=").append(mustConstraints);
141 sb.append(", mandatory=").append(mandatory);
142 sb.append(", minElements=").append(minElements);
143 sb.append(", maxElements=").append(maxElements);
145 return sb.toString();