]> source.dussan.org Git - sonarqube.git/commitdiff
NO-JIRA replace MoreCollectors.toArrayList with Collectors.toList
authorPierre <pierre.guillot@sonarsource.com>
Thu, 6 Jul 2023 14:32:54 +0000 (16:32 +0200)
committersonartech <sonartech@sonarsource.com>
Mon, 17 Jul 2023 20:03:45 +0000 (20:03 +0000)
13 files changed:
server/sonar-db-dao/src/main/java/org/sonar/db/permission/UserPermissionDao.java
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v00/PopulateInitialSchemaTest.java
server/sonar-server-common/src/main/java/org/sonar/server/es/OneToOneResilientIndexingListener.java
server/sonar-server-common/src/main/java/org/sonar/server/qualityprofile/index/ActiveRuleIndexer.java
server/sonar-server-common/src/main/java/org/sonar/server/rule/index/RuleIndexer.java
server/sonar-server-common/src/main/java/org/sonar/server/view/index/ViewIndexer.java
server/sonar-webserver-core/src/main/java/org/sonar/server/platform/StandaloneSystemInfoWriter.java
server/sonar-webserver-es/src/main/java/org/sonar/server/permission/index/PermissionIndexer.java
server/sonar-webserver-webapi/src/it/java/org/sonar/server/project/ws/BulkDeleteActionIT.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/measure/ws/SearchHistoryResult.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualityprofile/QProfileTreeImpl.java
sonar-core/src/main/java/org/sonar/core/util/stream/MoreCollectors.java
sonar-core/src/test/java/org/sonar/core/util/stream/MoreCollectorsTest.java

index cca5d88258598bdd9a5f6650232a179a3c4dfa25..212b418362246d8da147d1e93291c7a1c53268d2 100644 (file)
@@ -23,8 +23,8 @@ import com.google.common.annotations.VisibleForTesting;
 import java.util.Collection;
 import java.util.List;
 import java.util.Set;
+import java.util.stream.Collectors;
 import javax.annotation.Nullable;
-import org.sonar.core.util.stream.MoreCollectors;
 import org.sonar.db.Dao;
 import org.sonar.db.DatabaseUtils;
 import org.sonar.db.DbSession;
@@ -76,7 +76,7 @@ public class UserPermissionDao implements Dao {
       // Pagination is done in Java because it's too complex to use SQL pagination in Oracle and MsSQL with the distinct
       .skip(query.getPageOffset())
       .limit(query.getPageSize())
-      .collect(MoreCollectors.toArrayList());
+      .collect(Collectors.toList());
   }
 
   public int countUsersByQuery(DbSession dbSession, PermissionQuery query) {
index a2a2deb819848ed8ddeb64832b7781207fddd414..adad8363f180648694b8fad236c48cff8061ff78 100644 (file)
@@ -24,6 +24,7 @@ import java.util.Date;
 import java.util.List;
 import java.util.Map;
 import java.util.Random;
+import java.util.stream.Collectors;
 import java.util.stream.Stream;
 import org.junit.Before;
 import org.junit.Rule;
@@ -227,7 +228,7 @@ public class PopulateInitialSchemaTest {
       "where u.login='admin'");
     List<String> groupNames = rows.stream()
       .map(row -> (String) row.get("groupName"))
-      .collect(MoreCollectors.toArrayList());
+      .collect(Collectors.toList());
     assertThat(groupNames).containsOnly("sonar-administrators", "sonar-users");
   }
 
index c4a4bfd703a85ea1b449bcea52d010457a0c9328..5855695a52c5fe6c5b9c5e384c20439df1d841d8 100644 (file)
@@ -24,6 +24,7 @@ import java.util.Collection;
 import java.util.List;
 import java.util.Objects;
 import java.util.function.Function;
+import java.util.stream.Collectors;
 import org.sonar.core.util.stream.MoreCollectors;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
@@ -61,7 +62,7 @@ public class OneToOneResilientIndexingListener implements IndexingListener {
         .map(itemsById::get)
         .flatMap(Collection::stream)
         .filter(Objects::nonNull)
-        .collect(MoreCollectors.toArrayList());
+        .collect(Collectors.toList());
       dbClient.esQueueDao().delete(dbSession, itemsToDelete);
       dbSession.commit();
     }
index 26fbe67903149d04969f7a1e7fd60b591bd29dff..0047fae64167445160df5114f3e085327c482a92 100644 (file)
@@ -52,7 +52,6 @@ import org.sonar.server.qualityprofile.ActiveRuleChange;
 import org.sonar.server.qualityprofile.ActiveRuleInheritance;
 
 import static org.elasticsearch.index.query.QueryBuilders.termQuery;
-import static org.sonar.core.util.stream.MoreCollectors.toArrayList;
 import static org.sonar.server.qualityprofile.index.ActiveRuleDoc.docIdOf;
 import static org.sonar.server.rule.index.RuleIndexDefinition.FIELD_ACTIVE_RULE_PROFILE_UUID;
 import static org.sonar.server.rule.index.RuleIndexDefinition.TYPE_ACTIVE_RULE;
@@ -98,7 +97,7 @@ public class ActiveRuleIndexer implements ResilientIndexer {
     List<EsQueueDto> items = changes.stream()
       .map(ActiveRuleChange::getActiveRule)
       .map(ar -> newQueueDto(docIdOf(ar.getUuid()), ID_TYPE_ACTIVE_RULE_UUID, ar.getRuleUuid()))
-      .collect(toArrayList());
+      .collect(Collectors.toList());
 
     dbClient.esQueueDao().insert(dbSession, items);
     dbSession.commit();
@@ -110,7 +109,7 @@ public class ActiveRuleIndexer implements ResilientIndexer {
       .map(QProfileDto::getRulesProfileUuid)
       .distinct()
       .map(ruleProfileUuid -> newQueueDto(ruleProfileUuid, ID_TYPE_RULE_PROFILE_UUID, null))
-      .collect(toArrayList());
+      .collect(Collectors.toList());
 
     dbClient.esQueueDao().insert(dbSession, items);
     dbSession.commit();
index dcb59a115ad5b4efad43b8ee3d75d34e8cd84a83..2c6559e0884fba06d27f2bc905f2a9820c23bb75 100644 (file)
@@ -25,6 +25,7 @@ import java.util.Collection;
 import java.util.List;
 import java.util.Optional;
 import java.util.Set;
+import java.util.stream.Collectors;
 import java.util.stream.Stream;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -89,7 +90,7 @@ public class RuleIndexer implements ResilientIndexer {
   public void commitAndIndex(DbSession dbSession, Collection<String> ruleUuids) {
     List<EsQueueDto> items = ruleUuids.stream()
       .map(RuleIndexer::createQueueDtoForRule)
-      .collect(MoreCollectors.toArrayList());
+      .collect(Collectors.toList());
 
     dbClient.esQueueDao().insert(dbSession, items);
     dbSession.commit();
index 56cb492fabaf471f60d87af41f654748d0068289..c89f6d3a8609150f25a2920b82759c58437cd7cc 100644 (file)
@@ -24,9 +24,9 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.stream.Collectors;
 import org.elasticsearch.action.admin.indices.cache.clear.ClearIndicesCacheRequest;
 import org.elasticsearch.action.index.IndexRequest;
-import org.sonar.core.util.stream.MoreCollectors;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.component.ComponentDto;
@@ -171,7 +171,7 @@ public class ViewIndexer implements ResilientIndexer {
   public void delete(DbSession dbSession, Collection<String> viewUuids) {
     List<EsQueueDto> items = viewUuids.stream()
       .map(l -> EsQueueDto.create(TYPE_VIEW.format(), l))
-      .collect(MoreCollectors.toArrayList());
+      .collect(Collectors.toList());
 
     dbClient.esQueueDao().insert(dbSession, items);
     dbSession.commit();
index 1c96f9a0065376c8d9b12735a32589eeabd0c423..e64b834cbc6b694a277e19dc1db086dc009ca410 100644 (file)
@@ -20,8 +20,8 @@
 package org.sonar.server.platform;
 
 import java.util.List;
+import java.util.stream.Collectors;
 import org.sonar.api.utils.text.JsonWriter;
-import org.sonar.core.util.stream.MoreCollectors;
 import org.sonar.process.systeminfo.SystemInfoSection;
 import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo;
 import org.sonar.server.ce.http.CeHttpClient;
@@ -47,7 +47,7 @@ public class StandaloneSystemInfoWriter extends AbstractSystemInfoWriter {
 
     List<ProtobufSystemInfo.Section> sections = stream(systemInfoSections)
       .map(SystemInfoSection::toProtobuf)
-      .collect(MoreCollectors.toArrayList());
+      .collect(Collectors.toList());
     ceHttpClient.retrieveSystemInfo()
       .ifPresent(ce -> sections.addAll(ce.getSectionsList()));
 
index c54d43822aa06327684a88252c3d14ce1db5d0f3..7468cbe5fd372f5b7d319dd91db402f99c8531be 100644 (file)
@@ -44,7 +44,6 @@ import org.sonar.server.es.OneToOneResilientIndexingListener;
 import org.springframework.beans.factory.annotation.Autowired;
 
 import static java.util.Collections.emptyList;
-import static org.sonar.core.util.stream.MoreCollectors.toArrayList;
 
 /**
  * Populates the types "authorization" of each index requiring entity
@@ -117,7 +116,7 @@ public class PermissionIndexer implements EventIndexer {
   private Collection<EsQueueDto> insertIntoEsQueue(DbSession dbSession, Collection<String> projectUuids) {
     List<EsQueueDto> items = indexTypeByFormat.values().stream()
       .flatMap(indexType -> projectUuids.stream().map(projectUuid -> EsQueueDto.create(indexType.format(), AuthorizationDoc.idOf(projectUuid), null, projectUuid)))
-      .collect(toArrayList());
+      .collect(Collectors.toList());
 
     dbClient.esQueueDao().insert(dbSession, items);
     return items;
index f6d950d4ce0d1d9ee483d7db491f0e0a05d6bcaf..8ec6e55cbe05f78999680c25735fe30b64605110 100644 (file)
@@ -38,7 +38,6 @@ import org.mockito.ArgumentCaptor;
 import org.sonar.api.resources.Qualifiers;
 import org.sonar.api.server.ws.WebService.Param;
 import org.sonar.api.utils.System2;
-import org.sonar.core.util.stream.MoreCollectors;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
@@ -237,7 +236,7 @@ public class BulkDeleteActionIT {
   @Test
   public void delete_only_the_1000_first_projects() {
     userSession.logIn().addPermission(ADMINISTER);
-    List<String> keys = IntStream.range(0, 1_010).mapToObj(i -> "key" + i).collect(MoreCollectors.toArrayList());
+    List<String> keys = IntStream.range(0, 1_010).mapToObj(i -> "key" + i).collect(Collectors.toList());
     keys.forEach(key -> db.components().insertPrivateProject(p -> p.setKey(key)).getMainBranchComponent());
 
     ws.newRequest()
index 084ed09ab318ab7cef9b359644710b93a6f35577..62444aa77767b8e53ad9073d8d1ccedaf287fe77 100644 (file)
@@ -92,7 +92,7 @@ public class SearchHistoryResult {
     ImmutableList.Builder<MeasureDto> measuresBuilder = ImmutableList.builder();
     List<MeasureDto> filteredMeasures = measures.stream()
       .filter(measure -> analysisUuids.contains(measure.getAnalysisUuid()))
-      .collect(MoreCollectors.toArrayList());
+      .collect(Collectors.toList());
     measuresBuilder.addAll(filteredMeasures);
     measuresBuilder.addAll(computeBestValues(filteredMeasures));
 
index 0f4adba5154f04423b56db2de2b33c7a4fe92c4e..5460726274690f3c2b16ea320460d6fe0a2101d7 100644 (file)
@@ -22,9 +22,9 @@ package org.sonar.server.qualityprofile;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
+import java.util.stream.Collectors;
 import javax.annotation.Nullable;
 import org.sonar.api.utils.System2;
-import org.sonar.core.util.stream.MoreCollectors;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.qualityprofile.ActiveRuleDto;
@@ -89,7 +89,7 @@ public class QProfileTreeImpl implements QProfileTree {
 
     changes = getChangesFromRulesToBeRemoved(dbSession, profile, getRulesDifference(activeRules, parentActiveRules));
 
-    Collection<String> parentRuleUuids = parentActiveRules.stream().map(ActiveRuleDto::getRuleUuid).collect(MoreCollectors.toArrayList());
+    Collection<String> parentRuleUuids = parentActiveRules.stream().map(ActiveRuleDto::getRuleUuid).collect(Collectors.toList());
     RuleActivationContext context = ruleActivator.createContextForUserProfile(dbSession, profile, parentRuleUuids);
 
     for (ActiveRuleDto parentActiveRule : parentActiveRules) {
@@ -108,7 +108,7 @@ public class QProfileTreeImpl implements QProfileTree {
   private static List<OrgActiveRuleDto> getRulesDifference(Collection<OrgActiveRuleDto> rulesCollection1, Collection<OrgActiveRuleDto> rulesCollection2) {
     Collection<String> rulesCollection2Uuids = rulesCollection2.stream()
       .map(ActiveRuleDto::getRuleUuid)
-      .collect(MoreCollectors.toArrayList());
+      .collect(Collectors.toList());
 
     return rulesCollection1.stream()
       .filter(rule -> !rulesCollection2Uuids.contains(rule.getRuleUuid()))
@@ -134,7 +134,7 @@ public class QProfileTreeImpl implements QProfileTree {
   private List<ActiveRuleChange> getChangesFromRulesToBeRemoved(DbSession dbSession, QProfileDto profile, List<OrgActiveRuleDto> rules) {
     List<ActiveRuleChange> changes = new ArrayList<>();
 
-    Collection<String> ruleUuids = rules.stream().map(ActiveRuleDto::getRuleUuid).collect(MoreCollectors.toArrayList());
+    Collection<String> ruleUuids = rules.stream().map(ActiveRuleDto::getRuleUuid).collect(Collectors.toList());
     RuleActivationContext context = ruleActivator.createContextForUserProfile(dbSession, profile, ruleUuids);
 
     for (OrgActiveRuleDto activeRule : rules) {
index 3dc04a631f69d7ff2d9edfdcb9e97561e44aab18..a5d8bbc98c669f2b23c6e0ef2684166c683d5a6b 100644 (file)
@@ -57,44 +57,14 @@ public final class MoreCollectors {
     return Collectors.toCollection(() -> EnumSet.noneOf(enumClass));
   }
 
-  /**
-   * Delegates to {@link java.util.stream.Collectors#toCollection(Supplier)}.
-   */
-  public static <T> Collector<T, ?, ArrayList<T>> toArrayList() {
-    return java.util.stream.Collectors.toCollection(ArrayList::new);
-  }
-
-  /**
-   * Does {@code java.util.stream.MoreCollectors.toCollection(() -> new ArrayList<>(size));} which is equivalent to
-   * {@link #toArrayList()} but avoiding array copies when the size of the resulting list is already known.
-   *
-   * <p>Note: using this method with a parallel stream will likely not have the expected memory usage benefit as all
-   * processing threads will use a ArrayList with a capacity large enough for the final size.</p>
-   *
-   * @see java.util.stream.Collectors#toList()
-   * @see java.util.stream.Collectors#toCollection(Supplier)
-   */
   public static <T> Collector<T, ?, ArrayList<T>> toArrayList(int size) {
     return java.util.stream.Collectors.toCollection(() -> new ArrayList<>(size));
   }
 
-  /**
-   * Delegates to {@link java.util.stream.Collectors#toCollection(Supplier)}.
-   */
   public static <T> Collector<T, ?, HashSet<T>> toHashSet() {
     return java.util.stream.Collectors.toCollection(HashSet::new);
   }
 
-  /**
-   * Does {@code java.util.stream.MoreCollectors.toCollection(() -> new HashSet<>(size));} which is equivalent to
-   * {@link #toHashSet()} but avoiding array copies when the size of the resulting set is already known.
-   *
-   * <p>Note: using this method with a parallel stream will likely not have the expected memory usage benefit as all
-   * processing threads will use a HashSet with a capacity large enough for the final size.</p>
-   *
-   * @see java.util.stream.Collectors#toSet()
-   * @see java.util.stream.Collectors#toCollection(Supplier)
-   */
   public static <T> Collector<T, ?, HashSet<T>> toHashSet(int size) {
     return java.util.stream.Collectors.toCollection(() -> new HashSet<>(size));
   }
index 16678ad3f74339a8119fefa15f26c38d7e1587e9..eb42825ee15a4fc03e52da14aa82a0e8bf5e04cc 100644 (file)
@@ -80,18 +80,6 @@ public class MoreCollectorsTest {
       .isEmpty();
   }
 
-  @Test
-  public void toArrayList_builds_an_ArrayList() {
-    List<Integer> res = Stream.of(1, 2, 3, 4, 5).collect(toArrayList());
-    assertThat(res).isInstanceOf(ArrayList.class)
-      .containsExactly(1, 2, 3, 4, 5);
-  }
-
-  @Test
-  public void toArrayList_parallel_stream() {
-    assertThat(HUGE_LIST.parallelStream().collect(toArrayList())).isEqualTo(HUGE_LIST);
-  }
-
   @Test
   public void toArrayList_with_size_builds_an_ArrayList() {
     List<Integer> res = Stream.of(1, 2, 3, 4, 5).collect(toArrayList(30));