Share empty ConstraintDefinition instances
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / EffectiveConstraintDefinitionImpl.java
1 /**
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.ImmutableSet;
12 import java.util.Set;
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;
16
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;
25
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);
34     }
35
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();
40
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;
47         } else {
48             maxElements = Integer.valueOf(maxElementsArg);
49         }
50
51         final MandatoryEffectiveStatementImpl firstMandatoryStmt = parent
52                 .firstEffective(MandatoryEffectiveStatementImpl.class);
53         final boolean mandatory = (firstMandatoryStmt == null) ? minElements > 0 : firstMandatoryStmt.argument();
54
55         final Set<MustDefinition> mustSubstatements = ImmutableSet.copyOf(parent.allSubstatementsOfType(
56             MustDefinition.class));
57         final WhenEffectiveStatementImpl firstWhenStmt = parent.firstEffective(WhenEffectiveStatementImpl.class);
58
59         // Check for singleton instances
60         if (minElements == 0 && maxElements == UNBOUNDED_INT && mustSubstatements.isEmpty() && firstWhenStmt == null) {
61             return EmptyConstraintDefinition.create(mandatory);
62         }
63
64         return new EffectiveConstraintDefinitionImpl(mandatory, minElements, maxElements,
65             (firstWhenStmt == null) ? null : firstWhenStmt.argument(), mustSubstatements);
66     }
67
68     @Override
69     public RevisionAwareXPath getWhenCondition() {
70         return whenCondition;
71     }
72
73     @Override
74     public Set<MustDefinition> getMustConstraints() {
75         return mustConstraints;
76     }
77
78     @Override
79     public boolean isMandatory() {
80         return mandatory;
81     }
82
83     @Override
84     public Integer getMinElements() {
85         return minElements;
86     }
87
88     @Override
89     public Integer getMaxElements() {
90         return maxElements;
91     }
92
93     @Override
94     public int hashCode() {
95         return ConstraintDefinitions.hashCode(this);
96     }
97
98     @Override
99     public boolean equals(final Object obj) {
100         return ConstraintDefinitions.equals(this, obj);
101     }
102
103     @Override
104     public String toString() {
105         return ConstraintDefinitions.toString(this);
106     }
107 }