Skip to content

Commit

Permalink
[java] Refactoring to check for emptiness (#13335)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
manuelsblanco authored Dec 20, 2023
1 parent 7acc040 commit 22dcb17
Show file tree
Hide file tree
Showing 14 changed files with 22 additions and 22 deletions.
2 changes: 1 addition & 1 deletion java/src/org/openqa/selenium/By.java
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ protected final Map<String, Object> toJson() {

private String cssEscape(String using) {
using = CSS_ESCAPE.matcher(using).replaceAll("\\\\$1");
if (using.length() > 0 && Character.isDigit(using.charAt(0))) {
if (!using.isEmpty() && Character.isDigit(using.charAt(0))) {
using = "\\" + (30 + Integer.parseInt(using.substring(0, 1))) + " " + using.substring(1);
}
return using;
Expand Down
10 changes: 5 additions & 5 deletions java/src/org/openqa/selenium/devtools/CdpClientGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,12 @@ public void dumpTo(Path target) {
Path domainDir = target.resolve(name.toLowerCase());
ensureDirectoryExists(domainDir);
dumpMainClass(domainDir);
if (types.size() > 0) {
if (!types.isEmpty()) {
Path typesDir = domainDir.resolve("model");
ensureDirectoryExists(typesDir);
types.forEach(type -> type.dumpTo(typesDir));
}
if (events.size() > 0) {
if (!events.isEmpty()) {
Path eventsDir = domainDir.resolve("model");
ensureDirectoryExists(eventsDir);
events.forEach(event -> event.dumpTo(eventsDir));
Expand Down Expand Up @@ -486,7 +486,7 @@ public EventParser() {
parameter.parse(item);
parameters.add(parameter);
});
if (parameters.size() == 0) {
if (parameters.isEmpty()) {
event.type = new VoidType();
} else if (parameters.size() == 1) {
event.type = parameters.get(0).type;
Expand Down Expand Up @@ -721,7 +721,7 @@ public CommandSpecParser() {
res.parse(item);
returns.add(res);
});
if (returns.size() == 0) {
if (returns.isEmpty()) {
command.type = new VoidType();
} else if (returns.size() == 1) {
command.type = returns.get(0).type;
Expand Down Expand Up @@ -1165,7 +1165,7 @@ public TypeDeclaration<?> toTypeDeclaration() {
fromJson.setType(capitalize(name));
fromJson.addParameter(JsonInput.class, "input");
BlockStmt body = fromJson.getBody().get();
if (properties.size() > 0) {
if (!properties.isEmpty()) {
properties.forEach(
property -> {
if (property.optional) {
Expand Down
2 changes: 1 addition & 1 deletion java/src/org/openqa/selenium/docker/Device.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ private Device(String pathOnHost, String pathInContainer, String cgroupPermissio
}

public static Device device(String pathOnHost, String pathInContainer, String cgroupPermissions) {
if (Objects.isNull(cgroupPermissions) || cgroupPermissions.trim().length() == 0) {
if (Objects.isNull(cgroupPermissions) || cgroupPermissions.trim().isEmpty()) {
cgroupPermissions = "crw";
}
return new Device(pathOnHost, pathInContainer, cgroupPermissions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public Container apply(ContainerConfig info) {

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

Expand Down
2 changes: 1 addition & 1 deletion java/src/org/openqa/selenium/grid/TemplateGridCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public final Executable configure(PrintStream out, PrintStream err, String... ar
.filter(ParameterDescription::isAssigned)
.map(ParameterDescription::getLongestName)
.collect(Collectors.toSet());
if (cliArgs.size() > 0) {
if (!cliArgs.isEmpty()) {
allFlags.forEach(flags -> allConfigs.add(new AnnotatedConfig(flags, cliArgs, true)));
}

Expand Down
4 changes: 2 additions & 2 deletions java/src/org/openqa/selenium/grid/config/AnnotatedConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ public AnnotatedConfig(Object obj, Set<String> cliArgs, boolean includeCliArgs)
Parameter cliAnnotation = field.getAnnotation(Parameter.class);
boolean containsCliArg =
cliAnnotation != null && Arrays.stream(cliAnnotation.names()).anyMatch(cliArgs::contains);
if (cliArgs.size() > 0 && !containsCliArg && includeCliArgs) {
if (!cliArgs.isEmpty() && !containsCliArg && includeCliArgs) {
// Only getting config values for args entered by the user.
continue;
}
if (cliArgs.size() > 0 && containsCliArg && !includeCliArgs) {
if (!cliArgs.isEmpty() && containsCliArg && !includeCliArgs) {
// Excluding config values for args entered by the user.
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ private void addDetectedDrivers(
sessionFactories.putAll(capabilities, entry.getValue());
});

if (sessionFactories.build().size() == 0) {
if (sessionFactories.build().isEmpty()) {
String logMessage = "No drivers have been configured or have been found on PATH";
LOG.warning(logMessage);
throw new ConfigException(logMessage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public Response execute(Command command) throws IOException {
}
commandSpan.setAttribute("command", command.getName());
Map<String, ?> parameters = command.getParameters();
if (parameters != null && parameters.size() > 0) {
if (parameters != null && !parameters.isEmpty()) {
for (Map.Entry<String, ?> parameter : parameters.entrySet()) {
commandSpan.setAttribute(
"parameter." + parameter.getKey(), Objects.toString(parameter.getValue(), "null"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ private Map<String, String> asElement(Object id) {

private String cssEscape(String using) {
using = CSS_ESCAPE.matcher(using).replaceAll("\\\\$1");
if (using.length() > 0 && Character.isDigit(using.charAt(0))) {
if (!using.isEmpty() && Character.isDigit(using.charAt(0))) {
using = "\\" + (30 + Integer.parseInt(using.substring(0, 1))) + " " + using.substring(1);
}
return using;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ protected long sleepFor() {
protected void isLoaded() throws Error {
try {
elements = AjaxElementLocator.super.findElements();
if (elements.size() == 0) {
if (elements.isEmpty()) {
throw new NoSuchElementException("Unable to locate the element");
}
for (WebElement element : elements) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ public List<WebElement> apply(WebDriver driver) {
return null;
}
}
return elements.size() > 0 ? elements : null;
return !elements.isEmpty() ? elements : null;
}

@Override
Expand Down Expand Up @@ -265,7 +265,7 @@ public List<WebElement> apply(WebDriver driver) {
return null;
}
}
return elements.size() > 0 ? elements : null;
return !elements.isEmpty() ? elements : null;
}

@Override
Expand Down Expand Up @@ -316,7 +316,7 @@ public static ExpectedCondition<List<WebElement>> presenceOfAllElementsLocatedBy
@Override
public List<WebElement> apply(WebDriver driver) {
List<WebElement> elements = driver.findElements(locator);
return elements.size() > 0 ? elements : null;
return !elements.isEmpty() ? elements : null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ void canCast() {
List<Map<String, String>> castSinks = caster.getCastSinks();

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

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

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

caster.startDesktopMirroring(deviceName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ void canCast() throws InterruptedException {
List<Map<String, String>> castSinks = caster.getCastSinks();

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

caster.startTabMirroring(deviceName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ private void waitTillNodesAreRemoved(Distributor distributor) {
.until(
d -> {
Set<NodeStatus> nodes = d.getStatus().getNodes();
return nodes.size() == 0;
return nodes.isEmpty();
});
}

Expand Down

0 comments on commit 22dcb17

Please sign in to comment.