Class ParallelLongStreamSupport
- java.lang.Object
-
- com.github.ferstl.streams.ParallelLongStreamSupport
-
- All Implemented Interfaces:
AutoCloseable
,BaseStream<Long,LongStream>
,LongStream
public class ParallelLongStreamSupport extends Object implements LongStream
An implementation of
LongStream
which uses a customForkJoinPool
for parallel aggregate operations. This is thelong
primitive specialization ofParallelStreamSupport
.The following example illustrates an aggregate operation using
ParallelStreamSupport
andParallelLongStreamSupport
with a customForkJoinPool
, computing the sum of the weights of the red widgets:ForkJoinPool pool = new ForkJoinPool(); long sum = ParallelStreamSupport.parallelStream(widgets, pool) .filter(w -> w.getColor() == RED) .mapToLong(w -> w.getWeight()) .sum();
In case this stream is configured for parallel execution, i.e.
This implementation offers various factory methods which are based on:BaseStream.isParallel()
returnstrue
, a terminal operation will be executed asForkJoinTask
in the customForkJoinPool
. Otherwise it will be executed in the calling thread.- The static factory methods of
LongStream
, which are meaningful for parallel streams Arrays.stream(long[])
StreamSupport.longStream(Spliterator.OfLong, boolean)
StreamSupport.longStream(Supplier, int, boolean)
- API Note:
Internally, this stream wraps a
long
stream which is initially created in one of the static factory methods. Whenever a non-terminal operation is called the underlying stream will be replaced with the result of calling the same method on that stream. The return value of these operations is always this stream or, in case of operations that return a different type of stream, one ofParallelStreamSupport
,ParallelIntStreamSupport
orParallelDoubleStreamSupport
.Although each factory method returns a parallel stream, calling
BaseStream.sequential()
is still possible and leads to sequential execution of a terminal operation within the calling thread.- Implementation Note:
See the class documentation for
Stream
and the package documentation for java.util.stream for additional specification.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface java.util.stream.LongStream
LongStream.Builder
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
allMatch(LongPredicate predicate)
boolean
anyMatch(LongPredicate predicate)
DoubleStream
asDoubleStream()
OptionalDouble
average()
Stream<Long>
boxed()
void
close()
<R> R
collect(Supplier<R> supplier, ObjLongConsumer<R> accumulator, BiConsumer<R,R> combiner)
static LongStream
concat(LongStream a, LongStream b, ForkJoinPool workerPool)
Creates a lazily concatenated parallellong
stream whose elements are all the elements of the first stream followed by all the elements of the second stream.long
count()
LongStream
distinct()
LongStream
dropWhile(LongPredicate predicate)
protected void
execute(Runnable terminalOperation)
protected <R> R
execute(Callable<R> terminalOperation)
LongStream
filter(LongPredicate predicate)
OptionalLong
findAny()
OptionalLong
findFirst()
LongStream
flatMap(LongFunction<? extends LongStream> mapper)
void
forEach(LongConsumer action)
void
forEachOrdered(LongConsumer action)
static LongStream
generate(LongSupplier supplier, ForkJoinPool workerPool)
Creates a parallel infinite sequential unorderedlong
stream where each element is generated by the providedLongSupplier
.boolean
isParallel()
static LongStream
iterate(long seed, LongUnaryOperator operator, ForkJoinPool workerPool)
Creates a parallel infinite orderedlong
stream produced by iterative application of a functionf
to an initial elementseed
.PrimitiveIterator.OfLong
iterator()
LongStream
limit(long maxSize)
LongStream
map(LongUnaryOperator mapper)
DoubleStream
mapToDouble(LongToDoubleFunction mapper)
IntStream
mapToInt(LongToIntFunction mapper)
<U> Stream<U>
mapToObj(LongFunction<? extends U> mapper)
OptionalLong
max()
OptionalLong
min()
boolean
noneMatch(LongPredicate predicate)
S
onClose(Runnable closeHandler)
S
parallel()
static LongStream
parallelStream(long[] array, ForkJoinPool workerPool)
Creates a parallellong
stream from the given Array.static LongStream
parallelStream(Supplier<? extends Spliterator.OfLong> supplier, int characteristics, ForkJoinPool workerPool)
Creates a parallellong
stream from the given Spliterator supplier.static LongStream
parallelStream(Spliterator.OfLong spliterator, ForkJoinPool workerPool)
Creates a parallellong
stream from the given Spliterator.static LongStream
parallelStream(LongStream.Builder builder, ForkJoinPool workerPool)
Creates a parallellong
stream from the givenLongStream.Builder
.LongStream
peek(LongConsumer action)
static LongStream
range(long startInclusive, long endExclusive, ForkJoinPool workerPool)
Creates a parallel orderedlong
stream fromstartInclusive
(inclusive) toendExclusive
(exclusive) by an incremental step of1
.static LongStream
rangeClosed(long startInclusive, long endInclusive, ForkJoinPool workerPool)
Creates a parallel orderedlong
stream fromstartInclusive
(inclusive) toendInclusive
(inclusive) by an incremental step of1
.long
reduce(long identity, LongBinaryOperator op)
OptionalLong
reduce(LongBinaryOperator op)
S
sequential()
LongStream
skip(long n)
LongStream
sorted()
Spliterator.OfLong
spliterator()
long
sum()
LongSummaryStatistics
summaryStatistics()
LongStream
takeWhile(LongPredicate predicate)
long[]
toArray()
S
unordered()
-
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, onClose, unordered
-
Methods inherited from interface java.util.stream.LongStream
parallel, sequential
-
-
-
-
Method Detail
-
parallelStream
public static LongStream parallelStream(long[] array, ForkJoinPool workerPool)
Creates a parallellong
stream from the given Array. This operation is similar to callingArrays.stream(array).parallel()
with the difference that a parallel terminal operation will be executed in the givenForkJoinPool
.- Parameters:
array
- Array to create the parallel stream from. Must not benull
.workerPool
- Thread pool for parallel execution of a terminal operation. Must not benull
.- Returns:
- A parallel
long
stream that executes a terminal operation in the givenForkJoinPool
. - See Also:
Arrays.stream(long[])
-
parallelStream
public static LongStream parallelStream(Spliterator.OfLong spliterator, ForkJoinPool workerPool)
Creates a parallellong
stream from the given Spliterator. This operation is similar to callingStreamSupport.longStream(spliterator, true)
with the difference that a parallel terminal operation will be executed in the givenForkJoinPool
.- Parameters:
spliterator
- ASpliterator.OfLong
describing the stream elements. Must not benull
.workerPool
- Thread pool for parallel execution of a terminal operation. Must not benull
.- Returns:
- A parallel
long
stream that executes a terminal operation in the givenForkJoinPool
. - See Also:
StreamSupport.longStream(Spliterator.OfLong, boolean)
-
parallelStream
public static LongStream parallelStream(Supplier<? extends Spliterator.OfLong> supplier, int characteristics, ForkJoinPool workerPool)
Creates a parallellong
stream from the given Spliterator supplier. This operation is similar to callingStreamSupport.longStream(supplier, characteristics, true)
with the difference that a parallel terminal operation will be executed in the givenForkJoinPool
.- Parameters:
supplier
- ASupplier
of aSpliterator.OfLong
. Must not benull
.characteristics
- Spliterator characteristics of the suppliedSpliterator
. The characteristics must be equal tosupplier.get().characteristics()
, otherwise undefined behavior may occur when terminal operation commences.workerPool
- Thread pool for parallel execution of a terminal operation. Must not benull
.- Returns:
- A parallel
long
stream that executes a terminal operation in the givenForkJoinPool
. - See Also:
StreamSupport.longStream(Supplier, int, boolean)
-
parallelStream
public static LongStream parallelStream(LongStream.Builder builder, ForkJoinPool workerPool)
Creates a parallellong
stream from the givenLongStream.Builder
. This operation is similar to callingbuilder.build().parallel()
with the difference that a parallel terminal operation will be executed in the givenForkJoinPool
.- Parameters:
builder
- The builder to create the stream from. Must not benull
.workerPool
- Thread pool for parallel execution of a terminal operation. Must not benull
.- Returns:
- A parallel
long
stream that executes a terminal operation in the givenForkJoinPool
. builder()
-
iterate
public static LongStream iterate(long seed, LongUnaryOperator operator, ForkJoinPool workerPool)
Creates a parallel infinite orderedlong
stream produced by iterative application of a functionf
to an initial elementseed
. This operation is similar to callingLongStream.iterate(seed, operator).parallel()
with the difference that a parallel terminal operation will be executed in the givenForkJoinPool
.- Parameters:
seed
- The initial element.operator
- A function to be applied to to the previous element to produce a new element. Must not benull
.workerPool
- Thread pool for parallel execution of a terminal operation. Must not benull
.- Returns:
- A parallel
long
stream that executes a terminal operation in the givenForkJoinPool
. - See Also:
LongStream.iterate(long, LongUnaryOperator)
-
generate
public static LongStream generate(LongSupplier supplier, ForkJoinPool workerPool)
Creates a parallel infinite sequential unorderedlong
stream where each element is generated by the providedLongSupplier
. This operation is similar to callingLongStream.generate(supplier).parallel()
with the difference that a parallel terminal operation will be executed in the givenForkJoinPool
.- Parameters:
supplier
- TheLongSupplier
of generated elements. Must not benull
.workerPool
- Thread pool for parallel execution of a terminal operation. Must not benull
.- Returns:
- A parallel
long
stream that executes a terminal operation in the givenForkJoinPool
. - See Also:
LongStream.generate(LongSupplier)
-
range
public static LongStream range(long startInclusive, long endExclusive, ForkJoinPool workerPool)
Creates a parallel orderedlong
stream fromstartInclusive
(inclusive) toendExclusive
(exclusive) by an incremental step of1
. This operation is similar to callingLongStream.range(startInclusive, endExclusive).parallel()
with the difference that a parallel terminal operation will be executed in the givenForkJoinPool
.- Parameters:
startInclusive
- the (inclusive) initial valueendExclusive
- the exclusive upper boundworkerPool
- Thread pool for parallel execution of a terminal operation. Must not benull
.- Returns:
- A parallel
long
stream that executes a terminal operation in the givenForkJoinPool
for the range oflong
elements. - See Also:
LongStream.range(long, long)
-
rangeClosed
public static LongStream rangeClosed(long startInclusive, long endInclusive, ForkJoinPool workerPool)
Creates a parallel orderedlong
stream fromstartInclusive
(inclusive) toendInclusive
(inclusive) by an incremental step of1
. This operation is similar to callingLongStream.rangeClosed(startInclusive, endInclusive).parallel()
with the difference that a parallel terminal operation will be executed in the givenForkJoinPool
.- Parameters:
startInclusive
- the (inclusive) initial valueendInclusive
- the inclusive upper boundworkerPool
- Thread pool for parallel execution of a terminal operation. Must not benull
.- Returns:
- A parallel
long
stream that executes a terminal operation in the givenForkJoinPool
for the range oflong
elements. - See Also:
LongStream.rangeClosed(long, long)
-
concat
public static LongStream concat(LongStream a, LongStream b, ForkJoinPool workerPool)
Creates a lazily concatenated parallellong
stream whose elements are all the elements of the first stream followed by all the elements of the second stream. This operation is similar to callingLongStream.concat(a, b).parallel()
with the difference that a parallel terminal operation will be executed in the givenForkJoinPool
.- Parameters:
a
- The first stream. Must not benull
.b
- The second stream. Must not benull
.workerPool
- Thread pool for parallel execution of a terminal operation. Must not benull
.- Returns:
- A parallel stream that executes a terminal operation in the given
ForkJoinPool
. - See Also:
LongStream.concat(LongStream, LongStream)
-
filter
public LongStream filter(LongPredicate predicate)
- Specified by:
filter
in interfaceLongStream
-
map
public LongStream map(LongUnaryOperator mapper)
- Specified by:
map
in interfaceLongStream
-
mapToObj
public <U> Stream<U> mapToObj(LongFunction<? extends U> mapper)
- Specified by:
mapToObj
in interfaceLongStream
-
mapToInt
public IntStream mapToInt(LongToIntFunction mapper)
- Specified by:
mapToInt
in interfaceLongStream
-
mapToDouble
public DoubleStream mapToDouble(LongToDoubleFunction mapper)
- Specified by:
mapToDouble
in interfaceLongStream
-
flatMap
public LongStream flatMap(LongFunction<? extends LongStream> mapper)
- Specified by:
flatMap
in interfaceLongStream
-
distinct
public LongStream distinct()
- Specified by:
distinct
in interfaceLongStream
-
sorted
public LongStream sorted()
- Specified by:
sorted
in interfaceLongStream
-
peek
public LongStream peek(LongConsumer action)
- Specified by:
peek
in interfaceLongStream
-
limit
public LongStream limit(long maxSize)
- Specified by:
limit
in interfaceLongStream
-
skip
public LongStream skip(long n)
- Specified by:
skip
in interfaceLongStream
-
takeWhile
public LongStream takeWhile(LongPredicate predicate)
- Specified by:
takeWhile
in interfaceLongStream
-
dropWhile
public LongStream dropWhile(LongPredicate predicate)
- Specified by:
dropWhile
in interfaceLongStream
-
forEach
public void forEach(LongConsumer action)
- Specified by:
forEach
in interfaceLongStream
-
forEachOrdered
public void forEachOrdered(LongConsumer action)
- Specified by:
forEachOrdered
in interfaceLongStream
-
toArray
public long[] toArray()
- Specified by:
toArray
in interfaceLongStream
-
reduce
public long reduce(long identity, LongBinaryOperator op)
- Specified by:
reduce
in interfaceLongStream
-
reduce
public OptionalLong reduce(LongBinaryOperator op)
- Specified by:
reduce
in interfaceLongStream
-
collect
public <R> R collect(Supplier<R> supplier, ObjLongConsumer<R> accumulator, BiConsumer<R,R> combiner)
- Specified by:
collect
in interfaceLongStream
-
sum
public long sum()
- Specified by:
sum
in interfaceLongStream
-
min
public OptionalLong min()
- Specified by:
min
in interfaceLongStream
-
max
public OptionalLong max()
- Specified by:
max
in interfaceLongStream
-
count
public long count()
- Specified by:
count
in interfaceLongStream
-
average
public OptionalDouble average()
- Specified by:
average
in interfaceLongStream
-
summaryStatistics
public LongSummaryStatistics summaryStatistics()
- Specified by:
summaryStatistics
in interfaceLongStream
-
anyMatch
public boolean anyMatch(LongPredicate predicate)
- Specified by:
anyMatch
in interfaceLongStream
-
allMatch
public boolean allMatch(LongPredicate predicate)
- Specified by:
allMatch
in interfaceLongStream
-
noneMatch
public boolean noneMatch(LongPredicate predicate)
- Specified by:
noneMatch
in interfaceLongStream
-
findFirst
public OptionalLong findFirst()
- Specified by:
findFirst
in interfaceLongStream
-
findAny
public OptionalLong findAny()
- Specified by:
findAny
in interfaceLongStream
-
asDoubleStream
public DoubleStream asDoubleStream()
- Specified by:
asDoubleStream
in interfaceLongStream
-
boxed
public Stream<Long> boxed()
- Specified by:
boxed
in interfaceLongStream
-
iterator
public PrimitiveIterator.OfLong iterator()
- Specified by:
iterator
in interfaceBaseStream<Long,LongStream>
- Specified by:
iterator
in interfaceLongStream
-
spliterator
public Spliterator.OfLong spliterator()
- Specified by:
spliterator
in interfaceBaseStream<Long,LongStream>
- Specified by:
spliterator
in interfaceLongStream
-
isParallel
public boolean isParallel()
- Specified by:
isParallel
in interfaceBaseStream<T,S extends BaseStream<T,S>>
-
sequential
public S sequential()
- Specified by:
sequential
in interfaceBaseStream<T,S extends BaseStream<T,S>>
-
parallel
public S parallel()
- Specified by:
parallel
in interfaceBaseStream<T,S extends BaseStream<T,S>>
-
unordered
public S unordered()
- Specified by:
unordered
in interfaceBaseStream<T,S extends BaseStream<T,S>>
-
onClose
public S onClose(Runnable closeHandler)
- Specified by:
onClose
in interfaceBaseStream<T,S extends BaseStream<T,S>>
-
close
public void close()
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceBaseStream<T,S extends BaseStream<T,S>>
-
execute
protected void execute(Runnable terminalOperation)
-
execute
protected <R> R execute(Callable<R> terminalOperation)
-
-