Migrate coerceStatementArgument() callers
[yangtools.git] / yang / yang-parser-spi / src / main / java / org / opendaylight / yangtools / yang / parser / spi / meta / StmtContext.java
index e12c732df274535b99c1b6c0bc35a1f68a15211d..1bc1dbfceeb843a73016b36e966fc6c720c9fa2a 100644 (file)
@@ -9,6 +9,7 @@ package org.opendaylight.yangtools.yang.parser.spi.meta;
 
 import static com.google.common.base.Verify.verifyNotNull;
 
+import com.google.common.annotations.Beta;
 import com.google.common.base.VerifyException;
 import com.google.common.collect.Iterables;
 import com.google.common.collect.Streams;
@@ -20,7 +21,6 @@ import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.yangtools.yang.common.QNameModule;
 import org.opendaylight.yangtools.yang.common.YangVersion;
-import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
@@ -29,84 +29,92 @@ import org.opendaylight.yangtools.yang.model.api.meta.StatementSource;
 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
 
-public interface StmtContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>> {
-    /**
-     * Returns the origin of the statement.
-     *
-     * @return origin of statement
-     */
-    @NonNull StatementSource getStatementSource();
-
-    /**
-     * Returns a reference to statement source.
-     *
-     * @return reference of statement source
-     */
-    @NonNull StatementSourceReference getStatementSourceReference();
-
-    /**
-     * See {@link StatementSupport#getPublicView()}.
-     */
-    @NonNull StatementDefinition getPublicDefinition();
+/**
+ * An inference context associated with an instance of a statement.
+ *
+ * @param <A> Argument type
+ * @param <D> Declared Statement representation
+ * @param <E> Effective Statement representation
+ */
+public interface StmtContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
+        extends BoundStmtCtx<A> {
+    @Deprecated(forRemoval = true)
+    default @NonNull StatementDefinition getPublicDefinition() {
+        return publicDefinition();
+    }
 
-    /**
-     * Return the parent statement context, or null if this is the root statement.
-     *
-     * @return context of parent of statement, or null if this is the root statement.
-     */
-    @Nullable StmtContext<?, ?, ?> getParentContext();
+    @Deprecated(forRemoval = true)
+    default @NonNull StatementSource getStatementSource() {
+        return source();
+    }
 
-    /**
-     * Return the parent statement context, forcing a VerifyException if this is the root statement.
-     *
-     * @return context of parent of statement
-     * @throws VerifyException if this statement is the root statement
-     */
-    default @NonNull StmtContext<?, ?, ?> coerceParentContext() {
-        return verifyNotNull(getParentContext(), "Root context %s does not have a parent", this);
+    @Deprecated(forRemoval = true)
+    default @NonNull StatementSourceReference getStatementSourceReference() {
+        return sourceReference();
     }
 
     /**
      * Return the statement argument in literal format.
      *
      * @return raw statement argument string, or null if this statement does not have an argument.
+     * @deprecated Use {@link #rawArgument()} instead.
      */
-    @Nullable String rawStatementArgument();
+    @Deprecated(forRemoval = true)
+    default @Nullable String rawStatementArgument() {
+        return rawArgument();
+    }
 
     /**
      * Return the statement argument in literal format.
      *
      * @return raw statement argument string
      * @throws VerifyException if this statement does not have an argument
+     * @deprecated Use {@link #getRawArgument()} instead.
      */
+    @Deprecated(forRemoval = true)
     default @NonNull String coerceRawStatementArgument() {
-        return verifyNotNull(rawStatementArgument(), "Statement context %s does not have an argument", this);
+        return getRawArgument();
     }
 
     /**
      * Return the statement argument.
      *
      * @return statement argument, or null if this statement does not have an argument
+     * @deprecated Use {@link #argument()} instead.
      */
-    @Nullable A getStatementArgument();
+    @Deprecated(forRemoval = true)
+    default @Nullable A getStatementArgument() {
+        return argument();
+    }
 
     /**
      * Return the statement argument in literal format.
      *
      * @return raw statement argument string
      * @throws VerifyException if this statement does not have an argument
+     * @deprecated Use {@link #getArgument()} instead.
      */
+    @Deprecated(forRemoval = true)
     default @NonNull A coerceStatementArgument() {
-        return verifyNotNull(getStatementArgument(), "Statement context %s does not have an argument", this);
+        return getArgument();
     }
 
     /**
-     * Return the {@link SchemaPath} of this statement. Not all statements have a SchemaPath, in which case
-     * {@link Optional#empty()} is returned.
+     * Return the parent statement context, or null if this is the root statement.
+     *
+     * @return context of parent of statement, or null if this is the root statement.
+     */
+    @Nullable StmtContext<?, ?, ?> getParentContext();
+
+    /**
+     * Return the parent statement context, forcing a VerifyException if this is the root statement.
      *
-     * @return Optional SchemaPath
+     * @return context of parent of statement
+     * @throws VerifyException if this statement is the root statement
      */
-    @NonNull Optional<SchemaPath> getSchemaPath();
+    default @NonNull StmtContext<?, ?, ?> coerceParentContext() {
+        return verifyNotNull(getParentContext(), "Root context %s does not have a parent", this);
+    }
 
     boolean isConfiguration();
 
@@ -124,13 +132,14 @@ public interface StmtContext<A, D extends DeclaredStatement<A>, E extends Effect
      * @return Value, or null if there is no element
      * @throws NamespaceNotAvailableException when the namespace is not available.
      */
-    @NonNull <K, V, T extends K, N extends IdentifierNamespace<K, V>> V getFromNamespace(Class<N> type, T key);
+    <K, V, T extends K, N extends IdentifierNamespace<K, V>> @Nullable V getFromNamespace(Class<@NonNull N> type,
+            T key);
 
     <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromNamespace(Class<N> type);
 
     <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromCurrentStmtCtxNamespace(Class<N> type);
 
-    @NonNull StmtContext<?, ?, ?> getRoot();
+    @NonNull RootStmtContext<?, ?, ?> getRoot();
 
     /**
      * Return declared substatements. These are the statements which are explicitly written in the source model.
@@ -196,7 +205,7 @@ public interface StmtContext<A, D extends DeclaredStatement<A>, E extends Effect
      *
      * @return Original definition, if this statement was copied.
      */
-    Optional<StmtContext<?, ?, ?>> getOriginalCtx();
+    Optional<StmtContext<A, D, E>> getOriginalCtx();
 
     /**
      * Return the context of the previous copy of this statement -- effectively walking towards the source origin
@@ -204,7 +213,7 @@ public interface StmtContext<A, D extends DeclaredStatement<A>, E extends Effect
      *
      * @return Context of the previous copy of this statement, if this statement has been copied.
      */
-    Optional<? extends StmtContext<?, ?, ?>> getPreviousCopyCtx();
+    Optional<StmtContext<A, D, E>> getPreviousCopyCtx();
 
     ModelProcessingPhase getCompletedPhase();
 
@@ -215,6 +224,13 @@ public interface StmtContext<A, D extends DeclaredStatement<A>, E extends Effect
      */
     @NonNull YangVersion getRootVersion();
 
+    /**
+     * An mutable view of an inference context associated with an instance of a statement.
+     *
+     * @param <A> Argument type
+     * @param <D> Declared Statement representation
+     * @param <E> Effective Statement representation
+     */
     interface Mutable<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
             extends StmtContext<A, D, E> {
 
@@ -239,11 +255,11 @@ public interface StmtContext<A, D extends DeclaredStatement<A>, E extends Effect
          * @param <U> value type
          * @throws NamespaceNotAvailableException when the namespace is not available.
          */
-        <K, V, T extends K, U extends V, N extends IdentifierNamespace<K, V>> void addToNs(Class<N> type, T key,
-                U value);
+        <K, V, T extends K, U extends V, N extends IdentifierNamespace<K, V>> void addToNs(Class<@NonNull N> type,
+                T key, U value);
 
         @Override
-        Mutable<?, ?, ?> getRoot();
+        RootStmtContext.Mutable<?, ?, ?> getRoot();
 
         /**
          * Create a child sub-statement, which is a child of this statement, inheriting all attributes from specified
@@ -276,8 +292,22 @@ public interface StmtContext<A, D extends DeclaredStatement<A>, E extends Effect
             return childCopyOf(stmt, type, null);
         }
 
+        @Beta
+        @NonNull Optional<? extends Mutable<?, ?, ?>> copyAsChildOf(Mutable<?, ?, ?> parent, CopyType type,
+                @Nullable QNameModule targetModule);
+
+        @Override
+        default Collection<? extends StmtContext<?, ?, ?>> declaredSubstatements() {
+            return mutableDeclaredSubstatements();
+        }
+
         @NonNull Collection<? extends Mutable<?, ?, ?>> mutableDeclaredSubstatements();
 
+        @Override
+        default Collection<? extends StmtContext<?, ?, ?>> effectiveSubstatements() {
+            return mutableEffectiveSubstatements();
+        }
+
         @NonNull Collection<? extends Mutable<?, ?, ?>> mutableEffectiveSubstatements();
 
         /**
@@ -301,7 +331,7 @@ public interface StmtContext<A, D extends DeclaredStatement<A>, E extends Effect
          * @param stmt
          *            to be added to namespace map
          */
-        <K, KT extends K, N extends StatementNamespace<K, ?, ?>> void addContext(Class<N> namespace, KT key,
+        <K, KT extends K, N extends StatementNamespace<K, ?, ?>> void addContext(Class<@NonNull N> namespace, KT key,
                 StmtContext<?, ?, ?> stmt);
 
         /**
@@ -348,8 +378,39 @@ public interface StmtContext<A, D extends DeclaredStatement<A>, E extends Effect
         void setRootIdentifier(SourceIdentifier identifier);
 
         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);
+    }
 
-        // FIXME: this seems to be unused, but looks useful.
-        void setCompletedPhase(ModelProcessingPhase completedPhase);
+    /**
+     * 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);
     }
 }