Expand reactor documentation a bit
[yangtools.git] / yang / yang-parser-spi / src / main / java / org / opendaylight / yangtools / yang / parser / spi / meta / StmtContext.java
index 3d9e4e7dcfdbd7e46534e74a13963c9939ccd596..39c7bcfd0291f7b2b3391cb8baa694e9e59f150e 100644 (file)
@@ -7,14 +7,17 @@
  */
 package org.opendaylight.yangtools.yang.parser.spi.meta;
 
+import static com.google.common.base.Verify.verifyNotNull;
+
+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 javax.annotation.Nonnull;
-import javax.annotation.Nullable;
+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;
@@ -26,35 +29,83 @@ 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;
 
+/**
+ * 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>> {
+    /**
+     * Returns the origin of the statement.
+     *
+     * @return origin of statement
+     */
+    @NonNull StatementSource getStatementSource();
 
-    @Nonnull
-    StatementSource getStatementSource();
-
-    @Nonnull
-    StatementSourceReference getStatementSourceReference();
+    /**
+     * Returns a reference to statement source.
+     *
+     * @return reference of statement source
+     */
+    @NonNull StatementSourceReference getStatementSourceReference();
 
-    @Nonnull
-    StatementDefinition getPublicDefinition();
+    /**
+     * See {@link StatementSupport#getPublicView()}.
+     */
+    @NonNull StatementDefinition getPublicDefinition();
 
     /**
      * 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();
+    @Nullable StmtContext<?, ?, ?> getParentContext();
+
+    /**
+     * 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);
+    }
+
+    /**
+     * Return the statement argument in literal format.
+     *
+     * @return raw statement argument string, or null if this statement does not have an argument.
+     */
+    @Nullable String rawStatementArgument();
 
     /**
      * Return the statement argument in literal format.
      *
      * @return raw statement argument string
+     * @throws VerifyException if this statement does not have an argument
      */
-    @Nullable
-    String rawStatementArgument();
+    default @NonNull String coerceRawStatementArgument() {
+        return verifyNotNull(rawStatementArgument(), "Statement context %s does not have an argument", this);
+    }
 
-    @Nullable
-    A getStatementArgument();
+    /**
+     * Return the statement argument.
+     *
+     * @return statement argument, or null if this statement does not have an argument
+     */
+    @Nullable A getStatementArgument();
+
+    /**
+     * Return the statement argument in literal format.
+     *
+     * @return raw statement argument string
+     * @throws VerifyException if this statement does not have an argument
+     */
+    default @NonNull A coerceStatementArgument() {
+        return verifyNotNull(getStatementArgument(), "Statement context %s does not have an argument", this);
+    }
 
     /**
      * Return the {@link SchemaPath} of this statement. Not all statements have a SchemaPath, in which case
@@ -62,7 +113,7 @@ public interface StmtContext<A, D extends DeclaredStatement<A>, E extends Effect
      *
      * @return Optional SchemaPath
      */
-    @Nonnull Optional<SchemaPath> getSchemaPath();
+    @NonNull Optional<SchemaPath> getSchemaPath();
 
     boolean isConfiguration();
 
@@ -80,24 +131,20 @@ 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) ;
+    @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> getAllFromNamespace(Class<N> type);
 
     <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromCurrentStmtCtxNamespace(Class<N> type);
 
-    @Nonnull
-    StmtContext<?, ?, ?> getRoot();
+    @NonNull StmtContext<?, ?, ?> getRoot();
 
     /**
      * Return declared substatements. These are the statements which are explicitly written in the source model.
      *
      * @return Collection of declared substatements
      */
-    @Nonnull
-    Collection<? extends StmtContext<?, ?, ?>> declaredSubstatements();
+    @NonNull Collection<? extends StmtContext<?, ?, ?>> declaredSubstatements();
 
     /**
      * Return effective substatements. These are the statements which are added as this statement's substatements
@@ -105,8 +152,7 @@ public interface StmtContext<A, D extends DeclaredStatement<A>, E extends Effect
      *
      * @return Collection of declared substatements
      */
-    @Nonnull
-    Collection<? extends StmtContext<?, ?, ?>> effectiveSubstatements();
+    @NonNull Collection<? extends StmtContext<?, ?, ?>> effectiveSubstatements();
 
     default Iterable<? extends StmtContext<?, ?, ?>> allSubstatements() {
         return Iterables.concat(declaredSubstatements(), effectiveSubstatements());
@@ -128,14 +174,45 @@ public interface StmtContext<A, D extends DeclaredStatement<A>, E extends Effect
 
     boolean isSupportedToBuildEffective();
 
+    boolean isSupportedByFeatures();
+
     Collection<? extends StmtContext<?, ?, ?>> getEffectOfStatement();
 
-    CopyHistory getCopyHistory();
+    /*
+     * FIXME: YANGTOOLS-784: the next three methods are closely related to the copy process:
+     *        - getCopyHistory() 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()
+     *
+     *        When implementing YANGTOOLS-784, this needs to be taken into account and properly forwarded through
+     *        intermediate MutableTrees. Also note this closely relates to current namespace context, as taken into
+     *        account when creating the argument. At least parts of this are only needed during buildEffective()
+     *        and hence should become arguments to that method.
+     */
 
-    boolean isSupportedByFeatures();
+    /**
+     * 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();
 
+    /**
+     * Return the context of the previous copy of this statement -- effectively walking towards the source origin
+     * of this statement.
+     *
+     * @return Context of the previous copy of this statement, if this statement has been copied.
+     */
+    Optional<? extends StmtContext<?, ?, ?>> getPreviousCopyCtx();
+
     ModelProcessingPhase getCompletedPhase();
 
     /**
@@ -143,14 +220,26 @@ public interface StmtContext<A, D extends DeclaredStatement<A>, E extends Effect
      *
      * @return version of root statement context
      */
-    @Nonnull YangVersion getRootVersion();
+    @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> {
 
         @Override
         Mutable<?, ?, ?> getParentContext();
 
+        @Override
+        default Mutable<?, ?, ?> coerceParentContext() {
+            return verifyNotNull(getParentContext(), "Root context %s does not have a parent", this);
+        }
+
         /**
          * Associate a value with a key within a namespace.
          *
@@ -167,7 +256,6 @@ public interface StmtContext<A, D extends DeclaredStatement<A>, E extends Effect
         <K, V, T extends K, U extends V, N extends IdentifierNamespace<K, V>> void addToNs(Class<N> type, T key,
                 U value);
 
-        @Nonnull
         @Override
         Mutable<?, ?, ?> getRoot();
 
@@ -184,6 +272,8 @@ public interface StmtContext<A, D extends DeclaredStatement<A>, E extends Effect
          *                                  from an alien implementation.
          * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
          */
+        // FIXME: 5.0.0: remove generic arguments X, Y, Z. Callers should not care, as the returned copy can actually
+        //               be an encapsulating implicit statement.
         <X, Y extends DeclaredStatement<X>, Z extends EffectiveStatement<X, Y>> Mutable<X, Y, Z> childCopyOf(
                 StmtContext<X, Y, Z> stmt, CopyType type, @Nullable QNameModule targetModule);
 
@@ -204,11 +294,9 @@ public interface StmtContext<A, D extends DeclaredStatement<A>, E extends Effect
             return childCopyOf(stmt, type, null);
         }
 
-        @Nonnull
-        Collection<? extends Mutable<?, ?, ?>> mutableDeclaredSubstatements();
+        @NonNull Collection<? extends Mutable<?, ?, ?>> mutableDeclaredSubstatements();
 
-        @Nonnull
-        Collection<? extends Mutable<?, ?, ?>> mutableEffectiveSubstatements();
+        @NonNull Collection<? extends Mutable<?, ?, ?>> mutableEffectiveSubstatements();
 
         /**
          * Create a new inference action to be executed during specified phase. The action cannot be cancelled
@@ -219,7 +307,7 @@ public interface StmtContext<A, D extends DeclaredStatement<A>, E extends Effect
          * @return A new action builder.
          * @throws NullPointerException if the specified phase is null
          */
-        @Nonnull ModelActionBuilder newInferenceAction(@Nonnull ModelProcessingPhase phase);
+        @NonNull ModelActionBuilder newInferenceAction(@NonNull ModelProcessingPhase phase);
 
         /**
          * Adds s statement to namespace map with a key.