Centralize substatement validators
[yangtools.git] / yang / yang-parser-spi / src / main / java / org / opendaylight / yangtools / yang / parser / spi / meta / StatementSupport.java
index 241e208aa545cfa0c373cb1b30d27a0d2cf6b387..0db910ffaf15505e09b0fbd35900aad05b493ca3 100644 (file)
@@ -66,6 +66,18 @@ public abstract class StatementSupport<A, D extends DeclaredStatement<A>, E exte
             return (StatementPolicy<A, D>) EqualSemantics.CONTEXT_INDEPENDENT;
         }
 
+        /**
+         * Return a {@link StatementPolicy} for {@link CopyPolicy#EXACT_REPLICA}.
+         *
+         * @param <A> Argument type
+         * @param <D> Declared Statement representation
+         * @return Exact-replica policy
+         */
+        @SuppressWarnings("unchecked")
+        public static final <A, D extends DeclaredStatement<A>> @NonNull StatementPolicy<A, D> exactReplica() {
+            return (StatementPolicy<A, D>) EqualSemantics.EXACT_REPLICA;
+        }
+
         /**
          * Return a {@link StatementPolicy} for {@link CopyPolicy#IGNORE}.
          *
@@ -116,12 +128,6 @@ public abstract class StatementSupport<A, D extends DeclaredStatement<A>, E exte
             return (StatementPolicy<A, D>) EqualSemantics.ALWAYS_COPY;
         }
 
-        @Deprecated(forRemoval = true)
-        // FIXME: 7.0.0: remove this method
-        public static final <A, D extends DeclaredStatement<A>> @NonNull StatementPolicy<A, D> legacyDeclaredCopy() {
-            return alwaysCopyDeclared();
-        }
-
         abstract boolean canReuseCurrent(@NonNull Current<A, D> copy, @NonNull Current<A, D> current,
             @NonNull Collection<? extends EffectiveStatement<?, ?>> substatements);
 
@@ -145,6 +151,8 @@ public abstract class StatementSupport<A, D extends DeclaredStatement<A>, E exte
                 new EqualSemantics<>((copy, stmt, substatements) -> false);
             static final @NonNull EqualSemantics<?, ?> CONTEXT_INDEPENDENT =
                 new EqualSemantics<>(CopyPolicy.CONTEXT_INDEPENDENT, (copy, stmt, substatements) -> true);
+            static final @NonNull EqualSemantics<?, ?> EXACT_REPLICA =
+                new EqualSemantics<>(CopyPolicy.EXACT_REPLICA, (copy, stmt, substatements) -> true);
 
             private final @NonNull StatementEquality<A, D> equality;
 
@@ -317,7 +325,7 @@ public abstract class StatementSupport<A, D extends DeclaredStatement<A>, E exte
      *
      * <p>
      * Implementation may use method to perform actions on this event or register modification action using
-     * {@link StmtContext.Mutable#newInferenceAction(ModelProcessingPhase)}.
+     * {@link Mutable#newInferenceAction(ModelProcessingPhase)}.
      *
      * @param stmt Context of added statement.
      * @throws SourceException when an inconsistency is detected.
@@ -332,7 +340,7 @@ public abstract class StatementSupport<A, D extends DeclaredStatement<A>, E exte
      *
      * <p>
      * Implementation may use method to perform actions on this event or register modification action using
-     * {@link StmtContext.Mutable#newInferenceAction(ModelProcessingPhase)}.
+     * {@link Mutable#newInferenceAction(ModelProcessingPhase)}.
      *
      * @param stmt Context of added statement. Argument and statement parent is accessible.
      * @throws SourceException when an inconsistency is detected.
@@ -347,13 +355,13 @@ public abstract class StatementSupport<A, D extends DeclaredStatement<A>, E exte
      *
      * <p>
      * Implementation may use method to perform actions on this event or register modification action using
-     * {@link StmtContext.Mutable#newInferenceAction(ModelProcessingPhase)}.
+     * {@link Mutable#newInferenceAction(ModelProcessingPhase)}.
      *
      * @param stmt Context of added statement. Argument and statement parent is accessible.
      * @throws SourceException when an inconsistency is detected.
      */
-    public void onFullDefinitionDeclared(final StmtContext.Mutable<A, D, E> stmt) {
-        final SubstatementValidator validator = getSubstatementValidator();
+    public void onFullDefinitionDeclared(final Mutable<A, D, E> stmt) {
+        final SubstatementValidator validator = substatementValidator();
         if (validator != null) {
             validator.validate(stmt);
         }
@@ -364,11 +372,12 @@ public abstract class StatementSupport<A, D extends DeclaredStatement<A>, E exte
      *
      * @return substatement validator or null, if substatement validator is not defined
      */
-    // FIXME: rename to 'substatementValidator' and perhaps let it be passed in?
-    protected abstract @Nullable SubstatementValidator getSubstatementValidator();
+    protected abstract @Nullable SubstatementValidator substatementValidator();
 
     /**
      * Returns true if this support has argument specific supports.
+     *
+     * @return true if this support has argument specific supports.
      */
     public boolean hasArgumentSpecificSupports() {
         // Most of statement supports don't have any argument specific supports, so return 'false'.
@@ -398,18 +407,6 @@ public abstract class StatementSupport<A, D extends DeclaredStatement<A>, E exte
         return rawArgument;
     }
 
-    /**
-     * Returns unknown statement form of a regular YANG statement supplied as a parameter to the method. Default
-     * implementation does nothing.
-     *
-     * @param yangStmtDef statement definition of a regular YANG statement
-     * @return Optional of unknown statement form of a regular YANG statement or empty() if it is not supported by this
-     *         statement support
-     */
-    public Optional<StatementSupport<?, ?, ?>> getUnknownStatementDefinitionOf(final StatementDefinition yangStmtDef) {
-        return Optional.empty();
-    }
-
     /**
      * Returns true if this statement support and all its substatements ignore if-feature statements (e.g. yang-data
      * extension defined in <a href="https://tools.ietf.org/html/rfc8040#section-8">RFC 8040</a>). Default
@@ -479,6 +476,13 @@ public abstract class StatementSupport<A, D extends DeclaredStatement<A>, E exte
          */
         // TODO: does this mean source must have transitioned to ModelProcessingPhase.EFFECTIVE_MODEL?
         CONTEXT_INDEPENDENT,
+        /**
+         * Reuse the source statement context in the new place completely. This policy is more stringent than
+         * {@link #CONTEXT_INDEPENDENT} in that the statement is dependent on circumstances of its original definition
+         * and any copy operation must replicate it exactly as is. This implies ignoring the usual policy of its
+         * substatements. A typical example of such a statement is {@code type}.
+         */
+        EXACT_REPLICA,
         /**
          * Create a copy sharing declared instance, but otherwise having a separate disconnected lifecycle.
          */