All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.agzamovr.collectors.SummingDoubleCollector Maven / Gradle / Ivy

The newest version!
package org.agzamovr.collectors;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.function.ToDoubleFunction;
import java.util.stream.Collector;

import static java.util.Comparator.nullsLast;
import static java.util.stream.Collectors.toList;

class SummingDoubleCollector {
    static final SummingDoubleCollector SUMMING_DOUBLE_COLLECTOR = new SummingDoubleCollector();

    private void validateInput(ToDoubleFunction mapper,
                               Comparator comparator,
                               Collector downstream) {
        Objects.requireNonNull(mapper, "Mapper cannot be null");
        Objects.requireNonNull(comparator, "Comparator cannot be null");
        Objects.requireNonNull(downstream, "Downstream collector cannot be null");
    }

     R summingDoubleFinisher(ToDoubleFunction mapper,
                                      Comparator comparator,
                                      List list,
                                      Collector downstream) {
        validateInput(mapper, comparator, downstream);
        list.sort(comparator);
        double sum = 0.0;
        double compensation = 0.0;
        Supplier downstreamSupplier = downstream.supplier();
        Function finisher = downstream.finisher();
        BiConsumer downstreamAccumulator = downstream.accumulator();
        A container = downstreamSupplier.get();
        for (T item : list) {
            double val = mapper.applyAsDouble(item);
            double y = val - compensation;
            double t = sum + y;
            compensation = (t - sum) - y;
            sum = t;
            downstreamAccumulator.accept(container, sum);
        }
        return finisher.apply(container);
    }

    >
    Collector> summingDouble(ToDoubleFunction mapper) {
        return summingDouble(mapper, nullsLast(Comparator.naturalOrder()));
    }

    , R>
    Collector summingDouble(ToDoubleFunction mapper,
                                     Collector downstream) {
        return summingDouble(mapper, nullsLast(Comparator.naturalOrder()), downstream);
    }

    
    Collector> summingDouble(ToDoubleFunction mapper,
                                                Comparator comparator) {
        return summingDouble(mapper, comparator, toList());
    }

    
    Collector, R> summingDouble(ToDoubleFunction mapper,
                                           Comparator comparator,
                                           Collector downstream) {
        return Collector.of(ArrayList::new,
                List::add,
                CollectorEx::listCombiner,
                (list) -> summingDoubleFinisher(mapper, comparator, list, downstream));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy