978164ef727d9cf7266c275d4ad6e804032f95b6
[yangtools.git] / yang / yang-parser-reactor / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / reactor / AbstractResumedStatement.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.Preconditions.checkState;
11 import static java.util.Objects.requireNonNull;
12
13 import java.util.Collection;
14 import java.util.Optional;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.eclipse.jdt.annotation.Nullable;
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.CopyHistory;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupport;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
25 import org.opendaylight.yangtools.yang.parser.spi.source.ImplicitSubstatement;
26 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
27 import org.opendaylight.yangtools.yang.parser.spi.source.StatementWriter.ResumedStatement;
28
29 /**
30  * Intermediate subclass of StatementContextBase facing the parser stream via implementation of ResumedStatement. This
31  * shields inference-type substatements from these details.
32  *
33  * @param <A> Argument type
34  * @param <D> Declared Statement representation
35  * @param <E> Effective Statement representation
36  */
37 abstract class AbstractResumedStatement<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
38         extends StatementContextBase<A, D, E> implements ResumedStatement {
39     private final @NonNull StatementSourceReference statementDeclSource;
40     private final String rawArgument;
41
42     private StatementMap substatements = StatementMap.empty();
43     private @Nullable D declaredInstance;
44
45     // Copy constructor
46     AbstractResumedStatement(final AbstractResumedStatement<A, D, E> original) {
47         super(original);
48         this.statementDeclSource = original.statementDeclSource;
49         this.rawArgument = original.rawArgument;
50         this.substatements = original.substatements;
51         this.declaredInstance = original.declaredInstance;
52     }
53
54     AbstractResumedStatement(final StatementDefinitionContext<A, D, E> def, final StatementSourceReference ref,
55             final String rawArgument) {
56         super(def);
57         this.statementDeclSource = requireNonNull(ref);
58         this.rawArgument = def.support().internArgument(rawArgument);
59     }
60
61     AbstractResumedStatement(final StatementDefinitionContext<A, D, E> def, final StatementSourceReference ref,
62             final String rawArgument, final CopyType copyType) {
63         super(def, CopyHistory.of(copyType, CopyHistory.original()));
64         this.statementDeclSource = requireNonNull(ref);
65         this.rawArgument = rawArgument;
66     }
67
68     @Override
69     public final Optional<StmtContext<?, ?, ?>> getOriginalCtx() {
70         return Optional.empty();
71     }
72
73     @Override
74     public final Optional<? extends StmtContext<?, ?, ?>> getPreviousCopyCtx() {
75         return Optional.empty();
76     }
77
78     @Override
79     public final StatementSourceReference getStatementSourceReference() {
80         return statementDeclSource;
81     }
82
83     @Override
84     public final String rawStatementArgument() {
85         return rawArgument;
86     }
87
88     @Override
89     public Collection<? extends StatementContextBase<?, ?, ?>> mutableDeclaredSubstatements() {
90         return substatements;
91     }
92
93     @Override
94     public final D buildDeclared() {
95         final D existing = declaredInstance;
96         if (existing != null) {
97             return existing;
98         }
99         final ModelProcessingPhase phase = getCompletedPhase();
100         checkState(phase == ModelProcessingPhase.FULL_DECLARATION || phase == ModelProcessingPhase.EFFECTIVE_MODEL,
101                 "Cannot build declared instance after phase %s", phase);
102         return declaredInstance = definition().getFactory().createDeclared(this);
103     }
104
105     @Override
106     public @NonNull StatementDefinition getDefinition() {
107         return getPublicDefinition();
108     }
109
110     @Override
111     public @NonNull StatementSourceReference getSourceReference() {
112         return getStatementSourceReference();
113     }
114
115     @Override
116     public boolean isFullyDefined() {
117         return fullyDefined();
118     }
119
120     /**
121      * Create a new substatement at the specified offset.
122      *
123      * @param offset Substatement offset
124      * @param def definition context
125      * @param ref source reference
126      * @param argument statement argument
127      * @param <X> new substatement argument type
128      * @param <Y> new substatement declared type
129      * @param <Z> new substatement effective type
130      * @return A new substatement
131      */
132     @SuppressWarnings("checkstyle:methodTypeParameterName")
133     final <X, Y extends DeclaredStatement<X>, Z extends EffectiveStatement<X, Y>>
134             AbstractResumedStatement<X, Y, Z> createSubstatement(final int offset,
135                     final StatementDefinitionContext<X, Y, Z> def, final StatementSourceReference ref,
136                     final String argument) {
137         final ModelProcessingPhase inProgressPhase = getRoot().getSourceContext().getInProgressPhase();
138         checkState(inProgressPhase != ModelProcessingPhase.EFFECTIVE_MODEL,
139                 "Declared statement cannot be added in effective phase at: %s", getStatementSourceReference());
140
141         final Optional<StatementSupport<?, ?, ?>> implicitParent =
142                 definition().getImplicitParentFor(def.getPublicView());
143         if (implicitParent.isPresent()) {
144             return createImplicitParent(offset, implicitParent.get(), ref, argument).createSubstatement(offset, def,
145                     ref, argument);
146         }
147
148         final AbstractResumedStatement<X, Y, Z> ret = new SubstatementContext<>(this, def, ref, argument);
149         substatements = substatements.put(offset, ret);
150         def.onStatementAdded(ret);
151         return ret;
152     }
153
154     @Override
155     final boolean hasEmptySubstatements() {
156         return substatements.size() == 0 && hasEmptyEffectiveSubstatements();
157     }
158
159     /**
160      * Lookup substatement by its offset in this statement.
161      *
162      * @param offset Substatement offset
163      * @return Substatement, or null if substatement does not exist.
164      */
165     final AbstractResumedStatement<?, ?, ?> lookupSubstatement(final int offset) {
166         return substatements.get(offset);
167     }
168
169     final void resizeSubstatements(final int expectedSize) {
170         substatements = substatements.ensureCapacity(expectedSize);
171     }
172
173     final void walkChildren(final ModelProcessingPhase phase) {
174         checkState(isFullyDefined());
175         substatements.forEach(stmt -> {
176             stmt.walkChildren(phase);
177             stmt.endDeclared(phase);
178         });
179     }
180
181     private AbstractResumedStatement<?, ?, ?> createImplicitParent(final int offset,
182             final StatementSupport<?, ?, ?> implicitParent, final StatementSourceReference ref, final String argument) {
183         final StatementDefinitionContext<?, ?, ?> def = new StatementDefinitionContext<>(implicitParent);
184         return createSubstatement(offset, def, ImplicitSubstatement.of(ref), argument);
185     }
186 }