Interface Accumulator<T,A extends Accumulator<T,A,R>,R>

Type Parameters:
T - the type of input elements to the accumulate operation
A - the accumulator type
R - the result type of the accumulated operation
All Superinterfaces:
Collector<T,A,R>, Consumer<T>

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 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);
The code above gives you the following output.
 [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, the Accumulator is not mergeable. The accumulator is not thread-safe and can't be used for parallel streams when used as Consumer in the Stream.peek(Consumer) or Stream.forEach(Consumer) method. Obtaining a synchronized view of the accumulator with the synced() method, will solve this problem. If the accumulator is used as Collector, the usage in parallel streams is safe.
  • 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

      default A combine(A other)
      Combines this accumulator with the other 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 interface Collector<T,A extends Accumulator<T,A,R>,R>
    • combiner

      Specified by:
      combiner in interface Collector<T,A extends Accumulator<T,A,R>,R>
    • finisher

      default Function<A,R> finisher()
      Specified by:
      finisher in interface Collector<T,A extends Accumulator<T,A,R>,R>
    • characteristics

      Specified by:
      characteristics in interface Collector<T,A extends Accumulator<T,A,R>,R>
    • synced

      default Accumulator<T,?,R> synced(Object lock)
      Returns a synchronized (thread-safe) accumulator backed by this accumulator. The given lock 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 given lock is null
    • synced

      default Accumulator<T,?,R> synced()
      Returns a synchronized (thread-safe) accumulator backed by this 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 given collector.
      final Accumulator<Integer, ?, ISeq<Integer>> accu = Accumulator.of(ISeq.toISeq());
      Type Parameters:
      T - the type of input elements to the reduction operation
      R - 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 given collector is null