E_IN
- type of input elementsE_OUT
- type of output elementsS
- type of the subclass implementing BaseStream
public abstract class AbstractPipeline<E_IN,E_OUT,S extends BaseStream<E_OUT,S>> extends PipelineHelper<E_OUT> implements BaseStream<E_OUT,S>
An AbstractPipeline
represents an initial portion of a stream
pipeline, encapsulating a stream source and zero or more intermediate
operations. The individual AbstractPipeline
objects are often
referred to as stages, where each stage describes either the stream
source or an intermediate operation.
A concrete intermediate stage is generally built from an
AbstractPipeline
, a shape-specific pipeline class which extends it
(e.g., IntPipeline
) which is also abstract, and an operation-specific
concrete class which extends that. AbstractPipeline
contains most of
the mechanics of evaluating the pipeline, and implements methods that will be
used by the operation; the shape-specific classes add helper methods for
dealing with collection of results into the appropriate shape-specific
containers.
After chaining a new intermediate operation, or executing a terminal operation, the stream is considered to be consumed, and no more intermediate or terminal operations are permitted on this stream instance.
Modifier and Type | Field and Description |
---|---|
protected int |
sourceOrOpFlags
The operation flags for the intermediate operation represented by this
pipeline object.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes this stream, causing all close handlers for this stream pipeline
to be called.
|
<P_IN> Node<E_OUT> |
evaluate(Spliterator<P_IN> spliterator,
boolean flatten,
IntFunction<E_OUT[]> generator)
Collects all output elements resulting from applying the pipeline stages
to the source
Spliterator into a Node . |
Node<E_OUT> |
evaluateToArrayNode(IntFunction<E_OUT[]> generator)
Collect the elements output from the pipeline stage.
|
abstract <P_IN> Node<E_OUT> |
evaluateToNode(PipelineHelper<E_OUT> helper,
Spliterator<P_IN> spliterator,
boolean flattenTree,
IntFunction<E_OUT[]> generator)
Collect elements output from a pipeline into a Node that holds elements
of this shape.
|
abstract void |
forEachWithCancel(Spliterator<E_OUT> spliterator,
Sink<E_OUT> sink)
Traverse the elements of a spliterator compatible with this stream shape,
pushing those elements into a sink.
|
abstract StreamShape |
getOutputShape()
Get the output shape of the pipeline.
|
int |
getStreamAndOpFlags()
Gets the combined stream and operation flags for the output of the described
pipeline.
|
int |
getStreamFlags()
Returns the composition of stream flags of the stream source and all
intermediate operations.
|
boolean |
isParallel()
Returns whether this stream, if a terminal operation were to be executed,
would execute in parallel.
|
abstract Spliterator<E_OUT> |
lazySpliterator(Supplier<? extends Spliterator<E_OUT>> supplier)
Create a lazy spliterator that wraps and obtains the supplied the
spliterator when a method is invoked on the lazy spliterator.
|
abstract Node.Builder<E_OUT> |
makeNodeBuilder(long exactSizeIfKnown,
IntFunction<E_OUT[]> generator)
Make a node builder compatible with this stream shape.
|
S |
onClose(Runnable closeHandler)
Returns an equivalent stream with an additional close handler.
|
<P_IN> Node<E_OUT> |
opEvaluateParallel(PipelineHelper<E_OUT> helper,
Spliterator<P_IN> spliterator,
IntFunction<E_OUT[]> generator)
Performs a parallel evaluation of the operation using the specified
PipelineHelper which describes the upstream intermediate
operations. |
<P_IN> Spliterator<E_OUT> |
opEvaluateParallelLazy(PipelineHelper<E_OUT> helper,
Spliterator<P_IN> spliterator)
Returns a
Spliterator describing a parallel evaluation of the
operation, using the specified PipelineHelper which describes the
upstream intermediate operations. |
abstract boolean |
opIsStateful()
Returns whether this operation is stateful or not.
|
abstract Sink<E_IN> |
opWrapSink(int flags,
Sink<E_OUT> sink)
Accepts a
Sink which will receive the results of this operation,
and return a Sink which accepts elements of the input type of
this operation and which performs the operation, passing the results to
the provided Sink . |
S |
parallel()
Returns an equivalent stream that is parallel.
|
S |
sequential()
Returns an equivalent stream that is sequential.
|
Spliterator<E_OUT> |
spliterator()
Returns a spliterator for the elements of this stream.
|
abstract <P_IN> Spliterator<E_OUT> |
wrap(PipelineHelper<E_OUT> ph,
Supplier<Spliterator<P_IN>> supplier,
boolean isParallel)
Create a spliterator that wraps a source spliterator, compatible with
this stream shape, and operations associated with a
PipelineHelper . |
<P_IN> Sink<P_IN> |
wrapSink(Sink<E_OUT> sink)
Takes a
Sink that accepts elements of the output type of the
PipelineHelper , and wrap it with a Sink that accepts
elements of the input type and implements all the intermediate operations
described by this PipelineHelper , delivering the result into the
provided Sink . |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
iterator, unordered
protected final int sourceOrOpFlags
public final Node<E_OUT> evaluateToArrayNode(IntFunction<E_OUT[]> generator)
generator
- the array generator to be used to create array instancespublic final S sequential()
BaseStream
This is an intermediate operation.
sequential
in interface BaseStream<E_OUT,S extends BaseStream<E_OUT,S>>
public final S parallel()
BaseStream
This is an intermediate operation.
parallel
in interface BaseStream<E_OUT,S extends BaseStream<E_OUT,S>>
public void close()
BaseStream
close
in interface AutoCloseable
close
in interface BaseStream<E_OUT,S extends BaseStream<E_OUT,S>>
AutoCloseable.close()
public S onClose(Runnable closeHandler)
BaseStream
BaseStream.close()
method
is called on the stream, and are executed in the order they were
added. All close handlers are run, even if earlier close handlers throw
exceptions. If any close handler throws an exception, the first
exception thrown will be relayed to the caller of close()
, with
any remaining exceptions added to that exception as suppressed exceptions
(unless one of the remaining exceptions is the same exception as the
first exception, since an exception cannot suppress itself.) May
return itself.
This is an intermediate operation.
onClose
in interface BaseStream<E_OUT,S extends BaseStream<E_OUT,S>>
closeHandler
- A task to execute when the stream is closedpublic Spliterator<E_OUT> spliterator()
BaseStream
This is a terminal operation.
spliterator
in interface BaseStream<E_OUT,S extends BaseStream<E_OUT,S>>
public final boolean isParallel()
BaseStream
isParallel
in interface BaseStream<E_OUT,S extends BaseStream<E_OUT,S>>
true
if this stream would execute in parallel if executedpublic final int getStreamFlags()
StreamOpFlag
public final int getStreamAndOpFlags()
PipelineHelper
getStreamAndOpFlags
in class PipelineHelper<E_OUT>
StreamOpFlag
public final <P_IN> Sink<P_IN> wrapSink(Sink<E_OUT> sink)
PipelineHelper
Sink
that accepts elements of the output type of the
PipelineHelper
, and wrap it with a Sink
that accepts
elements of the input type and implements all the intermediate operations
described by this PipelineHelper
, delivering the result into the
provided Sink
.wrapSink
in class PipelineHelper<E_OUT>
sink
- the Sink
to receive the resultsSink
that implements the pipeline stages and sends
results to the provided Sink
public final <P_IN> Node<E_OUT> evaluate(Spliterator<P_IN> spliterator, boolean flatten, IntFunction<E_OUT[]> generator)
PipelineHelper
Spliterator
into a Node
.evaluate
in class PipelineHelper<E_OUT>
spliterator
- the source Spliterator
flatten
- if true and the pipeline is a parallel pipeline then the
Node
returned will contain no children, otherwise the
Node
may represent the root in a tree that reflects the
shape of the computation tree.generator
- a factory function for array instancesNode
containing all output elementspublic abstract StreamShape getOutputShape()
public abstract <P_IN> Node<E_OUT> evaluateToNode(PipelineHelper<E_OUT> helper, Spliterator<P_IN> spliterator, boolean flattenTree, IntFunction<E_OUT[]> generator)
helper
- the pipeline helper describing the pipeline stagesspliterator
- the source spliteratorflattenTree
- true if the returned node should be flattenedgenerator
- the array generatorpublic abstract <P_IN> Spliterator<E_OUT> wrap(PipelineHelper<E_OUT> ph, Supplier<Spliterator<P_IN>> supplier, boolean isParallel)
PipelineHelper
.ph
- the pipeline helper describing the pipeline stagessupplier
- the supplier of a spliteratorpublic abstract Spliterator<E_OUT> lazySpliterator(Supplier<? extends Spliterator<E_OUT>> supplier)
supplier
- the supplier of a spliteratorpublic abstract void forEachWithCancel(Spliterator<E_OUT> spliterator, Sink<E_OUT> sink)
spliterator
- the spliterator to pull elements fromsink
- the sink to push elements topublic abstract Node.Builder<E_OUT> makeNodeBuilder(long exactSizeIfKnown, IntFunction<E_OUT[]> generator)
exactSizeIfKnown
- if >=0, then a node builder will be
created that has a fixed capacity of at most sizeIfKnown elements. If
< 0, then the node builder has an unfixed capacity. A fixed
capacity node builder will throw exceptions if an element is added after
builder has reached capacity, or is built before the builder has reached
capacity.generator
- the array generator to be used to create instances of a
T[] array. For implementations supporting primitive nodes, this parameter
may be ignored.public abstract boolean opIsStateful()
opEvaluateParallel(PipelineHelper, java.util.Spliterator, java.util.function.IntFunction)
must be overridden.true
if this operation is statefulpublic abstract Sink<E_IN> opWrapSink(int flags, Sink<E_OUT> sink)
Sink
which will receive the results of this operation,
and return a Sink
which accepts elements of the input type of
this operation and which performs the operation, passing the results to
the provided Sink
.flags
- The combined stream and operation flags up to, but not
including, this operationsink
- sink to which elements should be sent after processingSink
.public <P_IN> Node<E_OUT> opEvaluateParallel(PipelineHelper<E_OUT> helper, Spliterator<P_IN> spliterator, IntFunction<E_OUT[]> generator)
PipelineHelper
which describes the upstream intermediate
operations. Only called on stateful operations. If opIsStateful()
returns true then implementations must override the
default implementation.helper
- the pipeline helper describing the pipeline stagesspliterator
- the source Spliterator
generator
- the array generatorNode
describing the result of the evaluationpublic <P_IN> Spliterator<E_OUT> opEvaluateParallelLazy(PipelineHelper<E_OUT> helper, Spliterator<P_IN> spliterator)
Spliterator
describing a parallel evaluation of the
operation, using the specified PipelineHelper
which describes the
upstream intermediate operations. Only called on stateful operations.
It is not necessary (though acceptable) to do a full computation of the
result here; it is preferable, if possible, to describe the result via a
lazily evaluated spliterator.helper
- the pipeline helperspliterator
- the source Spliterator
Spliterator
describing the result of the evaluation