Make getOriginalCtx() give out an Optional
[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.ImmutableSet;
13 import java.util.Comparator;
14 import java.util.LinkedHashSet;
15 import java.util.Objects;
16 import java.util.Set;
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.AugmentationSchema;
21 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
22 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
27 import org.opendaylight.yangtools.yang.model.api.stmt.ChoiceStatement;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
29 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangValidationBundles;
30
31 public final class ChoiceEffectiveStatementImpl extends AbstractEffectiveDataSchemaNode<ChoiceStatement> implements
32         ChoiceSchemaNode, DerivableSchemaNode {
33     /**
34      * Comparator based on alphabetical order of local name of SchemaNode's
35      * qname.
36      */
37     private static final Comparator<SchemaNode> SCHEMA_NODE_COMP = (o1, o2) -> o1.getQName().compareTo(o2.getQName());
38
39     private final ChoiceSchemaNode original;
40     private final String defaultCase;
41
42     private final Set<ChoiceCaseNode> cases;
43     private final Set<AugmentationSchema> augmentations;
44
45     public ChoiceEffectiveStatementImpl(
46             final StmtContext<QName, ChoiceStatement, EffectiveStatement<QName, ChoiceStatement>> ctx) {
47         super(ctx);
48         this.original = (ChoiceSchemaNode) ctx.getOriginalCtx().map(StmtContext::buildEffective).orElse(null);
49
50         final DefaultEffectiveStatementImpl defaultStmt = firstEffective(DefaultEffectiveStatementImpl.class);
51         this.defaultCase = defaultStmt == null ? null : defaultStmt.argument();
52
53         // initSubstatementCollectionsAndFields
54         final Set<AugmentationSchema> augmentationsInit = new LinkedHashSet<>();
55         final SortedSet<ChoiceCaseNode> casesInit = new TreeSet<>(SCHEMA_NODE_COMP);
56
57         for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
58             if (effectiveStatement instanceof AugmentationSchema) {
59                 final AugmentationSchema augmentationSchema = (AugmentationSchema) effectiveStatement;
60                 augmentationsInit.add(augmentationSchema);
61             }
62             if (effectiveStatement instanceof ChoiceCaseNode) {
63                 final ChoiceCaseNode choiceCaseNode = (ChoiceCaseNode) effectiveStatement;
64                 casesInit.add(choiceCaseNode);
65             }
66             if (YangValidationBundles.SUPPORTED_CASE_SHORTHANDS.contains(effectiveStatement.statementDefinition())) {
67                 final DataSchemaNode dataSchemaNode = (DataSchemaNode) effectiveStatement;
68                 final ChoiceCaseNode shorthandCase = new CaseShorthandImpl(dataSchemaNode);
69                 casesInit.add(shorthandCase);
70                 if (dataSchemaNode.isAugmenting() && !this.augmenting) {
71                     resetAugmenting(dataSchemaNode);
72                 }
73             }
74         }
75
76         this.augmentations = ImmutableSet.copyOf(augmentationsInit);
77         this.cases = ImmutableSet.copyOf(casesInit);
78     }
79
80     private static void resetAugmenting(final DataSchemaNode dataSchemaNode) {
81         if (dataSchemaNode instanceof LeafEffectiveStatementImpl) {
82             final LeafEffectiveStatementImpl leaf = (LeafEffectiveStatementImpl) dataSchemaNode;
83             leaf.augmenting = false;
84         } else if (dataSchemaNode instanceof ContainerEffectiveStatementImpl) {
85             final ContainerEffectiveStatementImpl container = (ContainerEffectiveStatementImpl) dataSchemaNode;
86             container.augmenting = false;
87         } else if (dataSchemaNode instanceof LeafListEffectiveStatementImpl) {
88             final LeafListEffectiveStatementImpl leafList = (LeafListEffectiveStatementImpl) dataSchemaNode;
89             leafList.augmenting = false;
90         } else if (dataSchemaNode instanceof ListEffectiveStatementImpl) {
91             final ListEffectiveStatementImpl list = (ListEffectiveStatementImpl) dataSchemaNode;
92             list.augmenting = false;
93         } else if (dataSchemaNode instanceof AnyXmlEffectiveStatementImpl) {
94             final AnyXmlEffectiveStatementImpl anyXml = (AnyXmlEffectiveStatementImpl) dataSchemaNode;
95             anyXml.augmenting = false;
96         }
97     }
98
99     @Override
100     public Optional<ChoiceSchemaNode> getOriginal() {
101         return Optional.fromNullable(original);
102     }
103
104     @Override
105     public Set<AugmentationSchema> getAvailableAugmentations() {
106         return augmentations;
107     }
108
109     @Override
110     public Set<ChoiceCaseNode> getCases() {
111         return cases;
112     }
113
114     @Override
115     public ChoiceCaseNode getCaseNodeByName(final QName name) {
116         Preconditions.checkArgument(name != null, "Choice Case QName cannot be NULL!");
117
118         for (final ChoiceCaseNode caseNode : cases) {
119             if (caseNode != null && name.equals(caseNode.getQName())) {
120                 return caseNode;
121             }
122         }
123         return null;
124     }
125
126     @Override
127     public ChoiceCaseNode getCaseNodeByName(final String name) {
128         Preconditions.checkArgument(name != null, "Choice Case string Name cannot be NULL!");
129
130         for (final ChoiceCaseNode caseNode : cases) {
131             if (caseNode != null && caseNode.getQName() != null && name.equals(caseNode.getQName().getLocalName())) {
132                 return caseNode;
133             }
134         }
135         return null;
136     }
137
138     @Override
139     public String getDefaultCase() {
140         return defaultCase;
141     }
142
143     @Override
144     public int hashCode() {
145         final int prime = 31;
146         int result = 1;
147         result = prime * result + Objects.hashCode(getQName());
148         result = prime * result + Objects.hashCode(getPath());
149         return result;
150     }
151
152     @Override
153     public boolean equals(final Object obj) {
154         if (this == obj) {
155             return true;
156         }
157         if (obj == null) {
158             return false;
159         }
160         if (getClass() != obj.getClass()) {
161             return false;
162         }
163         final ChoiceEffectiveStatementImpl other = (ChoiceEffectiveStatementImpl) obj;
164         return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
165     }
166
167     @Override
168     public String toString() {
169         return ChoiceEffectiveStatementImpl.class.getSimpleName() + "[" +
170                 "qname=" + getQName() +
171                 "]";
172     }
173 }