Improve type safety of CollectionUtils.toTreeSet#16306
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## 3.3 #16306 +/- ##
============================================
- Coverage 60.80% 60.78% -0.03%
+ Complexity 11766 11764 -2
============================================
Files 1953 1953
Lines 89188 89201 +13
Branches 13454 13457 +3
============================================
- Hits 54235 54218 -17
- Misses 29391 29413 +22
- Partials 5562 5570 +8
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
| } | ||
|
|
||
| public static <T> Set<T> toTreeSet(Set<T> set, Comparator<? super T> comparator) { | ||
| if (set == null) { |
There was a problem hiding this comment.
Why did it become null instead of checking for empty?
There was a problem hiding this comment.
The null check is intentional here. For this comparator overload, null and empty inputs need different handling: null keeps the existing null-return behavior, while an empty non-null set should still return an empty TreeSet initialized with the provided comparator. Returning the original set from isEmpty(set) would lose that comparator. The addAll call is a no-op for an empty set.
I also added a null-input test for this overload in 630215f.
What is the purpose of the change?
GitHub_issue: Fixes #16297
This pull request improves the type safety of
CollectionUtils.toTreeSet.The existing single-argument overload accepted any
Set<T>, but its implementation delegates toTreeSet, which requires naturally comparable elements unless a comparator is provided. The updated signature makes that requirement explicit at compile time:This pull request also adds a comparator-based overload for custom ordering and non-
Comparableelement types:For non-null empty inputs, the comparator overload returns an empty
TreeSetthat preserves the provided comparator.Verification:
mvn -pl dubbo-common -Dtest=CollectionUtilsTest testgit diff --checkChecklist