Refactor EffectiveStmtCtx.Parent.schemaPath()
[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.List;
14 import java.util.Optional;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.yangtools.yang.common.QNameModule;
17 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
18 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
19 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
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.NamespaceBehaviour.StorageNodeType;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.ParserNamespace;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementNamespace;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
27 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
28
29 /**
30  * A replica of a different statement. It does not allow modification, but produces an effective statement from a
31  * designated source.
32  */
33 final class ReplicaStatementContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
34         extends ReactorStmtCtx<A, D, E> {
35     private final StatementContextBase<?, ?, ?> parent;
36     private final ReactorStmtCtx<A, D, E> source;
37
38     ReplicaStatementContext(final StatementContextBase<?, ?, ?> parent, final ReactorStmtCtx<A, D, E> source) {
39         super(source);
40         this.parent = requireNonNull(parent);
41         this.source = requireNonNull(source);
42         if (source.isSupportedToBuildEffective()) {
43             source.incRef();
44             // FIXME: is this call really needed? it is inherited from source
45             setFullyDefined();
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 Collection<? extends StmtContext<?, ?, ?>> getEffectOfStatement() {
106         return List.of();
107     }
108
109     @Override
110     ReplicaStatementContext<A, D, E> replicaAsChildOf(final StatementContextBase<?, ?, ?> newParent) {
111         return source.replicaAsChildOf(newParent);
112     }
113
114     @Override
115     public Optional<? extends Mutable<?, ?, ?>> copyAsChildOf(final Mutable<?, ?, ?> newParent, final CopyType type,
116             final QNameModule targetModule) {
117         return source.copyAsChildOf(newParent, type, targetModule);
118     }
119
120     @Override
121     ReactorStmtCtx<?, ?, ?> asEffectiveChildOf(final StatementContextBase<?, ?, ?> newParent, final CopyType type,
122             final QNameModule targetModule) {
123         final ReactorStmtCtx<?, ?, ?> ret = source.asEffectiveChildOf(newParent, type, targetModule);
124         return ret == null ? null : this;
125     }
126
127     @Override
128     StatementDefinitionContext<A, D, E> definition() {
129         return source.definition();
130     }
131
132     @Override
133     void markNoParentRef() {
134         // No-op
135     }
136
137     @Override
138     int sweepSubstatements() {
139         if (fullyDefined()) {
140             source.decRef();
141         }
142         return 0;
143     }
144
145     @Override
146     public <K, V, T extends K, U extends V, N extends ParserNamespace<K, V>> void addToNs(final Class<@NonNull N> type,
147             final T key, final U value) {
148         throw new UnsupportedOperationException();
149     }
150
151     @Override
152     public Optional<StmtContext<A, D, E>> getPreviousCopyCtx() {
153         throw new UnsupportedOperationException();
154     }
155
156     @Override
157     public <K, KT extends K, N extends StatementNamespace<K, ?, ?>> void addContext(final Class<@NonNull N> namespace,
158             final KT key, final StmtContext<?, ?, ?> stmt) {
159         throw new UnsupportedOperationException();
160     }
161
162     @Override
163     public void addAsEffectOfStatement(final Collection<? extends StmtContext<?, ?, ?>> ctxs) {
164         throw new UnsupportedOperationException();
165     }
166
167     @Override
168     public Mutable<?, ?, ?> childCopyOf(final StmtContext<?, ?, ?> stmt, final CopyType type,
169             final QNameModule targetModule) {
170         throw new UnsupportedOperationException();
171     }
172
173     @Override boolean doTryToCompletePhase(final ModelProcessingPhase phase) {
174         throw new UnsupportedOperationException();
175     }
176
177     /*
178      * KEEP THINGS ORGANIZED!
179      *
180      * below methods exist in the same form in InferredStatementContext/SubstatementContext. If any adjustment is made
181      * here, make sure it is properly updated there.
182      */
183     @Override
184     @Deprecated
185     public SchemaPath schemaPath() {
186         return substatementGetSchemaPath();
187     }
188
189     @Override
190     public StatementContextBase<?, ?, ?> getParentContext() {
191         return parent;
192     }
193
194     @Override
195     public StorageNodeType getStorageNodeType() {
196         return StorageNodeType.STATEMENT_LOCAL;
197     }
198
199     @Override
200     public StatementContextBase<?, ?, ?> getParentNamespaceStorage() {
201         return parent;
202     }
203
204     @Override
205     public RootStatementContext<?, ?, ?> getRoot() {
206         return parent.getRoot();
207     }
208
209     @Override
210     protected boolean isIgnoringIfFeatures() {
211         return isIgnoringIfFeatures(parent);
212     }
213
214     @Override
215     protected boolean isIgnoringConfig() {
216         return isIgnoringConfig(parent);
217     }
218
219     @Override
220     protected boolean isParentSupportedByFeatures() {
221         return parent.isSupportedByFeatures();
222     }
223 }