Skip to content

Commit acbcdb8

Browse files
committed
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
1 parent 55a5c96 commit acbcdb8

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

Diff for: src/main/java/org/springframework/data/util/CloseableIterator.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2021 the original author or authors.
2+
* Copyright 2015-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -49,12 +49,14 @@ public interface CloseableIterator<T> extends Iterator<T>, Closeable {
4949
* The default implementation should be overridden by subclasses that can return a more efficient spliterator. To
5050
* preserve expected laziness behavior for the {@link #stream()} method, spliterators should either have the
5151
* characteristic of {@code IMMUTABLE} or {@code CONCURRENT}, or be late-binding.
52+
* <p>
53+
* The default implementation does not report a size.
5254
*
5355
* @return a {@link Spliterator} over the elements in this {@link Iterator}.
5456
* @since 2.4
5557
*/
5658
default Spliterator<T> spliterator() {
57-
return Spliterators.spliterator(this, 0, 0);
59+
return Spliterators.spliterator(this, -1, 0);
5860
}
5961

6062
/**

Diff for: src/test/java/org/springframework/data/util/CloseableIteratorUnitTests.java

+34-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2021 the original author or authors.
2+
* Copyright 2020-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@
2020
import java.util.Arrays;
2121
import java.util.Iterator;
2222
import java.util.stream.Collectors;
23+
import java.util.stream.IntStream;
2324

2425
import org.junit.jupiter.api.Test;
2526

@@ -33,18 +34,48 @@ class CloseableIteratorUnitTests {
3334
@Test // DATACMNS-1637
3435
void shouldCreateStream() {
3536

36-
var iterator = new CloseableIteratorImpl<String>(Arrays.asList("1", "2", "3").iterator());
37+
var iterator = new CloseableIteratorImpl<>(Arrays.asList("1", "2", "3").iterator());
3738

3839
var collection = iterator.stream().map(it -> "hello " + it).collect(Collectors.toList());
3940

4041
assertThat(collection).contains("hello 1", "hello 2", "hello 3");
4142
assertThat(iterator.closed).isFalse();
4243
}
4344

45+
@Test // GH-2519
46+
void shouldCount() {
47+
48+
var iterator = new CloseableIteratorImpl<>(Arrays.asList("1", "2", "3").iterator());
49+
50+
var count = iterator.stream().count();
51+
52+
assertThat(count).isEqualTo(3);
53+
}
54+
55+
@Test // GH-2519
56+
void shouldCountLargeStream() {
57+
58+
var iterator = new CloseableIteratorImpl<>(IntStream.range(0, 2048).boxed().iterator());
59+
60+
var count = iterator.stream().count();
61+
62+
assertThat(count).isEqualTo(2048);
63+
}
64+
65+
@Test // GH-2519
66+
void shouldApplyToList() {
67+
68+
var iterator = new CloseableIteratorImpl<>(Arrays.asList("1", "2", "3").iterator());
69+
70+
var list = iterator.stream().toList();
71+
72+
assertThat(list).isEqualTo(Arrays.asList("1", "2", "3"));
73+
}
74+
4475
@Test // DATACMNS-1637
4576
void closeStreamShouldCloseIterator() {
4677

47-
var iterator = new CloseableIteratorImpl<String>(Arrays.asList("1", "2", "3").iterator());
78+
var iterator = new CloseableIteratorImpl<>(Arrays.asList("1", "2", "3").iterator());
4879

4980
try (var stream = iterator.stream()) {
5081
assertThat(stream.findFirst()).hasValue("1");

0 commit comments

Comments
 (0)