Share Integer.MAX_VALUE
[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 static final Integer UNBOUNDED_INT = Integer.MAX_VALUE;
20     private static final String UNBOUNDED_STR = "unbounded";
21     private final RevisionAwareXPath whenCondition;
22     private final Set<MustDefinition> mustConstraints;
23     private final Boolean mandatory;
24     private final Integer minElements;
25     private final Integer maxElements;
26
27     public EffectiveConstraintDefinitionImpl(final EffectiveStatementBase<?, ?> parent) {
28
29         WhenEffectiveStatementImpl firstWhenStmt = parent.firstEffective(WhenEffectiveStatementImpl.class);
30         this.whenCondition = (firstWhenStmt == null) ? null : firstWhenStmt.argument();
31
32         MinElementsEffectiveStatementImpl firstMinElementsStmt = parent
33                 .firstEffective(MinElementsEffectiveStatementImpl.class);
34         this.minElements = (firstMinElementsStmt == null) ? 0 : firstMinElementsStmt.argument();
35
36         MaxElementsEffectiveStatementImpl firstMaxElementsStmt = parent
37                 .firstEffective(MaxElementsEffectiveStatementImpl.class);
38         String maxElementsArg = (firstMaxElementsStmt == null) ? UNBOUNDED_STR : firstMaxElementsStmt.argument();
39         if (UNBOUNDED_STR.equals(maxElementsArg)) {
40             this.maxElements = UNBOUNDED_INT;
41         } else {
42             this.maxElements = Integer.valueOf(maxElementsArg);
43         }
44
45         MandatoryEffectiveStatementImpl firstMandatoryStmt = parent
46                 .firstEffective(MandatoryEffectiveStatementImpl.class);
47         this.mandatory = (firstMandatoryStmt == null) ? minElements > 0 : firstMandatoryStmt.argument();
48
49         Collection<MustDefinition> mustSubstatements = parent.allSubstatementsOfType(MustDefinition.class);
50         this.mustConstraints = ImmutableSet.copyOf(mustSubstatements);
51     }
52
53     @Override
54     public RevisionAwareXPath getWhenCondition() {
55         return whenCondition;
56     }
57
58     @Override
59     public Set<MustDefinition> getMustConstraints() {
60         return mustConstraints;
61     }
62
63     @Override
64     public boolean isMandatory() {
65         return mandatory;
66     }
67
68     @Override
69     public Integer getMinElements() {
70         return minElements;
71     }
72
73     @Override
74     public Integer getMaxElements() {
75         return maxElements;
76     }
77
78     @Override
79     public int hashCode() {
80         final int prime = 31;
81         int result = 1;
82         result = prime * result + Objects.hashCode(whenCondition);
83         result = prime * result + Objects.hashCode(mustConstraints);
84         result = prime * result + Objects.hashCode(minElements);
85         result = prime * result + Objects.hashCode(maxElements);
86         result = prime * result + mandatory.hashCode();
87         return result;
88     }
89
90     @Override
91     public boolean equals(final Object obj) {
92         if (this == obj) {
93             return true;
94         }
95         if (obj == null) {
96             return false;
97         }
98         if (getClass() != obj.getClass()) {
99             return false;
100         }
101         EffectiveConstraintDefinitionImpl other = (EffectiveConstraintDefinitionImpl) obj;
102         if (!mandatory.equals(other.mandatory)) {
103             return false;
104         }
105         if (!Objects.equals(whenCondition, other.whenCondition)) {
106             return false;
107         }
108         if (!Objects.equals(mustConstraints, other.mustConstraints)) {
109             return false;
110         }
111         if (!Objects.equals(minElements, other.minElements)) {
112             return false;
113         }
114         if (!Objects.equals(maxElements, other.maxElements)) {
115             return false;
116         }
117         return true;
118     }
119
120     @Override
121     public String toString() {
122         StringBuilder sb = new StringBuilder(EffectiveConstraintDefinitionImpl.class.getSimpleName());
123         sb.append("[");
124         sb.append("whenCondition=").append(whenCondition);
125         sb.append(", mustConstraints=").append(mustConstraints);
126         sb.append(", mandatory=").append(mandatory);
127         sb.append(", minElements=").append(minElements);
128         sb.append(", maxElements=").append(maxElements);
129         sb.append("]");
130         return sb.toString();
131     }
132 }