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:
@@ -72,10 +72,10 @@ public final class PercentileCalculator {
|
|||||||
* @return a {@code Double} value representing the calculated percentile
|
* @return a {@code Double} value representing the calculated percentile
|
||||||
*/
|
*/
|
||||||
public static Double calculatePercentile(List<Double> values, Double percentile) {
|
public static Double calculatePercentile(List<Double> values, Double percentile) {
|
||||||
var sorted = values.stream().sorted().toList();
|
if (values.isEmpty()) {
|
||||||
if (sorted.isEmpty()) {
|
|
||||||
throw new IllegalArgumentException("Unable to sort an empty list.");
|
throw new IllegalArgumentException("Unable to sort an empty list.");
|
||||||
}
|
}
|
||||||
|
var sorted = values.stream().sorted().toList();
|
||||||
|
|
||||||
var rank = percentile / 100. * (sorted.size() - 1);
|
var rank = percentile / 100. * (sorted.size() - 1);
|
||||||
var lowerIndex = (int) Math.floor(rank);
|
var lowerIndex = (int) Math.floor(rank);
|
||||||
|
|||||||
Reference in New Issue
Block a user