fix: validate non-empty list before sorting

Move the empty list check before sorting to avoid processing an empty
collection and provide clearer error handling.
This commit is contained in:
zihluwang
2025-06-03 19:34:38 +08:00
parent bbc5215f84
commit 45f13a34e8
@@ -72,10 +72,10 @@ public final class PercentileCalculator {
* @return a {@code Double} value representing the calculated percentile
*/
public static Double calculatePercentile(List<Double> values, Double percentile) {
var sorted = values.stream().sorted().toList();
if (sorted.isEmpty()) {
if (values.isEmpty()) {
throw new IllegalArgumentException("Unable to sort an empty list.");
}
var sorted = values.stream().sorted().toList();
var rank = percentile / 100. * (sorted.size() - 1);
var lowerIndex = (int) Math.floor(rank);