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 final 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(final 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 (!mandatory.equals(other.mandatory)) {
103 if (!Objects.equals(whenCondition, other.whenCondition)) {
106 if (!Objects.equals(mustConstraints, other.mustConstraints)) {
109 if (!Objects.equals(minElements, other.minElements)) {
112 if (!Objects.equals(maxElements, other.maxElements)) {
119 public String toString() {
120 StringBuilder sb = new StringBuilder(EffectiveConstraintDefinitionImpl.class.getSimpleName());
122 sb.append("whenCondition=").append(whenCondition);
123 sb.append(", mustConstraints=").append(mustConstraints);
124 sb.append(", mandatory=").append(mandatory);
125 sb.append(", minElements=").append(minElements);
126 sb.append(", maxElements=").append(maxElements);
128 return sb.toString();