Bug 4412: New yang parser effective statements cleanup
[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.collect.ImmutableSet;
11 import java.util.Collection;
12 import java.util.Objects;
13 import java.util.Set;
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;
17
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;
24
25     public EffectiveConstraintDefinitionImpl(final EffectiveStatementBase<?, ?> parent) {
26
27         MandatoryEffectiveStatementImpl firstMandatoryStmt = parent
28                 .firstEffective(MandatoryEffectiveStatementImpl.class);
29         this.mandatory = (firstMandatoryStmt == null) ? false : firstMandatoryStmt.argument();
30
31         WhenEffectiveStatementImpl firstWhenStmt = parent.firstEffective(WhenEffectiveStatementImpl.class);
32         this.whenCondition = (firstWhenStmt == null) ? null : firstWhenStmt.argument();
33
34         MinElementsEffectiveStatementImpl firstMinElementsStmt = parent
35                 .firstEffective(MinElementsEffectiveStatementImpl.class);
36         this.minElements = (firstMinElementsStmt == null) ? 0 : firstMinElementsStmt.argument();
37
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;
43         } else {
44             this.maxElements = Integer.valueOf(maxElementsArg);
45         }
46
47         Collection<MustDefinition> mustSubstatements = parent.allSubstatementsOfType(MustDefinition.class);
48         this.mustConstraints = ImmutableSet.copyOf(mustSubstatements);
49     }
50
51     @Override
52     public RevisionAwareXPath getWhenCondition() {
53         return whenCondition;
54     }
55
56     @Override
57     public Set<MustDefinition> getMustConstraints() {
58         return mustConstraints;
59     }
60
61     @Override
62     public boolean isMandatory() {
63         return mandatory;
64     }
65
66     @Override
67     public Integer getMinElements() {
68         return minElements;
69     }
70
71     @Override
72     public Integer getMaxElements() {
73         return maxElements;
74     }
75
76     @Override
77     public int hashCode() {
78         final int prime = 31;
79         int result = 1;
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();
85         return result;
86     }
87
88     @Override
89     public boolean equals(final Object obj) {
90         if (this == obj) {
91             return true;
92         }
93         if (obj == null) {
94             return false;
95         }
96         if (getClass() != obj.getClass()) {
97             return false;
98         }
99         EffectiveConstraintDefinitionImpl other = (EffectiveConstraintDefinitionImpl) obj;
100         if (!mandatory.equals(other.mandatory)) {
101             return false;
102         }
103         if (!Objects.equals(whenCondition, other.whenCondition)) {
104             return false;
105         }
106         if (!Objects.equals(mustConstraints, other.mustConstraints)) {
107             return false;
108         }
109         if (!Objects.equals(minElements, other.minElements)) {
110             return false;
111         }
112         if (!Objects.equals(maxElements, other.maxElements)) {
113             return false;
114         }
115         return true;
116     }
117
118     @Override
119     public String toString() {
120         StringBuilder sb = new StringBuilder(EffectiveConstraintDefinitionImpl.class.getSimpleName());
121         sb.append("[");
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);
127         sb.append("]");
128         return sb.toString();
129     }
130 }