Пользовательский функциональный интерфейс

У меня есть основное приложение SpringBoot. Используя Spring Initializer, JPA, встроенный Tomcat, шаблонизатор Thymeleaf и пакет в качестве исполняемого файла JAR. Я хочу создать пользовательскую SummaryStatistics на основе POJO, имеющего 2 поля, price и updateDate. Эта статистика должна получить минимальную / максимальную цену и дату этой минимальной / максимальной цены.

public class MenuPriceSummaryStatistics implements Consumer<MenuPrice> {

    private long count;
    private double sum;
    private double sumCompensation; // Low order bits of sum
    private double simpleSum; // Used to compute right sum for non-finite inputs
    private double min = Double.POSITIVE_INFINITY;
    private double max = Double.NEGATIVE_INFINITY;

    private Date maxDate;
    private Date minDate;

    public MenuPriceSummaryStatistics() {
    }

    /**
     * Combines the state of another {@code DoubleSummaryStatistics} into this one.
     *
     * @param other
     *            another {@code DoubleSummaryStatistics}
     * @throws NullPointerException
     *             if {@code other} is null
     */
    public void combine(MenuPriceSummaryStatistics other) {
        count += other.count;
        simpleSum += other.simpleSum;
        sumWithCompensation(other.sum);
        sumWithCompensation(other.sumCompensation);
        min = Math.min(min, other.min);
        max = Math.max(max, other.max);

        if (min==other.min) {
            minDate=other.minDate;
        }
        if (max==other.max) {
            maxDate=other.maxDate;
        }
    }

    @Override
    public void accept(MenuPrice cp) {
        ++count;
        simpleSum += cp.getPrice();
        sumWithCompensation(cp.getPrice());
        min = Math.min(min, cp.getPrice());
        max = Math.max(max, cp.getPrice());

        if (min==cp.getPrice()) {
            minDate=cp.getUpdateDate();
        }

        if (max==cp.getPrice()) {
            maxDate=cp.getUpdateDate();
        }

    }

    private void sumWithCompensation(double value) {
        double tmp = value - sumCompensation;
        double velvel = sum + tmp; // Little wolf of rounding error
        sumCompensation = (velvel - sum) - tmp;
        sum = velvel;
    }

    public final long getCount() {
        return count;
    }

    public final double getSum() {
        // Better error bounds to add both terms as the final sum
        double tmp = sum + sumCompensation;
        if (Double.isNaN(tmp) && Double.isInfinite(simpleSum))
            // If the compensated sum is spuriously NaN from
            // accumulating one or more same-signed infinite values,
            // return the correctly-signed infinity stored in
            // simpleSum.
            return simpleSum;
        else
            return tmp;
    }

    public final double getMin() {
        return min;
    }

    public final double getMax() {
        return max;
    }

    public Date getMaxDate() {
        return maxDate;
    }

    public Date getMinDate() {
        return minDate;
    }

    public final double getAverage() {
        return getCount() > 0 ? getSum() / getCount() : 0.0d;
    }

    @Override
    public String toString() {
        return "MenuPriceSummaryStatistics [count=" + count + ", sum=" + sum + ", sumCompensation="
                + sumCompensation + ", simpleSum=" + simpleSum + ", min=" + min + ", max=" + max + ", maxDate="
                + maxDate + ", minDate=" + minDate + "]";
    }

}

но когда я пытаюсь использовать это

MenuPriceSummaryStatistics statistics =
                (menuPrices
                .stream()
                .map(cp -> cp))
                .summaryStatistics();

Я получил ошибку, потому что я должен определить метод .summaryStatistics();Я пытался определить класс

public interface MenuPriceStream extends BaseStream<MenuPrice, MenuPriceStream> {
..
}

но потом я усложняюсь

С предложенным решением:

public static Collector<MenuPrice, ?, MenuPriceSummaryStatistics> menuPriceSummary() {
    return Collector.of(
            MenuPriceSummaryStatistics::new,
            MenuPriceSummaryStatistics::accept,
            (a, b) -> {a.combine(b); return a;},
            Collector.Characteristics.IDENTITY_FINISH
        );
    }

У меня есть ошибки компиляции:

Multiple markers at this line
    - Syntax error, insert "[ ]" to complete Dimension
    - Syntax error, insert ";" to complete LocalVariableDeclarationStatement
    - Syntax error on token "menuPriceSummary", AnnotationName expected after this 
     token

1 ответ

Решение

Вы можете использовать свой класс с 3 аргументами collect:

MenuPriceSummaryStatistics statistics = menuPrices.stream()
        .collect(
                MenuPriceSummaryStatistics::new,
                MenuPriceSummaryStatistics::accept,
                MenuPriceSummaryStatistics::combine
        );

Вы можете определить коллекционера

public static Collector<MenuPrice, ?, MenuPriceSummaryStatistics> menuPriceSummary() {
    return Collector.of(
            MenuPriceSummaryStatistics::new,
            MenuPriceSummaryStatistics::accept,
            (a, b) -> {a.combine(b); return a;},
            Collector.Characteristics.IDENTITY_FINISH
        );
    }

Тогда вы сможете использовать его как любой другой сборщик:

MenuPriceSummaryStatistics s = menuPrices.stream().collect(menuPriceSummary());
Другие вопросы по тегам