e3ffc8003cdd68afb6af4e837aed8d87c3a47542
[yangtools.git] / yang / yang-parser-reactor / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / reactor / InferredStatementContext.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 com.google.common.base.Verify.verify;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.base.VerifyException;
14 import com.google.common.collect.ImmutableList;
15 import com.google.common.collect.Streams;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.HashMap;
19 import java.util.Iterator;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.Objects;
23 import java.util.Optional;
24 import java.util.stream.Collectors;
25 import java.util.stream.Stream;
26 import org.eclipse.jdt.annotation.NonNull;
27 import org.eclipse.jdt.annotation.Nullable;
28 import org.opendaylight.yangtools.concepts.Immutable;
29 import org.opendaylight.yangtools.yang.common.QName;
30 import org.opendaylight.yangtools.yang.common.QNameModule;
31 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
32 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
33 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
34 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
35 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeEffectiveStatement;
36 import org.opendaylight.yangtools.yang.parser.spi.SchemaTreeNamespace;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyHistory;
38 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
39 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
40 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.OnDemandSchemaTreeStorageNode;
41 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.StorageNodeType;
42 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementFactory;
43 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
44 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
45 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49 /**
50  * A statement which has been inferred to exist. Functionally it is equivalent to a SubstatementContext, but it is not
51  * backed by a declaration (and declared statements). It is backed by a prototype StatementContextBase and has only
52  * effective substatements, which are either transformed from that prototype or added by inference.
53  */
54 final class InferredStatementContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
55         extends StatementContextBase<A, D, E> implements OnDemandSchemaTreeStorageNode {
56     // An effective copy view, with enough information to decide what to do next
57     private static final class EffectiveCopy implements Immutable {
58         // Original statement
59         private final ReactorStmtCtx<?, ?, ?> orig;
60         // Effective view, if the statement is to be reused it equals to orig
61         private final ReactorStmtCtx<?, ?, ?> copy;
62
63         EffectiveCopy(final ReactorStmtCtx<?, ?, ?> orig, final ReactorStmtCtx<?, ?, ?> copy) {
64             this.orig = requireNonNull(orig);
65             this.copy = requireNonNull(copy);
66         }
67
68         boolean isReused() {
69             return orig == copy;
70         }
71
72         ReactorStmtCtx<?, ?, ?> toChildContext(final @NonNull InferredStatementContext<?, ?, ?> parent) {
73             return isReused() ? orig.replicaAsChildOf(parent) : copy;
74         }
75
76         ReactorStmtCtx<?, ?, ?> toReusedChild(final @NonNull InferredStatementContext<?, ?, ?> parent) {
77             verify(isReused(), "Attempted to discard copy %s", copy);
78             return orig.replicaAsChildOf(parent);
79         }
80     }
81
82     private static final Logger LOG = LoggerFactory.getLogger(InferredStatementContext.class);
83
84     // Sentinel object for 'substatements'
85     private static final Object SWEPT_SUBSTATEMENTS = new Object();
86
87     private final @NonNull StatementContextBase<A, D, E> prototype;
88     private final @NonNull StatementContextBase<?, ?, ?> parent;
89     private final @NonNull StmtContext<A, D, E> originalCtx;
90     private final @NonNull CopyType childCopyType;
91     private final QNameModule targetModule;
92     private final A argument;
93
94     /**
95      * Effective substatements, lazily materialized. This field can have four states:
96      * <ul>
97      *   <li>it can be {@code null}, in which case no materialization has taken place</li>
98      *   <li>it can be a {@link HashMap}, in which case partial materialization has taken place</li>
99      *   <li>it can be a {@link List}, in which case full materialization has taken place</li>
100      *   <li>it can be {@link SWEPT_SUBSTATEMENTS}, in which case materialized state is no longer available</li>
101      * </ul>
102      */
103     private Object substatements;
104
105     private InferredStatementContext(final InferredStatementContext<A, D, E> original,
106             final StatementContextBase<?, ?, ?> parent) {
107         super(original);
108         this.parent = requireNonNull(parent);
109         this.childCopyType = original.childCopyType;
110         this.targetModule = original.targetModule;
111         this.prototype = original.prototype;
112         this.originalCtx = original.originalCtx;
113         this.argument = original.argument;
114         // Substatements are initialized here
115         this.substatements = ImmutableList.of();
116     }
117
118     InferredStatementContext(final StatementContextBase<?, ?, ?> parent, final StatementContextBase<A, D, E> prototype,
119             final CopyType myCopyType, final CopyType childCopyType, final QNameModule targetModule) {
120         super(prototype.definition(), CopyHistory.of(myCopyType, prototype.history()));
121         this.parent = requireNonNull(parent);
122         this.prototype = requireNonNull(prototype);
123         this.argument = targetModule == null ? prototype.argument()
124                 : prototype.definition().adaptArgumentValue(prototype, targetModule);
125         this.childCopyType = requireNonNull(childCopyType);
126         this.targetModule = targetModule;
127         this.originalCtx = prototype.getOriginalCtx().orElse(prototype);
128
129         // Mark prototype as blocking statement cleanup
130         prototype.incRef();
131     }
132
133     @Override
134     public Collection<? extends StatementContextBase<?, ?, ?>> mutableDeclaredSubstatements() {
135         return ImmutableList.of();
136     }
137
138     @Override
139     public Collection<? extends Mutable<?, ?, ?>> mutableEffectiveSubstatements() {
140         return mutableEffectiveSubstatements(ensureEffectiveSubstatements());
141     }
142
143     @Override
144     public Iterable<? extends StmtContext<?, ?, ?>> allSubstatements() {
145         // No need to concat with declared
146         return effectiveSubstatements();
147     }
148
149     @Override
150     public Stream<? extends StmtContext<?, ?, ?>> allSubstatementsStream() {
151         // No need to concat with declared
152         return effectiveSubstatements().stream();
153     }
154
155     @Override
156     public StatementSourceReference sourceReference() {
157         return originalCtx.sourceReference();
158     }
159
160     @Override
161     public String rawArgument() {
162         return originalCtx.rawArgument();
163     }
164
165     @Override
166     public Optional<StmtContext<A, D, E>> getOriginalCtx() {
167         return Optional.of(originalCtx);
168     }
169
170     @Override
171     public Optional<StmtContext<A, D, E>> getPreviousCopyCtx() {
172         return Optional.of(prototype);
173     }
174
175     @Override
176     public D declared() {
177         /*
178          * Share original instance of declared statement between all effective statements which have been copied or
179          * derived from this original declared statement.
180          */
181         return originalCtx.declared();
182     }
183
184     @Override
185     public void removeStatementFromEffectiveSubstatements(final StatementDefinition statementDef) {
186         substatements = removeStatementFromEffectiveSubstatements(ensureEffectiveSubstatements(), statementDef);
187     }
188
189     @Override
190     public void removeStatementFromEffectiveSubstatements(final StatementDefinition statementDef,
191             final String statementArg) {
192         substatements = removeStatementFromEffectiveSubstatements(ensureEffectiveSubstatements(), statementDef,
193             statementArg);
194     }
195
196     @Override
197     public void addEffectiveSubstatement(final Mutable<?, ?, ?> substatement) {
198         substatements = addEffectiveSubstatement(ensureEffectiveSubstatements(), substatement);
199     }
200
201     @Override
202     void addEffectiveSubstatementsImpl(final Collection<? extends Mutable<?, ?, ?>> statements) {
203         substatements = addEffectiveSubstatementsImpl(ensureEffectiveSubstatements(), statements);
204     }
205
206     @Override
207     InferredStatementContext<A, D, E> reparent(final StatementContextBase<?, ?, ?> newParent) {
208         return new InferredStatementContext<>(this, newParent);
209     }
210
211     @Override
212     E createEffective(final StatementFactory<A, D, E> factory) {
213         // If we have not materialized we do not have a difference in effective substatements, hence we can forward
214         // towards the source of the statement.
215         return substatements == null ? tryToReusePrototype(factory) : super.createEffective(factory);
216     }
217
218     private @NonNull E tryToReusePrototype(final StatementFactory<A, D, E> factory) {
219         final E origEffective = prototype.buildEffective();
220         final Collection<? extends @NonNull EffectiveStatement<?, ?>> origSubstatements =
221             origEffective.effectiveSubstatements();
222
223         // First check if we can reuse the entire prototype
224         if (!factory.canReuseCurrent(this, prototype, origSubstatements)) {
225             return tryToReuseSubstatements(factory, origEffective);
226         }
227
228         // No substatements to deal with, we can freely reuse the original
229         if (origSubstatements.isEmpty()) {
230             LOG.debug("Reusing empty: {}", origEffective);
231             substatements = ImmutableList.of();
232             prototype.decRef();
233             return origEffective;
234         }
235
236         // We can reuse this statement let's see if all the statements agree
237         final List<EffectiveCopy> declCopy = prototype.streamDeclared()
238             .filter(StmtContext::isSupportedByFeatures)
239             .map(sub -> effectiveCopy((ReactorStmtCtx<?, ?, ?>) sub))
240             .filter(Objects::nonNull)
241             .collect(Collectors.toUnmodifiableList());
242         final List<EffectiveCopy> effCopy = prototype.streamEffective()
243             .map(sub -> effectiveCopy((ReactorStmtCtx<?, ?, ?>) sub))
244             .filter(Objects::nonNull)
245             .collect(Collectors.toUnmodifiableList());
246
247         if (allReused(declCopy) && allReused(effCopy)) {
248             LOG.debug("Reusing after substatement check: {}", origEffective);
249             // FIXME: can we skip this if !haveRef()?
250             substatements = reusePrototypeReplicas(Streams.concat(declCopy.stream(), effCopy.stream())
251                 .map(copy -> copy.toReusedChild(this)));
252             prototype.decRef();
253             return origEffective;
254         }
255
256         final List<ReactorStmtCtx<?, ?, ?>> declared = declCopy.stream()
257             .map(copy -> copy.toChildContext(this))
258             .collect(ImmutableList.toImmutableList());
259         final List<ReactorStmtCtx<?, ?, ?>> effective = effCopy.stream()
260             .map(copy -> copy.toChildContext(this))
261             .collect(ImmutableList.toImmutableList());
262         substatements = declared.isEmpty() ? effective
263             : Streams.concat(declared.stream(), effective.stream()).collect(ImmutableList.toImmutableList());
264         prototype.decRef();
265
266         // Values are the effective copies, hence this efficiently deals with recursion.
267         return factory.createEffective(this, declared.stream(), effective.stream());
268     }
269
270     private @NonNull E tryToReuseSubstatements(final StatementFactory<A, D, E> factory, final @NonNull E original) {
271         if (allSubstatementsContextIndependent()) {
272             LOG.debug("Reusing substatements of: {}", prototype);
273             // FIXME: can we skip this if !haveRef()?
274             substatements = reusePrototypeReplicas();
275             prototype.decRef();
276             return factory.copyEffective(this, original);
277         }
278
279         // Fall back to full instantiation, which populates our substatements. Then check if we should be reusing
280         // the substatement list, as this operation turned out to not affect them.
281         final E effective = super.createEffective(factory);
282         if (sameSubstatements(original.effectiveSubstatements(), effective)) {
283             LOG.debug("Reusing unchanged substatements of: {}", prototype);
284             return factory.copyEffective(this, original);
285         }
286         return effective;
287     }
288
289     private List<ReactorStmtCtx<?, ?, ?>> reusePrototypeReplicas() {
290         return reusePrototypeReplicas(Streams.concat(
291             prototype.streamDeclared().filter(StmtContext::isSupportedByFeatures),
292             prototype.streamEffective()));
293     }
294
295     private List<ReactorStmtCtx<?, ?, ?>> reusePrototypeReplicas(final Stream<StmtContext<?, ?, ?>> stream) {
296         return stream
297             .map(stmt -> {
298                 final ReplicaStatementContext<?, ?, ?> ret = ((ReactorStmtCtx<?, ?, ?>) stmt).replicaAsChildOf(this);
299                 ret.buildEffective();
300                 return ret;
301             })
302             .collect(Collectors.toUnmodifiableList());
303     }
304
305     private static boolean sameSubstatements(final Collection<?> original, final EffectiveStatement<?, ?> effective) {
306         final Collection<?> copied = effective.effectiveSubstatements();
307         if (copied != effective.effectiveSubstatements() || original.size() != copied.size()) {
308             // Do not bother if result is treating substatements as transient
309             return false;
310         }
311
312         final Iterator<?> oit = original.iterator();
313         final Iterator<?> cit = copied.iterator();
314         while (oit.hasNext()) {
315             verify(cit.hasNext());
316             // Identity comparison on purpose to side-step whatever equality there might be. We want to reuse instances
317             // after all.
318             if (oit.next() != cit.next()) {
319                 return false;
320             }
321         }
322         verify(!cit.hasNext());
323         return true;
324     }
325
326     private static boolean allReused(final List<EffectiveCopy> entries) {
327         return entries.stream().allMatch(EffectiveCopy::isReused);
328     }
329
330     @Override
331     boolean hasEmptySubstatements() {
332         if (substatements == null) {
333             return prototype.hasEmptySubstatements();
334         }
335         return substatements instanceof HashMap ? false : ((List<?>) substatements).isEmpty();
336     }
337
338     @Override
339     boolean noSensitiveSubstatements() {
340         accessSubstatements();
341         if (substatements == null) {
342             // No difference, defer to prototype
343             return prototype.allSubstatementsContextIndependent();
344         }
345         if (substatements instanceof List) {
346             // Fully materialized, walk all statements
347             return noSensitiveSubstatements(castEffective(substatements));
348         }
349
350         // Partially-materialized. This case has three distinct outcomes:
351         // - prototype does not have a sensitive statement (1)
352         // - protype has a sensitive substatement, and
353         //   - we have not marked is as unsupported (2)
354         //   - we have marked it as unsupported (3)
355         //
356         // Determining the outcome between (2) and (3) is a bother, this check errs on the side of false negative side
357         // and treats (3) as (2) -- i.e. even if we marked a sensitive statement as unsupported, we still consider it
358         // as affecting the result.
359         return prototype.allSubstatementsContextIndependent()
360             && noSensitiveSubstatements(castMaterialized(substatements).values());
361     }
362
363     @Override
364     public <X, Z extends EffectiveStatement<X, ?>> @NonNull Optional<X> findSubstatementArgument(
365             final @NonNull Class<Z> type) {
366         if (substatements instanceof List) {
367             return super.findSubstatementArgument(type);
368         }
369
370         final Optional<X> templateArg = prototype.findSubstatementArgument(type);
371         if (templateArg.isEmpty()) {
372             return templateArg;
373         }
374         if (SchemaTreeEffectiveStatement.class.isAssignableFrom(type)) {
375             // X is known to be QName
376             return (Optional<X>) templateArg.map(template -> ((QName) template).bindTo(targetModule));
377         }
378         return templateArg;
379     }
380
381     @Override
382     public boolean hasSubstatement(final @NonNull Class<? extends EffectiveStatement<?, ?>> type) {
383         return substatements instanceof List ? super.hasSubstatement(type)
384             // We do not allow deletion of partially-materialized statements, hence this is accurate
385             : prototype.hasSubstatement(type);
386     }
387
388     @Override
389     public <Y extends DeclaredStatement<QName>, Z extends SchemaTreeEffectiveStatement<Y>>
390             StmtContext<QName, Y, Z> requestSchemaTreeChild(final QName qname) {
391         if (substatements instanceof List) {
392             // We have performed materialization, hence we have triggered creation of all our schema tree child
393             // statements.
394             return null;
395         }
396
397         final QName templateQName = qname.bindTo(StmtContextUtils.getRootModuleQName(prototype));
398         LOG.debug("Materializing child {} from {}", qname, templateQName);
399
400         final StmtContext<?, ?, ?> template;
401         if (prototype instanceof InferredStatementContext) {
402             // Note: we need to access namespace here, as the target statement may have already been populated, in which
403             //       case we want to obtain the statement in local namespace storage.
404             template = (StmtContext) ((InferredStatementContext<?, ?, ?>) prototype).getFromNamespace(
405                 SchemaTreeNamespace.class, templateQName);
406         } else {
407             template = prototype.allSubstatementsStream()
408                 .filter(stmt -> stmt.producesEffective(SchemaTreeEffectiveStatement.class)
409                     && templateQName.equals(stmt.argument()))
410                 .findAny()
411                 .orElse(null);
412         }
413
414         if (template == null) {
415             // We do not have a template, this child does not exist. It may be added later, but that is someone else's
416             // responsibility.
417             LOG.debug("Child {} does not have a template", qname);
418             return null;
419         }
420
421         @SuppressWarnings("unchecked")
422         final Mutable<QName, Y, Z> ret = (Mutable<QName, Y, Z>) copySubstatement((Mutable<?, ?, ?>) template)
423             .orElseThrow(
424                 () -> new InferenceException(this, "Failed to materialize child %s template %s", qname, template));
425         ensureCompletedPhase(ret);
426         addMaterialized(template, ret);
427
428         LOG.debug("Child {} materialized", qname);
429         return ret;
430     }
431
432     // Instantiate this statement's effective substatements. Note this method has side-effects in namespaces and overall
433     // BuildGlobalContext, hence it must be called at most once.
434     private List<ReactorStmtCtx<?, ?, ?>> ensureEffectiveSubstatements() {
435         accessSubstatements();
436         return substatements instanceof List ? castEffective(substatements)
437             : initializeSubstatements(castMaterialized(substatements));
438     }
439
440     @Override
441     Iterable<ReactorStmtCtx<?, ?, ?>> effectiveChildrenToComplete() {
442         // When we have not initialized, there are no statements to catch up: we will catch up when we are copying
443         // from prototype (which is already at ModelProcessingPhase.EFFECTIVE_MODEL).
444         if (substatements == null) {
445             return ImmutableList.of();
446         }
447         accessSubstatements();
448         if (substatements instanceof HashMap) {
449             return castMaterialized(substatements).values();
450         } else {
451             return castEffective(substatements);
452         }
453     }
454
455     @Override
456     Stream<? extends StmtContext<?, ?, ?>> streamDeclared() {
457         return Stream.empty();
458     }
459
460     @Override
461     Stream<? extends StmtContext<?, ?, ?>> streamEffective() {
462         accessSubstatements();
463         return ensureEffectiveSubstatements().stream();
464     }
465
466     private void accessSubstatements() {
467         verify(substatements != SWEPT_SUBSTATEMENTS, "Attempted to access substatements of %s", this);
468     }
469
470     @Override
471     void markNoParentRef() {
472         final Object local = substatements;
473         if (local != null) {
474             markNoParentRef(castEffective(local));
475         }
476     }
477
478     @Override
479     int sweepSubstatements() {
480         final Object local = substatements;
481         substatements = SWEPT_SUBSTATEMENTS;
482         int count = 0;
483         if (local != null) {
484             final List<ReactorStmtCtx<?, ?, ?>> list = castEffective(local);
485             sweep(list);
486             count = countUnswept(list);
487         }
488         return count;
489     }
490
491     private List<ReactorStmtCtx<?, ?, ?>> initializeSubstatements(
492             final Map<StmtContext<?, ?, ?>, ReactorStmtCtx<?, ?, ?>> materializedSchemaTree) {
493         final Collection<? extends StatementContextBase<?, ?, ?>> declared = prototype.mutableDeclaredSubstatements();
494         final Collection<? extends Mutable<?, ?, ?>> effective = prototype.mutableEffectiveSubstatements();
495
496         final List<Mutable<?, ?, ?>> buffer = new ArrayList<>(declared.size() + effective.size());
497         for (final Mutable<?, ?, ?> stmtContext : declared) {
498             if (stmtContext.isSupportedByFeatures()) {
499                 copySubstatement(stmtContext, buffer, materializedSchemaTree);
500             }
501         }
502         for (final Mutable<?, ?, ?> stmtContext : effective) {
503             copySubstatement(stmtContext, buffer, materializedSchemaTree);
504         }
505
506         final List<ReactorStmtCtx<?, ?, ?>> ret = beforeAddEffectiveStatementUnsafe(ImmutableList.of(), buffer.size());
507         ret.addAll((Collection) buffer);
508         substatements = ret;
509
510         prototype.decRef();
511         return ret;
512     }
513
514     //
515     // Statement copy mess starts here. As it turns out, it's not that much of a mess, but it does make your head spin
516     // sometimes. Tread softly because you tread on my dreams.
517     //
518
519     private EffectiveCopy effectiveCopy(final ReactorStmtCtx<?, ?, ?> stmt) {
520         final ReactorStmtCtx<?, ?, ?> effective = stmt.asEffectiveChildOf(this, childCopyType, targetModule);
521         return effective == null ? null : new EffectiveCopy(stmt, effective);
522     }
523
524     private void copySubstatement(final Mutable<?, ?, ?> substatement, final Collection<Mutable<?, ?, ?>> buffer,
525             final Map<StmtContext<?, ?, ?>, ReactorStmtCtx<?, ?, ?>> materializedSchemaTree) {
526         // Consult materialized substatements. We are in a copy operation and will end up throwing materialized
527         // statements away -- hence we do not perform Map.remove() to save ourselves a mutation operation.
528         //
529         // We could also perform a Map.containsKey() and perform a bulk add, but that would mean the statement order
530         // against parent would change -- and we certainly do not want that to happen.
531         final ReactorStmtCtx<?, ?, ?> materialized = findMaterialized(materializedSchemaTree, substatement);
532         if (materialized == null) {
533             copySubstatement(substatement).ifPresent(copy -> {
534                 ensureCompletedPhase(copy);
535                 buffer.add(copy);
536             });
537         } else {
538             buffer.add(materialized);
539         }
540     }
541
542     private Optional<? extends Mutable<?, ?, ?>> copySubstatement(final Mutable<?, ?, ?> substatement) {
543         // FIXME: YANGTOOLS-1195: this is not exactly what we want to do here, because we are dealing with two different
544         //                        requests: copy for inference purposes (this method), while we also copy for purposes
545         //                        of buildEffective() -- in which case we want to probably invoke asEffectiveChildOf()
546         //                        or similar
547         return substatement.copyAsChildOf(this, childCopyType, targetModule);
548     }
549
550     private void addMaterialized(final StmtContext<?, ?, ?> template, final Mutable<?, ?, ?> copy) {
551         final HashMap<StmtContext<?, ?, ?>, ReactorStmtCtx<?, ?, ?>> materializedSchemaTree;
552         if (substatements == null) {
553             // Lazy initialization of backing map. We do not expect this to be used often or multiple times -- each hit
554             // here means an inference along schema tree, such as deviate/augment. HashMap requires power-of-two and
555             // defaults to 0.75 load factor -- we therefore size it to 4, i.e. next two inserts will not cause a
556             // resizing operation.
557             materializedSchemaTree = new HashMap<>(4);
558             substatements = materializedSchemaTree;
559         } else {
560             verify(substatements instanceof HashMap, "Unexpected substatements %s", substatements);
561             materializedSchemaTree = castMaterialized(substatements);
562         }
563
564         final StmtContext<?, ?, ?> existing = materializedSchemaTree.put(template,
565             (StatementContextBase<?, ?, ?>) copy);
566         if (existing != null) {
567             throw new VerifyException(
568                 "Unexpected duplicate request for " + copy.argument() + " previous result was " + existing);
569         }
570     }
571
572     private static @Nullable ReactorStmtCtx<?, ?, ?> findMaterialized(
573             final Map<StmtContext<?, ?, ?>, ReactorStmtCtx<?, ?, ?>> materializedSchemaTree,
574             final StmtContext<?, ?, ?> template) {
575         return materializedSchemaTree == null ? null : materializedSchemaTree.get(template);
576     }
577
578     @SuppressWarnings("unchecked")
579     private static List<ReactorStmtCtx<?, ?, ?>> castEffective(final Object substatements) {
580         return (List<ReactorStmtCtx<?, ?, ?>>) substatements;
581     }
582
583     @SuppressWarnings("unchecked")
584     private static HashMap<StmtContext<?, ?, ?>, ReactorStmtCtx<?, ?, ?>> castMaterialized(final Object substatements) {
585         return (HashMap<StmtContext<?, ?, ?>, ReactorStmtCtx<?, ?, ?>>) substatements;
586     }
587
588     // Statement copy mess ends here
589
590     /*
591      * KEEP THINGS ORGANIZED!
592      *
593      * below methods exist in the same form in SubstatementContext. If any adjustment is made here, make sure it is
594      * properly updated there.
595      */
596     @Override
597     @Deprecated
598     public SchemaPath schemaPath() {
599         return substatementGetSchemaPath();
600     }
601
602     @Override
603     public A argument() {
604         return argument;
605     }
606
607     @Override
608     public StatementContextBase<?, ?, ?> getParentContext() {
609         return parent;
610     }
611
612     @Override
613     public StorageNodeType getStorageNodeType() {
614         return StorageNodeType.STATEMENT_LOCAL;
615     }
616
617     @Override
618     public StatementContextBase<?, ?, ?> getParentNamespaceStorage() {
619         return parent;
620     }
621
622     @Override
623     public RootStatementContext<?, ?, ?> getRoot() {
624         return parent.getRoot();
625     }
626
627     @Override
628     public EffectiveConfig effectiveConfig() {
629         return effectiveConfig(parent);
630     }
631
632     @Override
633     protected boolean isIgnoringIfFeatures() {
634         return isIgnoringIfFeatures(parent);
635     }
636
637     @Override
638     protected boolean isIgnoringConfig() {
639         return isIgnoringConfig(parent);
640     }
641
642     @Override
643     protected boolean isParentSupportedByFeatures() {
644         return parent.isSupportedByFeatures();
645     }
646 }