Skip to content

Commit 22dcb17

Browse files
[java] Refactoring to check for emptiness (#13335)
* Optimized checking for empty collections in ChromeDriverFunctionalTest Replaced size-based checks for collection emptiness with isEmpty method calls in ChromeDriverFunctionalTest class. This change simplifies the code and potentially improves performance by avoiding unnecessary size calculations when just checking if the collection is empty or not. * Replace size checks with isEmpty method Replaced all the instances where the size of a collection was being checked to see if it was greater than zero, with the isEmpty method. The isEmpty method is more efficient and performant because it doesn't need to count all elements in order to determine if the collection has any elements, it just checks if there is at least one.
1 parent 7acc040 commit 22dcb17

File tree

14 files changed

+22
-22
lines changed

14 files changed

+22
-22
lines changed

java/src/org/openqa/selenium/By.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ protected final Map<String, Object> toJson() {
446446

447447
private String cssEscape(String using) {
448448
using = CSS_ESCAPE.matcher(using).replaceAll("\\\\$1");
449-
if (using.length() > 0 && Character.isDigit(using.charAt(0))) {
449+
if (!using.isEmpty() && Character.isDigit(using.charAt(0))) {
450450
using = "\\" + (30 + Integer.parseInt(using.substring(0, 1))) + " " + using.substring(1);
451451
}
452452
return using;

java/src/org/openqa/selenium/devtools/CdpClientGenerator.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -269,12 +269,12 @@ public void dumpTo(Path target) {
269269
Path domainDir = target.resolve(name.toLowerCase());
270270
ensureDirectoryExists(domainDir);
271271
dumpMainClass(domainDir);
272-
if (types.size() > 0) {
272+
if (!types.isEmpty()) {
273273
Path typesDir = domainDir.resolve("model");
274274
ensureDirectoryExists(typesDir);
275275
types.forEach(type -> type.dumpTo(typesDir));
276276
}
277-
if (events.size() > 0) {
277+
if (!events.isEmpty()) {
278278
Path eventsDir = domainDir.resolve("model");
279279
ensureDirectoryExists(eventsDir);
280280
events.forEach(event -> event.dumpTo(eventsDir));
@@ -486,7 +486,7 @@ public EventParser() {
486486
parameter.parse(item);
487487
parameters.add(parameter);
488488
});
489-
if (parameters.size() == 0) {
489+
if (parameters.isEmpty()) {
490490
event.type = new VoidType();
491491
} else if (parameters.size() == 1) {
492492
event.type = parameters.get(0).type;
@@ -721,7 +721,7 @@ public CommandSpecParser() {
721721
res.parse(item);
722722
returns.add(res);
723723
});
724-
if (returns.size() == 0) {
724+
if (returns.isEmpty()) {
725725
command.type = new VoidType();
726726
} else if (returns.size() == 1) {
727727
command.type = returns.get(0).type;
@@ -1165,7 +1165,7 @@ public TypeDeclaration<?> toTypeDeclaration() {
11651165
fromJson.setType(capitalize(name));
11661166
fromJson.addParameter(JsonInput.class, "input");
11671167
BlockStmt body = fromJson.getBody().get();
1168-
if (properties.size() > 0) {
1168+
if (!properties.isEmpty()) {
11691169
properties.forEach(
11701170
property -> {
11711171
if (property.optional) {

java/src/org/openqa/selenium/docker/Device.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ private Device(String pathOnHost, String pathInContainer, String cgroupPermissio
3434
}
3535

3636
public static Device device(String pathOnHost, String pathInContainer, String cgroupPermissions) {
37-
if (Objects.isNull(cgroupPermissions) || cgroupPermissions.trim().length() == 0) {
37+
if (Objects.isNull(cgroupPermissions) || cgroupPermissions.trim().isEmpty()) {
3838
cgroupPermissions = "crw";
3939
}
4040
return new Device(pathOnHost, pathInContainer, cgroupPermissions);

java/src/org/openqa/selenium/docker/v1_41/CreateContainer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public Container apply(ContainerConfig info) {
7171

7272
if (rawContainer.get("Warnings") instanceof Collection) {
7373
Collection<?> warnings = (Collection<?>) rawContainer.get("Warnings");
74-
if (warnings.size() > 0) {
74+
if (!warnings.isEmpty()) {
7575
String allWarnings =
7676
warnings.stream().map(String::valueOf).collect(Collectors.joining("\n", " * ", ""));
7777

java/src/org/openqa/selenium/grid/TemplateGridCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public final Executable configure(PrintStream out, PrintStream err, String... ar
9090
.filter(ParameterDescription::isAssigned)
9191
.map(ParameterDescription::getLongestName)
9292
.collect(Collectors.toSet());
93-
if (cliArgs.size() > 0) {
93+
if (!cliArgs.isEmpty()) {
9494
allFlags.forEach(flags -> allConfigs.add(new AnnotatedConfig(flags, cliArgs, true)));
9595
}
9696

java/src/org/openqa/selenium/grid/config/AnnotatedConfig.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,11 @@ public AnnotatedConfig(Object obj, Set<String> cliArgs, boolean includeCliArgs)
7777
Parameter cliAnnotation = field.getAnnotation(Parameter.class);
7878
boolean containsCliArg =
7979
cliAnnotation != null && Arrays.stream(cliAnnotation.names()).anyMatch(cliArgs::contains);
80-
if (cliArgs.size() > 0 && !containsCliArg && includeCliArgs) {
80+
if (!cliArgs.isEmpty() && !containsCliArg && includeCliArgs) {
8181
// Only getting config values for args entered by the user.
8282
continue;
8383
}
84-
if (cliArgs.size() > 0 && containsCliArg && !includeCliArgs) {
84+
if (!cliArgs.isEmpty() && containsCliArg && !includeCliArgs) {
8585
// Excluding config values for args entered by the user.
8686
continue;
8787
}

java/src/org/openqa/selenium/grid/node/config/NodeOptions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ private void addDetectedDrivers(
507507
sessionFactories.putAll(capabilities, entry.getValue());
508508
});
509509

510-
if (sessionFactories.build().size() == 0) {
510+
if (sessionFactories.build().isEmpty()) {
511511
String logMessage = "No drivers have been configured or have been found on PATH";
512512
LOG.warning(logMessage);
513513
throw new ConfigException(logMessage);

java/src/org/openqa/selenium/remote/TracedCommandExecutor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public Response execute(Command command) throws IOException {
4242
}
4343
commandSpan.setAttribute("command", command.getName());
4444
Map<String, ?> parameters = command.getParameters();
45-
if (parameters != null && parameters.size() > 0) {
45+
if (parameters != null && !parameters.isEmpty()) {
4646
for (Map.Entry<String, ?> parameter : parameters.entrySet()) {
4747
commandSpan.setAttribute(
4848
"parameter." + parameter.getKey(), Objects.toString(parameter.getValue(), "null"));

java/src/org/openqa/selenium/remote/codec/w3c/W3CHttpCommandCodec.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ private Map<String, String> asElement(Object id) {
393393

394394
private String cssEscape(String using) {
395395
using = CSS_ESCAPE.matcher(using).replaceAll("\\\\$1");
396-
if (using.length() > 0 && Character.isDigit(using.charAt(0))) {
396+
if (!using.isEmpty() && Character.isDigit(using.charAt(0))) {
397397
using = "\\" + (30 + Integer.parseInt(using.substring(0, 1))) + " " + using.substring(1);
398398
}
399399
return using;

java/src/org/openqa/selenium/support/pagefactory/AjaxElementLocator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ protected long sleepFor() {
195195
protected void isLoaded() throws Error {
196196
try {
197197
elements = AjaxElementLocator.super.findElements();
198-
if (elements.size() == 0) {
198+
if (elements.isEmpty()) {
199199
throw new NoSuchElementException("Unable to locate the element");
200200
}
201201
for (WebElement element : elements) {

java/src/org/openqa/selenium/support/ui/ExpectedConditions.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ public List<WebElement> apply(WebDriver driver) {
224224
return null;
225225
}
226226
}
227-
return elements.size() > 0 ? elements : null;
227+
return !elements.isEmpty() ? elements : null;
228228
}
229229

230230
@Override
@@ -265,7 +265,7 @@ public List<WebElement> apply(WebDriver driver) {
265265
return null;
266266
}
267267
}
268-
return elements.size() > 0 ? elements : null;
268+
return !elements.isEmpty() ? elements : null;
269269
}
270270

271271
@Override
@@ -316,7 +316,7 @@ public static ExpectedCondition<List<WebElement>> presenceOfAllElementsLocatedBy
316316
@Override
317317
public List<WebElement> apply(WebDriver driver) {
318318
List<WebElement> elements = driver.findElements(locator);
319-
return elements.size() > 0 ? elements : null;
319+
return !elements.isEmpty() ? elements : null;
320320
}
321321

322322
@Override

java/test/org/openqa/selenium/chrome/ChromeDriverFunctionalTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ void canCast() {
142142
List<Map<String, String>> castSinks = caster.getCastSinks();
143143

144144
// Can not call these commands if there are no sinks available
145-
if (castSinks.size() > 0) {
145+
if (!castSinks.isEmpty()) {
146146
String deviceName = castSinks.get(0).get("name");
147147

148148
caster.startTabMirroring(deviceName);
@@ -161,7 +161,7 @@ public void canCastOnDesktop() {
161161
List<Map<String, String>> castSinks = caster.getCastSinks();
162162

163163
// Can not call these commands if there are no sinks available
164-
if (castSinks.size() > 0) {
164+
if (!castSinks.isEmpty()) {
165165
String deviceName = castSinks.get(0).get("name");
166166

167167
caster.startDesktopMirroring(deviceName);

java/test/org/openqa/selenium/edge/EdgeDriverFunctionalTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ void canCast() throws InterruptedException {
151151
List<Map<String, String>> castSinks = caster.getCastSinks();
152152

153153
// Can not call these commands if there are no sinks available
154-
if (castSinks.size() > 0) {
154+
if (!castSinks.isEmpty()) {
155155
String deviceName = castSinks.get(0).get("name");
156156

157157
caster.startTabMirroring(deviceName);

java/test/org/openqa/selenium/grid/distributor/DistributorDrainingTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ private void waitTillNodesAreRemoved(Distributor distributor) {
169169
.until(
170170
d -> {
171171
Set<NodeStatus> nodes = d.getStatus().getNodes();
172-
return nodes.size() == 0;
172+
return nodes.isEmpty();
173173
});
174174
}
175175

0 commit comments

Comments
 (0)