2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.ImmutableSet;
13 import java.util.Collection;
14 import java.util.HashSet;
15 import java.util.Objects;
17 import java.util.SortedSet;
18 import java.util.TreeSet;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
22 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
23 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
31 import org.opendaylight.yangtools.yang.model.api.stmt.ChoiceStatement;
32 import org.opendaylight.yangtools.yang.parser.builder.util.Comparators;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
35 public final class ChoiceEffectiveStatementImpl extends AbstractEffectiveDataSchemaNode<ChoiceStatement> implements
36 ChoiceSchemaNode, DerivableSchemaNode {
38 private final ChoiceSchemaNode original;
39 private final String defaultCase;
41 private final Set<ChoiceCaseNode> cases;
42 private final Set<AugmentationSchema> augmentations;
44 public ChoiceEffectiveStatementImpl(
45 final StmtContext<QName, ChoiceStatement, EffectiveStatement<QName, ChoiceStatement>> ctx) {
47 this.original = ctx.getOriginalCtx() == null ? null : (ChoiceSchemaNode) ctx.getOriginalCtx().buildEffective();
49 DefaultEffectiveStatementImpl defaultStmt = firstEffective(DefaultEffectiveStatementImpl.class);
50 this.defaultCase = (defaultStmt == null) ? null : defaultStmt.argument();
52 // initSubstatementCollectionsAndFields
53 Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements = effectiveSubstatements();
54 Set<AugmentationSchema> augmentationsInit = new HashSet<>();
55 SortedSet<ChoiceCaseNode> casesInit = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
57 for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
58 if (effectiveStatement instanceof AugmentationSchema) {
59 AugmentationSchema augmentationSchema = (AugmentationSchema) effectiveStatement;
60 augmentationsInit.add(augmentationSchema);
62 if (effectiveStatement instanceof ChoiceCaseNode) {
63 ChoiceCaseNode choiceCaseNode = (ChoiceCaseNode) effectiveStatement;
64 casesInit.add(choiceCaseNode);
66 if (effectiveStatement instanceof AnyXmlSchemaNode || effectiveStatement instanceof ContainerSchemaNode
67 || effectiveStatement instanceof ListSchemaNode || effectiveStatement instanceof LeafListSchemaNode
68 || effectiveStatement instanceof LeafSchemaNode) {
70 DataSchemaNode dataSchemaNode = (DataSchemaNode) effectiveStatement;
71 ChoiceCaseNode shorthandCase = new CaseShorthandImpl(dataSchemaNode);
72 casesInit.add(shorthandCase);
74 if (dataSchemaNode.isAugmenting() && !this.augmenting) {
75 resetAugmenting(dataSchemaNode);
80 this.augmentations = ImmutableSet.copyOf(augmentationsInit);
81 this.cases = ImmutableSet.copyOf(casesInit);
84 private static void resetAugmenting(final DataSchemaNode dataSchemaNode) {
85 if (dataSchemaNode instanceof LeafEffectiveStatementImpl) {
86 LeafEffectiveStatementImpl leaf = (LeafEffectiveStatementImpl) dataSchemaNode;
87 leaf.augmenting = false;
88 } else if (dataSchemaNode instanceof ContainerEffectiveStatementImpl) {
89 ContainerEffectiveStatementImpl container = (ContainerEffectiveStatementImpl) dataSchemaNode;
90 container.augmenting = false;
91 } else if (dataSchemaNode instanceof LeafListEffectiveStatementImpl) {
92 LeafListEffectiveStatementImpl leafList = (LeafListEffectiveStatementImpl) dataSchemaNode;
93 leafList.augmenting = false;
94 } else if (dataSchemaNode instanceof ListEffectiveStatementImpl) {
95 ListEffectiveStatementImpl list = (ListEffectiveStatementImpl) dataSchemaNode;
96 list.augmenting = false;
97 } else if (dataSchemaNode instanceof AnyXmlEffectiveStatementImpl) {
98 AnyXmlEffectiveStatementImpl anyXml = (AnyXmlEffectiveStatementImpl) dataSchemaNode;
99 anyXml.augmenting = false;
104 public Optional<ChoiceSchemaNode> getOriginal() {
105 return Optional.fromNullable(original);
109 public Set<AugmentationSchema> getAvailableAugmentations() {
110 return augmentations;
114 public Set<ChoiceCaseNode> getCases() {
119 public ChoiceCaseNode getCaseNodeByName(final QName name) {
120 Preconditions.checkArgument(name != null, "Choice Case QName cannot be NULL!");
122 for (final ChoiceCaseNode caseNode : cases) {
123 if (caseNode != null && name.equals(caseNode.getQName())) {
131 public ChoiceCaseNode getCaseNodeByName(final String name) {
132 Preconditions.checkArgument(name != null, "Choice Case string Name cannot be NULL!");
134 for (final ChoiceCaseNode caseNode : cases) {
135 if (caseNode != null && (caseNode.getQName() != null) && name.equals(caseNode.getQName().getLocalName())) {
143 public String getDefaultCase() {
148 public int hashCode() {
149 final int prime = 31;
151 result = prime * result + Objects.hashCode(getQName());
152 result = prime * result + Objects.hashCode(getPath());
157 public boolean equals(final Object obj) {
164 if (getClass() != obj.getClass()) {
167 ChoiceEffectiveStatementImpl other = (ChoiceEffectiveStatementImpl) obj;
168 return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
172 public String toString() {
173 return ChoiceEffectiveStatementImpl.class.getSimpleName() + "[" +
174 "qname=" + getQName() +