aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>2016-07-20 18:10:01 +0200
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>2016-07-21 17:03:57 +0200
commit1bb0e1cfd1aa5f2de1d20d6d9a10af3803dde057 (patch)
tree0000a1b57d84753c7a19e811439c57b9a9bafd3b
parent8a59810c57e65e6dbbe8245dfcbce25e96cd4dc1 (diff)
downloadsonarqube-1bb0e1cfd1aa5f2de1d20d6d9a10af3803dde057.tar.gz
sonarqube-1bb0e1cfd1aa5f2de1d20d6d9a10af3803dde057.zip
add Collectors.join(Joiner)
-rw-r--r--sonar-core/src/main/java/org/sonar/core/util/stream/Collectors.java23
-rw-r--r--sonar-core/src/test/java/org/sonar/core/util/stream/CollectorsTest.java30
2 files changed, 53 insertions, 0 deletions
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
index a1f57685262..449261a6c4a 100644
--- 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
@@ -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");
+ };
+ }
}
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
index 9bac9ee7d8f..a5d9466a494 100644
--- 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
@@ -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");