From 1bb0e1cfd1aa5f2de1d20d6d9a10af3803dde057 Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Wed, 20 Jul 2016 18:10:01 +0200 Subject: [PATCH] add Collectors.join(Joiner) --- .../sonar/core/util/stream/Collectors.java | 23 ++++++++++++++ .../core/util/stream/CollectorsTest.java | 30 +++++++++++++++++++ 2 files changed, 53 insertions(+) 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 Collector, String> join(Joiner joiner) { + requireNonNull(joiner, "Joiner can't be null"); + + return Collector.of( + ArrayList::new, + List::add, + mergeNotSupportedMerger(), + joiner::join); + } + + private static BinaryOperator 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 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"); -- 2.39.5