Skip to content

Commit f9ea697

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 f9ea697

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-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

+23-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.
@@ -33,18 +33,38 @@ class CloseableIteratorUnitTests {
3333
@Test // DATACMNS-1637
3434
void shouldCreateStream() {
3535

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

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

4040
assertThat(collection).contains("hello 1", "hello 2", "hello 3");
4141
assertThat(iterator.closed).isFalse();
4242
}
4343

44+
@Test // GH-2519
45+
void shouldCount() {
46+
47+
var iterator = new CloseableIteratorImpl<>(Arrays.asList("1", "2", "3").iterator());
48+
49+
var count = iterator.stream().count();
50+
51+
assertThat(count).isEqualTo(3);
52+
}
53+
54+
@Test // GH-2519
55+
void shouldApplyToList() {
56+
57+
var iterator = new CloseableIteratorImpl<>(Arrays.asList("1", "2", "3").iterator());
58+
59+
var list = iterator.stream().toList();
60+
61+
assertThat(list).isEqualTo(Arrays.asList("1", "2", "3"));
62+
}
63+
4464
@Test // DATACMNS-1637
4565
void closeStreamShouldCloseIterator() {
4666

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

4969
try (var stream = iterator.stream()) {
5070
assertThat(stream.findFirst()).hasValue("1");

0 commit comments

Comments
 (0)