Module io.jenetics.base
Package io.jenetics.util
Interface Accumulator<T,A extends Accumulator<T,A,R>,R>
- Type Parameters:
T
- the type of input elements to the accumulate operationA
- the accumulator typeR
- the result type of the accumulated operation
public interface Accumulator<T,A extends Accumulator<T,A,R>,R>
extends Consumer<T>, Collector<T,A,R>
This interface lets you accumulate elements of type
The code above gives you the following output.
T
to a result of
type R
. In contrast to a Collector
an Accumulator
can deliver intermediate results while accumulating. An accumulator can be
created from any Collector
with the (of(Collector)
) method.
final Accumulator<Integer, ?, List<Integer>> accu =
Accumulator.of(Collectors.toList());
final ISeq<List<Integer>> result = IntStream.range(0, 10).boxed()
.peek(accu)
.map(i -> accu.result())
.collect(ISeq.toISeq());
result.forEach(System.out::println);
[0] [0,1] [0,1,2] [0,1,2,3] [0,1,2,3,4] [0,1,2,3,4,5] [0,1,2,3,4,5,6] [0,1,2,3,4,5,6,7] [0,1,2,3,4,5,6,7,8] [0,1,2,3,4,5,6,7,8,9]
- Since:
- 6.1
- Version:
- 6.1
- API Note:
- In contrast to the
Collector
interface, theAccumulator
is not mergeable. The accumulator is not thread-safe and can't be used for parallel streams when used asConsumer
in theStream.peek(Consumer)
orStream.forEach(Consumer)
method. Obtaining a synchronized view of the accumulator with thesynced()
method, will solve this problem. If the accumulator is used asCollector
, the usage in parallel streams is safe.
-
Nested Class Summary
Nested classes/interfaces inherited from interface java.util.stream.Collector
Collector.Characteristics
-
Method Summary
Modifier and TypeMethodDescriptiondefault BiConsumer
<A, T> default Set
<Collector.Characteristics> default A
Combinesthis
accumulator with theother
one.default BinaryOperator
<A> combiner()
finisher()
static <T,
R> Accumulator <T, ?, R> Create a new accumulator from the givencollector
.result()
Return a copy of the current result of the accumulated elements.default Accumulator
<T, ?, R> synced()
Returns a synchronized (thread-safe) accumulator backed bythis
accumulator.default Accumulator
<T, ?, R> Returns a synchronized (thread-safe) accumulator backed bythis
accumulator.
-
Method Details
-
result
Return a copy of the current result of the accumulated elements. The accumulated elements are not changed by this method.- Returns:
- the current result of the accumulated elements
-
combine
Combinesthis
accumulator with theother
one.- Parameters:
other
- the other accumulator- Returns:
- the combined accumulator
- Throws:
UnsupportedOperationException
- unless it's overridden by the implementation
-
accumulator
- Specified by:
accumulator
in interfaceCollector<T,
A extends Accumulator<T, A, R>, R>
-
combiner
-
finisher
-
characteristics
- Specified by:
characteristics
in interfaceCollector<T,
A extends Accumulator<T, A, R>, R>
-
synced
Returns a synchronized (thread-safe) accumulator backed bythis
accumulator. The givenlock
is used as a synchronization object.- Parameters:
lock
- the lock used for synchronization- Returns:
- a synchronized (thread-safe) accumulator backed by
this
accumulator - Throws:
NullPointerException
- if the givenlock
isnull
-
synced
Returns a synchronized (thread-safe) accumulator backed bythis
accumulator.this
accumulator is used as a synchronization object.- Returns:
- a synchronized (thread-safe) accumulator backed by
this
accumulator
-
of
Create a new accumulator from the givencollector
.final Accumulator<Integer, ?, ISeq<Integer>> accu = Accumulator.of(ISeq.toISeq());
- Type Parameters:
T
- the type of input elements to the reduction operationR
- the result type of the reduction operation- Parameters:
collector
- the collector which is used for accumulation and creation the result value.- Returns:
- a new accumulator which is backed by the given
collector
- Throws:
NullPointerException
- if the givencollector
isnull
-