]> source.dussan.org Git - sonarqube.git/commitdiff
add collectors util classes in sonar-core
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Wed, 29 Jun 2016 09:38:54 +0000 (11:38 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Mon, 4 Jul 2016 13:20:30 +0000 (15:20 +0200)
sonar-core/src/main/java/org/sonar/core/util/stream/Collectors.java [new file with mode: 0644]
sonar-core/src/main/java/org/sonar/core/util/stream/GuavaCollectors.java [new file with mode: 0644]
sonar-core/src/test/java/org/sonar/core/util/stream/CollectorsTest.java [new file with mode: 0644]
sonar-core/src/test/java/org/sonar/core/util/stream/GuavaCollectorsTest.java [new file with mode: 0644]

diff --git a/sonar-core/src/main/java/org/sonar/core/util/stream/Collectors.java b/sonar-core/src/main/java/org/sonar/core/util/stream/Collectors.java
new file mode 100644 (file)
index 0000000..bbe096f
--- /dev/null
@@ -0,0 +1,69 @@
+/*
+ * 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 java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.function.Supplier;
+import java.util.stream.Collector;
+
+public final class Collectors {
+  private Collectors() {
+    // prevents instantiation
+  }
+
+  /**
+   * Delegates to {@link java.util.stream.Collectors#toList()}.
+   */
+  public static <T> Collector<T, ?, List<T>> toList() {
+    return java.util.stream.Collectors.toList();
+  }
+
+  /**
+   * 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.
+   *
+   * @see java.util.stream.Collectors#toList()
+   * @see java.util.stream.Collectors#toCollection(Supplier)
+   */
+  public static <T> Collector<T, ?, List<T>> toList(int size) {
+    return java.util.stream.Collectors.toCollection(() -> new ArrayList<>(size));
+  }
+
+  /**
+   * Delegates to {@link java.util.stream.Collectors#toSet()}.
+   */
+  public static <T> Collector<T, ?, Set<T>> toSet() {
+    return java.util.stream.Collectors.toSet();
+  }
+
+  /**
+   * 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.
+   *
+   * @see java.util.stream.Collectors#toSet()
+   * @see java.util.stream.Collectors#toCollection(Supplier)
+   */
+  public static <T> Collector<T, ?, Set<T>> toSet(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
new file mode 100644 (file)
index 0000000..700c570
--- /dev/null
@@ -0,0 +1,93 @@
+/*
+ * 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);
+  }
+
+}
diff --git a/sonar-core/src/test/java/org/sonar/core/util/stream/CollectorsTest.java b/sonar-core/src/test/java/org/sonar/core/util/stream/CollectorsTest.java
new file mode 100644 (file)
index 0000000..e3bd3c1
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ * 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 java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import org.junit.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class CollectorsTest {
+  @Test
+  public void toList_builds_an_ArrayList() {
+    List<Integer> res = Arrays.asList(1, 2, 3, 4, 5).stream().collect(Collectors.toList());
+    assertThat(res).isInstanceOf(ArrayList.class)
+      .containsExactly(1, 2, 3, 4, 5);
+  }
+  @Test
+  public void toList_with_size_builds_an_ArrayList() {
+    List<Integer> res = Arrays.asList(1, 2, 3, 4, 5).stream().collect(Collectors.toList(30));
+    assertThat(res).isInstanceOf(ArrayList.class)
+      .containsExactly(1, 2, 3, 4, 5);
+  }
+
+  @Test
+  public void toSet_builds_an_HashSet() {
+    Set<Integer> res = Arrays.asList(1, 2, 3, 4, 5).stream().collect(Collectors.toSet());
+    assertThat(res).isInstanceOf(HashSet.class)
+      .containsExactly(1, 2, 3, 4, 5);
+  }
+  @Test
+  public void toSet_with_size_builds_an_ArrayList() {
+    Set<Integer> res = Arrays.asList(1, 2, 3, 4, 5).stream().collect(Collectors.toSet(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
new file mode 100644 (file)
index 0000000..2ff0c28
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+ * 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);
+  }
+
+
+}