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.base.Preconditions;
11 import com.google.common.collect.ImmutableSet;
13 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
14 import org.opendaylight.yangtools.yang.model.api.MustDefinition;
15 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
17 final class EffectiveConstraintDefinitionImpl implements ConstraintDefinition {
18 private static final Integer UNBOUNDED_INT = Integer.MAX_VALUE;
19 private static final String UNBOUNDED_STR = "unbounded";
20 private final RevisionAwareXPath whenCondition;
21 private final Set<MustDefinition> mustConstraints;
22 private final Integer minElements;
23 private final Integer maxElements;
24 private final boolean mandatory;
26 private EffectiveConstraintDefinitionImpl(final boolean mandatory, final Integer minElements,
27 final Integer maxElements, final RevisionAwareXPath whenCondition,
28 final Set<MustDefinition> mustConstraints) {
29 this.mandatory = mandatory;
30 this.minElements = Preconditions.checkNotNull(minElements);
31 this.maxElements = Preconditions.checkNotNull(maxElements);
32 this.whenCondition = whenCondition;
33 this.mustConstraints = Preconditions.checkNotNull(mustConstraints);
36 static ConstraintDefinition forParent(final EffectiveStatementBase<?, ?> parent) {
37 final MinElementsEffectiveStatementImpl firstMinElementsStmt = parent
38 .firstEffective(MinElementsEffectiveStatementImpl.class);
39 final Integer minElements = (firstMinElementsStmt == null) ? 0 : firstMinElementsStmt.argument();
41 final MaxElementsEffectiveStatementImpl firstMaxElementsStmt = parent
42 .firstEffective(MaxElementsEffectiveStatementImpl.class);
43 final String maxElementsArg = (firstMaxElementsStmt == null) ? UNBOUNDED_STR : firstMaxElementsStmt.argument();
44 final Integer maxElements;
45 if (UNBOUNDED_STR.equals(maxElementsArg)) {
46 maxElements = UNBOUNDED_INT;
48 maxElements = Integer.valueOf(maxElementsArg);
51 final MandatoryEffectiveStatement firstMandatoryStmt = parent.firstEffective(MandatoryEffectiveStatement.class);
52 final boolean mandatory = (firstMandatoryStmt == null) ? minElements > 0 : firstMandatoryStmt.argument();
54 final Set<MustDefinition> mustSubstatements = ImmutableSet.copyOf(parent.allSubstatementsOfType(
55 MustDefinition.class));
56 final WhenEffectiveStatementImpl firstWhenStmt = parent.firstEffective(WhenEffectiveStatementImpl.class);
58 // Check for singleton instances
59 if (minElements == 0 && maxElements == UNBOUNDED_INT && mustSubstatements.isEmpty() && firstWhenStmt == null) {
60 return EmptyConstraintDefinition.create(mandatory);
63 return new EffectiveConstraintDefinitionImpl(mandatory, minElements, maxElements,
64 (firstWhenStmt == null) ? null : firstWhenStmt.argument(), mustSubstatements);
68 public RevisionAwareXPath getWhenCondition() {
73 public Set<MustDefinition> getMustConstraints() {
74 return mustConstraints;
78 public boolean isMandatory() {
83 public Integer getMinElements() {
88 public Integer getMaxElements() {
93 public int hashCode() {
94 return ConstraintDefinitions.hashCode(this);
98 public boolean equals(final Object obj) {
99 return ConstraintDefinitions.equals(this, obj);
103 public String toString() {
104 return ConstraintDefinitions.toString(this);