Reduce code duplication in InferredStatementContext 52/102652/1
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 4 Oct 2022 13:15:11 +0000 (15:15 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 12 Oct 2022 15:10:03 +0000 (17:10 +0200)
We are treating declared/effective copies the same way -- isolate
duplicated code into utility methods.

Change-Id: I1faa7348fb7185a60abeec75c041aa0466998b00
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit e0a8b628efee26ec871f3ba6df65049fd7bc7311)

parser/yang-parser-reactor/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/InferredStatementContext.java

index 3d85bda8420965321ac635387ac7a57463f42fd4..48af7eb860e2673b8e56ab96a500442906a467b5 100644 (file)
@@ -231,9 +231,8 @@ final class InferredStatementContext<A, D extends DeclaredStatement<A>, E extend
     }
 
     private @NonNull E tryToReusePrototype(final @NonNull StatementFactory<A, D, E> factory) {
-        final E origEffective = prototype.buildEffective();
-        final Collection<? extends @NonNull EffectiveStatement<?, ?>> origSubstatements =
-            origEffective.effectiveSubstatements();
+        final var origEffective = prototype.buildEffective();
+        final var origSubstatements = origEffective.effectiveSubstatements();
 
         // First check if we can reuse the entire prototype
         if (!factory.canReuseCurrent(this, prototype, origSubstatements)) {
@@ -258,14 +257,8 @@ final class InferredStatementContext<A, D extends DeclaredStatement<A>, E extend
         }
 
         // ... copy-sensitive check
-        final List<EffectiveCopy> declCopy = prototype.streamDeclared()
-            .map(this::effectiveCopy)
-            .filter(Objects::nonNull)
-            .collect(Collectors.toUnmodifiableList());
-        final List<EffectiveCopy> effCopy = prototype.streamEffective()
-            .map(this::effectiveCopy)
-            .filter(Objects::nonNull)
-            .collect(Collectors.toUnmodifiableList());
+        final var declCopy = effectiveCopy(prototype.streamDeclared());
+        final var effCopy = effectiveCopy(prototype.streamEffective());
 
         // ... are any copy-sensitive?
         if (allReused(declCopy) && allReused(effCopy)) {
@@ -278,12 +271,8 @@ final class InferredStatementContext<A, D extends DeclaredStatement<A>, E extend
         }
 
         // *sigh*, ok, heavy lifting through a shallow copy
-        final List<ReactorStmtCtx<?, ?, ?>> declared = declCopy.stream()
-            .map(copy -> copy.toChildContext(this))
-            .collect(ImmutableList.toImmutableList());
-        final List<ReactorStmtCtx<?, ?, ?>> effective = effCopy.stream()
-            .map(copy -> copy.toChildContext(this))
-            .collect(ImmutableList.toImmutableList());
+        final var declared = adoptSubstatements(declCopy);
+        final var effective = adoptSubstatements(effCopy);
         substatements = declared.isEmpty() ? effective
             : Streams.concat(declared.stream(), effective.stream()).collect(ImmutableList.toImmutableList());
         prototype.decRef();
@@ -569,6 +558,19 @@ final class InferredStatementContext<A, D extends DeclaredStatement<A>, E extend
     // sometimes. Tread softly because you tread on my dreams.
     //
 
+    private ImmutableList<ReactorStmtCtx<?, ?, ?>> adoptSubstatements(final List<EffectiveCopy> list) {
+        return list.stream()
+            .map(copy -> copy.toChildContext(this))
+            .collect(ImmutableList.toImmutableList());
+    }
+
+    private List<EffectiveCopy> effectiveCopy(final Stream<? extends ReactorStmtCtx<?, ?, ?>> stream) {
+        return stream
+            .map(this::effectiveCopy)
+            .filter(Objects::nonNull)
+            .collect(Collectors.toUnmodifiableList());
+    }
+
     private EffectiveCopy effectiveCopy(final ReactorStmtCtx<?, ?, ?> stmt) {
         final ReactorStmtCtx<?, ?, ?> effective = stmt.asEffectiveChildOf(this, childCopyType(), targetModule);
         return effective == null ? null : new EffectiveCopy(stmt, effective);