PROTOCOLS-117 Add a stream helper We need a more readable way to concat (n) streams than chaining Stream.concat calls
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/5c904e1e Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/5c904e1e Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/5c904e1e Branch: refs/heads/master Commit: 5c904e1ea778803c6d391c424f8ebe40317bb15b Parents: 20a6c64 Author: benwa <[email protected]> Authored: Wed Nov 8 11:31:26 2017 +0700 Committer: benwa <[email protected]> Committed: Wed Nov 15 18:05:45 2017 +0700 ---------------------------------------------------------------------- .../java/org/apache/james/util/StreamUtils.java | 34 +++++++++ .../org/apache/james/util/StreamUtilsTest.java | 80 ++++++++++++++++++++ 2 files changed, 114 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/5c904e1e/server/container/util-java8/src/main/java/org/apache/james/util/StreamUtils.java ---------------------------------------------------------------------- diff --git a/server/container/util-java8/src/main/java/org/apache/james/util/StreamUtils.java b/server/container/util-java8/src/main/java/org/apache/james/util/StreamUtils.java new file mode 100644 index 0000000..f980802 --- /dev/null +++ b/server/container/util-java8/src/main/java/org/apache/james/util/StreamUtils.java @@ -0,0 +1,34 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.util; + +import java.util.Collection; +import java.util.function.Function; +import java.util.stream.Stream; + +public class StreamUtils { + public static <T> Stream<T> flatten(Collection<Stream<T>> streams) { + return flatten(streams.stream()); + } + + public static <T> Stream<T> flatten(Stream<Stream<T>> streams) { + return streams.flatMap(Function.identity()); + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/5c904e1e/server/container/util-java8/src/test/java/org/apache/james/util/StreamUtilsTest.java ---------------------------------------------------------------------- diff --git a/server/container/util-java8/src/test/java/org/apache/james/util/StreamUtilsTest.java b/server/container/util-java8/src/test/java/org/apache/james/util/StreamUtilsTest.java new file mode 100644 index 0000000..c14cd4c --- /dev/null +++ b/server/container/util-java8/src/test/java/org/apache/james/util/StreamUtilsTest.java @@ -0,0 +1,80 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.util; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.stream.Stream; + +import org.junit.Test; +import org.testcontainers.shaded.com.google.common.collect.ImmutableList; + +import com.github.steveash.guavate.Guavate; + +public class StreamUtilsTest { + + @Test + public void flattenShouldReturnEmptyWhenEmptyStreams() { + assertThat( + StreamUtils.<Integer>flatten(ImmutableList.of()) + .collect(Guavate.toImmutableList())) + .isEmpty(); + } + + @Test + public void flattenShouldPreserveSingleStreams() { + assertThat( + StreamUtils.flatten(ImmutableList.of( + Stream.of(1, 2, 3))) + .collect(Guavate.toImmutableList())) + .containsExactly(1, 2, 3); + } + + @Test + public void flattenShouldMergeSeveralStreamsTogether() { + assertThat( + StreamUtils.flatten(ImmutableList.of( + Stream.of(1, 2, 3), + Stream.of(4, 5))) + .collect(Guavate.toImmutableList())) + .containsExactly(1, 2, 3, 4, 5); + } + + @Test + public void flattenShouldAcceptEmptyStreams() { + assertThat( + StreamUtils.flatten(ImmutableList.of( + Stream.of())) + .collect(Guavate.toImmutableList())) + .isEmpty(); + } + + @Test + public void flattenShouldMergeEmptyStreamsWithOtherData() { + assertThat( + StreamUtils.flatten(ImmutableList.of( + Stream.of(1, 2), + Stream.of(), + Stream.of(3))) + .collect(Guavate.toImmutableList())) + .containsExactly(1, 2, 3); + } + +} \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
