/** * Adapts this collector to perform an additional finishing transformation. * * @param <RR> result type of the resulting collector * @param finisher a function to be applied to the final result of this * collector * @return a collector which performs the action of this collector, followed * by an additional finishing step * @since 0.3.7 */ default <RR> DoubleCollector<A, RR> andThen(Function<R, RR> finisher) { return of(supplier(), doubleAccumulator(), merger(), finisher().andThen(finisher)); }
/** * Performs a mutable reduction operation on the elements of this stream * using an {@link DoubleCollector} which encapsulates the supplier, * accumulator and merger functions making easier to reuse collection * strategies. * * <p> * Like {@link #reduce(double, DoubleBinaryOperator)}, {@code collect} * operations can be parallelized without requiring additional * synchronization. * * <p> * This is a terminal operation. * * @param <A> the intermediate accumulation type of the * {@code DoubleCollector} * @param <R> type of the result * @param collector the {@code DoubleCollector} describing the reduction * @return the result of the reduction * @see #collect(Supplier, ObjDoubleConsumer, BiConsumer) * @since 0.3.0 */ @SuppressWarnings("unchecked") public <A, R> R collect(DoubleCollector<A, R> collector) { if (collector.characteristics().contains(Collector.Characteristics.IDENTITY_FINISH)) return (R) collect(collector.supplier(), collector.doubleAccumulator(), collector.merger()); return collector.finisher().apply(collect(collector.supplier(), collector.doubleAccumulator(), collector .merger())); }
/** * Adapts a {@code DoubleCollector} to another one by applying a mapping * function to each input element before accumulation. * * @param <A> intermediate accumulation type of the downstream collector * @param <R> result type of collector * @param mapper a function to be applied to the input elements * @param downstream a collector which will accept mapped values * @return a collector which applies the mapping function to the input * elements and provides the mapped results to the downstream * collector */ static <A, R> DoubleCollector<?, R> mapping(DoubleUnaryOperator mapper, DoubleCollector<A, R> downstream) { ObjDoubleConsumer<A> downstreamAccumulator = downstream.doubleAccumulator(); return new DoubleCollectorImpl<>(downstream.supplier(), (r, t) -> downstreamAccumulator.accept(r, mapper .applyAsDouble(t)), downstream.merger(), downstream.finisher(), downstream.characteristics()); }