BUG-6972: inline copy operation modifications 63/58563/3
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 8 Jun 2017 23:38:56 +0000 (01:38 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Fri, 9 Jun 2017 09:03:11 +0000 (11:03 +0200)
Copy operations is really internal to StatementContextBase hence
all the work of setting state can be done in the constructor.

This allows us to make some of the state final. While it does not
help the footprint issue, it is a stepping-stone to defining a
proper set of copy operations.

Change-Id: I0586ecb0757a3126b9e4ed3dbd0ea6c5a2547544
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/CopyHistory.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

index eb1e26765b0a2baaca4a147b8d3655aec8599e37..10eb0fa252dee83ab45005fe449642cd034f68e4 100644 (file)
@@ -8,6 +8,7 @@
 package org.opendaylight.yangtools.yang.parser.spi.meta;
 
 import com.google.common.annotations.Beta;
+import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Verify;
 import org.opendaylight.yangtools.concepts.Immutable;
 
@@ -42,6 +43,10 @@ public final class CopyHistory implements Immutable {
         return ORIGINAL;
     }
 
+    public static CopyHistory of(final CopyType copyType, final CopyHistory copyHistory) {
+        return ORIGINAL.append(copyType, copyHistory);
+    }
+
     private static CopyHistory[] cacheArray(final CopyType lastOperation) {
         final int ordinal = lastOperation.ordinal();
         CopyHistory[] ret = CACHE[ordinal];
@@ -82,7 +87,8 @@ public final class CopyHistory implements Immutable {
         return VALUES[lastOperation];
     }
 
-    public CopyHistory append(final CopyType typeOfCopy, final CopyHistory toAppend) {
+    @VisibleForTesting
+    CopyHistory append(final CopyType typeOfCopy, final CopyHistory toAppend) {
         final int newOperations = operations | toAppend.operations | typeOfCopy.bit();
         if (newOperations == operations && typeOfCopy.ordinal() == lastOperation) {
             return this;
@@ -93,7 +99,7 @@ public final class CopyHistory implements Immutable {
 
     @Override
     public int hashCode() {
-        return Integer.hashCode(operations | (lastOperation << Short.SIZE));
+        return Integer.hashCode(operations | lastOperation << Short.SIZE);
     }
 
     @Override
index 86236bb099b8e5c118a17ef9feb8e7fbf03b9f00..d394e2715ed7d34146430c3235d33781c07efd5e 100644 (file)
@@ -81,6 +81,8 @@ public abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E
 
     private final StatementDefinitionContext<A, D, E> definition;
     private final StatementSourceReference statementDeclSource;
+    private final StmtContext<?, ?, ?> originalCtx;
+    private final CopyHistory copyHistory;
     private final String rawArgument;
 
     private Multimap<ModelProcessingPhase, OnPhaseFinished> phaseListeners = ImmutableMultimap.of();
@@ -90,10 +92,8 @@ public abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E
     private StatementMap substatements = StatementMap.empty();
 
     private Boolean supportedByFeatures = null;
-    private CopyHistory copyHistory = CopyHistory.original();
     private boolean isSupportedToBuildEffective = true;
     private ModelProcessingPhase completedPhase = null;
-    private StmtContext<?, ?, ?> originalCtx;
     private D declaredInstance;
     private E effectiveInstance;
     private int order = 0;
@@ -103,15 +103,24 @@ public abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E
         this.definition = Preconditions.checkNotNull(def);
         this.statementDeclSource = Preconditions.checkNotNull(ref);
         this.rawArgument = def.internArgument(rawArgument);
+        this.copyHistory = CopyHistory.original();
+        this.originalCtx = null;
     }
 
-    StatementContextBase(final StatementContextBase<A, D, E> original) {
+    StatementContextBase(final StatementContextBase<A, D, E> original, final CopyType copyType) {
         this.definition = Preconditions.checkNotNull(original.definition,
                 "Statement context definition cannot be null copying from: %s", original.getStatementSourceReference());
         this.statementDeclSource = Preconditions.checkNotNull(original.statementDeclSource,
                 "Statement context statementDeclSource cannot be null copying from: %s",
                 original.getStatementSourceReference());
         this.rawArgument = original.rawArgument;
+        this.copyHistory = CopyHistory.of(copyType, original.getCopyHistory());
+
+        if (original.getOriginalCtx() != null) {
+            this.originalCtx = original.getOriginalCtx();
+        } else {
+            this.originalCtx = original;
+        }
     }
 
     @Override
@@ -167,19 +176,11 @@ public abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E
         return copyHistory;
     }
 
-    final void appendCopyHistory(final CopyType typeOfCopy, final CopyHistory toAppend) {
-        copyHistory = copyHistory.append(typeOfCopy, toAppend);
-    }
-
     @Override
     public StmtContext<?, ?, ?> getOriginalCtx() {
         return originalCtx;
     }
 
-    final void setOriginalCtx(final StmtContext<?, ?, ?> originalCtx) {
-        this.originalCtx = originalCtx;
-    }
-
     @Override
     public void setOrder(final int order) {
         this.order = order;
index 59c3a650d301c52c9067abba4210ea57c2dfa4b5..08c53f156e9b49dce99089fa6d0170fd823819a9 100644 (file)
@@ -80,8 +80,8 @@ final class SubstatementContext<A, D extends DeclaredStatement<A>, E extends Eff
 
     @SuppressWarnings("unchecked")
     private SubstatementContext(final SubstatementContext<A, D, E> original, final QNameModule newQNameModule,
-            final StatementContextBase<?, ?, ?> newParent, final CopyType typeOfCopy) {
-        super(original);
+            final StatementContextBase<?, ?, ?> newParent, final CopyType copyType) {
+        super(original, copyType);
         this.parent = Preconditions.checkNotNull(newParent);
 
         if (newQNameModule != null) {
@@ -146,14 +146,6 @@ final class SubstatementContext<A, D extends DeclaredStatement<A>, E extends Eff
 
         final SubstatementContext<A, D, E> copy = new SubstatementContext<>(this, newQNameModule, newParent, typeOfCopy);
 
-        copy.appendCopyHistory(typeOfCopy, this.getCopyHistory());
-
-        if (this.getOriginalCtx() != null) {
-            copy.setOriginalCtx(this.getOriginalCtx());
-        } else {
-            copy.setOriginalCtx(this);
-        }
-
         definition().onStatementAdded(copy);
 
         copy.copyStatements(this, newQNameModule, typeOfCopy);