-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
[java] simplify unmodifiable collections operations #16549
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[java] simplify unmodifiable collections operations #16549
Conversation
PR Compliance Guide 🔍Below is a summary of compliance checks for this PR:
Compliance status legend🟢 - Fully Compliant🟡 - Partial Compliant 🔴 - Not Compliant ⚪ - Requires Further Human Verification 🏷️ - Compliance label |
|||||||||||||||||||||||||||||||||||
PR Code Suggestions ✨Explore these optional code suggestions:
|
|||||||||
asolntsev
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generally OK, but need to check if this code is compilable by Java version used in Selenium.
@asolntsev Line 55 in 3a0b3ae
As far as I remember, the project supports JDK 11 as a minimum version. I couldn't find any mention of a JDK version lower than 11 anywhere in the configurations. I ran tests in CI and compilation was successful https://github.com/iampopovich/selenium/actions/runs/19912486873 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This pull request modernizes collection creation throughout the Selenium Java codebase by replacing verbose legacy patterns with concise Java 9+ factory methods. The changes eliminate boilerplate code while maintaining the immutability guarantees of the original implementations.
Key changes:
- Replaced
Collections.unmodifiableMap(new HashMap<>(...))withMap.of,Map.copyOf, andMap.ofEntries - Replaced
Arrays.asListwithList.ofin test code where appropriate - Removed unnecessary imports for
Collections,HashMap, and related classes
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| DeviceRotation.java | Simplified parameters() method using Map.of for three key-value pairs |
| CapabilityCount.java | Replaced unmodifiableMap(new HashMap<>()) with Map.copyOf in constructor |
| CreateSessionRequest.java | Replaced unmodifiableMap(new HashMap<>()) with Map.copyOf for metadata field |
| NumberCoercer.java | Refactored static initializer to use Map.ofEntries for primitive type mapping |
| VirtualAuthenticatorOptions.java | Replaced manual map construction with Map.ofEntries in toMap() method |
| DefaultSlotSelectorTest.java | Modernized test data creation using List.of instead of Arrays.asList |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
User description
🔗 Related Issues
💥 What does this PR do?
This pull request refactors several places in the codebase to use modern Java collection factory methods (
Map.of,Map.ofEntries,Map.copyOf, andList.of) instead of older patterns that manually create and wrap collections. This improves code readability, reduces boilerplate, and leverages immutable collections directly from the JDK.Refactoring to use modern collection factory methods:
Map.ofinDeviceRotation.parameters()for more concise and immutable map creation.CapabilityCountandCreateSessionRequestto useMap.copyOffor immutable maps instead ofunmodifiableMap(new HashMap<>(...)). [1] [2]NumberCoercer.PRIMITIVE_NUMBER_TYPESinitialization to useMap.ofEntriesfor cleaner and more direct mapping.VirtualAuthenticatorOptions.toMap()to useMap.ofEntriesinstead of manual map creation and wrapping.Modernizing collection usage in tests:
DefaultSlotSelectorTestto useList.ofinstead ofArrays.asList, aligning with modern Java idioms.🔧 Implementation Notes
I primarily changed unmodifiable collections that do not affect the contract of classes and methods
There are still unmodifiableSet and unmodifiableMap types in the code, but I haven't figured out yet if I can do anything with them.
💡 Additional Considerations
🔄 Types of changes
PR Type
Enhancement
Description
Replace manual collection wrapping with modern Java factory methods
Use
Map.of,Map.copyOf, andMap.ofEntriesfor cleaner codeEliminate unnecessary
HashMapandCollections.unmodifiableMappatternsUpdate test code to use
List.ofinstead ofArrays.asListDiagram Walkthrough
File Walkthrough
DeviceRotation.java
Simplify map creation using Map.ofjava/src/org/openqa/selenium/DeviceRotation.java
HashMapconstruction withMap.ofinparameters()method
CollectionsandHashMapCapabilityCount.java
Use Map.copyOf for immutable map creationjava/src/org/openqa/selenium/grid/data/CapabilityCount.java
unmodifiableMap(new HashMap<>(counts))withMap.copyOf(counts)unmodifiableMapCreateSessionRequest.java
Modernize metadata map initializationjava/src/org/openqa/selenium/grid/data/CreateSessionRequest.java
unmodifiableMap(new HashMap<>(...))withMap.copyOfformetadata field
unmodifiableMapHashMapimportNumberCoercer.java
Use Map.ofEntries for primitive type mappingjava/src/org/openqa/selenium/json/NumberCoercer.java
PRIMITIVE_NUMBER_TYPESstatic initialization to useMap.ofEntriesHashMapconstruction andCollections.unmodifiableMapwrapping
VirtualAuthenticatorOptions.java
Simplify toMap method using Map.ofEntriesjava/src/org/openqa/selenium/virtualauthenticator/VirtualAuthenticatorOptions.java
toMap()method to useMap.ofEntriesinstead of manual mapconstruction
HashMapandCollections.unmodifiableMapusageDefaultSlotSelectorTest.java
Modernize test collections to use List.ofjava/test/org/openqa/selenium/grid/distributor/selector/DefaultSlotSelectorTest.java
Arrays.asListwithList.ofin four test cases