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