Do not instantiate needless objects in augment 03/61303/8
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 8 Aug 2017 02:15:43 +0000 (04:15 +0200)
committerRobert Varga <nite@hq.sk>
Thu, 24 Aug 2017 10:36:41 +0000 (10:36 +0000)
Non-mutable versions of accessors are cheaper and should be used
wherever we do not modify state.

Change-Id: I200d843b830af59d407da346d0b09aa4909a4467
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/spi/meta/StmtContextUtils.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/SubstatementValidator.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
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/CaseEffectiveStatementImpl.java

index a4cb0a6ea4ef7e061482d8b915d114b02c25d284..f6bd7abf68febf83efaae0d5c78ac408ed2e11f1 100644 (file)
@@ -7,9 +7,12 @@
  */
 package org.opendaylight.yangtools.yang.parser.spi.meta;
 
+import com.google.common.collect.Iterables;
+import com.google.common.collect.Streams;
 import java.util.Collection;
 import java.util.Map;
 import java.util.Optional;
+import java.util.stream.Stream;
 import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
 import org.opendaylight.yangtools.yang.common.QNameModule;
@@ -92,6 +95,14 @@ public interface StmtContext<A, D extends DeclaredStatement<A>, E extends Effect
     @Nonnull
     Collection<? extends StmtContext<?, ?, ?>> effectiveSubstatements();
 
+    default Iterable<? extends StmtContext<?, ?, ?>> allSubstatements() {
+        return Iterables.concat(declaredSubstatements(), effectiveSubstatements());
+    }
+
+    default Stream<? extends StmtContext<?, ?, ?>> allSubstatementsStream() {
+        return Streams.concat(declaredSubstatements().stream(), effectiveSubstatements().stream());
+    }
+
     /**
      * Builds {@link DeclaredStatement} for statement context.
      */
index a009f35d65052de11ae2912fde9cfe0f370fc26a..984c9e3eaba285ff026946486321eeba45d20ec4 100644 (file)
@@ -11,7 +11,6 @@ import com.google.common.base.Preconditions;
 import com.google.common.base.Splitter;
 import com.google.common.base.Strings;
 import com.google.common.collect.ImmutableList;
-import com.google.common.collect.Iterables;
 import java.util.Collection;
 import java.util.Date;
 import java.util.Optional;
@@ -72,8 +71,7 @@ public final class StmtContextUtils {
 
     public static <A, D extends DeclaredStatement<A>> A firstSubstatementAttributeOf(
             final StmtContext<?, ?, ?> ctx, final Class<D> declaredType) {
-        final A firstAttribute = firstAttributeOf(ctx.effectiveSubstatements(), declaredType);
-        return firstAttribute != null ? firstAttribute : firstAttributeOf(ctx.declaredSubstatements(), declaredType);
+        return firstAttributeOf(ctx.allSubstatements(), declaredType);
     }
 
     @SuppressWarnings("unchecked")
@@ -466,8 +464,7 @@ public final class StmtContextUtils {
     }
 
     private static void disallowIfFeatureAndWhenOnListKeys(final StmtContext<?, ?, ?> leafStmtCtx) {
-        Iterables.concat(leafStmtCtx.declaredSubstatements(), leafStmtCtx.effectiveSubstatements()).forEach(
-                leafSubstmtCtx -> {
+        leafStmtCtx.allSubstatements().forEach(leafSubstmtCtx -> {
             final StatementDefinition statementDef = leafSubstmtCtx.getPublicDefinition();
             SourceException.throwIf(YangStmtMapping.IF_FEATURE.equals(statementDef)
                     || YangStmtMapping.WHEN.equals(statementDef), leafStmtCtx.getStatementSourceReference(),
index 654d68599366d27b41d67ea7116c4eb407d14a95..2bacaa40af0f585a1f65b60d43c4b92872a7c950 100644 (file)
@@ -10,7 +10,6 @@ package org.opendaylight.yangtools.yang.parser.spi.meta;
 
 import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.Iterables;
 import com.google.common.collect.Maps;
 import java.util.HashMap;
 import java.util.Map;
@@ -117,7 +116,7 @@ public final class SubstatementValidator {
             MissingSubstatementException {
 
         final Map<StatementDefinition, Counter> stmtCounts = new HashMap<>();
-        for (StmtContext<?, ?, ?> stmtCtx : Iterables.concat(ctx.declaredSubstatements(), ctx.effectiveSubstatements())) {
+        for (StmtContext<?, ?, ?> stmtCtx : ctx.allSubstatements()) {
             stmtCounts.computeIfAbsent(stmtCtx.getPublicDefinition(), key -> new Counter()).increment();
         }
 
index 9a334b2b5a6f7537c3e295e25933895b99f29889..e85881d28697bd30c049aa0c43737b2162159395 100644 (file)
@@ -8,11 +8,9 @@
 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020;
 
 import com.google.common.base.Verify;
-import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableSet;
 import java.util.ArrayList;
 import java.util.Collection;
-import java.util.List;
 import java.util.Objects;
 import java.util.Set;
 import java.util.regex.Pattern;
@@ -266,11 +264,7 @@ public class AugmentStatementImpl extends AbstractDeclaredStatement<SchemaNodeId
                 checkForMandatoryNodes(sourceCtx);
             }
 
-            final List<Mutable<?, ?, ?>> targetSubStatements = ImmutableList.<Mutable<?, ?, ?>>builder()
-                    .addAll(targetCtx.mutableDeclaredSubstatements()).addAll(targetCtx.mutableEffectiveSubstatements())
-                    .build();
-
-            for (final Mutable<?, ?, ?> subStatement : targetSubStatements) {
+            for (final StmtContext<?, ?, ?> subStatement : targetCtx.allSubstatements()) {
                 final boolean sourceIsDataNode = DataDefinitionStatement.class.isAssignableFrom(sourceCtx
                         .getPublicDefinition().getDeclaredRepresentationClass());
                 final boolean targetIsDataNode = DataDefinitionStatement.class.isAssignableFrom(subStatement
@@ -293,8 +287,7 @@ public class AugmentStatementImpl extends AbstractDeclaredStatement<SchemaNodeId
                  * b) added to augment body also via uses of a grouping and
                  * such sub-statements are stored in effective sub-statements collection.
                  */
-                sourceCtx.declaredSubstatements().forEach(Definition::checkForMandatoryNodes);
-                sourceCtx.effectiveSubstatements().forEach(Definition::checkForMandatoryNodes);
+                sourceCtx.allSubstatementsStream().forEach(Definition::checkForMandatoryNodes);
             }
 
             InferenceException.throwIf(StmtContextUtils.isMandatoryNode(sourceCtx),
index d3eca3e746789f86c69d6d72812b2429c49f00f5..fda3df0b6beef35c9d2613e2c4beb2274579b284 100644 (file)
@@ -10,7 +10,6 @@ package org.opendaylight.yangtools.yang.parser.stmt.rfc6020;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableMap.Builder;
 import com.google.common.collect.ImmutableSet;
-import com.google.common.collect.Iterables;
 import java.util.Collection;
 import java.util.Map;
 import java.util.Objects;
@@ -215,10 +214,7 @@ public class DeviateStatementImpl extends AbstractDeclaredStatement<DeviateKind>
                 final StatementDefinition stmtToBeAdded = stmtCtxToBeAdded.getPublicDefinition();
                 if (SINGLETON_STATEMENTS.contains(stmtToBeAdded) || YangStmtMapping.DEFAULT.equals(stmtToBeAdded)
                         && YangStmtMapping.LEAF.equals(targetCtx.getPublicDefinition())) {
-                    final Iterable<StmtContext<?, ?, ?>> targetCtxSubstatements = Iterables.concat(
-                            targetCtx.declaredSubstatements(), targetCtx.effectiveSubstatements());
-
-                    for (final StmtContext<?, ?, ?> targetCtxSubstatement : targetCtxSubstatements) {
+                    for (final StmtContext<?, ?, ?> targetCtxSubstatement : targetCtx.allSubstatements()) {
                         InferenceException.throwIf(stmtToBeAdded.equals(targetCtxSubstatement.getPublicDefinition()),
                             stmtCtxToBeAdded.getStatementSourceReference(), "Deviation cannot add substatement %s "
                         + "to target node %s because it is already defined in target and can appear only once.",
index 3db84b8e93022083b036f063dc9abf3b4795f18d..7e39d918d5e61975e4c620eb2065e94a708b2b18 100644 (file)
@@ -260,7 +260,7 @@ public class UsesStatementImpl extends AbstractDeclaredStatement<QName> implemen
 
     public static void resolveUsesNode(
             final Mutable<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> usesNode,
-            final StatementContextBase<?, ?, ?> targetNodeStmtCtx) {
+            final StmtContext<?, ?, ?> targetNodeStmtCtx) {
         for (final Mutable<?, ?, ?> subStmtCtx : usesNode.mutableDeclaredSubstatements()) {
             if (StmtContextUtils.producesDeclared(subStmtCtx, RefineStatement.class)
                     && areFeaturesSupported(subStmtCtx)) {
@@ -269,15 +269,14 @@ public class UsesStatementImpl extends AbstractDeclaredStatement<QName> implemen
         }
     }
 
-    private static boolean areFeaturesSupported(final Mutable<?, ?, ?> subStmtCtx) {
+    private static boolean areFeaturesSupported(final StmtContext<?, ?, ?> subStmtCtx) {
         /*
          * In case of Yang 1.1, checks whether features are supported.
          */
         return !YangVersion.VERSION_1_1.equals(subStmtCtx.getRootVersion()) || subStmtCtx.isSupportedByFeatures();
     }
 
-    private static void performRefine(final Mutable<?, ?, ?> subStmtCtx,
-            final StatementContextBase<?, ?, ?> usesParentCtx) {
+    private static void performRefine(final Mutable<?, ?, ?> subStmtCtx, final StmtContext<?, ?, ?> usesParentCtx) {
 
         final Object refineArgument = subStmtCtx.getStatementArgument();
         InferenceException.throwIf(!(refineArgument instanceof SchemaNodeIdentifier),
@@ -292,10 +291,10 @@ public class UsesStatementImpl extends AbstractDeclaredStatement<QName> implemen
             "Refine target node %s not found.", refineTargetNodeIdentifier);
 
         if (StmtContextUtils.isUnknownStatement(refineTargetNodeCtx)) {
-            LOG.debug(
-                    "Refine node '{}' in uses '{}' has target node unknown statement '{}'. Refine has been skipped. At line: {}",
-                    subStmtCtx.getStatementArgument(), subStmtCtx.getParentContext().getStatementArgument(),
-                    refineTargetNodeCtx.getStatementArgument(), subStmtCtx.getStatementSourceReference());
+            LOG.debug("Refine node '{}' in uses '{}' has target node unknown statement '{}'. "
+                + "Refine has been skipped. At line: {}", subStmtCtx.getStatementArgument(),
+                subStmtCtx.getParentContext().getStatementArgument(),
+                refineTargetNodeCtx.getStatementArgument(), subStmtCtx.getStatementSourceReference());
             subStmtCtx.addAsEffectOfStatement(refineTargetNodeCtx);
             return;
         }
index c21e0865ab41d718ef79be2faa91299e99906785..7cc26e6a1df54ed82caa3a326ea827919a6ee44f 100644 (file)
@@ -8,7 +8,6 @@
 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
 
 import com.google.common.base.Optional;
-import java.util.Collection;
 import java.util.Objects;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
@@ -29,17 +28,12 @@ public final class CaseEffectiveStatementImpl extends AbstractEffectiveSimpleDat
         this.original = (ChoiceCaseNode) ctx.getOriginalCtx().map(StmtContext::buildEffective).orElse(null);
 
         if (ctx.isConfiguration()) {
-            configuration = isAtLeastOneChildConfiguration(ctx.declaredSubstatements())
-                    || isAtLeastOneChildConfiguration(ctx.effectiveSubstatements());
+            configuration = ctx.allSubstatementsStream().anyMatch(StmtContext::isConfiguration);
         } else {
             configuration = false;
         }
     }
 
-    private static boolean isAtLeastOneChildConfiguration(final Collection<? extends StmtContext<?, ?, ?>> children) {
-        return children.stream().anyMatch(StmtContext::isConfiguration);
-    }
-
     @Override
     public Optional<ChoiceCaseNode> getOriginal() {
         return Optional.fromNullable(original);