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.rfc7950.stmt.choice;
10 import com.google.common.collect.ImmutableSet;
11 import com.google.common.collect.ImmutableSortedMap;
12 import java.util.LinkedHashSet;
13 import java.util.Objects;
14 import java.util.Optional;
16 import java.util.SortedMap;
17 import java.util.TreeMap;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.ChoiceEffectiveStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.ChoiceStatement;
26 import org.opendaylight.yangtools.yang.model.api.stmt.DefaultEffectiveStatement;
27 import org.opendaylight.yangtools.yang.model.api.stmt.MandatoryEffectiveStatement;
28 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.AbstractEffectiveDataSchemaNode;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
31 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
33 final class ChoiceEffectiveStatementImpl extends AbstractEffectiveDataSchemaNode<ChoiceStatement>
34 implements ChoiceEffectiveStatement, ChoiceSchemaNode, DerivableSchemaNode {
36 private final ImmutableSet<AugmentationSchemaNode> augmentations;
37 private final ImmutableSortedMap<QName, CaseSchemaNode> cases;
38 private final CaseSchemaNode defaultCase;
39 private final ChoiceSchemaNode original;
40 private final boolean mandatory;
42 ChoiceEffectiveStatementImpl(
43 final StmtContext<QName, ChoiceStatement, EffectiveStatement<QName, ChoiceStatement>> ctx) {
45 this.original = (ChoiceSchemaNode) ctx.getOriginalCtx().map(StmtContext::buildEffective).orElse(null);
47 // initSubstatementCollectionsAndFields
48 final Set<AugmentationSchemaNode> augmentationsInit = new LinkedHashSet<>();
49 final SortedMap<QName, CaseSchemaNode> casesInit = new TreeMap<>();
51 for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
52 if (effectiveStatement instanceof AugmentationSchemaNode) {
53 final AugmentationSchemaNode augmentationSchema = (AugmentationSchemaNode) effectiveStatement;
54 augmentationsInit.add(augmentationSchema);
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);
63 this.augmentations = ImmutableSet.copyOf(augmentationsInit);
64 this.cases = ImmutableSortedMap.copyOfSorted(casesInit);
66 final Optional<String> defaultArg = findFirstEffectiveSubstatementArgument(DefaultEffectiveStatement.class);
67 if (defaultArg.isPresent()) {
68 final String arg = defaultArg.get();
71 qname = QName.create(getQName(), arg);
72 } catch (IllegalArgumentException e) {
73 throw new SourceException(ctx.getStatementSourceReference(), "Default statement has invalid name '%s'",
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);
84 mandatory = findFirstEffectiveSubstatementArgument(MandatoryEffectiveStatement.class).orElse(Boolean.FALSE)
89 public Optional<ChoiceSchemaNode> getOriginal() {
90 return Optional.ofNullable(original);
94 public Set<AugmentationSchemaNode> getAvailableAugmentations() {
99 public SortedMap<QName, CaseSchemaNode> getCases() {
104 public Optional<CaseSchemaNode> getDefaultCase() {
105 return Optional.ofNullable(defaultCase);
109 public boolean isMandatory() {
114 public int hashCode() {
115 final int prime = 31;
117 result = prime * result + Objects.hashCode(getQName());
118 result = prime * result + Objects.hashCode(getPath());
123 public boolean equals(final Object obj) {
130 if (getClass() != obj.getClass()) {
133 final ChoiceEffectiveStatementImpl other = (ChoiceEffectiveStatementImpl) obj;
134 return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
138 public String toString() {
139 return ChoiceEffectiveStatementImpl.class.getSimpleName() + "["
140 + "qname=" + getQName()