Separate out StatementSupport.applyCopyPolicy()
[yangtools.git] / yang / yang-parser-spi / src / main / java / org / opendaylight / yangtools / yang / parser / spi / meta / AbstractStatementSupport.java
index ba0a930ae74f5506236aebc461e46f135fca2730..0622463b8212cde180b5b7c2dfdcfef60eae35bc 100644 (file)
@@ -10,29 +10,34 @@ package org.opendaylight.yangtools.yang.parser.spi.meta;
 import static com.google.common.base.Preconditions.checkArgument;
 import static java.util.Objects.requireNonNull;
 
-import javax.annotation.Nullable;
+import com.google.common.annotations.Beta;
+import com.google.common.base.VerifyException;
+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;
+    private final @NonNull CopyPolicy copyPolicy;
 
-    protected AbstractStatementSupport(final StatementDefinition publicDefinition) {
+    @Beta
+    protected AbstractStatementSupport(final StatementDefinition publicDefinition, final CopyPolicy copyPolicy) {
         this.type = requireNonNull(publicDefinition);
+        this.copyPolicy = requireNonNull(copyPolicy);
         checkArgument(publicDefinition != this);
     }
 
@@ -41,6 +46,26 @@ public abstract class AbstractStatementSupport<A, D extends DeclaredStatement<A>
         return type;
     }
 
+    @Override
+    public final CopyPolicy copyPolicy() {
+        return copyPolicy;
+    }
+
+    @Override
+    public final StmtContext<?, ?, ?> effectiveCopyOf(final StmtContext<?, ?, ?> stmt, final Mutable<?, ?, ?> parent,
+            final CopyType copyType, final QNameModule targetModule) {
+        switch (copyPolicy) {
+            case CONTEXT_INDEPENDENT:
+                return stmt;
+            case DECLARED_COPY:
+                // FIXME: YANGTOOLS-1195: this is too harsh, we need to make a callout to subclass methods so they
+                //                        actually examine the differences.
+                return parent.childCopyOf(stmt, copyType, targetModule);
+            default:
+                throw new VerifyException("Attempted to apply " + copyPolicy);
+        }
+    }
+
     @Override
     public void onStatementAdded(final StmtContext.Mutable<A, D, E> stmt) {
         // NOOP for most implementations
@@ -99,24 +124,20 @@ public abstract class AbstractStatementSupport<A, D extends DeclaredStatement<A>
 
     @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;
     }
 
     /**
      * 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();
 }