Remove StatementNamespace
[yangtools.git] / parser / yang-parser-reactor / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / reactor / StatementContextBase.java
index c7bd79a01adc534bd35adac7fbf69b5c53ca7428..583bf61d53fc5131650e18e00eda2197943baec2 100644 (file)
@@ -13,7 +13,6 @@ import static com.google.common.base.Verify.verify;
 import static com.google.common.base.Verify.verifyNotNull;
 import static java.util.Objects.requireNonNull;
 
-import com.google.common.base.VerifyException;
 import com.google.common.collect.ImmutableCollection;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMultimap;
@@ -46,7 +45,6 @@ import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour;
 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceKeyCriterion;
 import org.opendaylight.yangtools.yang.parser.spi.meta.ParserNamespace;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementFactory;
-import org.opendaylight.yangtools.yang.parser.spi.meta.StatementNamespace;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupport;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupport.CopyPolicy;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
@@ -73,7 +71,8 @@ abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E extends
         /**
          * Invoked whenever a new item is added to a namespace.
          */
-        void namespaceItemAdded(StatementContextBase<?, ?, ?> context, Class<?> namespace, Object key, Object value);
+        void namespaceItemAdded(StatementContextBase<?, ?, ?> context, ParserNamespace<?, ?> namespace, Object key,
+            Object value);
     }
 
     /**
@@ -150,12 +149,6 @@ abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E extends
      */
     private byte executionOrder;
 
-    /**
-     * This field should live in AbstractResumedStatement, but is placed here for memory efficiency to squat in the
-     * alignment shadow of {@link #bitsAight} and {@link #executionOrder}.
-     */
-    private boolean implicitDeclaredFlag;
-
     // TODO: we a single byte of alignment shadow left, we should think how we can use it to cache information we build
     //       during InferredStatementContext.tryToReusePrototype(). We usually end up being routed to
     //       copyAsChildOfImpl() -- which performs an eager instantiation and checks for changes afterwards. We should
@@ -198,18 +191,12 @@ abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E extends
     }
 
     private static byte historyFlags(final CopyType copyType) {
-        switch (copyType) {
-            case ADDED_BY_AUGMENTATION:
-                return COPY_ADDED_BY_AUGMENTATION;
-            case ADDED_BY_USES:
-                return COPY_ADDED_BY_USES;
-            case ADDED_BY_USES_AUGMENTATION:
-                return COPY_ADDED_BY_AUGMENTATION | COPY_ADDED_BY_USES;
-            case ORIGINAL:
-                return COPY_ORIGINAL;
-            default:
-                throw new VerifyException("Unhandled type " + copyType);
-        }
+        return switch (copyType) {
+            case ADDED_BY_AUGMENTATION -> COPY_ADDED_BY_AUGMENTATION;
+            case ADDED_BY_USES -> COPY_ADDED_BY_USES;
+            case ADDED_BY_USES_AUGMENTATION -> COPY_ADDED_BY_AUGMENTATION | COPY_ADDED_BY_USES;
+            case ORIGINAL -> COPY_ORIGINAL;
+        };
     }
 
     @Override
@@ -276,8 +263,8 @@ abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E extends
     }
 
     @Override
-    public final <K, V, T extends K, U extends V, N extends ParserNamespace<K, V>> void addToNs(
-            final Class<@NonNull N> type, final T key, final U value) {
+    public final <K, V, T extends K, U extends V> void addToNs(final ParserNamespace<K, V> type, final T key,
+            final U value) {
         addToNamespace(type, key, value);
     }
 
@@ -331,27 +318,37 @@ abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E extends
 
     @Override
     public final <X, Y extends DeclaredStatement<X>, Z extends EffectiveStatement<X, Y>>
-            Mutable<X, Y, Z> addUndeclaredSubstatement(final StatementSupport<X, Y, Z> support, final X arg) {
+            Mutable<X, Y, Z> createUndeclaredSubstatement(final StatementSupport<X, Y, Z> support, final X arg) {
         requireNonNull(support);
         checkArgument(support instanceof UndeclaredStatementFactory, "Unsupported statement support %s", support);
 
         final var ret = new UndeclaredStmtCtx<>(this, support, arg);
         support.onStatementAdded(ret);
-        addEffectiveSubstatement(ret);
         return ret;
     }
 
     final List<ReactorStmtCtx<?, ?, ?>> addEffectiveSubstatement(final List<ReactorStmtCtx<?, ?, ?>> effective,
             final Mutable<?, ?, ?> substatement) {
-        verifyStatement(substatement);
-
+        final ReactorStmtCtx<?, ?, ?> stmt = verifyStatement(substatement);
         final List<ReactorStmtCtx<?, ?, ?>> resized = beforeAddEffectiveStatement(effective, 1);
-        final ReactorStmtCtx<?, ?, ?> stmt = (ReactorStmtCtx<?, ?, ?>) substatement;
         ensureCompletedExecution(stmt);
         resized.add(stmt);
         return resized;
     }
 
+    static final void afterAddEffectiveSubstatement(final Mutable<?, ?, ?> substatement) {
+        // Undeclared statements still need to have 'onDeclarationFinished()' triggered
+        if (substatement instanceof UndeclaredStmtCtx<?, ?, ?> undeclared) {
+            finishDeclaration(undeclared);
+        }
+    }
+
+    // Split out to keep generics working without a warning
+    private static <X, Y extends DeclaredStatement<X>, Z extends EffectiveStatement<X, Y>> void finishDeclaration(
+            final UndeclaredStmtCtx<X, Y, Z> substatement) {
+        substatement.definition().onDeclarationFinished(substatement, ModelProcessingPhase.FULL_DECLARATION);
+    }
+
     @Override
     public final void addEffectiveSubstatements(final Collection<? extends Mutable<?, ?, ?>> statements) {
         if (!statements.isEmpty()) {
@@ -380,9 +377,10 @@ abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E extends
     abstract Iterator<ReactorStmtCtx<?, ?, ?>> effectiveChildrenToComplete();
 
     // exposed for InferredStatementContext only
-    final void ensureCompletedPhase(final Mutable<?, ?, ?> stmt) {
-        verifyStatement(stmt);
-        ensureCompletedExecution((ReactorStmtCtx<?, ?, ?>) stmt);
+    final ReactorStmtCtx<?, ?, ?> ensureCompletedPhase(final Mutable<?, ?, ?> stmt) {
+        final var ret = verifyStatement(stmt);
+        ensureCompletedExecution(ret);
+        return ret;
     }
 
     // Make sure target statement has transitioned at least to our phase (if we have one). This method is just before we
@@ -398,8 +396,9 @@ abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E extends
         verify(stmt.tryToCompletePhase(executionOrder), "Statement %s cannot complete phase %s", stmt, executionOrder);
     }
 
-    private static void verifyStatement(final Mutable<?, ?, ?> stmt) {
+    private static ReactorStmtCtx<?, ?, ?> verifyStatement(final Mutable<?, ?, ?> stmt) {
         verify(stmt instanceof ReactorStmtCtx, "Unexpected statement %s", stmt);
+        return (ReactorStmtCtx<?, ?, ?>) stmt;
     }
 
     private List<ReactorStmtCtx<?, ?, ?>> beforeAddEffectiveStatement(final List<ReactorStmtCtx<?, ?, ?>> effective,
@@ -425,27 +424,14 @@ abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E extends
     @Override
     final E createEffective() {
         final E result = createEffective(definition.getFactory());
-        if (result instanceof MutableStatement) {
-            getRoot().addMutableStmtToSeal((MutableStatement) result);
+        if (result instanceof MutableStatement mutable) {
+            getRoot().addMutableStmtToSeal(mutable);
         }
         return result;
     }
 
     abstract @NonNull E createEffective(@NonNull StatementFactory<A, D, E> factory);
 
-    /**
-     * Routing of the request to build an effective statement from {@link InferredStatementContext} towards the original
-     * definition site. This is needed to pick the correct instantiation method: for declared statements we will
-     * eventually land in {@link AbstractResumedStatement}, for underclared statements that will be
-     * {@link UndeclaredStmtCtx}.
-     *
-     * @param factory Statement factory
-     * @param ctx Inferred statement context, i.e. where the effective statement is instantiated
-     * @return Built effective stateue
-     */
-    abstract @NonNull E createInferredEffective(@NonNull StatementFactory<A, D, E> factory,
-        @NonNull InferredStatementContext<A, D, E> ctx);
-
     /**
      * Return a stream of declared statements which can be built into an {@link EffectiveStatement}, as per
      * {@link StmtContext#buildEffective()} contract.
@@ -607,7 +593,7 @@ abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E extends
         return definition;
     }
 
-    final <K, V, N extends ParserNamespace<K, V>> void onNamespaceItemAddedAction(final Class<N> type, final K key,
+    final <K, V> void onNamespaceItemAddedAction(final ParserNamespace<K, V> type, final K key,
             final OnNamespaceItemAdded listener) {
         final Object potential = getFromNamespace(type, key);
         if (potential != null) {
@@ -624,7 +610,7 @@ abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E extends
         });
     }
 
-    final <K, V, N extends ParserNamespace<K, V>> void onNamespaceItemAddedAction(final Class<N> type,
+    final <K, V> void onNamespaceItemAddedAction(final ParserNamespace<K, V> type,
             final ModelProcessingPhase phase, final NamespaceKeyCriterion<K> criterion,
             final OnNamespaceItemAdded listener) {
         final Optional<Entry<K, V>> existing = getFromNamespace(type, criterion);
@@ -635,7 +621,7 @@ abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E extends
             return;
         }
 
-        final NamespaceBehaviourWithListeners<K, V, N> behaviour = getBehaviour(type);
+        final NamespaceBehaviourWithListeners<K, V> behaviour = getBehaviour(type);
         behaviour.addListener(new PredicateValueAddedListener<K, V>(this) {
             @Override
             boolean onValueAdded(final K key, final V value) {
@@ -650,8 +636,8 @@ abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E extends
         });
     }
 
-    final <K, V, N extends ParserNamespace<K, V>> void selectMatch(final Class<N> type,
-            final NamespaceKeyCriterion<K> criterion, final OnNamespaceItemAdded listener) {
+    final <K, V> void selectMatch(final ParserNamespace<K, V> type, final NamespaceKeyCriterion<K> criterion,
+            final OnNamespaceItemAdded listener) {
         final Optional<Entry<K, V>> optMatch = getFromNamespace(type, criterion);
         checkState(optMatch.isPresent(), "Failed to find a match for criterion %s in namespace %s node %s", criterion,
             type, this);
@@ -659,7 +645,7 @@ abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E extends
         listener.namespaceItemAdded(StatementContextBase.this, type, match.getKey(), match.getValue());
     }
 
-    final <K, V, N extends ParserNamespace<K, V>> void waitForPhase(final Object value, final Class<N> type,
+    final <K, V> void waitForPhase(final Object value, final ParserNamespace<K, V> type,
             final ModelProcessingPhase phase, final NamespaceKeyCriterion<K> criterion,
             final OnNamespaceItemAdded listener) {
         ((StatementContextBase<?, ? ,?>) value).addPhaseCompletedListener(phase,
@@ -669,13 +655,12 @@ abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E extends
             });
     }
 
-    private <K, V, N extends ParserNamespace<K, V>> NamespaceBehaviourWithListeners<K, V, N> getBehaviour(
-            final Class<N> type) {
-        final NamespaceBehaviour<K, V, N> behaviour = getBehaviourRegistry().getNamespaceBehaviour(type);
+    private <K, V> NamespaceBehaviourWithListeners<K, V> getBehaviour(final ParserNamespace<K, V> type) {
+        final NamespaceBehaviour<K, V> behaviour = getBehaviourRegistry().getNamespaceBehaviour(type);
         checkArgument(behaviour instanceof NamespaceBehaviourWithListeners, "Namespace %s does not support listeners",
             type);
 
-        return (NamespaceBehaviourWithListeners<K, V, N>) behaviour;
+        return (NamespaceBehaviourWithListeners<K, V>) behaviour;
     }
 
     private static <T> Multimap<ModelProcessingPhase, T> newMultimap() {
@@ -732,8 +717,8 @@ abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E extends
     }
 
     @Override
-    public final <K, KT extends K, N extends StatementNamespace<K, ?, ?>> void addContext(
-            final Class<@NonNull N> namespace, final KT key,final StmtContext<?, ?, ?> stmt) {
+    public final <K, KT extends K, Y extends DeclaredStatement<?>, Z extends EffectiveStatement<?, Y>> void addContext(
+            final ParserNamespace<K, StmtContext<?, Y, Z>> namespace, final KT key, final StmtContext<?, Y, Z> stmt) {
         addContextToNamespace(namespace, key, stmt);
     }
 
@@ -778,7 +763,7 @@ abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E extends
             return null;
         }
 
-        parent.ensureCompletedPhase(copy);
+        parent.ensureCompletedExecution(copy);
         return canReuseCurrent(copy) ? this : copy;
     }
 
@@ -793,10 +778,10 @@ abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E extends
     public final Mutable<?, ?, ?> childCopyOf(final StmtContext<?, ?, ?> stmt, final CopyType type,
             final QNameModule targetModule) {
         checkEffectiveModelCompleted(stmt);
-        if (stmt instanceof StatementContextBase) {
-            return childCopyOf((StatementContextBase<?, ?, ?>) stmt, type, targetModule);
-        } else if (stmt instanceof ReplicaStatementContext) {
-            return ((ReplicaStatementContext<?, ?, ?>) stmt).replicaAsChildOf(this);
+        if (stmt instanceof StatementContextBase<?, ?, ?> base) {
+            return childCopyOf(base, type, targetModule);
+        } else if (stmt instanceof ReplicaStatementContext<?, ?, ?> replica) {
+            return replica.replicaAsChildOf(this);
         } else {
             throw new IllegalArgumentException("Unsupported statement " + stmt);
         }
@@ -812,22 +797,14 @@ abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E extends
         if (implicitParent.isPresent()) {
             result = new UndeclaredStmtCtx(this, implicitParent.orElseThrow(), original, type);
 
-            final CopyType childCopyType;
-            switch (type) {
-                case ADDED_BY_AUGMENTATION:
-                    childCopyType = CopyType.ORIGINAL;
-                    break;
-                case ADDED_BY_USES_AUGMENTATION:
-                    childCopyType = CopyType.ADDED_BY_USES;
-                    break;
-                case ADDED_BY_USES:
-                case ORIGINAL:
-                default:
-                    childCopyType = type;
-            }
-
+            final CopyType childCopyType = switch (type) {
+                case ADDED_BY_AUGMENTATION -> CopyType.ORIGINAL;
+                case ADDED_BY_USES_AUGMENTATION -> CopyType.ADDED_BY_USES;
+                case ADDED_BY_USES, ORIGINAL -> type;
+            };
             copy = new InferredStatementContext<>(result, original, childCopyType, type, targetModule);
             result.addEffectiveSubstatement(copy);
+            result.definition.onStatementAdded(result);
         } else {
             result = copy = new InferredStatementContext<>(this, original, type, type, targetModule);
         }
@@ -877,13 +854,4 @@ abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E extends
      * @return True if {@link #allSubstatements()} and {@link #allSubstatementsStream()} would return an empty stream.
      */
     abstract boolean hasEmptySubstatements();
-
-    // Note: these two are exposed for AbstractResumedStatement only
-    final boolean getImplicitDeclaredFlag() {
-        return implicitDeclaredFlag;
-    }
-
-    final void setImplicitDeclaredFlag() {
-        implicitDeclaredFlag = true;
-    }
 }