Fix spliterator size hint in CloseableIterator.spliterator().

We now report -1 as size to avoid zero-size results for count() or toList() operators.

Closes #2519
This commit is contained in:
Mark Paluch
2022-01-03 11:14:09 +01:00
parent 6cf2634a64
commit 3f44d268b6
3 changed files with 143 additions and 4 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2020-2021 the original author or authors.
* Copyright 2020-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@ import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
@@ -43,6 +44,26 @@ class CloseableIteratorUnitTests {
assertThat(iterator.closed).isFalse();
}
@Test // GH-2519
void shouldCount() {
CloseableIteratorImpl<String> iterator = new CloseableIteratorImpl<>(Arrays.asList("1", "2", "3").iterator());
long count = iterator.stream().count();
assertThat(count).isEqualTo(3);
}
@Test // GH-2519
void shouldCountLargeStream() {
CloseableIteratorImpl<Integer> iterator = new CloseableIteratorImpl<>(IntStream.range(0, 2048).boxed().iterator());
long count = iterator.stream().count();
assertThat(count).isEqualTo(2048);
}
@Test // DATACMNS-1637
void closeStreamShouldCloseIterator() {