Skip to content

Commit 59a3df1

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 a21d596 commit 59a3df1

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
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

+11-1
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.
@@ -43,6 +43,16 @@ void shouldCreateStream() {
4343
assertThat(iterator.closed).isFalse();
4444
}
4545

46+
@Test // GH-2519
47+
void shouldCount() {
48+
49+
CloseableIteratorImpl<String> iterator = new CloseableIteratorImpl<>(Arrays.asList("1", "2", "3").iterator());
50+
51+
long count = iterator.stream().count();
52+
53+
assertThat(count).isEqualTo(3);
54+
}
55+
4656
@Test // DATACMNS-1637
4757
void closeStreamShouldCloseIterator() {
4858

0 commit comments

Comments
 (0)