Make CopyHistory implement CopyableNode 35/94835/6
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 27 Jan 2021 10:06:52 +0000 (11:06 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 27 Jan 2021 12:51:12 +0000 (13:51 +0100)
We have a few call sites checking the same thing. Make a strong
connection between CopyableNode and CopyHistory -- centralizing
checks and providing an opportunity for optimization.

CopyHistory.contains() now exists only for testing purposes.

JIRA: YANGTOOLS-1215
Change-Id: Ibc9cebe4bee6c5818570c52415c3d8a5ba1ff19d
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/BaseSchemaTreeStatementSupport.java
yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/EffectiveStatementMixins.java
yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/UnknownEffectiveStatementBase.java
yang/yang-parser-spi/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/CopyHistory.java

index d01bc67b2af24d0d06e4097ded09578e203f00fe..a2d2ea2436afef36e411ddbaa388905423a8bb6e 100644 (file)
@@ -18,7 +18,6 @@ import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeEffectiveStateme
 import org.opendaylight.yangtools.yang.parser.spi.SchemaTreeNamespace;
 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractQNameStatementSupport;
 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyHistory;
-import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
@@ -49,27 +48,13 @@ public abstract class BaseSchemaTreeStatementSupport<D extends DeclaredStatement
         @Override
         public boolean canReuseCurrent(final Current<QName, D> copy, final Current<QName, D> current,
                 final Collection<? extends EffectiveStatement<?, ?>> substatements) {
-            return equalHistory(copy, current)
+            return equalHistory(copy.history(), current.history())
                 // FIXME: this should devolve to getArgument() equality
                 && copy.getSchemaPath().equals(current.getSchemaPath());
         }
 
-        // TODO: can we speed this up?
-        private static boolean equalHistory(final Current<?, ?> copy, final Current<?, ?> current) {
-            return isAugmenting(copy) == isAugmenting(current)
-                && isAddedByUses(copy) == isAddedByUses(current);
-        }
-
-        private static boolean isAugmenting(final Current<?, ?> stmt) {
-            final CopyHistory history = stmt.history();
-            return history.contains(CopyType.ADDED_BY_AUGMENTATION)
-                || history.contains(CopyType.ADDED_BY_USES_AUGMENTATION);
-        }
-
-        private static boolean isAddedByUses(final Current<?, ?> stmt) {
-            final CopyHistory history = stmt.history();
-            return history.contains(CopyType.ADDED_BY_USES)
-                || history.contains(CopyType.ADDED_BY_USES_AUGMENTATION);
+        private static boolean equalHistory(final CopyHistory copy, final CopyHistory current) {
+            return copy.isAugmenting() == current.isAugmenting() && copy.isAddedByUses() == current.isAddedByUses();
         }
     }
 
index 3cb0eca95a3cca4ed1ae2caa956d291abf0eacb7..3eb78f0488bd4a1d2a9dba1b1488f9ae7b061c3c 100644 (file)
@@ -61,7 +61,6 @@ import org.opendaylight.yangtools.yang.model.api.stmt.WhenEffectiveStatement;
 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.EffectiveStatementMixins.EffectiveStatementWithFlags.FlagsBuilder;
 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyHistory;
-import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
 import org.opendaylight.yangtools.yang.xpath.api.YangXPathExpression.QualifiedBound;
 
 /**
@@ -505,20 +504,8 @@ public final class EffectiveStatementMixins {
             }
 
             public FlagsBuilder setHistory(final CopyHistory history) {
-                int bits;
-                if (history.contains(CopyType.ADDED_BY_USES_AUGMENTATION)) {
-                    bits = AUGMENTING | ADDED_BY_USES;
-                } else {
-                    bits = 0;
-                    if (history.contains(CopyType.ADDED_BY_AUGMENTATION)) {
-                        bits |= AUGMENTING;
-                    }
-                    if (history.contains(CopyType.ADDED_BY_USES)) {
-                        bits |= ADDED_BY_USES;
-                    }
-                }
-
-                flags = flags & ~MASK_HISTORY | bits;
+                flags = flags & ~MASK_HISTORY
+                    | (history.isAugmenting() ? AUGMENTING : 0) | (history.isAddedByUses() ? ADDED_BY_USES : 0);
                 return this;
             }
 
index d47c51e4e5544809ee0d4a02b4eddaeb20071793..708208c528d814812ce99094958da5fa8e0c3eef 100644 (file)
@@ -19,7 +19,6 @@ import org.opendaylight.yangtools.yang.model.api.stmt.ExtensionStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.UnknownStatement;
 import org.opendaylight.yangtools.yang.parser.spi.ExtensionNamespace;
 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyHistory;
-import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
 
@@ -53,13 +52,8 @@ public abstract class UnknownEffectiveStatementBase<A, D extends UnknownStatemen
 
         // initCopyType
         final CopyHistory copyTypesFromOriginal = stmt.history();
-        if (copyTypesFromOriginal.contains(CopyType.ADDED_BY_USES_AUGMENTATION)) {
-            this.augmenting = true;
-            this.addedByUses = true;
-        } else {
-            this.augmenting = copyTypesFromOriginal.contains(CopyType.ADDED_BY_AUGMENTATION);
-            this.addedByUses = copyTypesFromOriginal.contains(CopyType.ADDED_BY_USES);
-        }
+        this.augmenting = copyTypesFromOriginal.isAugmenting();
+        this.addedByUses = copyTypesFromOriginal.isAddedByUses();
 
         nodeParameter = stmt.rawArgument() == null ? "" : stmt.rawArgument();
     }
index 5b0e1340dd40a7e96d3b40540ad075cf765ed3ca..8c4086715722bdfbb6f2a12dc2895af9f4d16943 100644 (file)
@@ -14,9 +14,10 @@ import com.google.common.base.Verify;
 import java.util.Arrays;
 import java.util.stream.Collectors;
 import org.opendaylight.yangtools.concepts.Immutable;
+import org.opendaylight.yangtools.yang.model.api.CopyableNode;
 
 @Beta
-public final class CopyHistory implements Immutable {
+public final class CopyHistory implements Immutable, CopyableNode {
     private static final CopyType[] VALUES = CopyType.values();
 
     private static final CopyHistory[][] CACHE = new CopyHistory[VALUES.length][];
@@ -34,6 +35,10 @@ public final class CopyHistory implements Immutable {
     }
 
     private static final CopyHistory ORIGINAL = cacheObject(CopyType.ORIGINAL, CopyType.ORIGINAL.bit());
+    private static final int IS_ADDED_BY_USES_BITS =
+        CopyType.ADDED_BY_USES_AUGMENTATION.bit() | CopyType.ADDED_BY_USES.bit();
+    private static final int IS_AUGMENTING_BITS =
+        CopyType.ADDED_BY_USES_AUGMENTATION.bit() | CopyType.ADDED_BY_AUGMENTATION.bit();
 
     private final short operations;
     private final short lastOperation;
@@ -83,14 +88,27 @@ public final class CopyHistory implements Immutable {
         return ret;
     }
 
-    public boolean contains(final CopyType type) {
-        return (operations & type.bit()) != 0;
-    }
-
     public CopyType getLastOperation() {
         return VALUES[lastOperation];
     }
 
+    @Override
+    @Deprecated
+    public boolean isAugmenting() {
+        return (operations & IS_AUGMENTING_BITS) != 0;
+    }
+
+    @Override
+    @Deprecated
+    public boolean isAddedByUses() {
+        return (operations & IS_ADDED_BY_USES_BITS) != 0;
+    }
+
+    @VisibleForTesting
+    boolean contains(final CopyType type) {
+        return (operations & type.bit()) != 0;
+    }
+
     @VisibleForTesting
     CopyHistory append(final CopyType typeOfCopy, final CopyHistory toAppend) {
         final int newOperations = operations | toAppend.operations | typeOfCopy.bit();