9949d9ac734b3ce7b25b2be731ae6caca83edd17
[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.ImmutableList;
11 import com.google.common.collect.ImmutableSortedMap;
12 import java.util.Objects;
13 import java.util.Optional;
14 import java.util.SortedMap;
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.SchemaPath;
22 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
23 import org.opendaylight.yangtools.yang.model.api.stmt.ChoiceEffectiveStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.ChoiceStatement;
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 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
30
31 final class ChoiceEffectiveStatementImpl extends WithSubstatements<QName, ChoiceStatement, ChoiceEffectiveStatement>
32         implements ChoiceEffectiveStatement, ChoiceSchemaNode, DerivableSchemaNode,
33                    DataSchemaNodeMixin<QName, ChoiceStatement>, AugmentationTargetMixin<QName, ChoiceStatement>,
34                    MandatoryMixin<QName, ChoiceStatement> {
35     private final ImmutableSortedMap<QName, CaseSchemaNode> cases;
36     private final CaseSchemaNode defaultCase;
37     private final ChoiceSchemaNode original;
38     private final @NonNull SchemaPath path;
39     private final int flags;
40
41     ChoiceEffectiveStatementImpl(final ChoiceStatement declared,
42             final StmtContext<QName, ChoiceStatement, ChoiceEffectiveStatement> ctx,
43             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements, final int flags,
44             final SortedMap<QName, CaseSchemaNode> cases, final @Nullable CaseSchemaNode defaultCase,
45             final @Nullable ChoiceSchemaNode original) {
46         super(declared, ctx, substatements);
47         this.flags = flags;
48         this.path = ctx.getSchemaPath().get();
49         this.cases = ImmutableSortedMap.copyOfSorted(cases);
50         this.defaultCase = defaultCase;
51         this.original = original;
52     }
53
54     @Override
55     public @NonNull QName argument() {
56         return path.getLastComponent();
57     }
58
59     @Override
60     public QName getQName() {
61         return argument();
62     }
63
64     @Override
65     public SchemaPath getPath() {
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 SortedMap<QName, CaseSchemaNode> getCases() {
81         return cases;
82     }
83
84     @Override
85     public Optional<CaseSchemaNode> getDefaultCase() {
86         return Optional.ofNullable(defaultCase);
87     }
88
89     @Override
90     public int hashCode() {
91         final int prime = 31;
92         int result = 1;
93         result = prime * result + Objects.hashCode(getQName());
94         result = prime * result + Objects.hashCode(getPath());
95         return result;
96     }
97
98     @Override
99     public boolean equals(final Object obj) {
100         if (this == obj) {
101             return true;
102         }
103         if (obj == null) {
104             return false;
105         }
106         if (getClass() != obj.getClass()) {
107             return false;
108         }
109         final ChoiceEffectiveStatementImpl other = (ChoiceEffectiveStatementImpl) obj;
110         return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
111     }
112
113     @Override
114
115     public String toString() {
116         return ChoiceEffectiveStatementImpl.class.getSimpleName() + "["
117                 + "qname=" + getQName()
118                 + "]";
119     }
120 }