StreamUtils.zip(…) now treats infinite streams correctly.

When an infinite Stream was handed into StreamUtils.zip(…) as first argument, the resulting stream was infinite, too, while inverting the argument order was limiting the resulting stream to the length of the finite one. This is now fixed by actually evaluating whether we can advance on both of the streams and shortcutting the process if that is not possible on either of the streams, limiting the processing of the overall Stream to the shorter of the two as already advertised in the Javadoc.

Fixes #2426.
This commit is contained in:
Oliver Drotbohm
2021-08-09 17:04:11 +02:00
parent fd586ebfdf
commit fbe513446d
3 changed files with 62 additions and 2 deletions

View File

@@ -42,4 +42,13 @@ public class StreamUtilsTests {
assertThat(input).isEqualTo(output);
}
@Test // #2426
void combinesInfiniteStreamCorrectly() {
Stream<Long> indices = Stream.iterate(1L, n -> n + 1);
Stream<String> lines = Stream.of("first line", "second line");
assertThat(StreamUtils.zip(indices, lines, (index, line) -> index + ":" + line).count()).isEqualTo(2);
}
}