Skip to content

Commit

Permalink
Fix spliterator size hint in CloseableIterator.spliterator().
Browse files Browse the repository at this point in the history
We now report -1 as size to avoid zero-size results for count() or toList() operators.

Closes #2519
  • Loading branch information
mp911de committed Jan 3, 2022
1 parent 55a5c96 commit f9ea697
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015-2021 the original author or authors.
* Copyright 2015-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.
Expand Down Expand Up @@ -49,12 +49,14 @@ public interface CloseableIterator<T> extends Iterator<T>, Closeable {
* The default implementation should be overridden by subclasses that can return a more efficient spliterator. To
* preserve expected laziness behavior for the {@link #stream()} method, spliterators should either have the
* characteristic of {@code IMMUTABLE} or {@code CONCURRENT}, or be late-binding.
* <p>
* The default implementation does not report a size.
*
* @return a {@link Spliterator} over the elements in this {@link Iterator}.
* @since 2.4
*/
default Spliterator<T> spliterator() {
return Spliterators.spliterator(this, 0, 0);
return Spliterators.spliterator(this, -1, 0);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -33,18 +33,38 @@ class CloseableIteratorUnitTests {
@Test // DATACMNS-1637
void shouldCreateStream() {

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

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

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

@Test // GH-2519
void shouldCount() {

var iterator = new CloseableIteratorImpl<>(Arrays.asList("1", "2", "3").iterator());

var count = iterator.stream().count();

assertThat(count).isEqualTo(3);
}

@Test // GH-2519
void shouldApplyToList() {

var iterator = new CloseableIteratorImpl<>(Arrays.asList("1", "2", "3").iterator());

var list = iterator.stream().toList();

assertThat(list).isEqualTo(Arrays.asList("1", "2", "3"));
}

@Test // DATACMNS-1637
void closeStreamShouldCloseIterator() {

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

try (var stream = iterator.stream()) {
assertThat(stream.findFirst()).hasValue("1");
Expand Down

0 comments on commit f9ea697

Please sign in to comment.