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.Comparator;
15 import java.util.LinkedHashSet;
16 import java.util.Objects;
18 import java.util.SortedSet;
19 import java.util.TreeSet;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
23 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
24 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
33 import org.opendaylight.yangtools.yang.model.api.stmt.ChoiceStatement;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
36 public final class ChoiceEffectiveStatementImpl extends AbstractEffectiveDataSchemaNode<ChoiceStatement> implements
37 ChoiceSchemaNode, DerivableSchemaNode {
39 * Comparator based on alphabetical order of local name of SchemaNode's
42 private static final Comparator<SchemaNode> SCHEMA_NODE_COMP = (o1, o2) -> {
43 return o1.getQName().compareTo(o2.getQName());
46 private final ChoiceSchemaNode original;
47 private final String defaultCase;
49 private final Set<ChoiceCaseNode> cases;
50 private final Set<AugmentationSchema> augmentations;
52 public ChoiceEffectiveStatementImpl(
53 final StmtContext<QName, ChoiceStatement, EffectiveStatement<QName, ChoiceStatement>> ctx) {
55 this.original = ctx.getOriginalCtx() == null ? null : (ChoiceSchemaNode) ctx.getOriginalCtx().buildEffective();
57 DefaultEffectiveStatementImpl defaultStmt = firstEffective(DefaultEffectiveStatementImpl.class);
58 this.defaultCase = (defaultStmt == null) ? null : defaultStmt.argument();
60 // initSubstatementCollectionsAndFields
61 Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements = effectiveSubstatements();
62 Set<AugmentationSchema> augmentationsInit = new LinkedHashSet<>();
63 SortedSet<ChoiceCaseNode> casesInit = new TreeSet<>(SCHEMA_NODE_COMP);
65 for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
66 if (effectiveStatement instanceof AugmentationSchema) {
67 AugmentationSchema augmentationSchema = (AugmentationSchema) effectiveStatement;
68 augmentationsInit.add(augmentationSchema);
70 if (effectiveStatement instanceof ChoiceCaseNode) {
71 ChoiceCaseNode choiceCaseNode = (ChoiceCaseNode) effectiveStatement;
72 casesInit.add(choiceCaseNode);
74 if (effectiveStatement instanceof AnyXmlSchemaNode || effectiveStatement instanceof ContainerSchemaNode
75 || effectiveStatement instanceof ListSchemaNode || effectiveStatement instanceof LeafListSchemaNode
76 || effectiveStatement instanceof LeafSchemaNode) {
78 DataSchemaNode dataSchemaNode = (DataSchemaNode) effectiveStatement;
79 ChoiceCaseNode shorthandCase = new CaseShorthandImpl(dataSchemaNode);
80 casesInit.add(shorthandCase);
82 if (dataSchemaNode.isAugmenting() && !this.augmenting) {
83 resetAugmenting(dataSchemaNode);
88 this.augmentations = ImmutableSet.copyOf(augmentationsInit);
89 this.cases = ImmutableSet.copyOf(casesInit);
92 private static void resetAugmenting(final DataSchemaNode dataSchemaNode) {
93 if (dataSchemaNode instanceof LeafEffectiveStatementImpl) {
94 LeafEffectiveStatementImpl leaf = (LeafEffectiveStatementImpl) dataSchemaNode;
95 leaf.augmenting = false;
96 } else if (dataSchemaNode instanceof ContainerEffectiveStatementImpl) {
97 ContainerEffectiveStatementImpl container = (ContainerEffectiveStatementImpl) dataSchemaNode;
98 container.augmenting = false;
99 } else if (dataSchemaNode instanceof LeafListEffectiveStatementImpl) {
100 LeafListEffectiveStatementImpl leafList = (LeafListEffectiveStatementImpl) dataSchemaNode;
101 leafList.augmenting = false;
102 } else if (dataSchemaNode instanceof ListEffectiveStatementImpl) {
103 ListEffectiveStatementImpl list = (ListEffectiveStatementImpl) dataSchemaNode;
104 list.augmenting = false;
105 } else if (dataSchemaNode instanceof AnyXmlEffectiveStatementImpl) {
106 AnyXmlEffectiveStatementImpl anyXml = (AnyXmlEffectiveStatementImpl) dataSchemaNode;
107 anyXml.augmenting = false;
112 public Optional<ChoiceSchemaNode> getOriginal() {
113 return Optional.fromNullable(original);
117 public Set<AugmentationSchema> getAvailableAugmentations() {
118 return augmentations;
122 public Set<ChoiceCaseNode> getCases() {
127 public ChoiceCaseNode getCaseNodeByName(final QName name) {
128 Preconditions.checkArgument(name != null, "Choice Case QName cannot be NULL!");
130 for (final ChoiceCaseNode caseNode : cases) {
131 if (caseNode != null && name.equals(caseNode.getQName())) {
139 public ChoiceCaseNode getCaseNodeByName(final String name) {
140 Preconditions.checkArgument(name != null, "Choice Case string Name cannot be NULL!");
142 for (final ChoiceCaseNode caseNode : cases) {
143 if (caseNode != null && (caseNode.getQName() != null) && name.equals(caseNode.getQName().getLocalName())) {
151 public String getDefaultCase() {
156 public int hashCode() {
157 final int prime = 31;
159 result = prime * result + Objects.hashCode(getQName());
160 result = prime * result + Objects.hashCode(getPath());
165 public boolean equals(final Object obj) {
172 if (getClass() != obj.getClass()) {
175 ChoiceEffectiveStatementImpl other = (ChoiceEffectiveStatementImpl) obj;
176 return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
180 public String toString() {
181 return ChoiceEffectiveStatementImpl.class.getSimpleName() + "[" +
182 "qname=" + getQName() +