{"record":{"id":"350c2cec3adeafe2","repo":"xai-org/x-algorithm","slug":"collection-is-empty","errorCode":null,"errorMessage":"collection is empty","messagePattern":"collection is empty","errorType":"validation","errorClass":"NoSuchElementException","httpStatus":null,"severity":"error","filePath":"botmaker/src/java/com/twitter/botmaker/function/math/Max.java","lineNumber":58,"sourceCode":"  public Signature getSignature() {\n    return SIGNATURE;\n  }\n\n  @Override\n  protected CacheLevel getCacheLevel() {\n    return CacheLevel.Global;\n  }\n\n  @Override\n  protected Object apply(Context<Runtime> context, List<Number> numbers) {\n    Number max = null;\n    for (Number num : numbers) {\n      if (max == null || max.doubleValue() < num.doubleValue()) {\n        max = num;\n      }\n    }\n    if (max == null) {\n      throw new NoSuchElementException(\"collection is empty\");\n    }\n    return max;\n  }\n}\n","sourceCodeStart":40,"sourceCodeEnd":63,"githubUrl":"https://github.com/xai-org/x-algorithm/blob/24c60942c5c5fdad3a6addffb4c6e6d2f228f04f/botmaker/src/java/com/twitter/botmaker/function/math/Max.java#L40-L63","documentation":"Max.apply throws NoSuchElementException(\"collection is empty\") when the input collection of numbers contains no elements, because the loop never assigns a non-null max. It mirrors Guava's/Scala's empty-collection semantics for aggregate functions.","triggerScenarios":"Calling Max() on an empty list, e.g. Max().apply(ctx, ImmutableList.of()) or an expression aggregating over an empty partition/window.","commonSituations":"Aggregating over empty group-bys, empty filtered datasets, time windows with no events, or optional lists that defaulted to empty.","solutions":["Filter out or default empty collections before applying Max (e.g. coalesce to 0 or skip the aggregation)","Wrap the expression with an emptiness check: If(IsEmpty(list), default, Max(list))","If empty input is legitimately an error, catch NoSuchElementException at the pipeline boundary"],"exampleFix":"// before\nNumber m = Max().apply(ctx, numbers); // numbers empty -> NoSuchElementException\n// after\nNumber m = numbers.isEmpty() ? 0 : (Number) Max().apply(ctx, numbers);\n","handlingStrategy":"validation","validationCode":"if (numbers == null || numbers.isEmpty()) {\n  return DEFAULT_VALUE; // or skip aggregation\n}\nreturn Max().apply(ctx, numbers);","typeGuard":"public static boolean nonEmpty(Collection<?> c) { return c != null && !c.isEmpty(); }","tryCatchPattern":"try {\n  return (Number) Max().apply(ctx, numbers);\n} catch (NoSuchElementException e) {\n  return emptyDefault; // e.g. 0.0 or Optional.empty upstream\n}","preventionTips":["Default aggregates over empty windows/groups before evaluation","Use Optional<Number> wrappers for aggregation results","Assert non-empty inputs in test fixtures for aggregation pipelines"],"tags":["math","max","empty-collection","aggregation"],"backgroundTag":"aggregate-on-empty-collection","analyzedSha":"24c60942c5c5fdad3a6addffb4c6e6d2f228f04f","analyzedAt":"2026-08-28T11:40:14.686Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}