我在尝试将bigdecimal转换为ObjectProperty时遇到问题
public class LinePiece {
private int idLine;
private ObjectProperty<BigDecimal> qty = new SimpleObjectProperty<BigDecimal>(BigDecimal.ZERO);
private ObjectProperty<BigDecimal> price = new SimpleObjectProperty<BigDecimal>(BigDecimal.ZERO);
private ObjectProperty<BigDecimal> remise = new SimpleObjectProperty<BigDecimal>(BigDecimal.ZERO);
private IntegerProperty tva = new SimpleIntegerProperty();
private ObjectProperty<BigDecimal> subTotal = new SimpleObjectProperty<BigDecimal>(BigDecimal.ZERO);
// Getters and Setters
}
我们有一组LinePiece类型的项目
private Set<LinePiece> LinePieces = new HashSet<>();
LinePieces .add(new LinePiece());
LinePieces .add(new LinePiece());
LinePieces .add(new LinePiece());
对实际集合实施求和后:
BigDecimal sum = lineCommande.stream().map(LineCommande::getSubTotal)
.reduce(BigDecimal.ZERO,BigDecimal::add);
我想要总和可观察,所以我可以听取总和的变化。 我怎么能实现这个目标?
备注:我不能在这里使用ObservableList,因为我正在使用hibernate
@OneToMany(mappedBy = "piece", fetch = FetchType.EAGER, orphanRemoval = true, cascade = CascadeType.ALL)
private Set<LinePiece> LinePieces = new HashSet<>();
如果有另一个使用ObservableList的解决方案,请告诉我。
答案 0 :(得分:0)
创建ObjectBinding<BigInteger>
(实现ObservableValue<BigInteger>
):
ObjectBinding<BigInteger> sum = new ObjectBinding<BigInteger>() {
{
lineCommande.stream().forEach(l -> bind(l.subTotalProperty()));
}
@Override
protected BigInteger computeValue() {
return lineCommande.stream().map(LineCommande::getSubTotal)
.reduce(BigDecimal.ZERO,BigDecimal::add);
}
};