/** * A function that folds a value into a mutable result container. * * The default implementation calls {@link #doubleAccumulator()} on unboxed * value. * * @return a function which folds a value into a mutable result container */ @Override default BiConsumer<A, Double> accumulator() { return doubleAccumulator()::accept; }
/** * 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())); }
Supplier<A> downstreamSupplier = downstream.supplier(); Function<K, A> supplier = k -> downstreamSupplier.get(); ObjDoubleConsumer<A> downstreamAccumulator = downstream.doubleAccumulator(); ObjDoubleConsumer<Map<K, A>> accumulator = (m, t) -> { K key = Objects.requireNonNull(classifier.apply(t));
/** * Returns a {@code DoubleCollector} which partitions the input numbers * according to a {@code DoublePredicate}, reduces the values in each * partition according to another {@code IntCollector}, and organizes them * into a {@code Map<Boolean, D>} whose values are the result of the * downstream reduction. * * <p> * There are no guarantees on the type, mutability, serializability, or * thread-safety of the {@code Map} returned. * * @param <A> the intermediate accumulation type of the downstream collector * @param <D> the result type of the downstream reduction * @param predicate a predicate used for classifying input elements * @param downstream a {@code DoubleCollector} implementing the downstream * reduction * @return a {@code DoubleCollector} implementing the cascaded partitioning * operation */ static <A, D> DoubleCollector<?, Map<Boolean, D>> partitioningBy(DoublePredicate predicate, DoubleCollector<A, D> downstream) { ObjDoubleConsumer<A> downstreamAccumulator = downstream.doubleAccumulator(); ObjDoubleConsumer<BooleanMap<A>> accumulator = (result, t) -> downstreamAccumulator.accept( predicate.test(t) ? result.trueValue : result.falseValue, t); return BooleanMap.partialCollector(downstream).asDouble(accumulator); }
/** * 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()); }