2 * Copyright (c) 2021 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.stmt.reactor;
10 import static com.google.common.base.Verify.verify;
11 import static com.google.common.base.Verify.verifyNotNull;
12 import static java.util.Objects.requireNonNull;
14 import com.google.common.collect.ImmutableList;
15 import java.util.Collection;
16 import java.util.Iterator;
17 import java.util.List;
18 import java.util.Optional;
19 import java.util.stream.Stream;
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
22 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
23 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
27 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 abstract class OriginalStmtCtx<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
32 extends StatementContextBase<A, D, E> {
33 private static final Logger LOG = LoggerFactory.getLogger(OriginalStmtCtx.class);
35 private final @NonNull StatementSourceReference ref;
37 private List<ReactorStmtCtx<?, ?, ?>> effective = ImmutableList.of();
39 OriginalStmtCtx(final OriginalStmtCtx<A, D, E> original) {
41 this.ref = original.ref;
44 OriginalStmtCtx(final StatementDefinitionContext<A, D, E> def, final StatementSourceReference ref) {
46 this.ref = requireNonNull(ref);
49 OriginalStmtCtx(final StatementDefinitionContext<A, D, E> def, final StatementSourceReference ref,
50 final CopyType copyType) {
52 this.ref = requireNonNull(ref);
56 public final StatementSourceReference sourceReference() {
61 public final Optional<StmtContext<A, D, E>> getOriginalCtx() {
62 return Optional.empty();
66 public final Optional<StmtContext<A, D, E>> getPreviousCopyCtx() {
67 return Optional.empty();
71 public final Collection<? extends Mutable<?, ?, ?>> mutableEffectiveSubstatements() {
72 return mutableEffectiveSubstatements(effective);
76 public final void removeStatementFromEffectiveSubstatements(final StatementDefinition statementDef) {
77 effective = removeStatementFromEffectiveSubstatements(effective, statementDef);
81 public final void removeStatementFromEffectiveSubstatements(final StatementDefinition statementDef,
82 final String statementArg) {
83 effective = removeStatementFromEffectiveSubstatements(effective, statementDef, statementArg);
87 public final void addEffectiveSubstatement(final Mutable<?, ?, ?> substatement) {
88 effective = addEffectiveSubstatement(effective, substatement);
92 final void addEffectiveSubstatementsImpl(final Collection<? extends Mutable<?, ?, ?>> statements) {
93 effective = addEffectiveSubstatementsImpl(effective, statements);
97 final Iterator<ReactorStmtCtx<?, ?, ?>> effectiveChildrenToComplete() {
98 return effective.iterator();
102 final Stream<? extends @NonNull StmtContext<?, ?, ?>> streamEffective() {
103 return effective.stream().filter(StmtContext::isSupportedToBuildEffective);
107 final OriginalStmtCtx<A, D, E> unmodifiedEffectiveSource() {
108 // This statement is comes from the source
113 final boolean hasEmptySubstatements() {
114 return effective.isEmpty() && mutableDeclaredSubstatements().isEmpty();
118 final boolean noSensitiveSubstatements() {
119 return hasEmptySubstatements()
120 || noSensitiveSubstatements(effective) && noSensitiveSubstatements(mutableDeclaredSubstatements());
124 final void markNoParentRef() {
125 markNoParentRef(mutableDeclaredSubstatements());
126 markNoParentRef(effective);
130 final int sweepSubstatements() {
131 // First we need to sweep all statements, which may trigger sweeps all across the place, for example:
132 // - 'effective' member sweeping a 'substatements' member
133 // - 'substatements' member sweeping a 'substatements' member which came before it during iteration
134 // We then iterate once again, counting what remains unswept
135 final var declared = mutableDeclaredSubstatements();
139 final int count = countUnswept(declared) + countUnswept(effective);
141 LOG.debug("{} children left to sweep from {}", count, this);
144 dropDeclaredSubstatements();
148 abstract void dropDeclaredSubstatements();
150 void declarationFinished(final ModelProcessingPhase phase) {
151 finishDeclaration(phase);
155 * Ends declared section of current node for the specified phase.
157 * @param phase processing phase that ended
159 final void finishDeclaration(final ModelProcessingPhase phase) {
160 definition().onDeclarationFinished(this, phase);
163 final OriginalStmtCtx<?, ?, ?> getResumedSubstatement() {
164 final var local = verifyNotNull(effective);
165 verify(!local.isEmpty(), "Unexpected empty statements");
166 final var ret = local.get(0);
167 verify(ret instanceof OriginalStmtCtx, "Unexpected statement %s", ret);
168 return (OriginalStmtCtx<?, ?, ?>) ret;