Skip to content

Improve generic type safety of CollectionUtils.sort#16236

Open
codingkiddo wants to merge 4 commits intoapache:3.3from
codingkiddo:improve-collectionutils-sort-generics
Open

Improve generic type safety of CollectionUtils.sort#16236
codingkiddo wants to merge 4 commits intoapache:3.3from
codingkiddo:improve-collectionutils-sort-generics

Conversation

@codingkiddo
Copy link
Copy Markdown

What is changed

This PR updates CollectionUtils.sort to use a bounded generic type:

<T extends Comparable<? super T>>

To

public static <T extends Comparable<? super T>> List<T> sort(List<T> list)

The raw cast is also removed:

Collections.sort((List) list);

to

Collections.sort(list);

Why

CollectionUtils.sort currently accepts any List and relies on a raw cast with suppressed warnings.

This means callers can pass a list whose elements do not implement Comparable, and the code still compiles. However, sorting such a list can fail at runtime.

By using:

<T extends Comparable<? super T>>

the method now follows the same generic constraint style as java.util.Collections.sort.

This ensures that only naturally comparable elements can be passed to the method.

Benefit

  • Improves compile-time type safety
  • Removes raw type usage
  • Removes unchecked/raw warning suppression
  • Preserves the original return type as List
  • Aligns the method with Java collection sorting conventions
  • Supports inherited comparison cases, for example when a subtype inherits Comparable

Compatibility

The erased method signature remains sort(List), so this change should be binary-compatible.

However, this may be source-incompatible for callers that currently pass non-comparable element types to CollectionUtils.sort. Those usages would already be unsafe because they may fail at runtime.

Tests

Added/updated tests to verify:

Sorting a normal comparable list
Sorting a subtype whose parent implements Comparable

Fixes #16235

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Apr 27, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 60.79%. Comparing base (6916d1b) to head (d0fcc6b).

Additional details and impacted files
@@             Coverage Diff              @@
##                3.3   #16236      +/-   ##
============================================
+ Coverage     60.78%   60.79%   +0.01%     
- Complexity    11763    11768       +5     
============================================
  Files          1953     1953              
  Lines         89186    89186              
  Branches      13454    13454              
============================================
+ Hits          54209    54225      +16     
+ Misses        29400    29392       -8     
+ Partials       5577     5569       -8     
Flag Coverage Δ
integration-tests-java21 32.11% <100.00%> (-0.05%) ⬇️
integration-tests-java8 32.22% <100.00%> (-0.01%) ⬇️
samples-tests-java21 32.19% <0.00%> (-0.01%) ⬇️
samples-tests-java8 29.84% <0.00%> (+0.03%) ⬆️
unit-tests-java11 59.01% <100.00%> (-0.01%) ⬇️
unit-tests-java17 58.49% <100.00%> (-0.03%) ⬇️
unit-tests-java21 58.52% <100.00%> (+<0.01%) ⬆️
unit-tests-java25 58.47% <100.00%> (+0.01%) ⬆️
unit-tests-java8 59.04% <100.00%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@zrlw
Copy link
Copy Markdown
Contributor

zrlw commented Apr 29, 2026

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] The parameters of method CollectionUtils#sort should be comparable type

3 participants