3036302b4127734688a152209fc4fccc1f86e394
[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 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(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 (whenCondition == null) {
101             if (other.whenCondition != null) {
102                 return false;
103             }
104         } else if (!whenCondition.equals(other.whenCondition)) {
105             return false;
106         }
107         if (mustConstraints == null) {
108             if (other.mustConstraints != null) {
109                 return false;
110             }
111         } else if (!mustConstraints.equals(other.mustConstraints)) {
112             return false;
113         }
114         if (!mandatory.equals(other.mandatory)) {
115             return false;
116         }
117         if (minElements == null) {
118             if (other.minElements != null) {
119                 return false;
120             }
121         } else if (!minElements.equals(other.minElements)) {
122             return false;
123         }
124         if (maxElements == null) {
125             if (other.maxElements != null) {
126                 return false;
127             }
128         } else if (!maxElements.equals(other.maxElements)) {
129             return false;
130         }
131         return true;
132     }
133
134     @Override
135     public String toString() {
136         StringBuilder sb = new StringBuilder(
137                 EffectiveConstraintDefinitionImpl.class.getSimpleName());
138         sb.append("[");
139         sb.append("whenCondition=").append(whenCondition);
140         sb.append(", mustConstraints=").append(mustConstraints);
141         sb.append(", mandatory=").append(mandatory);
142         sb.append(", minElements=").append(minElements);
143         sb.append(", maxElements=").append(maxElements);
144         sb.append("]");
145         return sb.toString();
146     }
147 }