Refactor yang-model-api child traversal return types
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / choice / 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.rfc7950.stmt.choice;
9
10 import com.google.common.collect.ImmutableSet;
11 import com.google.common.collect.ImmutableSortedMap;
12 import java.util.Collection;
13 import java.util.LinkedHashSet;
14 import java.util.Objects;
15 import java.util.Optional;
16 import java.util.Set;
17 import java.util.SortedMap;
18 import java.util.TreeMap;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
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.ChoiceEffectiveStatement;
26 import org.opendaylight.yangtools.yang.model.api.stmt.ChoiceStatement;
27 import org.opendaylight.yangtools.yang.model.api.stmt.DefaultEffectiveStatement;
28 import org.opendaylight.yangtools.yang.model.api.stmt.MandatoryEffectiveStatement;
29 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.AbstractEffectiveDataSchemaNode;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
32 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
33
34 final class ChoiceEffectiveStatementImpl extends AbstractEffectiveDataSchemaNode<ChoiceStatement>
35         implements ChoiceEffectiveStatement, ChoiceSchemaNode, DerivableSchemaNode {
36
37     private final ImmutableSet<AugmentationSchemaNode> augmentations;
38     private final ImmutableSortedMap<QName, CaseSchemaNode> cases;
39     private final CaseSchemaNode defaultCase;
40     private final ChoiceSchemaNode original;
41     private final boolean mandatory;
42
43     ChoiceEffectiveStatementImpl(final StmtContext<QName, ChoiceStatement, ChoiceEffectiveStatement> ctx) {
44         super(ctx);
45         this.original = (ChoiceSchemaNode) ctx.getOriginalCtx().map(StmtContext::buildEffective).orElse(null);
46
47         // initSubstatementCollectionsAndFields
48         final Set<AugmentationSchemaNode> augmentationsInit = new LinkedHashSet<>();
49         final SortedMap<QName, CaseSchemaNode> casesInit = new TreeMap<>();
50
51         for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
52             if (effectiveStatement instanceof AugmentationSchemaNode) {
53                 final AugmentationSchemaNode augmentationSchema = (AugmentationSchemaNode) effectiveStatement;
54                 augmentationsInit.add(augmentationSchema);
55             }
56             if (effectiveStatement instanceof CaseSchemaNode) {
57                 final CaseSchemaNode choiceCaseNode = (CaseSchemaNode) effectiveStatement;
58                 // FIXME: we may be overwriting a previous entry, is that really okay?
59                 casesInit.put(choiceCaseNode.getQName(), choiceCaseNode);
60             }
61         }
62
63         this.augmentations = ImmutableSet.copyOf(augmentationsInit);
64         this.cases = ImmutableSortedMap.copyOfSorted(casesInit);
65
66         final Optional<String> defaultArg = findFirstEffectiveSubstatementArgument(DefaultEffectiveStatement.class);
67         if (defaultArg.isPresent()) {
68             final String arg = defaultArg.get();
69             final QName qname;
70             try {
71                 qname = QName.create(getQName(), arg);
72             } catch (IllegalArgumentException e) {
73                 throw new SourceException(ctx.getStatementSourceReference(), "Default statement has invalid name '%s'",
74                     arg, e);
75             }
76
77             // FIXME: this does not work with submodules, as they are
78             defaultCase = InferenceException.throwIfNull(cases.get(qname), ctx.getStatementSourceReference(),
79                 "Default statement refers to missing case %s", qname);
80         } else {
81             defaultCase = null;
82         }
83
84         mandatory = findFirstEffectiveSubstatementArgument(MandatoryEffectiveStatement.class).orElse(Boolean.FALSE)
85                 .booleanValue();
86     }
87
88     @Override
89     public Optional<ChoiceSchemaNode> getOriginal() {
90         return Optional.ofNullable(original);
91     }
92
93     @Override
94     public Collection<? extends AugmentationSchemaNode> getAvailableAugmentations() {
95         return augmentations;
96     }
97
98     @Override
99     public SortedMap<QName, CaseSchemaNode> getCases() {
100         return cases;
101     }
102
103     @Override
104     public Optional<CaseSchemaNode> getDefaultCase() {
105         return Optional.ofNullable(defaultCase);
106     }
107
108     @Override
109     public boolean isMandatory() {
110         return mandatory;
111     }
112
113     @Override
114     public int hashCode() {
115         final int prime = 31;
116         int result = 1;
117         result = prime * result + Objects.hashCode(getQName());
118         result = prime * result + Objects.hashCode(getPath());
119         return result;
120     }
121
122     @Override
123     public boolean equals(final Object obj) {
124         if (this == obj) {
125             return true;
126         }
127         if (obj == null) {
128             return false;
129         }
130         if (getClass() != obj.getClass()) {
131             return false;
132         }
133         final ChoiceEffectiveStatementImpl other = (ChoiceEffectiveStatementImpl) obj;
134         return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
135     }
136
137     @Override
138     public String toString() {
139         return ChoiceEffectiveStatementImpl.class.getSimpleName() + "["
140                 + "qname=" + getQName()
141                 + "]";
142     }
143 }