Defer copy decisions to StatementSupport
[yangtools.git] / yang / yang-parser-spi / src / main / java / org / opendaylight / yangtools / yang / parser / spi / meta / AbstractStatementSupport.java
index 389f6e73221d635231276c62fe2e0d1d3069cc1c..1318af957b73707f28d1abd7c6290fbad74d89c4 100644 (file)
@@ -11,68 +11,37 @@ import static com.google.common.base.Preconditions.checkArgument;
 import static java.util.Objects.requireNonNull;
 
 import java.util.Optional;
-import javax.annotation.Nonnull;
-import javax.annotation.Nullable;
-import org.opendaylight.yangtools.yang.common.QName;
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.Nullable;
+import org.opendaylight.yangtools.yang.common.QNameModule;
 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.StatementDefinition;
+import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
 
 /**
  * Class providing necessary support for processing a YANG statement. This class is intended to be subclassed
  * by developers who want to add semantic support for a statement to a parser reactor.
  *
- * @param <A>
- *            Argument type
- * @param <D>
- *            Declared Statement representation
- * @param <E>
- *            Effective Statement representation
+ * @param <A> Argument type
+ * @param <D> Declared Statement representation
+ * @param <E> Effective Statement representation
  */
 public abstract class AbstractStatementSupport<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
         implements StatementDefinition, StatementFactory<A, D, E>, StatementSupport<A, D, E> {
 
-    private final StatementDefinition type;
+    private final @NonNull StatementDefinition type;
 
     protected AbstractStatementSupport(final StatementDefinition publicDefinition) {
         this.type = requireNonNull(publicDefinition);
         checkArgument(publicDefinition != this);
     }
 
-    @Nonnull
-    @Override
-    public final QName getStatementName() {
-        return type.getStatementName();
-    }
-
-    @Override
-    public final QName getArgumentName() {
-        return type.getArgumentName();
-    }
-
-    @Nonnull
-    @Override
-    public final Class<? extends DeclaredStatement<?>> getDeclaredRepresentationClass() {
-        return type.getDeclaredRepresentationClass();
-    }
-
-    @Nonnull
-    @Override
-    public final Class<? extends EffectiveStatement<?,?>> getEffectiveRepresentationClass() {
-        return type.getEffectiveRepresentationClass();
-    }
-
     @Override
     public final StatementDefinition getPublicView() {
         return type;
     }
 
-    @Override
-    public Optional<StatementSupport<?, ?, ?>> getImplicitParentFor(final StatementDefinition stmtDef) {
-        // NOOP for most implementations and also no implicit parent
-        return Optional.empty();
-    }
-
     @Override
     public void onStatementAdded(final StmtContext.Mutable<A, D, E> stmt) {
         // NOOP for most implementations
@@ -129,31 +98,37 @@ public abstract class AbstractStatementSupport<A, D extends DeclaredStatement<A>
         }
     }
 
-    @Override
-    public boolean isArgumentYinElement() {
-        return getPublicView().isArgumentYinElement();
-    }
-
     @Override
     public boolean hasArgumentSpecificSupports() {
-        // Most of statement supports don't have any argument specific
-        // supports, so return 'false'.
+        // Most of statement supports don't have any argument specific supports, so return 'false'.
         return false;
     }
 
     @Override
     public StatementSupport<?, ?, ?> getSupportSpecificForArgument(final String argument) {
-        // Most of statement supports don't have any argument specific
-        // supports, so return null.
+        // Most of statement supports don't have any argument specific supports, so return null.
         return null;
     }
 
+    @Override
+    public Optional<? extends Mutable<?, ?, ?>> copyAsChildOf(final Mutable<?, ?, ?> stmt,
+            final Mutable<?, ?, ?> parent, final CopyType copyType, final QNameModule targetModule) {
+        // Most of statement supports will just want to copy the statement
+        // FIXME: YANGTOOLS-694: that is not strictly true. Subclasses of this should indicate if they are themselves
+        //                       copy-sensitive:
+        //                       1) if they are not and cannot be targeted by inference, and all their current
+        //                          substatements are also non-sensitive, we want to return the same context.
+        //                       2) if they are not and their current substatements are sensitive, we want to copy
+        //                          as a lazily-instantiated interceptor to let it deal with substatements when needed
+        //                          (YANGTOOLS-1067 prerequisite)
+        //                       3) otherwise perform this eager copy
+        return Optional.of(parent.childCopyOf(stmt, copyType, targetModule));
+    }
+
     /**
      * Returns corresponding substatement validator of a statement support.
      *
-     * @return substatement validator or null, if substatement validator is not
-     *         defined
+     * @return substatement validator or null, if substatement validator is not defined
      */
-    @Nullable
-    protected abstract SubstatementValidator getSubstatementValidator();
+    protected abstract @Nullable SubstatementValidator getSubstatementValidator();
 }