Memory hog: ImmutableSet is appropriate here
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / builder / impl / ConstraintsBuilder.java
1 /*
2  * Copyright (c) 2013 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.builder.impl;
9
10 import java.util.Collections;
11 import java.util.HashSet;
12 import java.util.Set;
13
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 import org.opendaylight.yangtools.yang.model.util.RevisionAwareXPathImpl;
18
19 import com.google.common.collect.ImmutableSet;
20
21 public final class ConstraintsBuilder {
22     private static final int HASH_IF_BOOL_TRUE = 1231;
23     private static final int HASH_IF_BOOL_FALSE = 1237;
24
25     private final String moduleName;
26     private final int line;
27     private final ConstraintDefinitionImpl instance;
28     private final Set<MustDefinition> mustDefinitions;
29     private RevisionAwareXPath whenStmt;
30     private String whenCondition;
31     private boolean mandatory;
32     private Integer min;
33     private Integer max;
34
35     public ConstraintsBuilder(final String moduleName, final int line) {
36         this.moduleName = moduleName;
37         this.line = line;
38         instance = new ConstraintDefinitionImpl();
39         mustDefinitions = new HashSet<MustDefinition>();
40     }
41
42     ConstraintsBuilder(final ConstraintsBuilder b) {
43         this.moduleName = b.getModuleName();
44         this.line = b.getLine();
45         instance = new ConstraintDefinitionImpl();
46         mustDefinitions = new HashSet<MustDefinition>(b.getMustDefinitions());
47         whenCondition = b.getWhenCondition();
48         mandatory = b.isMandatory();
49         min = b.getMinElements();
50         max = b.getMaxElements();
51     }
52
53     ConstraintsBuilder(final String moduleName, final int line, final ConstraintDefinition base) {
54         this.moduleName = moduleName;
55         this.line = line;
56         instance = new ConstraintDefinitionImpl();
57         whenStmt = base.getWhenCondition();
58         mustDefinitions = new HashSet<MustDefinition>(base.getMustConstraints());
59         mandatory = base.isMandatory();
60         min = base.getMinElements();
61         max = base.getMaxElements();
62     }
63
64     public ConstraintDefinition build() {
65         if (whenStmt == null) {
66             if (whenCondition == null) {
67                 whenStmt = null;
68             } else {
69                 whenStmt = new RevisionAwareXPathImpl(whenCondition, false);
70             }
71         }
72         instance.setWhenCondition(whenStmt);
73         instance.setMustConstraints(mustDefinitions);
74         instance.setMandatory(mandatory);
75         instance.setMinElements(min);
76         instance.setMaxElements(max);
77         return instance;
78     }
79
80     public String getModuleName() {
81         return moduleName;
82     }
83
84     public int getLine() {
85         return line;
86     }
87
88     public Integer getMinElements() {
89         return min;
90     }
91
92     public void setMinElements(final Integer minElements) {
93         this.min = minElements;
94     }
95
96     public Integer getMaxElements() {
97         return max;
98     }
99
100     public void setMaxElements(final Integer maxElements) {
101         this.max = maxElements;
102     }
103
104     public Set<MustDefinition> getMustDefinitions() {
105         return mustDefinitions;
106     }
107
108     public void addMustDefinition(final MustDefinition must) {
109         mustDefinitions.add(must);
110     }
111
112     public String getWhenCondition() {
113         return whenCondition;
114     }
115
116     public void addWhenCondition(final String whenCondition) {
117         this.whenCondition = whenCondition;
118     }
119
120     public boolean isMandatory() {
121         return mandatory;
122     }
123
124     public void setMandatory(final boolean mandatory) {
125         this.mandatory = mandatory;
126     }
127
128     private static final class ConstraintDefinitionImpl implements ConstraintDefinition {
129         private RevisionAwareXPath whenCondition;
130         private Set<MustDefinition> mustConstraints = Collections.emptySet();
131         private boolean mandatory;
132         private Integer minElements;
133         private Integer maxElements;
134
135         @Override
136         public RevisionAwareXPath getWhenCondition() {
137             return whenCondition;
138         }
139
140         private void setWhenCondition(final RevisionAwareXPath whenCondition) {
141             this.whenCondition = whenCondition;
142         }
143
144         @Override
145         public Set<MustDefinition> getMustConstraints() {
146             return mustConstraints;
147         }
148
149         private void setMustConstraints(final Set<MustDefinition> mustConstraints) {
150             if (mustConstraints != null) {
151                 this.mustConstraints = ImmutableSet.copyOf(mustConstraints);
152             }
153         }
154
155         @Override
156         public boolean isMandatory() {
157             return mandatory;
158         }
159
160         private void setMandatory(final boolean mandatory) {
161             this.mandatory = mandatory;
162         }
163
164         @Override
165         public Integer getMinElements() {
166             return minElements;
167         }
168
169         private void setMinElements(final Integer minElements) {
170             this.minElements = minElements;
171         }
172
173         @Override
174         public Integer getMaxElements() {
175             return maxElements;
176         }
177
178         private void setMaxElements(final Integer maxElements) {
179             this.maxElements = maxElements;
180         }
181
182         @Override
183         public int hashCode() {
184             final int prime = 31;
185             int result = 1;
186             result = prime * result + ((whenCondition == null) ? 0 : whenCondition.hashCode());
187             result = prime * result + ((mustConstraints == null) ? 0 : mustConstraints.hashCode());
188             result = prime * result + ((minElements == null) ? 0 : minElements.hashCode());
189             result = prime * result + ((maxElements == null) ? 0 : maxElements.hashCode());
190             result = prime * result + (mandatory ? HASH_IF_BOOL_TRUE : HASH_IF_BOOL_FALSE);
191             return result;
192         }
193
194         @Override
195         public boolean equals(final Object obj) {
196             if (this == obj) {
197                 return true;
198             }
199             if (obj == null) {
200                 return false;
201             }
202             if (getClass() != obj.getClass()) {
203                 return false;
204             }
205             ConstraintDefinitionImpl other = (ConstraintDefinitionImpl) obj;
206             if (whenCondition == null) {
207                 if (other.whenCondition != null) {
208                     return false;
209                 }
210             } else if (!whenCondition.equals(other.whenCondition)) {
211                 return false;
212             }
213             if (mustConstraints == null) {
214                 if (other.mustConstraints != null) {
215                     return false;
216                 }
217             } else if (!mustConstraints.equals(other.mustConstraints)) {
218                 return false;
219             }
220             if (mandatory != other.mandatory) {
221                 return false;
222             }
223             if (minElements == null) {
224                 if (other.minElements != null) {
225                     return false;
226                 }
227             } else if (!minElements.equals(other.minElements)) {
228                 return false;
229             }
230             if (maxElements == null) {
231                 if (other.maxElements != null) {
232                     return false;
233                 }
234             } else if (!maxElements.equals(other.maxElements)) {
235                 return false;
236             }
237             return true;
238         }
239
240         @Override
241         public String toString() {
242             StringBuilder sb = new StringBuilder(ConstraintDefinitionImpl.class.getSimpleName());
243             sb.append("[");
244             sb.append("whenCondition=" + whenCondition);
245             sb.append(", mustConstraints=" + mustConstraints);
246             sb.append(", mandatory=" + mandatory);
247             sb.append(", minElements=" + minElements);
248             sb.append(", maxElements=" + maxElements);
249             sb.append("]");
250             return sb.toString();
251         }
252     }
253
254 }