Skip to content

Commit

Permalink
OPIK-287 PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
idoberko2 committed Dec 23, 2024
1 parent 7d0cdc9 commit 7a8905c
Showing 1 changed file with 0 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,6 @@
import java.util.stream.Collector;

public class BigDecimalCollectors {

public static Collector<BigDecimal, ?, BigDecimal> averagingBigDecimal() {
return Collector.of(
// Supplier: Create an array with two elements to hold total and count
() -> new BigDecimal[]{BigDecimal.ZERO, BigDecimal.ZERO},
// Accumulator: Update total and count
(result, value) -> {
result[0] = result[0].add(value); // Accumulate total
result[1] = result[1].add(BigDecimal.ONE); // Increment count
},
// Combiner: Merge two arrays (used for parallel streams)
(result1, result2) -> {
result1[0] = result1[0].add(result2[0]); // Combine totals
result1[1] = result1[1].add(result2[1]); // Combine counts
return result1;
},
// Finisher: Compute the average (total / count) with rounding
result -> result[1].compareTo(BigDecimal.ZERO) == 0
? BigDecimal.ZERO // Avoid division by zero
: result[0].divide(result[1], ValidationUtils.SCALE, RoundingMode.HALF_UP));
}

public static <T> Collector<T, ?, BigDecimal> averagingBigDecimal(Function<T, BigDecimal> mapper) {
return Collector.of(
() -> new BigDecimal[]{BigDecimal.ZERO, BigDecimal.ZERO},
Expand All @@ -49,5 +27,4 @@ public class BigDecimalCollectors {
? BigDecimal.ZERO
: result[0].divide(result[1], ValidationUtils.SCALE, RoundingMode.HALF_UP));
}

}

0 comments on commit 7a8905c

Please sign in to comment.