JavaFX 8 Bindings.when 和 Bindings.divide 创建除以零

JavaFX 8 Bindings.when and Bindings.divide creates Divide by zero

我正在努力 Bindings.when...我正在尝试创建一个百分比分数,该分数会在成功测试的数量发生变化时更新(在下面的代码中反映为 'successCountProperty') ,或者测试的总数变化(反映在 'results' 的 sizeProperty 中)。当我执行这段代码时,我得到 java.lang.ArithmeticException: / 为零。当我最初遇到异常时,我发现Bindings.when().then().otherwise(),在我看来应该可以解决问题。不幸的是,在执行此代码时,尽管 'when' 返回 false,'then' 仍在执行。有人可以帮忙吗?

public void foo()
{
    DoubleProperty scoreProperty = new SimpleDoubleProperty(0);
    ListProperty<String> results = new SimpleListProperty<>(FXCollections.observableArrayList());

    IntegerProperty successCountProperty = new SimpleIntegerProperty(0);

    scoreProperty.bind(Bindings.when(results.sizeProperty().greaterThan(0))
            .then(Bindings.divide(successCountProperty, results.sizeProperty())).otherwise(0));
}

使用 Bindings 方法或 属性 本身的更复杂的绑定很容易变得难以阅读且难以维护。

在这种情况下,我建议使用自定义评估方法创建 DoubleBinding

scoreProperty.bind(Bindings.createDoubleBinding(() -> {
    int size = results.size();
    return size == 0 ? 0d : ((double) successCountProperty.get()) / size;
}, results.sizeProperty(), successCountProperty));

你的问题是

Bindings.divide(successCountProperty, results.sizeProperty())

立即评估,您似乎期待 lazy evaluation

您可以编写自己的惰性除法器,它只在实际查询值时才进行除法:

class LazyDivision extends DoubleBinding
{
    private IntegerProperty dividend;
    private ReadOnlyIntegerProperty divisor;

    LazyDivision(final IntegerProperty dividend, final ReadOnlyIntegerProperty divisor)
    {
        this.dividend = dividend;
        this.divisor = divisor;
    }

    @Override
    protected double computeValue()
    {
        return dividend.getValue() / divisor.getValue().doubleValue();
    }
}

然后像这样使用它:

scoreProperty.bind(
    Bindings
        .when(results.sizeProperty().greaterThan(0))
        .then(
            new LazyDivision(successCountProperty, results.sizeProperty())
        )
        .otherwise(0)
);