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 MandatoryEffectiveStatementImpl firstMandatoryStmt = parent
52 .firstEffective(MandatoryEffectiveStatementImpl.class);
53 final boolean mandatory = (firstMandatoryStmt == null) ? minElements > 0 : firstMandatoryStmt.argument();
55 final Set<MustDefinition> mustSubstatements = ImmutableSet.copyOf(parent.allSubstatementsOfType(
56 MustDefinition.class));
57 final WhenEffectiveStatementImpl firstWhenStmt = parent.firstEffective(WhenEffectiveStatementImpl.class);
59 // Check for singleton instances
60 if (minElements == 0 && maxElements == UNBOUNDED_INT && mustSubstatements.isEmpty() && firstWhenStmt == null) {
61 return EmptyConstraintDefinition.create(mandatory);
64 return new EffectiveConstraintDefinitionImpl(mandatory, minElements, maxElements,
65 (firstWhenStmt == null) ? null : firstWhenStmt.argument(), mustSubstatements);
69 public RevisionAwareXPath getWhenCondition() {
74 public Set<MustDefinition> getMustConstraints() {
75 return mustConstraints;
79 public boolean isMandatory() {
84 public Integer getMinElements() {
89 public Integer getMaxElements() {
94 public int hashCode() {
95 return ConstraintDefinitions.hashCode(this);
99 public boolean equals(final Object obj) {
100 return ConstraintDefinitions.equals(this, obj);
104 public String toString() {
105 return ConstraintDefinitions.toString(this);