Remove hashCode()/equals() from SchemaNode implementations
[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.Optional;
15 import java.util.Set;
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;
32
33 final class ChoiceEffectiveStatementImpl extends AbstractEffectiveDataSchemaNode<ChoiceStatement>
34         implements ChoiceEffectiveStatement, ChoiceSchemaNode, DerivableSchemaNode {
35
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;
41
42     ChoiceEffectiveStatementImpl(final StmtContext<QName, ChoiceStatement, ChoiceEffectiveStatement> ctx) {
43         super(ctx);
44         this.original = (ChoiceSchemaNode) ctx.getOriginalCtx().map(StmtContext::buildEffective).orElse(null);
45
46         // initSubstatementCollectionsAndFields
47         final Set<AugmentationSchemaNode> augmentationsInit = new LinkedHashSet<>();
48         final SortedMap<QName, CaseSchemaNode> casesInit = new TreeMap<>();
49
50         for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
51             if (effectiveStatement instanceof AugmentationSchemaNode) {
52                 final AugmentationSchemaNode augmentationSchema = (AugmentationSchemaNode) effectiveStatement;
53                 augmentationsInit.add(augmentationSchema);
54             }
55             if (effectiveStatement instanceof CaseSchemaNode) {
56                 final CaseSchemaNode choiceCaseNode = (CaseSchemaNode) effectiveStatement;
57                 // FIXME: we may be overwriting a previous entry, is that really okay?
58                 casesInit.put(choiceCaseNode.getQName(), choiceCaseNode);
59             }
60         }
61
62         this.augmentations = ImmutableSet.copyOf(augmentationsInit);
63         this.cases = ImmutableSortedMap.copyOfSorted(casesInit);
64
65         final Optional<String> defaultArg = findFirstEffectiveSubstatementArgument(DefaultEffectiveStatement.class);
66         if (defaultArg.isPresent()) {
67             final String arg = defaultArg.get();
68             final QName qname;
69             try {
70                 qname = QName.create(getQName(), arg);
71             } catch (IllegalArgumentException e) {
72                 throw new SourceException(ctx.getStatementSourceReference(), "Default statement has invalid name '%s'",
73                     arg, e);
74             }
75
76             // FIXME: this does not work with submodules, as they are
77             defaultCase = InferenceException.throwIfNull(cases.get(qname), ctx.getStatementSourceReference(),
78                 "Default statement refers to missing case %s", qname);
79         } else {
80             defaultCase = null;
81         }
82
83         mandatory = findFirstEffectiveSubstatementArgument(MandatoryEffectiveStatement.class).orElse(Boolean.FALSE)
84                 .booleanValue();
85     }
86
87     @Override
88     public Optional<ChoiceSchemaNode> getOriginal() {
89         return Optional.ofNullable(original);
90     }
91
92     @Override
93     public Collection<? extends AugmentationSchemaNode> getAvailableAugmentations() {
94         return augmentations;
95     }
96
97     @Override
98     public SortedMap<QName, CaseSchemaNode> getCases() {
99         return cases;
100     }
101
102     @Override
103     public Optional<CaseSchemaNode> getDefaultCase() {
104         return Optional.ofNullable(defaultCase);
105     }
106
107     @Override
108     public boolean isMandatory() {
109         return mandatory;
110     }
111
112     @Override
113     public String toString() {
114         return ChoiceEffectiveStatementImpl.class.getSimpleName() + "[" + "qname=" + getQName() + "]";
115     }
116 }