]> source.dussan.org Git - sonarqube.git/commitdiff
add Collectors.join(Joiner)
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Wed, 20 Jul 2016 16:10:01 +0000 (18:10 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 21 Jul 2016 15:03:57 +0000 (17:03 +0200)
sonar-core/src/main/java/org/sonar/core/util/stream/Collectors.java
sonar-core/src/test/java/org/sonar/core/util/stream/CollectorsTest.java

index a1f57685262c436b6dfdbb9025939c7e7b0a6595..449261a6c4a2d7d9dd467a38ee704f574774e290 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.core.util.stream;
 
+import com.google.common.base.Joiner;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableListMultimap;
 import com.google.common.collect.ImmutableMap;
@@ -322,4 +323,26 @@ public final class Collectors {
       merger,
       ImmutableListMultimap.Builder::build);
   }
+
+  /**
+   * Applies the specified {@link Joiner} to the current stream.
+   *
+   * @throws NullPointerException of {@code joiner} is {@code null}
+   * @throws IllegalStateException if a merge operation happens because parallel processing has been enabled on the current stream
+   */
+  public static <E> Collector<E, List<E>, String> join(Joiner joiner) {
+    requireNonNull(joiner, "Joiner can't be null");
+
+    return Collector.of(
+      ArrayList::new,
+      List::add,
+      mergeNotSupportedMerger(),
+      joiner::join);
+  }
+
+  private static <R> BinaryOperator<R> mergeNotSupportedMerger() {
+    return (m1, m2) -> {
+      throw new IllegalStateException("Parallel processing is not supported");
+    };
+  }
 }
index 9bac9ee7d8fc22a8f40bbd849f29cb31d00dc0e7..a5d9466a494902d1059f49671ec1191fb9d726ea 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.core.util.stream;
 
+import com.google.common.base.Joiner;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Multimap;
@@ -37,6 +38,7 @@ import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.api.Assertions.entry;
 import static org.assertj.guava.api.Assertions.assertThat;
 import static org.sonar.core.util.stream.Collectors.index;
+import static org.sonar.core.util.stream.Collectors.join;
 import static org.sonar.core.util.stream.Collectors.toArrayList;
 import static org.sonar.core.util.stream.Collectors.toHashSet;
 import static org.sonar.core.util.stream.Collectors.toList;
@@ -351,6 +353,34 @@ public class CollectorsTest {
     assertThat(multimap).contains(entry(1, "A"), entry(2, "B"), entry(3, "C"));
   }
 
+  @Test
+  public void join_on_empty_stream_returns_empty_string() {
+    assertThat(Collections.emptyList().stream().collect(join(Joiner.on(",")))).isEmpty();
+  }
+
+  @Test
+  public void join_fails_with_NPE_if_joiner_is_null() {
+    expectedException.expect(NullPointerException.class);
+    expectedException.expectMessage("Joiner can't be null");
+
+    join(null);
+  }
+
+  @Test
+  public void join_applies_joiner_to_stream() {
+    assertThat(Arrays.asList("1", "2", "3", "4").stream().collect(join(Joiner.on(","))))
+      .isEqualTo("1,2,3,4");
+  }
+
+  @Test
+  public void join_supports_null_if_joiner_does() {
+    Stream<String> stream = Arrays.asList("1", null).stream();
+
+    expectedException.expect(NullPointerException.class);
+
+    stream.collect(join(Joiner.on(",")));
+  }
+
   private void expectedDuplicateKey1IAE() {
     expectedException.expect(IllegalArgumentException.class);
     expectedException.expectMessage("Duplicate key 1");