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 static final Integer UNBOUNDED_INT = Integer.MAX_VALUE;
20 private static final String UNBOUNDED_STR = "unbounded";
21 private final RevisionAwareXPath whenCondition;
22 private final Set<MustDefinition> mustConstraints;
23 private final Boolean mandatory;
24 private final Integer minElements;
25 private final Integer maxElements;
27 public EffectiveConstraintDefinitionImpl(final EffectiveStatementBase<?, ?> parent) {
29 WhenEffectiveStatementImpl firstWhenStmt = parent.firstEffective(WhenEffectiveStatementImpl.class);
30 this.whenCondition = (firstWhenStmt == null) ? null : firstWhenStmt.argument();
32 MinElementsEffectiveStatementImpl firstMinElementsStmt = parent
33 .firstEffective(MinElementsEffectiveStatementImpl.class);
34 this.minElements = (firstMinElementsStmt == null) ? 0 : firstMinElementsStmt.argument();
36 MaxElementsEffectiveStatementImpl firstMaxElementsStmt = parent
37 .firstEffective(MaxElementsEffectiveStatementImpl.class);
38 String maxElementsArg = (firstMaxElementsStmt == null) ? UNBOUNDED_STR : firstMaxElementsStmt.argument();
39 if (UNBOUNDED_STR.equals(maxElementsArg)) {
40 this.maxElements = UNBOUNDED_INT;
42 this.maxElements = Integer.valueOf(maxElementsArg);
45 MandatoryEffectiveStatementImpl firstMandatoryStmt = parent
46 .firstEffective(MandatoryEffectiveStatementImpl.class);
47 this.mandatory = (firstMandatoryStmt == null) ? minElements > 0 : firstMandatoryStmt.argument();
49 Collection<MustDefinition> mustSubstatements = parent.allSubstatementsOfType(MustDefinition.class);
50 this.mustConstraints = ImmutableSet.copyOf(mustSubstatements);
54 public RevisionAwareXPath getWhenCondition() {
59 public Set<MustDefinition> getMustConstraints() {
60 return mustConstraints;
64 public boolean isMandatory() {
69 public Integer getMinElements() {
74 public Integer getMaxElements() {
79 public int hashCode() {
82 result = prime * result + Objects.hashCode(whenCondition);
83 result = prime * result + Objects.hashCode(mustConstraints);
84 result = prime * result + Objects.hashCode(minElements);
85 result = prime * result + Objects.hashCode(maxElements);
86 result = prime * result + mandatory.hashCode();
91 public boolean equals(final Object obj) {
98 if (getClass() != obj.getClass()) {
101 EffectiveConstraintDefinitionImpl other = (EffectiveConstraintDefinitionImpl) obj;
102 if (!mandatory.equals(other.mandatory)) {
105 if (!Objects.equals(whenCondition, other.whenCondition)) {
108 if (!Objects.equals(mustConstraints, other.mustConstraints)) {
111 if (!Objects.equals(minElements, other.minElements)) {
114 if (!Objects.equals(maxElements, other.maxElements)) {
121 public String toString() {
122 StringBuilder sb = new StringBuilder(EffectiveConstraintDefinitionImpl.class.getSimpleName());
124 sb.append("whenCondition=").append(whenCondition);
125 sb.append(", mustConstraints=").append(mustConstraints);
126 sb.append(", mandatory=").append(mandatory);
127 sb.append(", minElements=").append(minElements);
128 sb.append(", maxElements=").append(maxElements);
130 return sb.toString();