自定义功能接口SummaryStatistics

问题描述 投票:2回答:1

我有一个基本的SpringBoot应用程序。使用Spring Initializer,JPA,嵌入式Tomcat,Thymeleaf模板引擎和包作为可执行JAR文件我想创建一个基于POJO的自定义SummaryStatistics,它有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
lambda java-8 functional-programming java-stream functional-interface
1个回答
5
投票

你可以使用你的类与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());
© www.soinside.com 2019 - 2024. All rights reserved.