From 41e765bb5128e374ced9f193feb6adb2e0dbc954 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Tue, 4 Oct 2022 15:15:11 +0200 Subject: [PATCH] Reduce code duplication in InferredStatementContext We are treating declared/effective copies the same way -- isolate duplicated code into utility methods. Change-Id: I1faa7348fb7185a60abeec75c041aa0466998b00 Signed-off-by: Robert Varga (cherry picked from commit e0a8b628efee26ec871f3ba6df65049fd7bc7311) --- .../reactor/InferredStatementContext.java | 36 ++++++++++--------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/parser/yang-parser-reactor/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/InferredStatementContext.java b/parser/yang-parser-reactor/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/InferredStatementContext.java index 3d85bda842..48af7eb860 100644 --- a/parser/yang-parser-reactor/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/InferredStatementContext.java +++ b/parser/yang-parser-reactor/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/InferredStatementContext.java @@ -231,9 +231,8 @@ final class InferredStatementContext, E extend } private @NonNull E tryToReusePrototype(final @NonNull StatementFactory factory) { - final E origEffective = prototype.buildEffective(); - final Collection> 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, E extend } // ... copy-sensitive check - final List declCopy = prototype.streamDeclared() - .map(this::effectiveCopy) - .filter(Objects::nonNull) - .collect(Collectors.toUnmodifiableList()); - final List 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, E extend } // *sigh*, ok, heavy lifting through a shallow copy - final List> declared = declCopy.stream() - .map(copy -> copy.toChildContext(this)) - .collect(ImmutableList.toImmutableList()); - final List> 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, E extend // sometimes. Tread softly because you tread on my dreams. // + private ImmutableList> adoptSubstatements(final List list) { + return list.stream() + .map(copy -> copy.toChildContext(this)) + .collect(ImmutableList.toImmutableList()); + } + + private List effectiveCopy(final Stream> 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); -- 2.36.6