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