From e301ef01599040ab70ee8f5ff3dc122549270e5f Mon Sep 17 00:00:00 2001 From: Alex Popov Date: Tue, 4 Nov 2025 11:51:51 +0700 Subject: [PATCH 1/2] Refactor map creation to use Map.of and Map.copyOf for improved readability --- .../org/openqa/selenium/DeviceRotation.java | 11 ++--------- .../selenium/grid/data/CapabilityCount.java | 3 +-- .../grid/data/CreateSessionRequest.java | 4 +--- .../openqa/selenium/json/NumberCoercer.java | 19 ++++++++----------- .../VirtualAuthenticatorOptions.java | 17 +++++++---------- .../selector/DefaultSlotSelectorTest.java | 8 ++++---- 6 files changed, 23 insertions(+), 39 deletions(-) diff --git a/java/src/org/openqa/selenium/DeviceRotation.java b/java/src/org/openqa/selenium/DeviceRotation.java index fb6e226e112ef..026c24c25802a 100644 --- a/java/src/org/openqa/selenium/DeviceRotation.java +++ b/java/src/org/openqa/selenium/DeviceRotation.java @@ -17,8 +17,6 @@ package org.openqa.selenium; -import java.util.Collections; -import java.util.HashMap; import java.util.Map; import java.util.Objects; @@ -96,23 +94,18 @@ public int getZ() { * @return All axes mapped to a Map. */ public Map parameters() { - HashMap values = new HashMap<>(); - values.put("x", this.x); - values.put("y", this.y); - values.put("z", this.z); - return Collections.unmodifiableMap(values); + return Map.of("x", this.x, "y", this.y, "z", this.z); } @Override public boolean equals(Object o) { - if (!(o instanceof DeviceRotation)) { + if (!(o instanceof DeviceRotation obj)) { return false; } if (o == this) { return true; } - DeviceRotation obj = (DeviceRotation) o; return obj.getX() == this.getX() && obj.getY() == this.getY() && obj.getZ() == this.getZ(); } diff --git a/java/src/org/openqa/selenium/grid/data/CapabilityCount.java b/java/src/org/openqa/selenium/grid/data/CapabilityCount.java index 3e76fe8af4821..f04af7680fc2c 100644 --- a/java/src/org/openqa/selenium/grid/data/CapabilityCount.java +++ b/java/src/org/openqa/selenium/grid/data/CapabilityCount.java @@ -17,7 +17,6 @@ package org.openqa.selenium.grid.data; -import static java.util.Collections.unmodifiableMap; import static java.util.stream.Collector.Characteristics.UNORDERED; import java.util.ArrayList; @@ -33,7 +32,7 @@ public class CapabilityCount { private final Map counts; public CapabilityCount(Map counts) { - this.counts = unmodifiableMap(new HashMap<>(counts)); + this.counts = Map.copyOf(counts); } public Map getCounts() { diff --git a/java/src/org/openqa/selenium/grid/data/CreateSessionRequest.java b/java/src/org/openqa/selenium/grid/data/CreateSessionRequest.java index 4a055ca3ca054..e89ba10f81886 100644 --- a/java/src/org/openqa/selenium/grid/data/CreateSessionRequest.java +++ b/java/src/org/openqa/selenium/grid/data/CreateSessionRequest.java @@ -17,11 +17,9 @@ package org.openqa.selenium.grid.data; -import static java.util.Collections.unmodifiableMap; import static java.util.Collections.unmodifiableSet; import static org.openqa.selenium.json.Json.MAP_TYPE; -import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; @@ -43,7 +41,7 @@ public CreateSessionRequest( this.downstreamDialects = unmodifiableSet(new HashSet<>(Require.nonNull("Downstream dialects", downstreamDialects))); this.capabilities = ImmutableCapabilities.copyOf(Require.nonNull("Capabilities", capabilities)); - this.metadata = unmodifiableMap(new HashMap<>(Require.nonNull("Metadata", metadata))); + this.metadata = Map.copyOf(Require.nonNull("Metadata", metadata)); } public Set getDownstreamDialects() { diff --git a/java/src/org/openqa/selenium/json/NumberCoercer.java b/java/src/org/openqa/selenium/json/NumberCoercer.java index 6572bc0f4b33d..78d352a5b25a6 100644 --- a/java/src/org/openqa/selenium/json/NumberCoercer.java +++ b/java/src/org/openqa/selenium/json/NumberCoercer.java @@ -19,8 +19,6 @@ import java.lang.reflect.Type; import java.math.BigDecimal; -import java.util.Collections; -import java.util.HashMap; import java.util.Map; import java.util.function.BiFunction; import java.util.function.Function; @@ -31,15 +29,14 @@ class NumberCoercer extends TypeCoercer { private static final Map, Class> PRIMITIVE_NUMBER_TYPES; static { - Map, Class> builder = new HashMap<>(); - builder.put(byte.class, Byte.class); - builder.put(double.class, Double.class); - builder.put(float.class, Float.class); - builder.put(int.class, Integer.class); - builder.put(long.class, Long.class); - builder.put(short.class, Short.class); - - PRIMITIVE_NUMBER_TYPES = Collections.unmodifiableMap(builder); + PRIMITIVE_NUMBER_TYPES = + Map.ofEntries( + Map.entry(byte.class, Byte.class), + Map.entry(double.class, Double.class), + Map.entry(float.class, Float.class), + Map.entry(int.class, Integer.class), + Map.entry(long.class, Long.class), + Map.entry(short.class, Short.class)); } private final Class stereotype; diff --git a/java/src/org/openqa/selenium/virtualauthenticator/VirtualAuthenticatorOptions.java b/java/src/org/openqa/selenium/virtualauthenticator/VirtualAuthenticatorOptions.java index 792f487826105..6a5620c3c38ed 100644 --- a/java/src/org/openqa/selenium/virtualauthenticator/VirtualAuthenticatorOptions.java +++ b/java/src/org/openqa/selenium/virtualauthenticator/VirtualAuthenticatorOptions.java @@ -17,8 +17,6 @@ package org.openqa.selenium.virtualauthenticator; -import java.util.Collections; -import java.util.HashMap; import java.util.Map; import org.jspecify.annotations.NullMarked; @@ -95,13 +93,12 @@ public VirtualAuthenticatorOptions setIsUserVerified(boolean isUserVerified) { } public Map toMap() { - Map map = new HashMap<>(); - map.put("protocol", protocol.id); - map.put("transport", transport.id); - map.put("hasResidentKey", hasResidentKey); - map.put("hasUserVerification", hasUserVerification); - map.put("isUserConsenting", isUserConsenting); - map.put("isUserVerified", isUserVerified); - return Collections.unmodifiableMap(map); + return Map.ofEntries( + Map.entry("protocol", protocol.id), + Map.entry("transport", transport.id), + Map.entry("hasResidentKey", hasResidentKey), + Map.entry("hasUserVerification", hasUserVerification), + Map.entry("isUserConsenting", isUserConsenting), + Map.entry("isUserVerified", isUserVerified)); } } diff --git a/java/test/org/openqa/selenium/grid/distributor/selector/DefaultSlotSelectorTest.java b/java/test/org/openqa/selenium/grid/distributor/selector/DefaultSlotSelectorTest.java index df3eecbd5d53a..f2012b156e8e0 100644 --- a/java/test/org/openqa/selenium/grid/distributor/selector/DefaultSlotSelectorTest.java +++ b/java/test/org/openqa/selenium/grid/distributor/selector/DefaultSlotSelectorTest.java @@ -98,16 +98,16 @@ void nodesAreOrderedNodesByBrowserVersion() { ImmutableMap.of("browserName", "chrome", "browserVersion", "132.0"))); NodeStatus node2 = createNodeWithStereotypes( - Arrays.asList(ImmutableMap.of("browserName", "chrome", "browserVersion", "131.0"))); + List.of(ImmutableMap.of("browserName", "chrome", "browserVersion", "131.0"))); NodeStatus node3 = createNodeWithStereotypes( - Arrays.asList(ImmutableMap.of("browserName", "chrome", "browserVersion", ""))); + List.of(ImmutableMap.of("browserName", "chrome", "browserVersion", ""))); NodeStatus node4 = createNodeWithStereotypes( - Arrays.asList(ImmutableMap.of("browserName", "chrome", "browserVersion", "131.1"))); + List.of(ImmutableMap.of("browserName", "chrome", "browserVersion", "131.1"))); NodeStatus node5 = createNodeWithStereotypes( - Arrays.asList(ImmutableMap.of("browserName", "chrome", "browserVersion", "beta"))); + List.of(ImmutableMap.of("browserName", "chrome", "browserVersion", "beta"))); Set nodes = ImmutableSet.of(node1, node2, node3, node4, node5); Set slots = selector.selectSlot(caps, nodes, new DefaultSlotMatcher()); From 69605dd7efdb43520cf74cad9edbcfaee804d650 Mon Sep 17 00:00:00 2001 From: Alex Popov Date: Tue, 4 Nov 2025 12:07:52 +0700 Subject: [PATCH 2/2] revert changes in equals method to use explicit casting --- java/src/org/openqa/selenium/DeviceRotation.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/java/src/org/openqa/selenium/DeviceRotation.java b/java/src/org/openqa/selenium/DeviceRotation.java index 026c24c25802a..8ce201ce53c9a 100644 --- a/java/src/org/openqa/selenium/DeviceRotation.java +++ b/java/src/org/openqa/selenium/DeviceRotation.java @@ -99,13 +99,14 @@ public Map parameters() { @Override public boolean equals(Object o) { - if (!(o instanceof DeviceRotation obj)) { + if (!(o instanceof DeviceRotation)) { return false; } if (o == this) { return true; } + DeviceRotation obj = (DeviceRotation) o; return obj.getX() == this.getX() && obj.getY() == this.getY() && obj.getZ() == this.getZ(); }