DATACMNS-1110 - Introduced StreamUtils.createStreamFromIterator(CloseableIterator).

This is an overload for the already existing method taking a plain Iterator so that we only apply the close callback to the Stream in the newly introduced one.
This commit is contained in:
Oliver Gierke
2017-07-12 10:54:01 +02:00
parent b724797dc6
commit 4d2cc9ab8e

View File

@@ -41,25 +41,30 @@ import org.springframework.util.MultiValueMap;
public interface StreamUtils {
/**
* Returns a {@link Stream} backed by the given {@link Iterator}.
* <p>
* If the given iterator is an {@link CloseableIterator} add a {@link CloseableIteratorDisposingRunnable} wrapping the
* given iterator to propagate {@link Stream#close()} accordingly.
* Returns a {@link Stream} backed by the given {@link Iterator}
*
* @param iterator must not be {@literal null}
* @param iterator must not be {@literal null}.
* @return
* @since 1.8
*/
public static <T> Stream<T> createStreamFromIterator(Iterator<T> iterator) {
Spliterator<T> spliterator = Spliterators.spliteratorUnknownSize(iterator, Spliterator.NONNULL);
return StreamSupport.stream(spliterator, false);
}
/**
* Returns a {@link Stream} backed by the given {@link CloseableIterator} and forwarding calls to
* {@link Stream#close()} to the iterator.
*
* @param iterator must not be {@literal null}.
* @return
* @since 2.0
*/
public static <T> Stream<T> createStreamFromIterator(CloseableIterator<T> iterator) {
Assert.notNull(iterator, "Iterator must not be null!");
Spliterator<T> spliterator = Spliterators.spliteratorUnknownSize(iterator, Spliterator.NONNULL);
Stream<T> stream = StreamSupport.stream(spliterator, false);
return iterator instanceof CloseableIterator//
? stream.onClose(() -> ((CloseableIterator<T>) iterator).close()) //
: stream;
return createStreamFromIterator((Iterator<T>) iterator).onClose(() -> iterator.close());
}
/**