BUG-7052: remove StmtContext.createCopy() 38/61238/11
authorRobert Varga <robert.varga@pantheon.tech>
Sun, 6 Aug 2017 23:03:08 +0000 (01:03 +0200)
committerRobert Varga <nite@hq.sk>
Wed, 16 Aug 2017 16:11:48 +0000 (16:11 +0000)
The two StmtContext.createCopy() methods are the final bits of
spi.meta dependending on stmt.reactor.

This patch eliminates them by introducing Mutable.childCopyOf()
methods, which perform the same function, except the are hosted
in the new parent object, which internally knows it is
a StatementContextBase.

Change-Id: I16978eb62851295a5c922224a051d200c5e32abc
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/StmtContext.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/RootStatementContext.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/StatementContextBase.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/SubstatementContext.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/AugmentStatementImpl.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/DeviateStatementImpl.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/UsesStatementImpl.java

index 4668555ec88ec89dcbc1ceb76401036730ba92f7..a4cb0a6ea4ef7e061482d8b915d114b02c25d284 100644 (file)
@@ -22,7 +22,6 @@ import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
 import org.opendaylight.yangtools.yang.model.api.meta.StatementSource;
 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
-import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
 
 public interface StmtContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>> {
 
@@ -107,21 +106,6 @@ public interface StmtContext<A, D extends DeclaredStatement<A>, E extends Effect
 
     Collection<? extends StmtContext<?, ?, ?>> getEffectOfStatement();
 
-    /**
-     * @return copy of this considering {@link CopyType} (augment, uses)
-     *
-     * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
-     */
-    Mutable<A, D, E> createCopy(StatementContextBase<?, ?, ?> newParent, CopyType typeOfCopy);
-
-    /**
-     * @return copy of this considering {@link CopyType} (augment, uses)
-     *
-     * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
-     */
-    Mutable<A, D, E> createCopy(QNameModule newQNameModule, StatementContextBase<?, ?, ?> newParent,
-            CopyType typeOfCopy);
-
     CopyHistory getCopyHistory();
 
     boolean isSupportedByFeatures();
@@ -150,6 +134,39 @@ public interface StmtContext<A, D extends DeclaredStatement<A>, E extends Effect
         @Override
         Mutable<?, ?, ?> getRoot();
 
+        /**
+         * Create a child sub-statement, which is a child of this statement, inheriting all attributes from specified
+         * child and recording copy type. Resulting object may only be added as a child of this statement.
+         *
+         * @param stmt Statement to be used as a template
+         * @param type Type of copy to record in history
+         * @param targetModule Optional new target module
+         * @return copy of statement considering {@link CopyType} (augment, uses)
+         *
+         * @throws IllegalArgumentException if stmt cannot be copied into this statement, for example because it comes
+         *                                  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);
+
+        /**
+         * Create a child sub-statement, which is a child of this statement, inheriting all attributes from specified
+         * child and recording copy type. Resulting object may only be added as a child of this statement.
+         *
+         * @param stmt Statement to be used as a template
+         * @param type Type of copy to record in history
+         * @return copy of statement considering {@link CopyType} (augment, uses)
+         *
+         * @throws IllegalArgumentException if stmt cannot be copied into this statement, for example because it comes
+         *                                  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) {
+            return childCopyOf(stmt, type, null);
+        }
+
         @Nonnull
         Collection<? extends Mutable<?, ?, ?>> mutableDeclaredSubstatements();
 
index 60db37533505a59a4884015a9cf1e8249f85354e..31f197e79ce7d8191d9bd7bf18afc50053e0bc01 100644 (file)
@@ -18,14 +18,12 @@ import java.util.Map;
 import java.util.Optional;
 import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
-import org.opendaylight.yangtools.yang.common.QNameModule;
 import org.opendaylight.yangtools.yang.common.YangVersion;
 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 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.IdentifierNamespace;
-import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
 import org.opendaylight.yangtools.yang.parser.spi.meta.MutableStatement;
 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.NamespaceStorageNode;
@@ -113,18 +111,6 @@ public class RootStatementContext<A, D extends DeclaredStatement<A>, E extends E
         return argument;
     }
 
-    @Override
-    public StatementContextBase<A, D, E> createCopy(final StatementContextBase<?, ?, ?> newParent,
-            final CopyType typeOfCopy) {
-        throw new UnsupportedOperationException("Root context cannot be copied");
-    }
-
-    @Override
-    public StatementContextBase<A, D, E> createCopy(final QNameModule newQNameModule,
-            final StatementContextBase<?, ?, ?> newParent, final CopyType typeOfCopy) {
-        throw new UnsupportedOperationException("Root context cannot be copied");
-    }
-
     @Nonnull
     @Override
     public Optional<SchemaPath> getSchemaPath() {
index c59c81552922bdb33385de73e9832e346fb2b246..712bc83f135ed308fc2e93ffe416d361e278d7cc 100644 (file)
@@ -26,6 +26,7 @@ import java.util.Set;
 import javax.annotation.Nonnull;
 import org.opendaylight.yangtools.util.OptionalBoolean;
 import org.opendaylight.yangtools.yang.common.QName;
+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.IdentifierNamespace;
@@ -641,6 +642,14 @@ public abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E
         addContextToNamespace(namespace, key, stmt);
     }
 
+    @Override
+    public <X, Y extends DeclaredStatement<X>, Z extends EffectiveStatement<X, Y>> Mutable<X, Y, Z> childCopyOf(
+            final StmtContext<X, Y, Z> stmt, final CopyType type, final QNameModule targetModule) {
+        Preconditions.checkArgument(stmt instanceof SubstatementContext, "Unsupported statement %s", stmt);
+        final SubstatementContext<X, Y, Z> original = (SubstatementContext<X, Y, Z>)stmt;
+        return original.createCopy(targetModule, this, type);
+    }
+
     @Override
     public final String toString() {
         return addToStringAttributes(MoreObjects.toStringHelper(this).omitNullValues()).toString();
index 83215464fa3d9a84df12353af5478b4bc3ad5116..fed83cec5ebc76f3c8f0bd9f4b05c77e0bd2acff 100644 (file)
@@ -91,27 +91,11 @@ final class SubstatementContext<A, D extends DeclaredStatement<A>, E extends Eff
         this.argument = def.parseArgumentValue(this, rawStatementArgument());
     }
 
-    @SuppressWarnings("unchecked")
-    private SubstatementContext(final SubstatementContext<A, D, E> original, final QNameModule newQNameModule,
-            final StatementContextBase<?, ?, ?> newParent, final CopyType copyType) {
+    SubstatementContext(final StatementContextBase<A, D, E> original, final StatementContextBase<?, ?, ?> parent,
+            final A argument, final CopyType copyType) {
         super(original, copyType);
-        this.parent = Preconditions.checkNotNull(newParent);
-
-        if (newQNameModule != null) {
-            final A originalArg = original.argument;
-            if (originalArg instanceof QName) {
-                final QName originalQName = (QName) originalArg;
-                this.argument = (A) getFromNamespace(QNameCacheNamespace.class,
-                        QName.create(newQNameModule, originalQName.getLocalName()));
-            } else if (StmtContextUtils.producesDeclared(original, KeyStatement.class)) {
-                this.argument = (A) StmtContextUtils.replaceModuleQNameForKey(
-                        (StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, ?>) original, newQNameModule);
-            } else {
-                this.argument = original.argument;
-            }
-        } else {
-            this.argument = original.argument;
-        }
+        this.parent = Preconditions.checkNotNull(parent);
+        this.argument = argument;
     }
 
     @Override
@@ -145,19 +129,29 @@ final class SubstatementContext<A, D extends DeclaredStatement<A>, E extends Eff
         return argument;
     }
 
-    @Override
-    public StatementContextBase<A, D, E> createCopy(final StatementContextBase<?, ?, ?> newParent,
-            final CopyType typeOfCopy) {
-        return createCopy(null, newParent, typeOfCopy);
-    }
-
-    @Override
-    public StatementContextBase<A, D, E> createCopy(final QNameModule newQNameModule,
+    @SuppressWarnings("unchecked")
+    StatementContextBase<A, D, E> createCopy(final QNameModule newQNameModule,
             final StatementContextBase<?, ?, ?> newParent, final CopyType typeOfCopy) {
         Preconditions.checkState(getCompletedPhase() == ModelProcessingPhase.EFFECTIVE_MODEL,
                 "Attempted to copy statement %s which has completed phase %s", this, getCompletedPhase());
 
-        final SubstatementContext<A, D, E> copy = new SubstatementContext<>(this, newQNameModule, newParent, typeOfCopy);
+        // FIXME: this should live in StatementSupport or similar
+        final A argumentCopy;
+        if (newQNameModule != null) {
+            if (argument instanceof QName) {
+                argumentCopy = (A) getFromNamespace(QNameCacheNamespace.class,
+                        QName.create(newQNameModule, ((QName) argument).getLocalName()));
+            } else if (StmtContextUtils.producesDeclared(this, KeyStatement.class)) {
+                argumentCopy = (A) StmtContextUtils.replaceModuleQNameForKey(
+                        (StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, ?>) this, newQNameModule);
+            } else {
+                argumentCopy = argument;
+            }
+        } else {
+            argumentCopy = argument;
+        }
+
+        final SubstatementContext<A, D, E> copy = new SubstatementContext<>(this, newParent, argumentCopy, typeOfCopy);
 
         definition().onStatementAdded(copy);
 
@@ -187,7 +181,7 @@ final class SubstatementContext<A, D extends DeclaredStatement<A>, E extends Eff
     private void copySubstatement(final Mutable<?, ?, ?> stmtContext, final QNameModule newQNameModule,
             final CopyType typeOfCopy, final Collection<Mutable<?, ?, ?>> buffer) {
         if (needToCopyByUses(stmtContext)) {
-            final Mutable<?, ?, ?> copy = stmtContext.createCopy(newQNameModule, this, typeOfCopy);
+            final Mutable<?, ?, ?> copy = childCopyOf(stmtContext, typeOfCopy, newQNameModule);
             LOG.debug("Copying substatement {} for {} as", stmtContext, this, copy);
             buffer.add(copy);
         } else if (isReusedByUses(stmtContext)) {
index 819ca5630be90ad9f8d737e20b6df9edb91c3c99..9a334b2b5a6f7537c3e295e25933895b99f29889 100644 (file)
@@ -247,8 +247,7 @@ public class AugmentStatementImpl extends AbstractDeclaredStatement<SchemaNodeId
             if (needToCopyByAugment(original)) {
                 validateNodeCanBeCopiedByAugment(original, target, typeOfCopy, skipCheckOfMandatoryNodes);
 
-                final Mutable<?, ?, ?> copy = original.createCopy(target, typeOfCopy);
-                buffer.add(copy);
+                buffer.add(target.childCopyOf(original, typeOfCopy));
             } else if (isReusedByAugment(original)) {
                 buffer.add(original);
             }
index 3e91e08586bb88980fddeebdd40778c0292829f1..60fc2f32550ef0e79c9f4b6fb1c8472a986dec64 100644 (file)
@@ -212,7 +212,7 @@ public class DeviateStatementImpl extends AbstractDeclaredStatement<DeviateKind>
         private static void addStatement(final Mutable<?, ?, ?> stmtCtxToBeAdded,
                 final StatementContextBase<?, ?, ?> targetCtx) {
             if (StmtContextUtils.isUnknownStatement(stmtCtxToBeAdded)) {
-                targetCtx.addEffectiveSubstatement(stmtCtxToBeAdded.createCopy(targetCtx, CopyType.ORIGINAL));
+                targetCtx.addEffectiveSubstatement(targetCtx.childCopyOf(stmtCtxToBeAdded, CopyType.ORIGINAL));
                 return;
             }
 
@@ -231,7 +231,7 @@ public class DeviateStatementImpl extends AbstractDeclaredStatement<DeviateKind>
                 }
             }
 
-            targetCtx.addEffectiveSubstatement(stmtCtxToBeAdded.createCopy(targetCtx, CopyType.ORIGINAL));
+            targetCtx.addEffectiveSubstatement(targetCtx.childCopyOf(stmtCtxToBeAdded, CopyType.ORIGINAL));
         }
 
         private static void performDeviateReplace(final StatementContextBase<?, ?, ?> deviateStmtCtx,
@@ -257,7 +257,7 @@ public class DeviateStatementImpl extends AbstractDeclaredStatement<DeviateKind>
             for (final StmtContext<?, ?, ?> targetCtxSubstatement : targetCtx.effectiveSubstatements()) {
                 if (stmtToBeReplaced.equals(targetCtxSubstatement.getPublicDefinition())) {
                     targetCtx.removeStatementFromEffectiveSubstatements(stmtToBeReplaced);
-                    targetCtx.addEffectiveSubstatement(stmtCtxToBeReplaced.createCopy(targetCtx, CopyType.ORIGINAL));
+                    targetCtx.addEffectiveSubstatement(targetCtx.childCopyOf(stmtCtxToBeReplaced, CopyType.ORIGINAL));
                     return;
                 }
             }
@@ -265,7 +265,7 @@ public class DeviateStatementImpl extends AbstractDeclaredStatement<DeviateKind>
             for (final Mutable<?, ?, ?> targetCtxSubstatement : targetCtx.mutableDeclaredSubstatements()) {
                 if (stmtToBeReplaced.equals(targetCtxSubstatement.getPublicDefinition())) {
                     targetCtxSubstatement.setIsSupportedToBuildEffective(false);
-                    targetCtx.addEffectiveSubstatement(stmtCtxToBeReplaced.createCopy(targetCtx, CopyType.ORIGINAL));
+                    targetCtx.addEffectiveSubstatement(targetCtx.childCopyOf(stmtCtxToBeReplaced, CopyType.ORIGINAL));
                     return;
                 }
             }
index 657dd27df93c65109497996d204a391bbcfba4ce..0a2b12e0d3ffa4ad0610b355edc89a44aba2d1d2 100644 (file)
@@ -194,7 +194,7 @@ public class UsesStatementImpl extends AbstractDeclaredStatement<QName> implemen
      * @throws SourceException
      *             instance of SourceException
      */
-    private static void copyFromSourceToTarget(final Mutable<?, ?, ?> sourceGrpStmtCtx,
+    static void copyFromSourceToTarget(final Mutable<?, ?, ?> sourceGrpStmtCtx,
             final StatementContextBase<?, ?, ?> targetCtx,
             final StmtContext.Mutable<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> usesNode) {
         final Collection<? extends Mutable<?, ?, ?>> declared = sourceGrpStmtCtx.mutableDeclaredSubstatements();
@@ -220,7 +220,7 @@ public class UsesStatementImpl extends AbstractDeclaredStatement<QName> implemen
             final StatementContextBase<?, ?, ?> targetCtx, final QNameModule targetModule,
             final Collection<Mutable<?, ?, ?>> buffer) {
         if (needToCopyByUses(original)) {
-            final Mutable<?, ?, ?> copy = original.createCopy(targetModule, targetCtx, CopyType.ADDED_BY_USES);
+            final Mutable<?, ?, ?> copy = targetCtx.childCopyOf(original, CopyType.ADDED_BY_USES, targetModule);
             buffer.add(copy);
         } else if (isReusedByUsesOnTop(original)) {
             buffer.add(original);