Add a refcount mechanism for substatements
[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 com.google.common.collect.ImmutableList;
13 import java.util.Collection;
14 import java.util.Optional;
15 import java.util.stream.Stream;
16 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
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.NamespaceBehaviour.StorageNodeType;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
22 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * A replica of a different statement. It does not allow modification, but produces an effective statement from a
28  * designated source.
29  */
30 final class ReplicaStatementContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
31         extends StatementContextBase<A, D, E> {
32     private static final Logger LOG = LoggerFactory.getLogger(ReplicaStatementContext.class);
33
34     private final StatementContextBase<?, ?, ?> parent;
35     private final StatementContextBase<A, D, E> source;
36
37     private final boolean haveRef;
38
39     ReplicaStatementContext(final StatementContextBase<?, ?, ?> parent, final StatementContextBase<A, D, E> source) {
40         super(source);
41         this.parent = requireNonNull(parent);
42         this.source = requireNonNull(source);
43         if (source.isSupportedToBuildEffective()) {
44             source.incRef();
45             haveRef = true;
46         } else {
47             setIsSupportedToBuildEffective(false);
48             haveRef = false;
49         }
50     }
51
52     @Override
53     E createEffective() {
54         return source.buildEffective();
55     }
56
57     @Override
58     public boolean isConfiguration() {
59         return source.isConfiguration();
60     }
61
62     @Override
63     public D buildDeclared() {
64         return source.buildDeclared();
65     }
66
67     @Override
68     public A argument() {
69         return source.argument();
70     }
71
72     @Override
73     public StatementSourceReference sourceReference() {
74         return source.sourceReference();
75     }
76
77     @Override
78     public String rawArgument() {
79         return source.rawArgument();
80     }
81
82     @Override
83     public Optional<StmtContext<A, D, E>> getOriginalCtx() {
84         return source.getOriginalCtx();
85     }
86
87     @Override
88     public Collection<? extends StatementContextBase<?, ?, ?>> mutableDeclaredSubstatements() {
89         return source.mutableDeclaredSubstatements();
90     }
91
92     @Override
93     public Collection<? extends Mutable<?, ?, ?>> mutableEffectiveSubstatements() {
94         return source.mutableEffectiveSubstatements();
95     }
96
97     @Override
98     boolean hasEmptySubstatements() {
99         return source.hasEmptySubstatements();
100     }
101
102     @Override
103     Iterable<StatementContextBase<?, ?, ?>> effectiveChildrenToComplete() {
104         return ImmutableList.of();
105     }
106
107     @Override
108     int sweepSubstatements() {
109         if (haveRef) {
110             source.decRef();
111         }
112         return 0;
113     }
114
115     @Override
116     public Optional<StmtContext<A, D, E>> getPreviousCopyCtx() {
117         throw new UnsupportedOperationException();
118     }
119
120     @Override
121     public void removeStatementFromEffectiveSubstatements(final StatementDefinition statementDef) {
122         throw new UnsupportedOperationException();
123     }
124
125     @Override
126     public void removeStatementFromEffectiveSubstatements(final StatementDefinition statementDef,
127             final String statementArg) {
128         throw new UnsupportedOperationException();
129     }
130
131     @Override
132     public void addEffectiveSubstatement(final Mutable<?, ?, ?> substatement) {
133         throw new UnsupportedOperationException();
134     }
135
136     @Override
137     void addEffectiveSubstatementsImpl(final Collection<? extends Mutable<?, ?, ?>> statements) {
138         throw new UnsupportedOperationException();
139     }
140
141     @Override
142     Stream<? extends StmtContext<?, ?, ?>> streamDeclared() {
143         throw new UnsupportedOperationException();
144     }
145
146     @Override
147     Stream<? extends StmtContext<?, ?, ?>> streamEffective() {
148         throw new UnsupportedOperationException();
149     }
150
151     @Override
152     StatementContextBase<A, D, E> reparent(final StatementContextBase<?, ?, ?> newParent) {
153         throw new UnsupportedOperationException();
154     }
155
156     /*
157      * KEEP THINGS ORGANIZED!
158      *
159      * below methods exist in the same form in InferredStatementContext/SubstatementContext. If any adjustment is made
160      * here, make sure it is properly updated there.
161      */
162     @Override
163     @Deprecated
164     Optional<SchemaPath> schemaPath() {
165         return substatementGetSchemaPath();
166     }
167
168     @Override
169     public StatementContextBase<?, ?, ?> getParentContext() {
170         return parent;
171     }
172
173     @Override
174     public StorageNodeType getStorageNodeType() {
175         return StorageNodeType.STATEMENT_LOCAL;
176     }
177
178     @Override
179     public StatementContextBase<?, ?, ?> getParentNamespaceStorage() {
180         return parent;
181     }
182
183     @Override
184     public RootStatementContext<?, ?, ?> getRoot() {
185         return parent.getRoot();
186     }
187
188     @Override
189     protected boolean isIgnoringIfFeatures() {
190         return isIgnoringIfFeatures(parent);
191     }
192
193     @Override
194     protected boolean isIgnoringConfig() {
195         return isIgnoringConfig(parent);
196     }
197
198     @Override
199     protected boolean isParentSupportedByFeatures() {
200         return parent.isSupportedByFeatures();
201     }
202 }