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