Class DeferredStream<T>
- Type Parameters:
T- the type of objects contained in the stream, as specified inStreaminterface.
- All Implemented Interfaces:
AutoCloseable,BaseStream<T,,Stream<T>> Stream<T>
Spliterator created by createSourceIterator().
The call to that method is deferred until a terminal operation is invoked.
COUNT query on SQL databases),
a DeferredStream subclass can override the StreamWrapper.count() method for running the count query
instead of counting elements of the stream manually.
Deferred streams are also useful with intermediate operations. For example, a subclass can override
the StreamWrapper.skip(long) and StreamWrapper.limit(long) methods for modifying the SQL query with addition of
OFFSET and FETCH NEXT clauses before the worker stream is created.
- Since:
- 1.1
- Version:
- 1.1
- Author:
- Alexis Manin (Geomatys), Martin Desruisseaux (Geomatys)
-
Nested Class Summary
Nested classes/interfaces inherited from interface java.util.stream.Stream
Stream.Builder<T> -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedDeferredStream(int characteristics, boolean parallel) Creates a new deferred stream. -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract Spliterator<T> Creates the iterator which will provide the actual data.protected final voidsetCloseHandler(AutoCloseable handler) Registers a handler to run for releasing resources created by the worker.Methods inherited from class org.apache.sis.internal.stream.StreamWrapper
allMatch, anyMatch, collect, collect, count, delegate, distinct, filter, findAny, findFirst, flatMap, flatMapToDouble, flatMapToInt, flatMapToLong, forEach, forEachOrdered, iterator, limit, map, mapToDouble, mapToInt, mapToLong, max, min, noneMatch, onClose, parallel, peek, reduce, reduce, reduce, sequential, skip, sorted, sorted, source, spliterator, toArray, toArray, unorderedMethods inherited from class org.apache.sis.internal.stream.BaseStreamWrapper
close, isParallelMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.util.stream.BaseStream
close, isParallel
-
Constructor Details
-
DeferredStream
protected DeferredStream(int characteristics, boolean parallel) Creates a new deferred stream.- Parameters:
characteristics- characteristics of the iterator to be created bycreateSourceIterator().parallel- whether the stream is initially parallel.
-
-
Method Details
-
createSourceIterator
Creates the iterator which will provide the actual data. The characteristics of the returned iterator must be thecharacteristicsargument given to theDeferredStreamconstructor.This method is invoked at most once, generally when a stream terminal operation is invoked. After this method is invoked, this stream will not be active anymore. The stream returned by the public methods should be used instead.
- Returns:
- an iterator over the elements.
- Throws:
Exception- if the iterator cannot be created.
-
setCloseHandler
Registers a handler to run for releasing resources created by the worker. This method can be invoked by thecreateSourceIterator()implementation. The specified handler will be executed exactly once, unless it is discarded by a subsequent call tosetCloseHandler(…).This method can be invoked many times, each invocation replacing the previous handler. The following example uses JDBC connection and assumes that
MyIteratorimplementsAutoCloseable, and that the resource disposal done by that method includes closing the JDBC connection:- Parameters:
handler- the action to execute for releasing resources, includes the case whencreateSourceIterator()fails. Can benullfor no action,
-