Update SchemaPath.getPath() implementation
[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 static java.util.Objects.requireNonNull;
11
12 import com.google.common.collect.ImmutableList;
13 import java.util.Collection;
14 import java.util.Optional;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
22 import org.opendaylight.yangtools.yang.model.api.stmt.ChoiceEffectiveStatement;
23 import org.opendaylight.yangtools.yang.model.api.stmt.ChoiceStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeEffectiveStatement;
25 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.AbstractDeclaredEffectiveStatement.DefaultWithSchemaTree.WithSubstatements;
26 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.EffectiveStatementMixins.AugmentationTargetMixin;
27 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.EffectiveStatementMixins.DataSchemaNodeMixin;
28 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.EffectiveStatementMixins.MandatoryMixin;
29
30 final class ChoiceEffectiveStatementImpl extends WithSubstatements<QName, ChoiceStatement, ChoiceEffectiveStatement>
31         implements ChoiceEffectiveStatement, ChoiceSchemaNode, DerivableSchemaNode,
32                    DataSchemaNodeMixin<QName, ChoiceStatement>, AugmentationTargetMixin<QName, ChoiceStatement>,
33                    MandatoryMixin<QName, ChoiceStatement> {
34     private final CaseSchemaNode defaultCase;
35     private final ChoiceSchemaNode original;
36     private final @NonNull Object path;
37     private final int flags;
38
39     ChoiceEffectiveStatementImpl(final ChoiceStatement declared,
40             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements, final int flags,
41             final Object path, final @Nullable CaseSchemaNode defaultCase,
42             final @Nullable ChoiceSchemaNode original) {
43         super(declared, substatements);
44         this.path = requireNonNull(path);
45         this.flags = flags;
46         this.defaultCase = defaultCase;
47         this.original = original;
48     }
49
50     ChoiceEffectiveStatementImpl(final ChoiceEffectiveStatementImpl origEffective, final int flags,
51             final Object path, final ChoiceSchemaNode original) {
52         super(origEffective);
53         this.path = requireNonNull(path);
54         this.flags = flags;
55         this.defaultCase = origEffective.defaultCase;
56         this.original = original;
57     }
58
59     @Override
60     public @NonNull QName argument() {
61         return getQName();
62     }
63
64     @Override
65     public Object pathObject() {
66         return path;
67     }
68
69     @Override
70     public int flags() {
71         return flags;
72     }
73
74     @Override
75     public Optional<ChoiceSchemaNode> getOriginal() {
76         return Optional.ofNullable(original);
77     }
78
79     @Override
80     public Optional<? extends CaseSchemaNode> findCase(final QName qname) {
81         final SchemaTreeEffectiveStatement<?> child = schemaTreeNamespace().get(requireNonNull(qname));
82         return child instanceof CaseSchemaNode ? Optional.of((CaseSchemaNode) child) : Optional.empty();
83     }
84
85     @Override
86     public Collection<? extends @NonNull CaseSchemaNode> getCases() {
87         return filterEffectiveStatements(CaseSchemaNode.class);
88     }
89
90     @Override
91     public Optional<CaseSchemaNode> getDefaultCase() {
92         return Optional.ofNullable(defaultCase);
93     }
94
95     @Override
96     public ChoiceEffectiveStatement asEffectiveStatement() {
97         return this;
98     }
99
100     @Override
101     public String toString() {
102         return ChoiceEffectiveStatementImpl.class.getSimpleName() + "[" + "qname=" + getQName() + "]";
103     }
104 }