From 45f13a34e8eb4a9d73ee836f803bca0306645151 Mon Sep 17 00:00:00 2001 From: zihluwang Date: Tue, 3 Jun 2025 19:34:38 +0800 Subject: [PATCH] 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. --- .../src/main/java/com/onixbyte/nums/PercentileCalculator.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/num4j/src/main/java/com/onixbyte/nums/PercentileCalculator.java b/num4j/src/main/java/com/onixbyte/nums/PercentileCalculator.java index 2ae1b54..3578ec5 100644 --- a/num4j/src/main/java/com/onixbyte/nums/PercentileCalculator.java +++ b/num4j/src/main/java/com/onixbyte/nums/PercentileCalculator.java @@ -72,10 +72,10 @@ public final class PercentileCalculator { * @return a {@code Double} value representing the calculated percentile */ public static Double calculatePercentile(List 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);