Skip to content

Commit

Permalink
Use isEmpty() where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
basil committed Dec 17, 2024
1 parent 898e98d commit ca0562e
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/main/java/hudson/plugins/ec2/CloudHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ static Instance getInstance(String instanceId, EC2Cloud cloud) throws AmazonClie
if (reservations.size() != 1) {
String message = "Unexpected number of reservations reported by EC2 for instance id '" + instanceId
+ "', expected 1 result, found " + reservations + ".";
if (reservations.size() == 0) {
if (reservations.isEmpty()) {
message += " Instance seems to be dead.";
}
LOGGER.info(message);
Expand All @@ -70,7 +70,7 @@ static Instance getInstance(String instanceId, EC2Cloud cloud) throws AmazonClie
if (instances.size() != 1) {
String message = "Unexpected number of instances reported by EC2 for instance id '" + instanceId
+ "', expected 1 result, found " + instances + ".";
if (instances.size() == 0) {
if (instances.isEmpty()) {
message += " Instance seems to be dead.";
}
LOGGER.info(message);
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/hudson/plugins/ec2/EC2AbstractSlave.java
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,7 @@ public long getLaunchTimeoutInMillis() {
}

public String getRemoteAdmin() {
if (remoteAdmin == null || remoteAdmin.length() == 0) {
if (remoteAdmin == null || remoteAdmin.isEmpty()) {
return amiType.isWindows() ? "Administrator" : "root";
}
return remoteAdmin;
Expand All @@ -775,7 +775,7 @@ String getRootCommandPrefix() {
String commandPrefix = (amiType.isUnix()
? ((UnixData) amiType).getRootCommandPrefix()
: (amiType.isMac() ? ((MacData) amiType).getRootCommandPrefix() : ""));
if (commandPrefix == null || commandPrefix.length() == 0) {
if (commandPrefix == null || commandPrefix.isEmpty()) {
return "";
}
return commandPrefix + " ";
Expand All @@ -785,7 +785,7 @@ String getSlaveCommandPrefix() {
String commandPrefix = (amiType.isUnix()
? ((UnixData) amiType).getSlaveCommandPrefix()
: (amiType.isMac() ? ((MacData) amiType).getSlaveCommandPrefix() : ""));
if (commandPrefix == null || commandPrefix.length() == 0) {
if (commandPrefix == null || commandPrefix.isEmpty()) {
return "";
}
return commandPrefix + " ";
Expand All @@ -795,7 +795,7 @@ String getSlaveCommandSuffix() {
String commandSuffix = (amiType.isUnix()
? ((UnixData) amiType).getSlaveCommandSuffix()
: (amiType.isMac() ? ((MacData) amiType).getSlaveCommandSuffix() : ""));
if (commandSuffix == null || commandSuffix.length() == 0) {
if (commandSuffix == null || commandSuffix.isEmpty()) {
return "";
}
return " " + commandSuffix;
Expand All @@ -813,7 +813,7 @@ public int getSshPort() {
String sshPort = (amiType.isUnix()
? ((UnixData) amiType).getSshPort()
: (amiType.isMac() ? ((MacData) amiType).getSshPort() : "22"));
if (sshPort == null || sshPort.length() == 0) {
if (sshPort == null || sshPort.isEmpty()) {
return 22;
}

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/hudson/plugins/ec2/EC2Cloud.java
Original file line number Diff line number Diff line change
Expand Up @@ -965,7 +965,7 @@ void attemptReattachOrphanOrStoppedNodes(Jenkins jenkinsInstance, SlaveTemplate
orphansOrStopped.remove(0);
}
attachSlavesToJenkins(jenkinsInstance, template.toSlaves(orphansOrStopped), template);
if (orphansOrStopped.size() > 0) {
if (!orphansOrStopped.isEmpty()) {
LOGGER.info("Found and re-attached " + orphansOrStopped.size() + " orphan/stopped nodes");
}
}
Expand Down Expand Up @@ -1170,7 +1170,7 @@ public static String getAwsPartitionHostForService(String region, String service
* Convert a configured hostname like 'us-east-1' to a FQDN or ip address
*/
public static String convertHostName(String ec2HostName) {
if (ec2HostName == null || ec2HostName.length() == 0) {
if (ec2HostName == null || ec2HostName.isEmpty()) {
ec2HostName = DEFAULT_EC2_HOST;
}
if (!ec2HostName.contains(".")) {
Expand All @@ -1183,7 +1183,7 @@ public static String convertHostName(String ec2HostName) {
* Convert a user entered string into a port number "" -> -1 to indicate default based on SSL setting
*/
public static Integer convertPort(String ec2Port) {
if (ec2Port == null || ec2Port.length() == 0) {
if (ec2Port == null || ec2Port.isEmpty()) {
return -1;
}
return Integer.parseInt(ec2Port);
Expand Down Expand Up @@ -1424,7 +1424,7 @@ protected FormValidation doTestConnection(
AmazonEC2 ec2 = AmazonEC2Factory.getInstance().connect(credentialsProvider, ec2endpoint);
ec2.describeInstances();

if (privateKey.trim().length() > 0) {
if (!privateKey.trim().isEmpty()) {
// check if this key exists
EC2PrivateKey pk = new EC2PrivateKey(privateKey);
if (pk.find(ec2) == null) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/hudson/plugins/ec2/SlaveTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -2117,7 +2117,7 @@ private void setupRootDevice(Image image, List<BlockDeviceMapping> deviceMapping

// get the root device (only one expected in the blockmappings)
final List<BlockDeviceMapping> rootDeviceMappings = image.getBlockDeviceMappings();
if (rootDeviceMappings.size() == 0) {
if (rootDeviceMappings.isEmpty()) {
LOGGER.warning("AMI missing block devices");
return;
}
Expand Down Expand Up @@ -2553,7 +2553,7 @@ private List<String> getEc2SecurityGroups(AmazonEC2 ec2) throws AmazonClientExce
securityGroupSet, this.getParent().name, getCurrentSubnetId()));
List<String> groupIds = new ArrayList<>();
DescribeSecurityGroupsResult groupResult = getSecurityGroupsBy("group-name", securityGroupSet, ec2);
if (groupResult.getSecurityGroups().size() == 0) {
if (groupResult.getSecurityGroups().isEmpty()) {
groupResult = getSecurityGroupsBy("group-id", securityGroupSet, ec2);
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/hudson/plugins/ec2/ssh/EC2MacLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ protected void launchScript(EC2Computer computer, TaskListener listener)
conn.exec("mkdir -p " + tmpDir, logger);

if (initScript != null
&& initScript.trim().length() > 0
&& !initScript.trim().isEmpty()
&& conn.exec("test -e ~/.hudson-run-init", logger) != 0) {
logInfo(computer, listener, "Executing init script");
scp.put(initScript.getBytes("UTF-8"), "init.sh", tmpDir, "0700");
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/hudson/plugins/ec2/ssh/EC2UnixLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ protected void launchScript(EC2Computer computer, TaskListener listener)
conn.exec("mkdir -p " + tmpDir, logger);

if (initScript != null
&& initScript.trim().length() > 0
&& !initScript.trim().isEmpty()
&& conn.exec("test -e ~/.hudson-run-init", logger) != 0) {
logInfo(computer, listener, "Executing init script");
scp.put(initScript.getBytes("UTF-8"), "init.sh", tmpDir, "0700");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ HostKey getHostKeyFromConsole(
}

String line = getLineWithKey(logger, computer, serverHostKeyAlgorithm);
if (line != null && line.length() > 0) {
if (line != null && !line.isEmpty()) {
key = getKeyFromLine(logger, line, listener);
} else if (line != null) {
key = new HostKey(serverHostKeyAlgorithm, new byte[] {});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected void launchScript(EC2Computer computer, TaskListener listener)
return;
}

if (initScript != null && initScript.trim().length() > 0 && !connection.exists(tmpDir + ".jenkins-init")) {
if (initScript != null && !initScript.trim().isEmpty() && !connection.exists(tmpDir + ".jenkins-init")) {
logger.println("Executing init script");
try (OutputStream init = connection.putFile(tmpDir + "init.bat")) {
init.write(initScript.getBytes("utf-8"));
Expand Down

0 comments on commit ca0562e

Please sign in to comment.