Deprecate StmtContext.getSchemaPath()
[yangtools.git] / yang / yang-parser-spi / src / main / java / org / opendaylight / yangtools / yang / parser / spi / meta / StmtContext.java
index 4566ba91903fd0bb70029eb47a96a42aade719b9..c264043d5a0a46aeae51b4d69891d4819371f12c 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;
@@ -29,12 +30,31 @@ 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();
 
+    /**
+     * Returns a reference to statement source.
+     *
+     * @return reference of statement source
+     */
     @NonNull StatementSourceReference getStatementSourceReference();
 
+    /**
+     * See {@link StatementSupport#getPublicView()}.
+     */
     @NonNull StatementDefinition getPublicDefinition();
 
     /**
@@ -88,12 +108,24 @@ public interface StmtContext<A, D extends DeclaredStatement<A>, E extends Effect
         return verifyNotNull(getStatementArgument(), "Statement context %s does not have an argument", this);
     }
 
+    default <X, Y extends DeclaredStatement<X>> boolean producesDeclared(final Class<? super Y> type) {
+        return type.isAssignableFrom(getPublicDefinition().getDeclaredRepresentationClass());
+    }
+
+    default <X, Y extends DeclaredStatement<X>, Z extends EffectiveStatement<A, D>> boolean producesEffective(
+            final Class<? super Z> type) {
+        return type.isAssignableFrom(getPublicDefinition().getEffectiveRepresentationClass());
+    }
+
     /**
      * Return the {@link SchemaPath} of this statement. Not all statements have a SchemaPath, in which case
      * {@link Optional#empty()} is returned.
      *
      * @return Optional SchemaPath
+     * @deprecated Use of SchemaPath in the context of effective statements is going away. Consider not providing this
+     *             information, if your users can exist without it.
      */
+    @Deprecated
     @NonNull Optional<SchemaPath> getSchemaPath();
 
     boolean isConfiguration();
@@ -112,7 +144,7 @@ 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<N> type, T key);
 
     <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromNamespace(Class<N> type);
 
@@ -155,13 +187,44 @@ public interface StmtContext<A, D extends DeclaredStatement<A>, E extends Effect
 
     boolean isSupportedToBuildEffective();
 
+    boolean isSupportedByFeatures();
+
     Collection<? extends StmtContext<?, ?, ?>> getEffectOfStatement();
 
+    /*
+     * 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.
+     */
+
+    /**
+     * Return the executive summary of the copy process that has produced this context.
+     *
+     * @return A simplified summary of the copy process.
+     */
     CopyHistory getCopyHistory();
 
-    boolean isSupportedByFeatures();
+    /**
+     * 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<A, D, E>> getOriginalCtx();
 
-    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<StmtContext<A, D, E>> getPreviousCopyCtx();
 
     ModelProcessingPhase getCompletedPhase();
 
@@ -172,6 +235,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> {
 
@@ -215,8 +285,7 @@ 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
          */
-        <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);
+        Mutable<?, ?, ?> childCopyOf(StmtContext<?, ?, ?> stmt, CopyType type, @Nullable QNameModule targetModule);
 
         /**
          * Create a child sub-statement, which is a child of this statement, inheriting all attributes from specified
@@ -230,13 +299,26 @@ 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
          */
-        default <X, Y extends DeclaredStatement<X>, Z extends EffectiveStatement<X, Y>> Mutable<X, Y, Z> childCopyOf(
-                final StmtContext<X, Y, Z> stmt, final CopyType type) {
+        default Mutable<?, ?, ?> childCopyOf(final StmtContext<?, ?, ?> stmt, final CopyType type) {
             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();
 
         /**
@@ -307,8 +389,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);
     }
 }