Do not force materialization when not needed
[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 java.util.Collection;
13 import java.util.Optional;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.opendaylight.yangtools.yang.common.QNameModule;
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.parser.spi.meta.CopyHistory;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.StorageNodeType;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.ParserNamespace;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementNamespace;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
26 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
27
28 /**
29  * A replica of a different statement. It does not allow modification, but produces an effective statement from a
30  * designated source.
31  */
32 final class ReplicaStatementContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
33         extends ReactorStmtCtx<A, D, E> {
34     private final StatementContextBase<?, ?, ?> parent;
35     private final ReactorStmtCtx<A, D, E> source;
36
37     ReplicaStatementContext(final StatementContextBase<?, ?, ?> parent, final ReactorStmtCtx<A, D, E> source) {
38         super(source);
39         this.parent = requireNonNull(parent);
40         this.source = requireNonNull(source);
41         if (source.isSupportedToBuildEffective()) {
42             source.incRef();
43             setFullyDefined();
44         } else {
45             setIsSupportedToBuildEffective(false);
46         }
47     }
48
49     @Override
50     E createEffective() {
51         return source.buildEffective();
52     }
53
54     @Override
55     public EffectiveConfig effectiveConfig() {
56         return source.effectiveConfig();
57     }
58
59     @Override
60     public D declared() {
61         return source.declared();
62     }
63
64     @Override
65     public A argument() {
66         return source.argument();
67     }
68
69     @Override
70     public StatementSourceReference sourceReference() {
71         return source.sourceReference();
72     }
73
74     @Override
75     public String rawArgument() {
76         return source.rawArgument();
77     }
78
79     @Override
80     public Optional<StmtContext<A, D, E>> getOriginalCtx() {
81         return source.getOriginalCtx();
82     }
83
84     @Override
85     public Collection<? extends StatementContextBase<?, ?, ?>> mutableDeclaredSubstatements() {
86         return source.mutableDeclaredSubstatements();
87     }
88
89     @Override
90     public Collection<? extends Mutable<?, ?, ?>> mutableEffectiveSubstatements() {
91         return source.mutableEffectiveSubstatements();
92     }
93
94     @Override
95     public ModelProcessingPhase getCompletedPhase() {
96         return source.getCompletedPhase();
97     }
98
99     @Override
100     public CopyHistory history() {
101         return source.history();
102     }
103
104     @Override
105     public Mutable<A, D, E> replicaAsChildOf(final Mutable<?, ?, ?> newParent) {
106         return source.replicaAsChildOf(newParent);
107     }
108
109     @Override
110     public Optional<? extends Mutable<?, ?, ?>> copyAsChildOf(final Mutable<?, ?, ?> newParent, final CopyType type,
111             final QNameModule targetModule) {
112         return source.copyAsChildOf(newParent, type, targetModule);
113     }
114
115     @Override
116     ReactorStmtCtx<?, ?, ?> asEffectiveChildOf(final StatementContextBase<?, ?, ?> newParent, final CopyType type,
117             final QNameModule targetModule) {
118         return source.asEffectiveChildOf(newParent, type, targetModule);
119     }
120
121     @Override
122     StatementDefinitionContext<A, D, E> definition() {
123         return source.definition();
124     }
125
126     @Override
127     void markNoParentRef() {
128         // No-op
129     }
130
131     @Override
132     int sweepSubstatements() {
133         if (fullyDefined()) {
134             source.decRef();
135         }
136         return 0;
137     }
138
139     @Override
140     public <K, V, T extends K, U extends V, N extends ParserNamespace<K, V>> void addToNs(final Class<@NonNull N> type,
141             final T key, final U value) {
142         throw new UnsupportedOperationException();
143     }
144
145     @Override
146     public Optional<StmtContext<A, D, E>> getPreviousCopyCtx() {
147         throw new UnsupportedOperationException();
148     }
149
150     @Override
151     public <K, KT extends K, N extends StatementNamespace<K, ?, ?>> void addContext(final Class<@NonNull N> namespace,
152             final KT key, final StmtContext<?, ?, ?> stmt) {
153         throw new UnsupportedOperationException();
154     }
155
156     @Override
157     public void addAsEffectOfStatement(final StmtContext<?, ?, ?> ctx) {
158         throw new UnsupportedOperationException();
159     }
160
161     @Override
162     public void addAsEffectOfStatement(final Collection<? extends StmtContext<?, ?, ?>> ctxs) {
163         throw new UnsupportedOperationException();
164     }
165
166     @Override
167     public Collection<? extends StmtContext<?, ?, ?>> getEffectOfStatement() {
168         throw new UnsupportedOperationException();
169     }
170
171     @Override
172     public Mutable<?, ?, ?> childCopyOf(final StmtContext<?, ?, ?> stmt, final CopyType type,
173             final QNameModule targetModule) {
174         throw new UnsupportedOperationException();
175     }
176
177     @Override boolean doTryToCompletePhase(final ModelProcessingPhase phase) {
178         throw new UnsupportedOperationException();
179     }
180
181     /*
182      * KEEP THINGS ORGANIZED!
183      *
184      * below methods exist in the same form in InferredStatementContext/SubstatementContext. If any adjustment is made
185      * here, make sure it is properly updated there.
186      */
187     @Override
188     @Deprecated
189     public Optional<SchemaPath> schemaPath() {
190         return substatementGetSchemaPath();
191     }
192
193     @Override
194     public StatementContextBase<?, ?, ?> getParentContext() {
195         return parent;
196     }
197
198     @Override
199     public StorageNodeType getStorageNodeType() {
200         return StorageNodeType.STATEMENT_LOCAL;
201     }
202
203     @Override
204     public StatementContextBase<?, ?, ?> getParentNamespaceStorage() {
205         return parent;
206     }
207
208     @Override
209     public RootStatementContext<?, ?, ?> getRoot() {
210         return parent.getRoot();
211     }
212
213     @Override
214     protected boolean isIgnoringIfFeatures() {
215         return isIgnoringIfFeatures(parent);
216     }
217
218     @Override
219     protected boolean isIgnoringConfig() {
220         return isIgnoringConfig(parent);
221     }
222
223     @Override
224     protected boolean isParentSupportedByFeatures() {
225         return parent.isSupportedByFeatures();
226     }
227 }