Move findSubstatementArgument()/hasSubstatement() to BoundStmtCtx
[yangtools.git] / yang / yang-parser-spi / src / main / java / org / opendaylight / yangtools / yang / parser / spi / meta / StmtContext.java
index e47ea2f7ed5a69a98958d55072694369bcc08851..dcbafed8370a66040a122221326d568ceb72555d 100644 (file)
@@ -9,18 +9,17 @@ 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;
 import java.util.Collection;
-import java.util.Map;
 import java.util.Optional;
 import java.util.stream.Stream;
 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;
@@ -36,108 +35,94 @@ import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReferenc
  * @param <D> Declared Statement representation
  * @param <E> Effective Statement representation
  */
-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();
+public interface StmtContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
+        extends NamespaceStmtCtx, BoundStmtCtxCompat<A, D> {
+    @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 Optional SchemaPath
+     * @return context of parent of statement, or null if this is the root statement.
      */
-    @NonNull Optional<SchemaPath> getSchemaPath();
+    @Nullable StmtContext<?, ?, ?> getParentContext();
 
-    boolean isConfiguration();
+    /**
+     * 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);
+    }
 
     boolean isEnabledSemanticVersioning();
 
     /**
-     * Return a value associated with specified key within a namespace.
+     * Returns the model root for this statement.
      *
-     * @param type Namespace type
-     * @param key Key
-     * @param <K> namespace key type
-     * @param <V> namespace value type
-     * @param <N> namespace type
-     * @param <T> key type
-     * @return Value, or null if there is no element
-     * @throws NamespaceNotAvailableException when the namespace is not available.
+     * @return root context of statement
      */
-    @NonNull <K, V, T extends K, N extends IdentifierNamespace<K, V>> V getFromNamespace(Class<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.
@@ -162,11 +147,6 @@ public interface StmtContext<A, D extends DeclaredStatement<A>, E extends Effect
         return Streams.concat(declaredSubstatements().stream(), effectiveSubstatements().stream());
     }
 
-    /**
-     * Builds {@link DeclaredStatement} for statement context.
-     */
-    D buildDeclared();
-
     /**
      * Builds {@link EffectiveStatement} for statement context.
      */
@@ -180,7 +160,7 @@ public interface StmtContext<A, D extends DeclaredStatement<A>, E extends Effect
 
     /*
      * FIXME: YANGTOOLS-784: the next three methods are closely related to the copy process:
-     *        - getCopyHistory() is a brief summary of what went on
+     *        - copyHistory() is a brief summary of what went on
      *        - getOriginalContext() points to the CopyHistory.ORIGINAL
      *        - getPreviousCopyCtx() points to the immediate predecessor forming a singly-linked list terminated
      *          at getOriginalContext()
@@ -191,19 +171,12 @@ public interface StmtContext<A, D extends DeclaredStatement<A>, E extends Effect
      *        and hence should become arguments to that method.
      */
 
-    /**
-     * Return the executive summary of the copy process that has produced this context.
-     *
-     * @return A simplified summary of the copy process.
-     */
-    CopyHistory getCopyHistory();
-
     /**
      * Return the statement context of the original definition, if this statement is an instantiated copy.
      *
      * @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
@@ -211,17 +184,10 @@ 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();
 
-    /**
-     * Return version of root statement context.
-     *
-     * @return version of root statement context
-     */
-    @NonNull YangVersion getRootVersion();
-
     /**
      * An mutable view of an inference context associated with an instance of a statement.
      *
@@ -253,11 +219,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
@@ -290,6 +256,21 @@ public interface StmtContext<A, D extends DeclaredStatement<A>, E extends Effect
             return childCopyOf(stmt, type, null);
         }
 
+        /**
+         * Create a replica of this statement as a substatement of specified {@code parent}. The replica must not be
+         * modified and acts as a source of {@link EffectiveStatement} from outside of {@code parent}'s subtree.
+         *
+         * @param parent Parent of the replica statement
+         * @return replica of this statement
+         * @throws IllegalArgumentException if this statement cannot be replicated into parent, for example because it
+         *                                  comes from an alien implementation.
+         */
+        @NonNull Mutable<A, D, E> replicaAsChildOf(Mutable<?, ?, ?> parent);
+
+        @Beta
+        @NonNull Optional<? extends Mutable<?, ?, ?>> copyAsChildOf(Mutable<?, ?, ?> parent, CopyType type,
+                @Nullable QNameModule targetModule);
+
         @Override
         default Collection<? extends StmtContext<?, ?, ?>> declaredSubstatements() {
             return mutableDeclaredSubstatements();
@@ -325,7 +306,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);
 
         /**
@@ -372,8 +353,5 @@ public interface StmtContext<A, D extends DeclaredStatement<A>, E extends Effect
         void setRootIdentifier(SourceIdentifier identifier);
 
         void setIsSupportedToBuildEffective(boolean isSupportedToBuild);
-
-        // FIXME: this seems to be unused, but looks useful.
-        void setCompletedPhase(ModelProcessingPhase completedPhase);
     }
 }