]> source.dussan.org Git - sonarqube.git/commitdiff
merge GuavaCollectors into Collectors and rename methods
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Tue, 19 Jul 2016 10:50:35 +0000 (12:50 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 21 Jul 2016 15:03:57 +0000 (17:03 +0200)
server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/PersistComponentsStep.java
server/sonar-server/src/main/java/org/sonar/server/issue/index/IssueIndex.java
sonar-core/src/main/java/org/sonar/core/util/stream/Collectors.java
sonar-core/src/main/java/org/sonar/core/util/stream/GuavaCollectors.java [deleted file]
sonar-core/src/test/java/org/sonar/core/util/stream/CollectorsTest.java
sonar-core/src/test/java/org/sonar/core/util/stream/GuavaCollectorsTest.java [deleted file]
sonar-db/src/main/java/org/sonar/db/purge/PurgeDao.java
sonar-db/src/main/java/org/sonar/db/purge/period/DefaultPeriodCleaner.java
sonar-scanner-engine/src/main/java/org/sonar/scanner/rule/RuleFinderCompatibility.java

index 31de0218bdb921ee2da93d6e2eda48ef3f7be86b..424021b4e3431e5416a96272477bf5d6ccf48d20 100644 (file)
@@ -32,7 +32,6 @@ import org.apache.commons.lang.StringUtils;
 import org.sonar.api.resources.Qualifiers;
 import org.sonar.api.resources.Scopes;
 import org.sonar.api.utils.System2;
-import org.sonar.core.util.stream.GuavaCollectors;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.component.ComponentDto;
@@ -49,6 +48,7 @@ import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolde
 import org.sonar.server.computation.task.step.ComputationStep;
 
 import static com.google.common.collect.FluentIterable.from;
+import static org.sonar.core.util.stream.Collectors.toList;
 import static org.sonar.db.component.ComponentDto.UUID_PATH_SEPARATOR;
 import static org.sonar.db.component.ComponentDto.formatUuidPathFromParent;
 import static org.sonar.server.computation.task.projectanalysis.component.ComponentVisitor.Order.PRE_ORDER;
@@ -111,7 +111,7 @@ public class PersistComponentsStep implements ComputationStep {
           .setBEnabled(false);
         dbClient.componentDao().update(dbSession, update);
       });
-    disabledComponentsHolder.setUuids(dtos.stream().map(ComponentDto::uuid).collect(GuavaCollectors.toList(dtos.size())));
+    disabledComponentsHolder.setUuids(dtos.stream().map(ComponentDto::uuid).collect(toList(dtos.size())));
   }
 
   /**
index 937b6c47122847e57fbdd71d9a0103481888aec7..7137467703240b2915ddf659e825f99ec6b13388 100644 (file)
@@ -544,7 +544,7 @@ public class IssueIndex extends BaseIndex {
     if (values == null) {
       return Collections.emptyList();
     }
-    return values.stream().map(Pattern::quote).collect(Collectors.toList(values.size()));
+    return values.stream().map(Pattern::quote).collect(Collectors.toArrayList(values.size()));
   }
 
   private void addAssignedToMeFacetIfNeeded(SearchRequestBuilder builder, SearchOptions options, IssueQuery query, Map<String, QueryBuilder> filters, QueryBuilder queryBuilder) {
index bbe096f0ff0492f0525cf5d9c574b8ecd9bd7e9c..e13d3c14271df25f2a845bea29a76af3ed1938e7 100644 (file)
@@ -19,6 +19,8 @@
  */
 package org.sonar.core.util.stream;
 
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
 import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.List;
@@ -32,38 +34,96 @@ public final class Collectors {
   }
 
   /**
-   * Delegates to {@link java.util.stream.Collectors#toList()}.
+   * A Collector into an {@link ImmutableList}.
    */
-  public static <T> Collector<T, ?, List<T>> toList() {
-    return java.util.stream.Collectors.toList();
+  public static <T> Collector<T, List<T>, List<T>> toList() {
+    return Collector.of(
+      ArrayList::new,
+      List::add,
+      (left, right) -> {
+        left.addAll(right);
+        return left;
+      },
+      ImmutableList::copyOf);
+  }
+
+  /**
+   * A Collector into an {@link ImmutableList} of the specified expected size.
+   */
+  public static <T> Collector<T, List<T>, List<T>> 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);
+  }
+
+  /**
+   * A Collector into an {@link ImmutableSet}.
+   */
+  public static <T> Collector<T, Set<T>, Set<T>> toSet() {
+    return Collector.of(
+      HashSet::new,
+      Set::add,
+      (left, right) -> {
+        left.addAll(right);
+        return left;
+      },
+      ImmutableSet::copyOf);
+  }
+
+  /**
+   * A Collector into an {@link ImmutableSet} of the specified expected size.
+   */
+  public static <T> Collector<T, Set<T>, Set<T>> 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);
+  }
+
+  /**
+   * 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.Collectors.toCollection(() -> new ArrayList<>(size));} which is equivalent to
-   * {@link #toList()} but avoiding array copies when the size of the resulting set is already known.
+   * {@link #toArrayList()} but avoiding array copies when the size of the resulting list is already known.
    *
    * @see java.util.stream.Collectors#toList()
    * @see java.util.stream.Collectors#toCollection(Supplier)
    */
-  public static <T> Collector<T, ?, List<T>> toList(int size) {
+  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#toSet()}.
+   * Delegates to {@link java.util.stream.Collectors#toCollection(Supplier)}.
    */
-  public static <T> Collector<T, ?, Set<T>> toSet() {
-    return java.util.stream.Collectors.toSet();
+  public static <T> Collector<T, ?, HashSet<T>> toHashSet() {
+    return java.util.stream.Collectors.toCollection(HashSet::new);
   }
 
   /**
    * Does {@code java.util.stream.Collectors.toCollection(() -> new HashSet<>(size));} which is equivalent to
-   * {@link #toSet()} but avoiding array copies when the size of the resulting set is already known.
+   * {@link #toHashSet()} but avoiding array copies when the size of the resulting set is already known.
    *
    * @see java.util.stream.Collectors#toSet()
    * @see java.util.stream.Collectors#toCollection(Supplier)
    */
-  public static <T> Collector<T, ?, Set<T>> toSet(int size) {
+  public static <T> Collector<T, ?, HashSet<T>> toHashSet(int size) {
     return java.util.stream.Collectors.toCollection(() -> new HashSet<>(size));
   }
 }
diff --git a/sonar-core/src/main/java/org/sonar/core/util/stream/GuavaCollectors.java b/sonar-core/src/main/java/org/sonar/core/util/stream/GuavaCollectors.java
deleted file mode 100644 (file)
index 700c570..0000000
+++ /dev/null
@@ -1,93 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- */
-package org.sonar.core.util.stream;
-
-import com.google.common.collect.ImmutableList;
-import com.google.common.collect.ImmutableSet;
-import java.util.ArrayList;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Set;
-import java.util.stream.Collector;
-
-public final class GuavaCollectors {
-  private GuavaCollectors() {
-    // prevents instantiation
-  }
-
-  /**
-   * A Collector into an {@link ImmutableList}.
-   */
-  public static <T> Collector<T, List<T>, ImmutableList<T>> toList() {
-    return Collector.of(
-      ArrayList::new,
-      List::add,
-      (left, right) -> {
-        left.addAll(right);
-        return left;
-      },
-      ImmutableList::copyOf);
-  }
-
-  /**
-   * A Collector into an {@link ImmutableList} of the specified expected size.
-   */
-  public static <T> Collector<T, List<T>, ImmutableList<T>> 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);
-  }
-
-  /**
-   * A Collector into an {@link ImmutableSet}.
-   */
-  public static <T> Collector<T, Set<T>, ImmutableSet<T>> toSet() {
-    return Collector.of(
-      HashSet::new,
-      Set::add,
-      (left, right) -> {
-        left.addAll(right);
-        return left;
-      },
-      ImmutableSet::copyOf);
-  }
-
-  /**
-   * A Collector into an {@link ImmutableSet} of the specified expected size.
-   */
-  public static <T> Collector<T, Set<T>, ImmutableSet<T>> 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);
-  }
-
-}
index e3bd3c1b101fe5be728ba43d16f412928c269a65..a8c02735f51e73ff10da95f9425bee7b89350e9e 100644 (file)
@@ -19,6 +19,8 @@
  */
 package org.sonar.core.util.stream;
 
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashSet;
@@ -30,27 +32,57 @@ import static org.assertj.core.api.Assertions.assertThat;
 
 public class CollectorsTest {
   @Test
-  public void toList_builds_an_ArrayList() {
+  public void toList_builds_an_ImmutableList() {
     List<Integer> res = Arrays.asList(1, 2, 3, 4, 5).stream().collect(Collectors.toList());
-    assertThat(res).isInstanceOf(ArrayList.class)
+    assertThat(res).isInstanceOf(ImmutableList.class)
       .containsExactly(1, 2, 3, 4, 5);
   }
+
   @Test
-  public void toList_with_size_builds_an_ArrayList() {
+  public void toList_with_size_builds_an_ImmutableList() {
     List<Integer> res = Arrays.asList(1, 2, 3, 4, 5).stream().collect(Collectors.toList(30));
-    assertThat(res).isInstanceOf(ArrayList.class)
+    assertThat(res).isInstanceOf(ImmutableList.class)
       .containsExactly(1, 2, 3, 4, 5);
   }
 
   @Test
-  public void toSet_builds_an_HashSet() {
+  public void toSet_builds_an_ImmutableSet() {
     Set<Integer> res = Arrays.asList(1, 2, 3, 4, 5).stream().collect(Collectors.toSet());
-    assertThat(res).isInstanceOf(HashSet.class)
+    assertThat(res).isInstanceOf(ImmutableSet.class)
       .containsExactly(1, 2, 3, 4, 5);
   }
+
   @Test
-  public void toSet_with_size_builds_an_ArrayList() {
+  public void toSet_with_size_builds_an_ImmutableSet() {
     Set<Integer> res = Arrays.asList(1, 2, 3, 4, 5).stream().collect(Collectors.toSet(30));
+    assertThat(res).isInstanceOf(ImmutableSet.class)
+      .containsExactly(1, 2, 3, 4, 5);
+  }
+
+  @Test
+  public void toArrayList_builds_an_ArrayList() {
+    List<Integer> res = Arrays.asList(1, 2, 3, 4, 5).stream().collect(Collectors.toArrayList());
+    assertThat(res).isInstanceOf(ArrayList.class)
+      .containsExactly(1, 2, 3, 4, 5);
+  }
+
+  @Test
+  public void toArrayList_with_size_builds_an_ArrayList() {
+    List<Integer> res = Arrays.asList(1, 2, 3, 4, 5).stream().collect(Collectors.toArrayList(30));
+    assertThat(res).isInstanceOf(ArrayList.class)
+      .containsExactly(1, 2, 3, 4, 5);
+  }
+
+  @Test
+  public void toHashSet_builds_an_HashSet() {
+    Set<Integer> res = Arrays.asList(1, 2, 3, 4, 5).stream().collect(Collectors.toHashSet());
+    assertThat(res).isInstanceOf(HashSet.class)
+      .containsExactly(1, 2, 3, 4, 5);
+  }
+
+  @Test
+  public void toHashSet_with_size_builds_an_ArrayList() {
+    Set<Integer> res = Arrays.asList(1, 2, 3, 4, 5).stream().collect(Collectors.toHashSet(30));
     assertThat(res).isInstanceOf(HashSet.class)
       .containsExactly(1, 2, 3, 4, 5);
   }
diff --git a/sonar-core/src/test/java/org/sonar/core/util/stream/GuavaCollectorsTest.java b/sonar-core/src/test/java/org/sonar/core/util/stream/GuavaCollectorsTest.java
deleted file mode 100644 (file)
index 2ff0c28..0000000
+++ /dev/null
@@ -1,59 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- */
-package org.sonar.core.util.stream;
-
-import com.google.common.collect.ImmutableList;
-import com.google.common.collect.ImmutableSet;
-import java.util.Arrays;
-import java.util.List;
-import java.util.Set;
-import org.junit.Test;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
-public class GuavaCollectorsTest {
-  @Test
-  public void toList_builds_an_ImmutableList() {
-    List<Integer> res = Arrays.asList(1, 2, 3, 4, 5).stream().collect(GuavaCollectors.toList());
-    assertThat(res).isInstanceOf(ImmutableList.class)
-        .containsExactly(1, 2, 3, 4, 5);
-  }
-  @Test
-  public void toList_with_size_builds_an_ImmutableList() {
-    List<Integer> res = Arrays.asList(1, 2, 3, 4, 5).stream().collect(GuavaCollectors.toList(30));
-    assertThat(res).isInstanceOf(ImmutableList.class)
-        .containsExactly(1, 2, 3, 4, 5);
-  }
-
-  @Test
-  public void toSet_builds_an_ImmutableSet() {
-    Set<Integer> res = Arrays.asList(1, 2, 3, 4, 5).stream().collect(GuavaCollectors.toSet());
-    assertThat(res).isInstanceOf(ImmutableSet.class)
-        .containsExactly(1, 2, 3, 4, 5);
-  }
-  @Test
-  public void toSet_with_size_builds_an_ImmutableSet() {
-    Set<Integer> res = Arrays.asList(1, 2, 3, 4, 5).stream().collect(GuavaCollectors.toSet(30));
-    assertThat(res).isInstanceOf(ImmutableSet.class)
-        .containsExactly(1, 2, 3, 4, 5);
-  }
-
-
-}
index bd8da6680003e5081a2b57ff85c5c5e39c5f064b..0b5a2f4870059ba43cb7c421aa2a97d972177cd9 100644 (file)
@@ -28,7 +28,7 @@ import java.util.List;
 import org.sonar.api.utils.System2;
 import org.sonar.api.utils.log.Logger;
 import org.sonar.api.utils.log.Loggers;
-import org.sonar.core.util.stream.GuavaCollectors;
+import org.sonar.core.util.stream.Collectors;
 import org.sonar.db.Dao;
 import org.sonar.db.DbSession;
 import org.sonar.db.component.ComponentDao;
@@ -117,7 +117,7 @@ public class PurgeDao implements Dao {
           .setQualifiers(Arrays.asList(scopesWithoutHistoricalData))
           .build())
       .stream().map(ComponentDto::uuid)
-      .collect(GuavaCollectors.toList());
+      .collect(Collectors.toList());
 
     purgeCommands.deleteComponentMeasures(analysisUuids, componentWithoutHistoricalDataUuids);
   }
index 29fd532a7604741a5b4601260edb297cb5e4cd02..0378ced5b29e607559af9166e08f0d1983743f2d 100644 (file)
@@ -34,7 +34,7 @@ import org.sonar.db.purge.PurgeDao;
 import org.sonar.db.purge.PurgeProfiler;
 import org.sonar.db.purge.PurgeableAnalysisDto;
 
-import static org.sonar.core.util.stream.GuavaCollectors.toList;
+import static org.sonar.core.util.stream.Collectors.toList;
 
 public class DefaultPeriodCleaner {
 
@@ -67,11 +67,11 @@ public class DefaultPeriodCleaner {
         Joiner.on(", ").join(
           snapshots.stream()
             .map(snapshot -> snapshot.getAnalysisUuid() + "@" + DateUtils.formatDateTime(snapshot.getDate()))
-            .collect(Collectors.toList(snapshots.size()))));
+            .collect(Collectors.toArrayList(snapshots.size()))));
     }
     purgeDao.deleteAnalyses(
       session, profiler,
-      snapshots.stream().map(DefaultPeriodCleaner::toIdUuidPair).collect(toList(snapshots.size())));
+      snapshots.stream().map(DefaultPeriodCleaner::toIdUuidPair).collect(Collectors.toList(snapshots.size())));
     return snapshots;
   }
 
index cee346fc94c3e64dee0fdb9fb0c8bf4b19905b85..4d16945fc9721e3f25c549baab4cab179eba9975 100644 (file)
@@ -31,6 +31,7 @@ import org.sonar.api.rule.RuleKey;
 import org.sonar.api.rules.Rule;
 import org.sonar.api.rules.RuleFinder;
 import org.sonar.api.rules.RuleQuery;
+import org.sonar.core.util.stream.Collectors;
 
 import static org.sonar.core.util.stream.Collectors.toList;
 
@@ -88,7 +89,7 @@ public class RuleFinderCompatibility implements RuleFinder {
   private Collection<Rule> byRepository(RuleQuery query) {
     return rules.findByRepository(query.getRepositoryKey()).stream()
       .map(RuleFinderCompatibility::toRule)
-      .collect(toList());
+      .collect(Collectors.toArrayList());
   }
 
   private Collection<Rule> byKey(RuleQuery query) {
@@ -99,7 +100,7 @@ public class RuleFinderCompatibility implements RuleFinder {
   private Collection<Rule> byInternalKey(RuleQuery query) {
     return rules.findByInternalKey(query.getRepositoryKey(), query.getConfigKey()).stream()
       .map(RuleFinderCompatibility::toRule)
-      .collect(toList());
+      .collect(Collectors.toArrayList());
   }
 
   @CheckForNull