Use statementContextBase when creating effecive
[yangtools.git] / yang / yang-parser-reactor / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / reactor / ReplicaStatementContext.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, s.r.o. 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.stmt.reactor;
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 java.util.stream.Stream;
16 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
17 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
18 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
19 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.StorageNodeType;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
22 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
23
24 /**
25  * A replica of a different statement. It does not allow modification, but produces an effective statement from a
26  * designated source.
27  */
28 final class ReplicaStatementContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
29         extends StatementContextBase<A, D, E> {
30     private final StatementContextBase<?, ?, ?> parent;
31     private final StatementContextBase<A, D, E> source;
32
33     ReplicaStatementContext(final StatementContextBase<?, ?, ?> parent, final StatementContextBase<A, D, E> source) {
34         super(source);
35         this.parent = requireNonNull(parent);
36         this.source = requireNonNull(source);
37         if (source.isSupportedToBuildEffective()) {
38             source.incRef();
39             setFullyDefined();
40         } else {
41             setIsSupportedToBuildEffective(false);
42         }
43     }
44
45     @Override
46     E createEffective() {
47         return source.buildEffective();
48     }
49
50     @Override
51     public boolean effectiveConfig() {
52         return source.effectiveConfig();
53     }
54
55     @Override
56     public D declared() {
57         return source.declared();
58     }
59
60     @Override
61     public A argument() {
62         return source.argument();
63     }
64
65     @Override
66     public StatementSourceReference sourceReference() {
67         return source.sourceReference();
68     }
69
70     @Override
71     public String rawArgument() {
72         return source.rawArgument();
73     }
74
75     @Override
76     public Optional<StmtContext<A, D, E>> getOriginalCtx() {
77         return source.getOriginalCtx();
78     }
79
80     @Override
81     public Collection<? extends StatementContextBase<?, ?, ?>> mutableDeclaredSubstatements() {
82         return source.mutableDeclaredSubstatements();
83     }
84
85     @Override
86     public Collection<? extends Mutable<?, ?, ?>> mutableEffectiveSubstatements() {
87         return source.mutableEffectiveSubstatements();
88     }
89
90     @Override
91     boolean hasEmptySubstatements() {
92         return source.hasEmptySubstatements();
93     }
94
95     @Override
96     Iterable<StatementContextBase<?, ?, ?>> effectiveChildrenToComplete() {
97         return ImmutableList.of();
98     }
99
100     @Override
101     int sweepSubstatements() {
102         if (fullyDefined()) {
103             source.decRef();
104         }
105         return 0;
106     }
107
108     @Override
109     public Optional<StmtContext<A, D, E>> getPreviousCopyCtx() {
110         throw new UnsupportedOperationException();
111     }
112
113     @Override
114     public void removeStatementFromEffectiveSubstatements(final StatementDefinition statementDef) {
115         throw new UnsupportedOperationException();
116     }
117
118     @Override
119     public void removeStatementFromEffectiveSubstatements(final StatementDefinition statementDef,
120             final String statementArg) {
121         throw new UnsupportedOperationException();
122     }
123
124     @Override
125     public void addEffectiveSubstatement(final Mutable<?, ?, ?> substatement) {
126         throw new UnsupportedOperationException();
127     }
128
129     @Override
130     void addEffectiveSubstatementsImpl(final Collection<? extends Mutable<?, ?, ?>> statements) {
131         throw new UnsupportedOperationException();
132     }
133
134     @Override
135     Stream<? extends StmtContext<?, ?, ?>> streamDeclared() {
136         throw new UnsupportedOperationException();
137     }
138
139     @Override
140     Stream<? extends StmtContext<?, ?, ?>> streamEffective() {
141         throw new UnsupportedOperationException();
142     }
143
144     @Override
145     StatementContextBase<A, D, E> reparent(final StatementContextBase<?, ?, ?> newParent) {
146         throw new UnsupportedOperationException();
147     }
148
149     /*
150      * KEEP THINGS ORGANIZED!
151      *
152      * below methods exist in the same form in InferredStatementContext/SubstatementContext. If any adjustment is made
153      * here, make sure it is properly updated there.
154      */
155     @Override
156     @Deprecated
157     public Optional<SchemaPath> schemaPath() {
158         return substatementGetSchemaPath();
159     }
160
161     @Override
162     public StatementContextBase<?, ?, ?> getParentContext() {
163         return parent;
164     }
165
166     @Override
167     public StorageNodeType getStorageNodeType() {
168         return StorageNodeType.STATEMENT_LOCAL;
169     }
170
171     @Override
172     public StatementContextBase<?, ?, ?> getParentNamespaceStorage() {
173         return parent;
174     }
175
176     @Override
177     public RootStatementContext<?, ?, ?> getRoot() {
178         return parent.getRoot();
179     }
180
181     @Override
182     protected boolean isIgnoringIfFeatures() {
183         return isIgnoringIfFeatures(parent);
184     }
185
186     @Override
187     protected boolean isIgnoringConfig() {
188         return isIgnoringConfig(parent);
189     }
190
191     @Override
192     protected boolean isParentSupportedByFeatures() {
193         return parent.isSupportedByFeatures();
194     }
195 }