Use Objects.hashCode()
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / ChoiceEffectiveStatementImpl.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.Optional;
11 import com.google.common.collect.ImmutableList;
12 import com.google.common.collect.ImmutableSet;
13 import java.util.Collection;
14 import java.util.HashSet;
15 import java.util.LinkedList;
16 import java.util.List;
17 import java.util.Objects;
18 import java.util.Set;
19 import java.util.SortedSet;
20 import java.util.TreeSet;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
24 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
25 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
27 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
34 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
36 import org.opendaylight.yangtools.yang.model.api.stmt.ChoiceStatement;
37 import org.opendaylight.yangtools.yang.parser.builder.util.Comparators;
38 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
39 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.TypeOfCopy;
40 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
41
42 public class ChoiceEffectiveStatementImpl extends
43         AbstractEffectiveDocumentedNode<QName, ChoiceStatement> implements
44         ChoiceSchemaNode, DerivableSchemaNode {
45     private final QName qname;
46     private final SchemaPath path;
47
48     boolean augmenting;
49     boolean addedByUses;
50     ChoiceSchemaNode original;
51     boolean configuration = true;
52     ConstraintDefinition constraints;
53     String defaultCase;
54
55     ImmutableSet<ChoiceCaseNode> cases;
56     ImmutableSet<AugmentationSchema> augmentations;
57     ImmutableList<UnknownSchemaNode> unknownNodes;
58
59     public ChoiceEffectiveStatementImpl(
60             final StmtContext<QName, ChoiceStatement, EffectiveStatement<QName, ChoiceStatement>> ctx) {
61         super(ctx);
62
63         this.qname = ctx.getStatementArgument();
64         this.path = Utils.getSchemaPath(ctx);
65         this.constraints = new EffectiveConstraintDefinitionImpl(this);
66
67         initCopyType(ctx);
68         initSubstatementCollectionsAndFields();
69     }
70
71     private void initCopyType(
72             final StmtContext<QName, ChoiceStatement, EffectiveStatement<QName, ChoiceStatement>> ctx) {
73
74         List<TypeOfCopy> copyTypesFromOriginal = ctx.getCopyHistory();
75
76         if (copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_AUGMENTATION)) {
77             augmenting = true;
78         }
79         if (copyTypesFromOriginal.contains(TypeOfCopy.ADDED_BY_USES)) {
80             addedByUses = true;
81         }
82         if (copyTypesFromOriginal
83                 .contains(TypeOfCopy.ADDED_BY_USES_AUGMENTATION)) {
84             addedByUses = augmenting = true;
85         }
86
87         if (ctx.getOriginalCtx() != null) {
88             original = (ChoiceSchemaNode) ctx.getOriginalCtx().buildEffective();
89         }
90     }
91
92     private void initSubstatementCollectionsAndFields() {
93         Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements = effectiveSubstatements();
94
95         List<UnknownSchemaNode> unknownNodesInit = new LinkedList<>();
96         Set<AugmentationSchema> augmentationsInit = new HashSet<>();
97         SortedSet<ChoiceCaseNode> casesInit = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
98
99         boolean configurationInit = false;
100         boolean defaultInit = false;
101         for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
102             if (effectiveStatement instanceof UnknownSchemaNode) {
103                 UnknownSchemaNode unknownNode = (UnknownSchemaNode) effectiveStatement;
104                 unknownNodesInit.add(unknownNode);
105             }
106             if (effectiveStatement instanceof AugmentationSchema) {
107                 AugmentationSchema augmentationSchema = (AugmentationSchema) effectiveStatement;
108                 augmentationsInit.add(augmentationSchema);
109             }
110             if (effectiveStatement instanceof ChoiceCaseNode) {
111                 ChoiceCaseNode choiceCaseNode = (ChoiceCaseNode) effectiveStatement;
112                 casesInit.add(choiceCaseNode);
113             }
114             if (effectiveStatement instanceof AnyXmlSchemaNode
115                     || effectiveStatement instanceof ContainerSchemaNode
116                     || effectiveStatement instanceof ListSchemaNode
117                     || effectiveStatement instanceof LeafListSchemaNode
118                     || effectiveStatement instanceof LeafSchemaNode) {
119
120                 DataSchemaNode dataSchemaNode = (DataSchemaNode) effectiveStatement;
121                 ChoiceCaseNode shorthandCase = new CaseShorthandImpl(
122                         dataSchemaNode);
123                 casesInit.add(shorthandCase);
124
125                 if (dataSchemaNode.isAugmenting() && !this.augmenting) {
126                     resetAugmenting(dataSchemaNode);
127                 }
128             }
129             if (!configurationInit
130                     && effectiveStatement instanceof ConfigEffectiveStatementImpl) {
131                 ConfigEffectiveStatementImpl configStmt = (ConfigEffectiveStatementImpl) effectiveStatement;
132                 this.configuration = configStmt.argument();
133                 configurationInit = true;
134             }
135             if (!defaultInit
136                     && effectiveStatement instanceof DefaultEffectiveStatementImpl) {
137                 DefaultEffectiveStatementImpl defaultCaseStmt = (DefaultEffectiveStatementImpl) effectiveStatement;
138                 this.defaultCase = defaultCaseStmt.argument();
139                 defaultInit = true;
140             }
141         }
142
143         this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
144         this.augmentations = ImmutableSet.copyOf(augmentationsInit);
145         this.cases = ImmutableSet.copyOf(casesInit);
146     }
147
148     private static void resetAugmenting(final DataSchemaNode dataSchemaNode) {
149         if (dataSchemaNode instanceof LeafEffectiveStatementImpl) {
150             LeafEffectiveStatementImpl leaf = (LeafEffectiveStatementImpl) dataSchemaNode;
151             leaf.augmenting = false;
152         } else if (dataSchemaNode instanceof ContainerEffectiveStatementImpl) {
153             ContainerEffectiveStatementImpl container = (ContainerEffectiveStatementImpl) dataSchemaNode;
154             container.augmenting = false;
155         } else if (dataSchemaNode instanceof LeafListEffectiveStatementImpl) {
156             LeafListEffectiveStatementImpl leafList = (LeafListEffectiveStatementImpl) dataSchemaNode;
157             leafList.augmenting = false;
158         } else if (dataSchemaNode instanceof ListEffectiveStatementImpl) {
159             ListEffectiveStatementImpl list = (ListEffectiveStatementImpl) dataSchemaNode;
160             list.augmenting = false;
161         } else if (dataSchemaNode instanceof AnyXmlEffectiveStatementImpl) {
162             AnyXmlEffectiveStatementImpl anyXml = (AnyXmlEffectiveStatementImpl) dataSchemaNode;
163             anyXml.augmenting = false;
164         }
165     }
166
167     @Override
168     public QName getQName() {
169         return qname;
170     }
171
172     @Override
173     public SchemaPath getPath() {
174         return path;
175     }
176
177     @Override
178     public boolean isAugmenting() {
179         return augmenting;
180     }
181
182     @Override
183     public boolean isAddedByUses() {
184         return addedByUses;
185     }
186
187     @Override
188     public Optional<ChoiceSchemaNode> getOriginal() {
189         return Optional.fromNullable(original);
190     }
191
192     @Override
193     public boolean isConfiguration() {
194         return configuration;
195     }
196
197     @Override
198     public ConstraintDefinition getConstraints() {
199         return constraints;
200     }
201
202     @Override
203     public Set<AugmentationSchema> getAvailableAugmentations() {
204         return augmentations;
205     }
206
207     @Override
208     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
209         return unknownNodes;
210     }
211
212     @Override
213     public Set<ChoiceCaseNode> getCases() {
214         return cases;
215     }
216
217     @Override
218     public ChoiceCaseNode getCaseNodeByName(final QName name) {
219         if (name == null) {
220             throw new IllegalArgumentException(
221                     "Choice Case QName cannot be NULL!");
222         }
223         for (final ChoiceCaseNode caseNode : cases) {
224             if (caseNode != null && name.equals(caseNode.getQName())) {
225                 return caseNode;
226             }
227         }
228         return null;
229     }
230
231     @Override
232     public ChoiceCaseNode getCaseNodeByName(final String name) {
233         if (name == null) {
234             throw new IllegalArgumentException(
235                     "Choice Case string Name cannot be NULL!");
236         }
237         for (final ChoiceCaseNode caseNode : cases) {
238             if (caseNode != null && (caseNode.getQName() != null)
239                     && name.equals(caseNode.getQName().getLocalName())) {
240                 return caseNode;
241             }
242         }
243         return null;
244     }
245
246     @Override
247     public String getDefaultCase() {
248         return defaultCase;
249     }
250
251     @Override
252     public int hashCode() {
253         final int prime = 31;
254         int result = 1;
255         result = prime * result + Objects.hashCode(qname);
256         result = prime * result + Objects.hashCode(path);
257         return result;
258     }
259
260     @Override
261     public boolean equals(final Object obj) {
262         if (this == obj) {
263             return true;
264         }
265         if (obj == null) {
266             return false;
267         }
268         if (getClass() != obj.getClass()) {
269             return false;
270         }
271         ChoiceEffectiveStatementImpl other = (ChoiceEffectiveStatementImpl) obj;
272         if (qname == null) {
273             if (other.qname != null) {
274                 return false;
275             }
276         } else if (!qname.equals(other.qname)) {
277             return false;
278         }
279         if (path == null) {
280             if (other.path != null) {
281                 return false;
282             }
283         } else if (!path.equals(other.path)) {
284             return false;
285         }
286         return true;
287     }
288
289     @Override
290     public String toString() {
291         StringBuilder sb = new StringBuilder(
292                 ChoiceEffectiveStatementImpl.class.getSimpleName());
293         sb.append("[");
294         sb.append("qname=").append(qname);
295         sb.append("]");
296         return sb.toString();
297     }
298
299 }