diff --git a/src/main/java/org/springframework/data/util/CloseableIterator.java b/src/main/java/org/springframework/data/util/CloseableIterator.java index 6210dfab1b..a9e22ca0fa 100644 --- a/src/main/java/org/springframework/data/util/CloseableIterator.java +++ b/src/main/java/org/springframework/data/util/CloseableIterator.java @@ -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. @@ -49,12 +49,14 @@ public interface CloseableIterator extends Iterator, 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. + *

+ * The default implementation does not report a size. * * @return a {@link Spliterator} over the elements in this {@link Iterator}. * @since 2.4 */ default Spliterator spliterator() { - return Spliterators.spliterator(this, 0, 0); + return Spliterators.spliterator(this, -1, 0); } /** diff --git a/src/test/java/org/springframework/data/util/CloseableIteratorUnitTests.java b/src/test/java/org/springframework/data/util/CloseableIteratorUnitTests.java index 5bde4ee1b6..075fca46a5 100644 --- a/src/test/java/org/springframework/data/util/CloseableIteratorUnitTests.java +++ b/src/test/java/org/springframework/data/util/CloseableIteratorUnitTests.java @@ -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. @@ -33,7 +33,7 @@ class CloseableIteratorUnitTests { @Test // DATACMNS-1637 void shouldCreateStream() { - var iterator = new CloseableIteratorImpl(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()); @@ -41,10 +41,30 @@ void shouldCreateStream() { 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(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");