src/share/classes/java/util/stream/ReduceOps.java
Print this page
rev 7597 : 8015318: Extend Collector with 'finish' operation
Reviewed-by:
Contributed-by: brian.goetz@oracle.com
*** 146,184 ****
/**
* Constructs a {@code TerminalOp} that implements a mutable reduce on
* reference values.
*
* @param <T> the type of the input elements
! * @param <R> the type of the result
* @param collector a {@code Collector} defining the reduction
* @return a {@code ReduceOp} implementing the reduction
*/
! public static <T,R> TerminalOp<T, R>
! makeRef(Collector<? super T,R> collector) {
! Supplier<R> supplier = Objects.requireNonNull(collector).resultSupplier();
! BiFunction<R, ? super T, R> accumulator = collector.accumulator();
! BinaryOperator<R> combiner = collector.combiner();
! class ReducingSink extends Box<R>
! implements AccumulatingSink<T, R, ReducingSink> {
@Override
public void begin(long size) {
state = supplier.get();
}
@Override
public void accept(T t) {
! R newResult = accumulator.apply(state, t);
! if (state != newResult)
! state = newResult;
}
@Override
public void combine(ReducingSink other) {
state = combiner.apply(state, other.state);
}
}
! return new ReduceOp<T, R, ReducingSink>(StreamShape.REFERENCE) {
@Override
public ReducingSink makeSink() {
return new ReducingSink();
}
--- 146,183 ----
/**
* Constructs a {@code TerminalOp} that implements a mutable reduce on
* reference values.
*
* @param <T> the type of the input elements
! * @param <I> the type of the intermediate reduction result
! * @param <I> the type of the final reduction result
* @param collector a {@code Collector} defining the reduction
* @return a {@code ReduceOp} implementing the reduction
*/
! public static <T, I, R> TerminalOp<T, I>
! makeRef(Collector<? super T, I, R> collector) {
! Supplier<I> supplier = Objects.requireNonNull(collector).supplier();
! BiConsumer<I, ? super T> accumulator = collector.accumulator();
! BinaryOperator<I> combiner = collector.combiner();
! class ReducingSink extends Box<I>
! implements AccumulatingSink<T, I, ReducingSink> {
@Override
public void begin(long size) {
state = supplier.get();
}
@Override
public void accept(T t) {
! accumulator.accept(state, t);
}
@Override
public void combine(ReducingSink other) {
state = combiner.apply(state, other.state);
}
}
! return new ReduceOp<T, I, ReducingSink>(StreamShape.REFERENCE) {
@Override
public ReducingSink makeSink() {
return new ReducingSink();
}