AbstractAggregatedValues.javaA generic abstract base class for accumulating aggregated values over time. Used by the statistics module and specifically by LiquidityForecast for weighted-average payment timing calculations. Subclasses (IntAggregatedValues, BigDecimalAggregatedValues) provide type-specific implementations.
Generic parameter <T> allows arithmetic operations on any numeric type without code duplication. The class stores two parallel LinkedLists — one for values, one for weights — enabling both simple averages and weighted averages on the same data set.
The class implements the Template Method pattern. Core algorithms (getAverage(), getWeightedAverage()) are defined in the abstract class, while type-specific arithmetic is delegated to subclasses via six abstract methods:
| Abstract Method | Purpose |
|---|---|
getZero() | Returns the additive identity for type T (e.g. 0 or BigDecimal.ZERO) |
sum(T, T) | Adds two values of type T |
convert(int) | Converts an int count into type T for division |
divide(T, T) | Divides sum by count/weight |
multiply(T, T) | Multiplies value by weight (for weighted average) |
isZero(T) | Checks if a value is zero (guards against division by zero) |
Both average and weightedAverage results are lazily computed and cached using a dirty-flag pattern. Two boolean fields (averageDirty, weightedAverageDirty) track whether the cached value is stale. Calling add() sets both dirty flags to true. The cached values are only recomputed when getAverage() or getWeightedAverage() is called and the respective dirty flag is set.
This optimization avoids recomputing averages on every addition, which is important when many values are added in sequence before the average is queried.
Both add() overloads and clear() return this, enabling fluent chaining:
aggregatedValues.add(val1).add(val2, weight2).add(val3).getWeightedAverage();
In LiquidityForecast, this class is used to compute the weighted average payment time across multiple invoice positions. Each position contributes a value (amount) and a weight (days until payment). The weighted average yields a single representative payment time for the forecast.
868d6abb7 2025 -> 2026 63081666f Source file headers: 2024-> 2025. b6092df09 Copyright 2023 -> 2024 ab45d51fa Copyright 2001-2022 -> 2001-2023. 5f7ef41b8 Copyright 2021 -> 2022 ceb63e8a1 Source code header: (C) 2001-2021. 7c79f1922 Copyright of source header -> 2020. 73a9755df Code cleanup: collapsed catch blocks, diamond operator, StringBuilder, list.sort 000ca723d Remove pointless boolean expressions (business) dd5ca38ac CopyRight of all java file-header updated or created.