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 typeT
to a result of typeR
. In contrast to aCollector
anAccumulator
can deliver intermediate results while accumulating. An accumulator can be created from anyCollector
with the (of(Collector)
) method.The code above gives you the following output.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
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default BiConsumer<A,T>
accumulator()
default Set<Collector.Characteristics>
characteristics()
default A
combine(A other)
Combinesthis
accumulator with theother
one.default BinaryOperator<A>
combiner()
default Function<A,R>
finisher()
static <T,R>
Accumulator<T,?,R>of(Collector<T,?,R> collector)
Create a new accumulator from the givencollector
.R
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>
synced(Object lock)
Returns a synchronized (thread-safe) accumulator backed bythis
accumulator.
-
-
-
Method Detail
-
result
R 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
default A combine(A other)
Combinesthis
accumulator with theother
one.- Parameters:
other
- the other accumulator- Returns:
- the combined accumulator
- Throws:
UnsupportedOperationException
- unless it's overridden by the implementation
-
accumulator
default BiConsumer<A,T> accumulator()
- Specified by:
accumulator
in interfaceCollector<T,A extends Accumulator<T,A,R>,R>
-
combiner
default BinaryOperator<A> combiner()
-
characteristics
default Set<Collector.Characteristics> characteristics()
- Specified by:
characteristics
in interfaceCollector<T,A extends Accumulator<T,A,R>,R>
-
synced
default Accumulator<T,?,R> synced(Object lock)
Returns a synchronized (thread-safe) accumulator backed bythis
accumulator. The givenlock
is used as 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
default Accumulator<T,?,R> synced()
Returns a synchronized (thread-safe) accumulator backed bythis
accumulator.this
accumulator is used as synchronization object.- Returns:
- a synchronized (thread-safe) accumulator backed by
this
accumulator
-
of
static <T,R> Accumulator<T,?,R> of(Collector<T,?,R> collector)
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
-
-