Remove NamespaceStmtCtx.get(All)FromNamespace()
[yangtools.git] / parser / yang-parser-reactor / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / reactor / StatementContextBase.java
index 1be883cd2b2213fea83f50024247b09c67e87a7c..b9aaaf70577634f9e79a7e1881966a40248576d8 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);
     }
 
@@ -342,10 +329,8 @@ abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E extends
 
     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;
@@ -353,8 +338,8 @@ abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E extends
 
     static final void afterAddEffectiveSubstatement(final Mutable<?, ?, ?> substatement) {
         // Undeclared statements still need to have 'onDeclarationFinished()' triggered
-        if (substatement instanceof UndeclaredStmtCtx) {
-            finishDeclaration((UndeclaredStmtCtx<?, ?, ?>) substatement);
+        if (substatement instanceof UndeclaredStmtCtx<?, ?, ?> undeclared) {
+            finishDeclaration(undeclared);
         }
     }
 
@@ -392,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
@@ -410,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,
@@ -437,8 +424,8 @@ 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;
     }
@@ -452,7 +439,7 @@ abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E extends
      * @return Stream of supported declared statements.
      */
     // FIXME: we really want to unify this with streamEffective(), under its name
-    abstract Stream<? extends @NonNull StmtContext<?, ?, ?>> streamDeclared();
+    abstract Stream<? extends @NonNull ReactorStmtCtx<?, ?, ?>> streamDeclared();
 
     /**
      * Return a stream of inferred statements which can be built into an {@link EffectiveStatement}, as per
@@ -461,7 +448,7 @@ abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E extends
      * @return Stream of supported effective statements.
      */
     // FIXME: this method is currently a misnomer, but unifying with streamDeclared() would make this accurate again
-    abstract Stream<? extends @NonNull StmtContext<?, ?, ?>> streamEffective();
+    abstract Stream<? extends @NonNull ReactorStmtCtx<?, ?, ?>> streamEffective();
 
     @Override
     final boolean doTryToCompletePhase(final byte targetOrder) {
@@ -606,9 +593,9 @@ 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);
+        final Object potential = namespaceItem(type, key);
         if (potential != null) {
             LOG.trace("Listener on {} key {} satisfied immediately", type, key);
             listener.namespaceItemAdded(this, type, key, potential);
@@ -623,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);
@@ -634,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) {
@@ -649,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);
@@ -658,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,
@@ -668,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() {
@@ -731,13 +717,13 @@ 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, C extends StmtContext<?, ?, ?>> void addContext(
+            final ParserNamespace<K, ? super C> namespace, final KT key, final C stmt) {
         addContextToNamespace(namespace, key, stmt);
     }
 
     @Override
-    public final Optional<? extends Mutable<?, ?, ?>> copyAsChildOf(final Mutable<?, ?, ?> parent, final CopyType type,
+    public final Optional<Mutable<A, D, E>> copyAsChildOf(final Mutable<?, ?, ?> parent, final CopyType type,
             final QNameModule targetModule) {
         checkEffectiveModelCompleted(this);
         return Optional.ofNullable(copyAsChildOfImpl(parent, type, targetModule));
@@ -777,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;
     }
 
@@ -792,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);
         }
@@ -811,20 +797,11 @@ 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);
@@ -858,15 +835,13 @@ abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E extends
         if (optImplicit.isEmpty()) {
             return original;
         }
-
-        checkArgument(original instanceof StatementContextBase, "Unsupported original %s", original);
-        final var origBase = (StatementContextBase<?, ?, ?>)original;
-
-        @SuppressWarnings({ "rawtypes", "unchecked" })
-        final UndeclaredStmtCtx<?, ?, ?> result = new UndeclaredStmtCtx(origBase, optImplicit.orElseThrow());
-        result.addEffectiveSubstatement(origBase.reparent(result));
-        result.setCompletedPhase(original.getCompletedPhase());
-        return result;
+        if (original instanceof StatementContextBase<?, ?, ?> origBase) {
+            final var result = new UndeclaredStmtCtx<>(origBase, optImplicit.orElseThrow());
+            result.addEffectiveSubstatement(origBase.reparent(result));
+            result.setCompletedPhase(original.getCompletedPhase());
+            return result;
+        }
+        throw new IllegalArgumentException("Unsupported original " + original);
     }
 
     abstract StatementContextBase<A, D, E> reparent(StatementContextBase<?, ?, ?> newParent);
@@ -877,13 +852,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;
-    }
 }