Move findSubstatementArgument()/hasSubstatement() to BoundStmtCtx 78/94078/4
authorRobert Varga <robert.varga@pantheon.tech>
Sun, 6 Dec 2020 12:49:28 +0000 (13:49 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Sun, 6 Dec 2020 20:42:33 +0000 (21:42 +0100)
Since we have StmtContext and BoundStmtCtx unified, move
hasSubstatement() into BoundStmtCtx and implement it in
ReactorStmtCtx. This allows better access to this functionality.

JIRA: YANGTOOLS-1157
Change-Id: I33eaba254c82e4b8e0152c03d8da91fef4453f63
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
yang/yang-parser-reactor/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/InferredStatementContext.java
yang/yang-parser-reactor/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/ReactorStmtCtx.java
yang/yang-parser-spi/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/BoundStmtCtx.java
yang/yang-parser-spi/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/StmtContext.java
yang/yang-parser-spi/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/StmtContextDefaults.java [deleted file]
yang/yang-parser-spi/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/StmtContextUtils.java

index a424cf795bb860b24e304046c0b6de6dcd2b467a..1f062e62e66966cba7db8632ab5522abf9597475 100644 (file)
@@ -37,7 +37,6 @@ import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.OnDemandSchemaTreeStorageNode;
 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.StorageNodeType;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
-import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextDefaults;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
 import org.slf4j.Logger;
@@ -191,7 +190,7 @@ final class InferredStatementContext<A, D extends DeclaredStatement<A>, E extend
     public <X, Z extends EffectiveStatement<X, ?>> @NonNull Optional<X> findSubstatementArgument(
             final @NonNull Class<Z> type) {
         if (substatements instanceof List) {
-            return StmtContextDefaults.findSubstatementArgument(this, type);
+            return super.findSubstatementArgument(type);
         }
 
         final Optional<X> templateArg = prototype.findSubstatementArgument(type);
@@ -207,7 +206,7 @@ final class InferredStatementContext<A, D extends DeclaredStatement<A>, E extend
 
     @Override
     public boolean hasSubstatement(final @NonNull Class<? extends EffectiveStatement<?, ?>> type) {
-        return substatements instanceof List ? StmtContextDefaults.hasSubstatement(prototype, type)
+        return substatements instanceof List ? super.hasSubstatement(type)
             // We do not allow deletion of partially-materialized statements, hence this is accurate
             : prototype.hasSubstatement(type);
     }
index 212cae9035165a40bbe7b88cc63c253340da73bc..e51579b703b4743ed8036911c9f32d62e5f9f810 100644 (file)
@@ -238,6 +238,23 @@ abstract class ReactorStmtCtx<A, D extends DeclaredStatement<A>, E extends Effec
         return getOriginalCtx().map(StmtContext::buildEffective).orElse(null);
     }
 
+    @Override
+    // Non-final due to InferredStatementContext's override
+    public <X, Z extends EffectiveStatement<X, ?>> @NonNull Optional<X> findSubstatementArgument(
+            final @NonNull Class<Z> type) {
+        return allSubstatementsStream()
+            .filter(ctx -> ctx.isSupportedToBuildEffective() && ctx.producesEffective(type))
+            .findAny()
+            .map(ctx -> (X) ctx.getArgument());
+    }
+
+    @Override
+    // Non-final due to InferredStatementContext's override
+    public boolean hasSubstatement(final @NonNull Class<? extends EffectiveStatement<?, ?>> type) {
+        return allSubstatementsStream()
+            .anyMatch(ctx -> ctx.isSupportedToBuildEffective() && ctx.producesEffective(type));
+    }
+
     @Override
     @Deprecated
     @SuppressWarnings("unchecked")
index 147b1ec2f23357a88c3430478729d4276bb4e5a2..80b08c00444674ae268a5a0c5b0cce4c0cc43f48 100644 (file)
@@ -11,9 +11,11 @@ import static com.google.common.base.Verify.verifyNotNull;
 
 import com.google.common.annotations.Beta;
 import com.google.common.base.VerifyException;
+import java.util.Optional;
 import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.yangtools.yang.common.YangVersion;
+import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
 
 /**
  * A {@link CommonStmtCtx} which has additionally been bound to a {@link StatementSupport}. It provides
@@ -46,4 +48,24 @@ public interface BoundStmtCtx<A> extends CommonStmtCtx {
      * @return YANG version used to bind this statement
      */
     @NonNull YangVersion yangVersion();
+
+    /**
+     * Search of any child statement context of specified type and return its argument. If such a statement exists, it
+     * is assumed to have the right argument. Users should be careful to use this method for statements which have
+     * cardinality {@code 0..1}, otherwise this method can return any one of the statement's argument.
+     *
+     * @param <X> Substatement argument type
+     * @param <Z> Substatement effective statement representation
+     * @param type Effective statement representation being look up
+     * @return {@link Optional#empty()} if no statement exists, otherwise the argument value
+     */
+    <X, Z extends EffectiveStatement<X, ?>> @NonNull Optional<X> findSubstatementArgument(@NonNull Class<Z> type);
+
+    /**
+     * Check if there is any child statement context of specified type.
+     *
+     * @param type Effective statement representation being look up
+     * @return True if such a child statement exists, false otherwise
+     */
+    boolean hasSubstatement(@NonNull Class<? extends EffectiveStatement<?, ?>> type);
 }
index 1f090b3c0f26040cea7e6a2bf4e0f2cdf9c9bc36..dcbafed8370a66040a122221326d568ceb72555d 100644 (file)
@@ -354,38 +354,4 @@ public interface StmtContext<A, D extends DeclaredStatement<A>, E extends Effect
 
         void setIsSupportedToBuildEffective(boolean isSupportedToBuild);
     }
-
-    /**
-     * Search of any child statement context of specified type and return its argument. If such a statement exists, it
-     * is assumed to have the right argument. Users should be careful to use this method for statements which have
-     * cardinality {@code 0..1}, otherwise this method can return any one of the statement's argument.
-     *
-     * <p>
-     * The default implementation defers to
-     * {@link StmtContextDefaults#findSubstatementArgument(StmtContext, Class)}, subclasses are expected to provide
-     * optimized implementation if possible.
-     *
-     * @param <X> Substatement argument type
-     * @param <Z> Substatement effective statement representation
-     * @param type Effective statement representation being look up
-     * @return {@link Optional#empty()} if no statement exists, otherwise the argument value
-     */
-    default <X, Z extends EffectiveStatement<X, ?>> @NonNull Optional<X> findSubstatementArgument(
-            final @NonNull Class<Z> type) {
-        return StmtContextDefaults.findSubstatementArgument(this, type);
-    }
-
-    /**
-     * Check if there is any child statement context of specified type.
-     *
-     * <p>
-     * The default implementation defers to {@link StmtContextDefaults#hasSubstatement(StmtContext, Class)},
-     * subclasses are expected to provide optimized implementation if possible.
-     *
-     * @param type Effective statement representation being look up
-     * @return True if such a child statement exists, false otherwise
-     */
-    default boolean hasSubstatement(final @NonNull Class<? extends EffectiveStatement<?, ?>> type) {
-        return StmtContextDefaults.hasSubstatement(this, type);
-    }
 }
diff --git a/yang/yang-parser-spi/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/StmtContextDefaults.java b/yang/yang-parser-spi/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/StmtContextDefaults.java
deleted file mode 100644 (file)
index e885bf8..0000000
+++ /dev/null
@@ -1,53 +0,0 @@
-/*
- * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others.  All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.yangtools.yang.parser.spi.meta;
-
-import java.util.Optional;
-import org.eclipse.jdt.annotation.NonNull;
-import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
-
-/**
- * Default implementations for various {@link StmtContext} methods. Code hosted here is meant for use by actual
- * {@link StmtContext} implementations, not for general use by others.
- */
-public final class StmtContextDefaults {
-    private StmtContextDefaults() {
-        // Hidden on purpose
-    }
-
-    /**
-     * Default implementation of {@link StmtContext#findSubstatementArgument(Class)}. See that method for API contract.
-     *
-     * @param <A> Substatement argument type
-     * @param <E> Substatement effective statement representation
-     * @param stmt Statement context to search
-     * @param type Effective statement representation being look up
-     * @return Effective statement argument, if found
-     */
-    @SuppressWarnings({"unchecked" })
-    public static <A, E extends EffectiveStatement<A, ?>> @NonNull Optional<A> findSubstatementArgument(
-            final @NonNull StmtContext<?, ?, ?> stmt, final @NonNull Class<E> type) {
-        return stmt.allSubstatementsStream()
-                .filter(ctx -> ctx.isSupportedToBuildEffective() && ctx.producesEffective(type))
-                .findAny()
-                .map(ctx -> (A) ctx.getArgument());
-    }
-
-    /**
-     * Default implementation of {@link StmtContext#hasSubstatement(Class)}. See that method for API contract.
-     *
-     * @param stmt Statement context to search
-     * @param type Effective statement representation being look up
-     * @return True if a match is found, false otherwise
-     */
-    public static boolean hasSubstatement(final @NonNull StmtContext<?, ?, ?> stmt,
-            final @NonNull Class<? extends EffectiveStatement<?, ?>> type) {
-        return stmt.allSubstatementsStream()
-            .anyMatch(ctx -> ctx.isSupportedToBuildEffective() && ctx.producesEffective(type));
-    }
-}
index 3a0a1c8481b97d0247a17621011a057ed2efdead..8aaad4478c78a9ba81d70d04fb717b69ed4c9b8b 100644 (file)
@@ -151,7 +151,7 @@ public final class StmtContextUtils {
      * @param <A> statement argument type
      * @param <D> declared statement type
      * @return statement context that was searched for or null if was not found
-     * @deprecated Use {@link StmtContext#findSubstatementArgument(Class)} instead.
+     * @deprecated Use {@link BoundStmtCtx#findSubstatementArgument(Class)} instead.
      */
     @Deprecated(forRemoval = true)
     public static <A, D extends DeclaredStatement<A>> StmtContext<A, ?, ?> findFirstSubstatement(