From 81e6bd88fc88a93c962798944009eb41abd0ebd4 Mon Sep 17 00:00:00 2001 From: Pierre Date: Thu, 6 Jul 2023 13:52:41 +0200 Subject: [PATCH] NO-JIRA replace MoreCollectors operations to JDK or guava implementations --- .../server/es/response/NodeStatsResponse.java | 2 +- .../QGChangeEventListenersImpl.java | 3 +- .../server/project/ws/BulkDeleteAction.java | 6 +- .../qualitygate/RegisterQualityGates.java | 2 +- .../core/util/stream/MoreCollectors.java | 73 +++++-------------- .../core/util/stream/MoreCollectorsTest.java | 11 +-- 6 files changed, 29 insertions(+), 68 deletions(-) diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/es/response/NodeStatsResponse.java b/server/sonar-server-common/src/main/java/org/sonar/server/es/response/NodeStatsResponse.java index cb43294f676..b66b20640ae 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/es/response/NodeStatsResponse.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/es/response/NodeStatsResponse.java @@ -37,7 +37,7 @@ public class NodeStatsResponse { .map(Map.Entry::getValue) .map(JsonElement::getAsJsonObject) .map(NodeStats::toNodeStats) - .collect(MoreCollectors.toList()); + .collect(MoreCollectors.toImmutableList()); } public static NodeStatsResponse toNodeStatsResponse(JsonObject jsonObject) { diff --git a/server/sonar-webserver-api/src/main/java/org/sonar/server/qualitygate/changeevent/QGChangeEventListenersImpl.java b/server/sonar-webserver-api/src/main/java/org/sonar/server/qualitygate/changeevent/QGChangeEventListenersImpl.java index 7f545f8b5bf..c8c8c3dcbc3 100644 --- a/server/sonar-webserver-api/src/main/java/org/sonar/server/qualitygate/changeevent/QGChangeEventListenersImpl.java +++ b/server/sonar-webserver-api/src/main/java/org/sonar/server/qualitygate/changeevent/QGChangeEventListenersImpl.java @@ -31,7 +31,6 @@ import org.sonar.core.util.stream.MoreCollectors; import org.sonar.server.qualitygate.changeevent.QGChangeEventListener.ChangedIssue; import static java.lang.String.format; -import static org.sonar.core.util.stream.MoreCollectors.toSet; /** * Broadcast a given collection of {@link QGChangeEvent} for a specific trigger to all the registered @@ -81,7 +80,7 @@ public class QGChangeEventListenersImpl implements QGChangeEventListeners { private static ImmutableSet toChangedIssues(Collection defaultIssues, boolean fromAlm) { return defaultIssues.stream() .map(defaultIssue -> new ChangedIssueImpl(defaultIssue, fromAlm)) - .collect(toSet()); + .collect(MoreCollectors.toImmutableSet()); } private void broadcastChangeEventToListeners(Set changedIssues, QGChangeEvent changeEvent) { diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/project/ws/BulkDeleteAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/project/ws/BulkDeleteAction.java index fdc1793fd8c..f6647ccbbeb 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/project/ws/BulkDeleteAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/project/ws/BulkDeleteAction.java @@ -158,19 +158,19 @@ public class BulkDeleteAction implements ProjectsWsAction { try { entities.forEach(p -> componentCleanerService.deleteEntity(dbSession, p)); } finally { - callDeleteListeners(dbSession, componentDtos, entities); + callDeleteListeners(dbSession, entities); } } response.noContent(); } - private void callDeleteListeners(DbSession dbSession, Set componentDtos, List entities) { + private void callDeleteListeners(DbSession dbSession, List entities) { Set entityUuids = entities.stream().map(EntityDto::getUuid).collect(toSet()); Map mainBranchUuidByEntityUuid = dbClient.branchDao().selectMainBranchesByProjectUuids(dbSession, entityUuids).stream() .collect(Collectors.toMap(BranchDto::getProjectUuid, BranchDto::getUuid)); ImmutableSet deletedProjects = entities.stream().map(entity -> new DeletedProject(Project.from(entity), mainBranchUuidByEntityUuid.get(entity.getUuid()))) - .collect(MoreCollectors.toSet(componentDtos.size())); + .collect(MoreCollectors.toImmutableSet()); projectLifeCycleListeners.onProjectsDeleted(deletedProjects); } diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualitygate/RegisterQualityGates.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualitygate/RegisterQualityGates.java index 92b2a56e959..1ddbb7621ce 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualitygate/RegisterQualityGates.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualitygate/RegisterQualityGates.java @@ -132,7 +132,7 @@ public class RegisterQualityGates implements Startable { return qualityGateConditionDao.selectForQualityGate(dbSession, builtinQualityGate.getUuid()) .stream() .map(dto -> QualityGateCondition.from(dto, uuidToKeyMetric)) - .collect(MoreCollectors.toList()); + .collect(MoreCollectors.toImmutableList()); } private List removeExtraConditions(DbSession dbSession, QualityGateDto builtinQualityGate, List qualityGateConditions) { diff --git a/sonar-core/src/main/java/org/sonar/core/util/stream/MoreCollectors.java b/sonar-core/src/main/java/org/sonar/core/util/stream/MoreCollectors.java index 99ce4fb5e87..d20e48a809a 100644 --- a/sonar-core/src/main/java/org/sonar/core/util/stream/MoreCollectors.java +++ b/sonar-core/src/main/java/org/sonar/core/util/stream/MoreCollectors.java @@ -52,68 +52,28 @@ public final class MoreCollectors { // prevents instantiation } - /** - * A Collector into an {@link ImmutableList}. - */ - public static Collector, ImmutableList> toList() { - return Collector.of( - ArrayList::new, - List::add, - (left, right) -> { - left.addAll(right); - return left; - }, - ImmutableList::copyOf); + public static Collector> toList() { + return Collectors.toList(); } - /** - * A Collector into an {@link ImmutableList} of the specified expected size. - * - *

Note: using this method with a parallel stream will likely not have the expected memory usage benefit as all - * processing threads will use a List with a capacity large enough for the final size.

- */ - public static Collector, ImmutableList> toList(int expectedSize) { - // use ArrayList rather than ImmutableList.Builder because initial capacity of builder can not be specified - return Collector.of( - () -> new ArrayList<>(expectedSize), - List::add, - (left, right) -> { - left.addAll(right); - return left; - }, - ImmutableList::copyOf); + public static Collector> toImmutableList() { + return ImmutableList.toImmutableList(); } - /** - * A Collector into an {@link ImmutableSet}. - */ - public static Collector, ImmutableSet> toSet() { - return Collector.of( - HashSet::new, - Set::add, - (left, right) -> { - left.addAll(right); - return left; - }, - ImmutableSet::copyOf); + public static Collector> toList(int expectedSize) { + return Collectors.toList(); } - /** - * A Collector into an {@link ImmutableSet} of the specified expected size. - * - *

Note: using this method with a parallel stream will likely not have the expected memory usage benefit as all - * processing threads will use a Set with a capacity large enough for the final size.

- */ - public static Collector, ImmutableSet> toSet(int expectedSize) { - // use HashSet rather than ImmutableSet.Builder because initial capacity of builder can not be specified - return Collector.of( - () -> new HashSet<>(expectedSize), - Set::add, - (left, right) -> { - left.addAll(right); - return left; - }, - ImmutableSet::copyOf); + public static Collector> toSet() { + return Collectors.toSet(); + } + + public static Collector> toImmutableSet() { + return ImmutableSet.toImmutableSet(); + } + + public static Collector> toSet(int expectedSize) { + return toSet(); } /** @@ -461,4 +421,5 @@ public final class MoreCollectors { throw new IllegalStateException("Parallel processing is not supported"); }; } + } diff --git a/sonar-core/src/test/java/org/sonar/core/util/stream/MoreCollectorsTest.java b/sonar-core/src/test/java/org/sonar/core/util/stream/MoreCollectorsTest.java index 570658a7aa7..2314dfb8210 100644 --- a/sonar-core/src/test/java/org/sonar/core/util/stream/MoreCollectorsTest.java +++ b/sonar-core/src/test/java/org/sonar/core/util/stream/MoreCollectorsTest.java @@ -27,7 +27,6 @@ import com.google.common.collect.SetMultimap; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; -import java.util.Collections; import java.util.EnumSet; import java.util.HashSet; import java.util.List; @@ -46,6 +45,8 @@ import static org.sonar.core.util.stream.MoreCollectors.index; import static org.sonar.core.util.stream.MoreCollectors.join; import static org.sonar.core.util.stream.MoreCollectors.toArrayList; import static org.sonar.core.util.stream.MoreCollectors.toHashSet; +import static org.sonar.core.util.stream.MoreCollectors.toImmutableList; +import static org.sonar.core.util.stream.MoreCollectors.toImmutableSet; import static org.sonar.core.util.stream.MoreCollectors.toList; import static org.sonar.core.util.stream.MoreCollectors.toSet; import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex; @@ -74,7 +75,7 @@ public class MoreCollectorsTest { @Test public void toList_builds_an_ImmutableList() { - List res = Stream.of(1, 2, 3, 4, 5).collect(toList()); + List res = Stream.of(1, 2, 3, 4, 5).collect(toImmutableList()); assertThat(res).isInstanceOf(ImmutableList.class) .containsExactly(1, 2, 3, 4, 5); } @@ -86,7 +87,7 @@ public class MoreCollectorsTest { @Test public void toList_with_size_builds_an_ImmutableList() { - List res = Stream.of(1, 2, 3, 4, 5).collect(toList(30)); + List res = Stream.of(1, 2, 3, 4, 5).collect(toImmutableList()); assertThat(res).isInstanceOf(ImmutableList.class) .containsExactly(1, 2, 3, 4, 5); } @@ -98,7 +99,7 @@ public class MoreCollectorsTest { @Test public void toSet_builds_an_ImmutableSet() { - Set res = Stream.of(1, 2, 3, 4, 5).collect(toSet()); + Set res = Stream.of(1, 2, 3, 4, 5).collect(toImmutableSet()); assertThat(res).isInstanceOf(ImmutableSet.class) .containsExactly(1, 2, 3, 4, 5); } @@ -110,7 +111,7 @@ public class MoreCollectorsTest { @Test public void toSet_with_size_builds_an_ImmutableSet() { - Set res = Stream.of(1, 2, 3, 4, 5).collect(toSet(30)); + Set res = Stream.of(1, 2, 3, 4, 5).collect(toImmutableSet()); assertThat(res).isInstanceOf(ImmutableSet.class) .containsExactly(1, 2, 3, 4, 5); } -- 2.39.5