-
-
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
base: trunk
Are you sure you want to change the base?
[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:
|
|||||||||
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