Class DeferredStream<T>
- Type Parameters:
T
- the type of objects contained in the stream, as specified inStream
interface.
- All Implemented Interfaces:
AutoCloseable
,BaseStream<T,
,Stream<T>> Stream<T>
- Direct Known Subclasses:
FeatureStream
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
-
Nested Class Summary
Nested ClassesNested classes/interfaces inherited from interface java.util.stream.Stream
Stream.Builder<T extends Object>
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final DeferredStream.CloseHandler
A proxy to the handler to run for releasing resources.Fields inherited from class org.apache.sis.internal.stream.StreamWrapper
source
Fields inherited from class org.apache.sis.internal.stream.BaseStreamWrapper
toClose
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
DeferredStream
(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 void
setCloseHandler
(AutoCloseable handler) Registers a handler to run for releasing resources created by the worker.private Spliterator<T>
terminal()
Creates the worker iterator and marks this stream as not active anymore.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, unordered
Methods inherited from class org.apache.sis.internal.stream.BaseStreamWrapper
close, inactive, isParallel
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.util.stream.BaseStream
close, isParallel
Methods inherited from interface java.util.stream.Stream
dropWhile, mapMulti, mapMultiToDouble, mapMultiToInt, mapMultiToLong, takeWhile, toList
-
Field Details
-
closeHandler
A proxy to the handler to run for releasing resources. This is registered as a stream close handler immediately at stream creation time, but the actual work to do for releasing resource is set later, atcreateSourceIterator()
creation time.
-
-
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
-
terminal
Creates the worker iterator and marks this stream as not active anymore. This method is invoked byStreamSupport
when a terminal operation is invoked. -
createSourceIterator
Creates the iterator which will provide the actual data. The characteristics of the returned iterator must be thecharacteristics
argument given to theDeferredStream
constructor.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
MyIterator
implementsAutoCloseable
, 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 benull
for no action,
-