[cleanup] Replace Collections singleton factory usages#18837
Conversation
3b79b0e to
c77d9dc
Compare
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #18837 +/- ##
=========================================
Coverage 64.76% 64.76%
Complexity 1322 1322
=========================================
Files 3393 3393
Lines 211022 211022
Branches 33135 33135
=========================================
Hits 136676 136676
+ Misses 63330 63326 -4
- Partials 11016 11020 +4
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
c77d9dc to
874fcb0
Compare
| <property name="format" value="StringUtils\.replace\("/> | ||
| <property name="message" value="StringUtils.replace() is deprecated. Use Strings.CS.replace() instead."/> | ||
| </module> | ||
| <module name="RegexpSingleline"> |
There was a problem hiding this comment.
Suggest not banning this completely. It make sense to use it when element is null. As long as we remove most usage of it, AI should be able to use List.of() when applicable.
We can also replace usages of Collections.singleton() and Collections.singletonMap().
We can remove usage of Collections.emptyList()/Set()/Map()
There was a problem hiding this comment.
Addressed. I removed the hard Checkstyle ban, replaced the remaining Collections.singleton(), Collections.emptyList(), Collections.emptySet(), and Collections.emptyMap() usages with Set.of(), List.of(), and Map.of() where applicable, and kept null-preserving cases explicit instead of banning the old APIs outright.
There was a problem hiding this comment.
Follow-up update: kept the Checkstyle guard targeted to Collections.emptyList and Collections.emptyMap (including method references), while leaving Collections.singletonList/Collections.singletonMap unblocked so null-preserving test/compat cases remain possible. Agent docs and C7.12 now call out that distinction explicitly.
There was a problem hiding this comment.
Also updated the agent docs/C7.12 to make the singleton exception stricter: Collections.singleton* should only be used when an element/key/value argument is intentionally null, since List.of(null), Set.of(null), and Map.of(...) with null keys or values throw NullPointerException.
There was a problem hiding this comment.
Follow-up: added Collections.emptySet to the same Checkstyle guard and agent/C7.12 docs, including method references. Empty list/set/map factories are now all blocked in favor of List.of()/Set.of()/Map.of(). Collections.singleton* remains allowed only for intentional null element/key/value cases because the modern factories reject nulls.
bfe6918 to
d86ae93
Compare
50c7a34 to
bc9d633
Compare
bc9d633 to
7322e1c
Compare
Summary
This PR addresses the follow-up from #18835 and the review comment on #18837 by modernizing immutable collection factory usage while keeping targeted Checkstyle guards for empty collection factories.
Changes:
Collections.singletonList(...)withList.of(...)where the element is non-null.Collections.singletonMap(...)withMap.of(...)where keys/values are non-null.Collections.singleton(...)withSet.of(...).Collections.emptyList(),Collections.emptySet(), andCollections.emptyMap()withList.of(),Set.of(), andMap.of()where mutability semantics are unchanged.Collections.emptyList,Collections.emptySet, andCollections.emptyMap, including method references.PinotHelixResourceManager.deleteTable().Collections.singletonList(),Collections.singleton(), andCollections.singletonMap()out of the Checkstyle ban, but documented thatCollections.singleton*should only be used when an element, key, or value argument is intentionally null becauseList.of(null),Set.of(null), andMap.of(...)with nulls throwNullPointerException.NullPointerException.AGENTS.md,CLAUDE.md,.github/copilot-instructions.md, andkb/) with the review rule: blockCollections.emptyList/Collections.emptySet/Collections.emptyMap, prefer modern factories for non-null immutable literals, scan method references too, check mutating callers before replacing empty factories, and do not blanket-banCollections.singleton*, but only allow it for intentional null element/key/value cases.Why
The modern immutable factories are the preferred style for non-null collection literals.
Collections.emptyList(),Collections.emptySet(), andCollections.emptyMap()should not be reintroduced, so Checkstyle now blocks them. Singleton factories are handled by review judgment instead of a blanket ban because they can still be valid when null elements, keys, or values are intentional.User Manual / Contributor Guidance
For new Pinot code, use
List.of(),Set.of(), andMap.of()instead ofCollections.emptyList(),Collections.emptySet(), andCollections.emptyMap(); Checkstyle enforces this. PreferList.of(...),Set.of(...), andMap.of(...)for non-null immutable collection literals. UseCollections.singleton*only when an element, key, or value argument is intentionally null; otherwise preferList.of(...),Set.of(...), andMap.of(...). If a test intentionally needs null contents, keep that intent explicit and local to the test. Before replacing empty collection factories, check whether callers mutate the returned collection and copy locally if needed.Sample Table Config / Queries
No table config or query syntax changes are introduced. Query behavior is unchanged; this is a source cleanup only.
Validation
rg -n "Collections(\\.|::)(emptyList|emptySet|emptyMap)\\b" --glob '*.java' --glob '*.jj' --glob '!target/**' .git diff --check./mvnw spotless:apply./mvnw checkstyle:check./mvnw license:format./mvnw license:check./mvnw -DskipTests test-compilepinot-minion-builtin-taskspassed on immediate rerun with-rf :pinot-minion-builtin-tasks -e../mvnw -pl pinot-controller -Dtest=PinotHelixResourceManagerStatelessTest#testUpdateTableConfigRefreshesBrokerAndServerCaches test./mvnw spotless:apply -pl pinot-controller./mvnw checkstyle:check -pl pinot-controller./mvnw license:format -pl pinot-controller./mvnw license:check -pl pinot-controller./mvnw spotless:apply -pl pinot-core,pinot-query-runtime./mvnw checkstyle:check -pl pinot-core,pinot-query-runtime./mvnw license:format -pl pinot-core,pinot-query-runtime./mvnw license:check -pl pinot-core,pinot-query-runtime./mvnw -pl pinot-server -Dtest=TableTierResourceTest test./mvnw spotless:apply -pl pinot-server./mvnw checkstyle:check -pl pinot-server./mvnw license:format -pl pinot-server./mvnw license:check -pl pinot-server