Use String#isEmpty where possible (#7690)

This commit is contained in:
Benoit Lacelle authored and GitHub committed 2023-04-08 08:04:47 -07:00
1 parent ff654d142e
commit c48728d0f4
40 files changed
+59 -59

No files matched your search

+1 -1
View File
@@ -144,7 +144,7 @@ public class EnvVars extends TreeMap<String, String> {
* Handles {@code PATH+XYZ} notation.
*/
public void override(String key, String value) {
if (value == null || value.length() == 0) {
if (value == null || value.isEmpty()) {
remove(key);
return;
}
+2 -2
View File
@@ -1655,7 +1655,7 @@ public class Functions {
* Converts "abc" to "Abc".
*/
public static String capitalize(String s) {
if (s == null || s.length() == 0) return s;
if (s == null || s.isEmpty()) return s;
return Character.toUpperCase(s.charAt(0)) + s.substring(1);
}
@@ -1867,7 +1867,7 @@ public class Functions {
public static String joinPath(String... components) {
StringBuilder buf = new StringBuilder();
for (String s : components) {
if (s.length() == 0) continue;
if (s.isEmpty()) continue;
if (buf.length() > 0) {
if (buf.charAt(buf.length() - 1) != '/')
+1 -1
View File
@@ -662,7 +662,7 @@ public abstract class PluginManager extends AbstractModelObject implements OnMas
for (String pluginPath : plugins) {
String fileName = pluginPath.substring(pluginPath.lastIndexOf('/') + 1);
if (fileName.length() == 0) {
if (fileName.isEmpty()) {
// see http://www.nabble.com/404-Not-Found-error-when-clicking-on-help-td24508544.html
// I suspect some containers are returning directory names.
continue;
@@ -222,7 +222,7 @@ public final class ProxyConfiguration extends AbstractDescribableImpl<ProxyConfi
List<Pattern> r = new ArrayList<>();
for (String s : noProxyHost.split("[ \t\n,|]+")) {
if (s.length() == 0) continue;
if (s.isEmpty()) continue;
r.add(Pattern.compile(s.replace(".", "\\.").replace("*", ".*")));
}
return r;
+2 -2
View File
@@ -1110,7 +1110,7 @@ public class Util {
*/
@CheckForNull
public static String fixEmpty(@CheckForNull String s) {
if (s == null || s.length() == 0) return null;
if (s == null || s.isEmpty()) return null;
return s;
}
@@ -1487,7 +1487,7 @@ public class Util {
*/
@CheckForNull
public static Number tryParseNumber(@CheckForNull String numberStr, @CheckForNull Number defaultNumber) {
if (numberStr == null || numberStr.length() == 0) {
if (numberStr == null || numberStr.isEmpty()) {
return defaultNumber;
}
try {
@@ -82,7 +82,7 @@ abstract class TaskMethodFinder<T extends Annotation> extends TaskBuilder {
protected String getDisplayNameOf(Method e, T i) {
Class<?> c = e.getDeclaringClass();
String key = displayNameOf(i);
if (key.length() == 0) return c.getSimpleName() + "." + e.getName();
if (key.isEmpty()) return c.getSimpleName() + "." + e.getName();
try {
ResourceBundleHolder rb = ResourceBundleHolder.get(
c.getClassLoader().loadClass(c.getPackage().getName() + ".Messages"));
@@ -312,27 +312,27 @@ public class LogRecorder extends AbstractModelObject implements Saveable {
public boolean includes(LogRecord r) {
if (r.getLevel().intValue() < level)
return false; // below the threshold
if (name.length() == 0) {
if (name.isEmpty()) {
return true; // like root logger, includes everything
}
String logName = r.getLoggerName();
if (logName == null || !logName.startsWith(name))
return false; // not within this logger
String rest = logName.substring(name.length());
return rest.startsWith(".") || rest.length() == 0;
return rest.startsWith(".") || rest.isEmpty();
}
@SuppressFBWarnings(value = "NP_BOOLEAN_RETURN_NULL", justification = "converting this to YesNoMaybe would break backward compatibility")
public Boolean matches(LogRecord r) {
boolean levelSufficient = r.getLevel().intValue() >= level;
if (name.length() == 0) {
if (name.isEmpty()) {
return levelSufficient; // include if level matches
}
String logName = r.getLoggerName();
if (logName == null || !logName.startsWith(name))
return null; // not in the domain of this logger
String rest = logName.substring(name.length());
if (rest.startsWith(".") || rest.length() == 0) {
if (rest.startsWith(".") || rest.isEmpty()) {
return levelSufficient; // include if level matches
}
return null;
@@ -210,7 +210,7 @@ public abstract class AbstractBuild<P extends AbstractProject<P, R>, R extends A
* null, for example if the agent that this build run no longer exists.
*/
public @CheckForNull Node getBuiltOn() {
if (builtOn == null || builtOn.equals(""))
if (builtOn == null || builtOn.isEmpty())
return Jenkins.get();
else
return Jenkins.get().getNode(builtOn);
@@ -479,7 +479,7 @@ public abstract class AbstractItem extends Actionable implements Item, HttpDelet
@Exported
public final String getFullName() {
String n = getParent().getFullName();
if (n.length() == 0) return getName();
if (n.isEmpty()) return getName();
else return n + '/' + getName();
}
@@ -487,7 +487,7 @@ public abstract class AbstractItem extends Actionable implements Item, HttpDelet
@Exported
public final String getFullDisplayName() {
String n = getParent().getFullDisplayName();
if (n.length() == 0) return getDisplayName();
if (n.isEmpty()) return getDisplayName();
else return n + " » " + getDisplayName();
}
@@ -683,7 +683,7 @@ public abstract class AbstractProject<P extends AbstractProject<P, R>, R extends
*/
public FormValidation doCheckRetryCount(@QueryParameter String value)throws IOException, ServletException {
// retry count is optional so this is ok
if (value == null || value.trim().equals(""))
if (value == null || value.trim().isEmpty())
return FormValidation.ok();
if (!value.matches("[0-9]*")) {
return FormValidation.error("Invalid retry count");
@@ -149,7 +149,7 @@ public class AutoCompletionCandidates implements HttpResponse {
}
private String contextualNameOf(Item i) {
if (prefix.endsWith("/") || prefix.length() == 0)
if (prefix.endsWith("/") || prefix.isEmpty())
return prefix + i.getName();
else
return prefix + '/' + i.getName();
@@ -440,8 +440,8 @@ public abstract class Descriptor<T extends Describable<T>> implements Saveable,
QueryParameter qp = p.annotation(QueryParameter.class);
if (qp != null) {
String name = qp.value();
if (name.length() == 0) name = p.name();
if (name == null || name.length() == 0)
if (name.isEmpty()) name = p.name();
if (name == null || name.isEmpty())
continue; // unknown parameter name. we'll report the error when the form is submitted.
RelativePath rp = p.annotation(RelativePath.class);
@@ -306,7 +306,7 @@ public final class DirectoryBrowserSupport implements HttpResponse {
return;
}
if (rest.length() == 0) {
if (rest.isEmpty()) {
// if the target page to be displayed is a directory and the path doesn't end with '/', redirect
StringBuffer reqUrl = req.getRequestURL();
if (reqUrl.charAt(reqUrl.length() - 1) != '/') {
@@ -408,7 +408,7 @@ public final class DirectoryBrowserSupport implements HttpResponse {
if (!ResourceDomainConfiguration.isResourceRequest(req)) {
// if we're serving this from the main domain, set CSP headers
String csp = SystemProperties.getString(CSP_PROPERTY_NAME, DEFAULT_CSP_VALUE);
if (!csp.trim().equals("")) {
if (!csp.trim().isEmpty()) {
// allow users to prevent sending this header by setting empty system property
for (String header : new String[]{"Content-Security-Policy", "X-WebKit-CSP", "X-Content-Security-Policy"}) {
rsp.setHeader(header, csp);
@@ -500,7 +500,7 @@ public final class DirectoryBrowserSupport implements HttpResponse {
private String getPath(StaplerRequest req) {
String path = req.getRestOfPath();
if (path.length() == 0)
if (path.isEmpty())
path = "/";
return path;
}
+3 -3
View File
@@ -255,17 +255,17 @@ public class Items {
Stack<String> name = new Stack<>();
for (int i = 0; i < c.length; i++) {
if (i == 0 && c[i].equals("")) continue;
if (i == 0 && c[i].isEmpty()) continue;
name.push(c[i]);
}
for (int i = 0; i < p.length; i++) {
if (i == 0 && p[i].equals("")) {
if (i == 0 && p[i].isEmpty()) {
// Absolute path starting with a "/"
name.clear();
continue;
}
if (p[i].equals("..")) {
if (name.size() == 0) {
if (name.isEmpty()) {
throw new IllegalArgumentException(String.format(
"Illegal relative path '%s' within context '%s'", path, context.getFullName()
));
+1 -1
View File
@@ -201,7 +201,7 @@ public abstract class Slave extends Node implements Serializable {
this.nodeProperties.replaceBy(nodeProperties);
if (name.equals(""))
if (name.isEmpty())
throw new FormException(Messages.Slave_InvalidConfig_NoName(), null);
if (this.numExecutors <= 0)
+1 -1
View File
@@ -1357,7 +1357,7 @@ public abstract class View extends AbstractModelObject implements AccessControll
if (owner.getView(name) != null)
throw new Failure(Messages.Hudson_ViewAlreadyExists(name));
if (mode == null || mode.length() == 0) {
if (mode == null || mode.isEmpty()) {
if (isXmlSubmission) {
View v = createViewFromXML(name, req.getInputStream());
owner.getACL().checkCreatePermission(owner, v.getDescriptor());
@@ -123,7 +123,7 @@ public final class CronTabList {
continue;
}
if (line.length() == 0 || line.startsWith("#"))
if (line.isEmpty() || line.startsWith("#"))
continue; // ignorable line
try {
r.add(new CronTab(line, lineNumber, hash, timezone));
+1 -1
View File
@@ -749,7 +749,7 @@ public abstract class SCM implements Describable<SCM>, ExtensionPoint {
protected final String nullify(String s) {
if (s == null) return null;
if (s.trim().length() == 0) return null;
if (s.trim().isEmpty()) return null;
return s;
}
@@ -420,7 +420,7 @@ public class HudsonPrivateSecurityRealm extends AbstractPasswordBasedSecurityRea
si.errors.put("captcha", Messages.HudsonPrivateSecurityRealm_CreateAccount_TextNotMatchWordInImage());
}
if (si.username == null || si.username.length() == 0) {
if (si.username == null || si.username.isEmpty()) {
si.errors.put("username", Messages.HudsonPrivateSecurityRealm_CreateAccount_UserNameRequired());
} else if (!containsOnlyAcceptableCharacters(si.username)) {
if (ID_REGEX == null) {
@@ -445,7 +445,7 @@ public class HudsonPrivateSecurityRealm extends AbstractPasswordBasedSecurityRea
si.errors.put("password1", Messages.HudsonPrivateSecurityRealm_CreateAccount_PasswordRequired());
}
if (si.fullname == null || si.fullname.length() == 0) {
if (si.fullname == null || si.fullname.isEmpty()) {
si.fullname = si.username;
}
@@ -79,7 +79,7 @@ public class CrumbFilter implements Filter {
private static String canonicalPath(String path) {
List<String> r = new ArrayList<>(Arrays.asList(path.split("/+")));
for (int i = 0; i < r.size(); ) {
if (r.get(i).length() == 0 || r.get(i).equals(".")) {
if (r.get(i).isEmpty() || r.get(i).equals(".")) {
// empty token occurs for example, "".split("/+") is [""]
r.remove(i);
} else
@@ -25,7 +25,7 @@ public abstract class AbstractCloudImpl extends Cloud {
}
protected void setInstanceCapStr(String value) {
if (value == null || value.equals(""))
if (value == null || value.isEmpty())
this.instanceCap = Integer.MAX_VALUE;
else
this.instanceCap = Integer.parseInt(value);
@@ -236,7 +236,7 @@ public class ArtifactArchiver extends Recorder implements SimpleBuildStep {
@Override
public void perform(Run<?, ?> build, FilePath ws, EnvVars environment, Launcher launcher, TaskListener listener) throws IOException, InterruptedException {
if (artifacts.length() == 0) {
if (artifacts.isEmpty()) {
throw new AbortException(Messages.ArtifactArchiver_NoIncludes());
}
+1 -1
View File
@@ -560,7 +560,7 @@ public class Maven extends Builder {
// olamy : but will add a dependency on maven in core -> so not so good
String mavenVersion = launcher.getChannel().call(new GetMavenVersion(getHome()));
if (!mavenVersion.equals("")) {
if (!mavenVersion.isEmpty()) {
if (mavenReqVersion == MAVEN_20) {
if (mavenVersion.startsWith("2."))
return true;
@@ -297,7 +297,7 @@ public class ArgumentListBuilder implements Serializable, Cloneable {
for (String arg : args) {
if (buf.length() > 0) buf.append(' ');
if (arg.indexOf(' ') >= 0 || arg.length() == 0)
if (arg.indexOf(' ') >= 0 || arg.isEmpty())
buf.append('"').append(arg).append('"');
else
buf.append(arg);
@@ -439,7 +439,7 @@ public class ArgumentListBuilder implements Serializable, Cloneable {
if (buf.length() > 0) buf.append(' ');
if (arg.indexOf(' ') >= 0 || arg.length() == 0)
if (arg.indexOf(' ') >= 0 || arg.isEmpty())
buf.append('"').append(arg).append('"');
else
buf.append(arg);
@@ -617,7 +617,7 @@ public abstract class FormFieldValidator {
}
}
v = v.trim();
if (!allowEmpty && v.length() == 0) {
if (!allowEmpty && v.isEmpty()) {
fail();
return;
}
@@ -449,7 +449,7 @@ public abstract class FormValidation extends IOException implements HttpResponse
return error(errorMessage);
}
v = v.trim();
if (!allowEmpty && v.length() == 0)
if (!allowEmpty && v.isEmpty())
return error(errorMessage);
Base64.getDecoder().decode(v.getBytes(StandardCharsets.UTF_8));
@@ -655,8 +655,8 @@ public abstract class FormValidation extends IOException implements HttpResponse
QueryParameter qp = p.annotation(QueryParameter.class);
if (qp != null) {
String name = qp.value();
if (name.length() == 0) name = p.name();
if (name == null || name.length() == 0)
if (name.isEmpty()) name = p.name();
if (name == null || name.isEmpty())
continue; // unknown parameter name. we'll report the error when the form is submitted.
if (name.equals("value"))
continue; // 'value' parameter is implicit
@@ -32,7 +32,7 @@ public class LineEndingConversion {
* String updated with the new line endings or null if given null.
*/
public static String convertEOL(String input, EOLType type) {
if (null == input || 0 == input.length()) {
if (null == input || input.isEmpty()) {
return input;
}
// Convert line endings to Unix LF,
@@ -56,7 +56,7 @@ public class QueryParameterMap {
* String that looks like {@code abc=def&ghi=jkl}
*/
public QueryParameterMap(String queryString) {
if (queryString == null || queryString.length() == 0) return;
if (queryString == null || queryString.isEmpty()) return;
for (String param : queryString.split("&")) {
String[] kv = param.split("=");
String key = URLDecoder.decode(kv[0], StandardCharsets.UTF_8);
@@ -170,7 +170,7 @@ public class SecretRewriter {
if (child.isDirectory()) {
if (!isIgnoredDir(child))
rewritten += rewriteRecursive(child,
relative.length() == 0 ? cn : relative + '/' + cn,
relative.isEmpty() ? cn : relative + '/' + cn,
listener);
}
}
+2 -2
View File
@@ -57,7 +57,7 @@ public class Service {
String line;
while ((line = configFile.readLine()) != null) {
line = line.trim();
if (line.startsWith("#") || line.length() == 0) continue;
if (line.startsWith("#") || line.isEmpty()) continue;
try {
Class<?> t = classLoader.loadClass(line);
@@ -89,7 +89,7 @@ public class Service {
if (line.startsWith("#"))
continue; // comment line
line = line.trim();
if (line.length() == 0)
if (line.isEmpty())
continue; // empty line. ignore.
try {
@@ -52,8 +52,8 @@ public class RegistryKey implements AutoCloseable {
}
private static String combine(String a, String b) {
if (a.length() == 0) return b;
if (b.length() == 0) return a;
if (a.isEmpty()) return b;
if (b.isEmpty()) return a;
return a + '\\' + b;
}
@@ -4229,7 +4229,7 @@ public class Jenkins extends AbstractCIBase implements DirectlyModifiableTopLeve
* if the given name is not good
*/
public static void checkGoodName(String name) throws Failure {
if (name == null || name.length() == 0)
if (name == null || name.isEmpty())
throw new Failure(Messages.Hudson_NoName());
if (".".equals(name.trim()))
@@ -99,7 +99,7 @@ public class InetAddressValidator implements Serializable {
// verify that address subgroups are legal
for (String ipSegment : groups) {
if (ipSegment == null || ipSegment.length() == 0) {
if (ipSegment == null || ipSegment.isEmpty()) {
return false;
}
@@ -186,7 +186,7 @@ public class InetAddressValidator implements Serializable {
int emptyOctets = 0; // consecutive empty chunks
for (int index = 0; index < octets.length; index++) {
String octet = octets[index];
if (octet.length() == 0) {
if (octet.isEmpty()) {
emptyOctets++;
if (emptyOctets > 1) {
return false;
@@ -131,7 +131,7 @@ public class RegexValidator implements Serializable {
patterns = new Pattern[regexs.length];
int flags = caseSensitive ? 0 : Pattern.CASE_INSENSITIVE;
for (int i = 0; i < regexs.length; i++) {
if (regexs[i] == null || regexs[i].length() == 0) {
if (regexs[i] == null || regexs[i].isEmpty()) {
throw new IllegalArgumentException("Regular expression[" + i + "] is missing");
}
patterns[i] = Pattern.compile(regexs[i], flags);
@@ -66,7 +66,7 @@ public class StaplerDispatches extends Telemetry {
@Override
public JSONObject createContent() {
if (traces.size() == 0) {
if (traces.isEmpty()) {
return null;
}
Map<String, Object> info = new TreeMap<>();
@@ -82,7 +82,7 @@ public class UserLanguages extends Telemetry {
@Override
public JSONObject createContent() {
if (requestsByLanguage.size() == 0) {
if (requestsByLanguage.isEmpty()) {
return null;
}
Map<String, AtomicLong> currentRequests = new TreeMap<>(requestsByLanguage);
@@ -37,7 +37,7 @@ public class TreeStringBuilder {
* if any.
*/
public Child intern(final String s) {
if (s.length() == 0) {
if (s.isEmpty()) {
return this;
}
@@ -1096,7 +1096,7 @@ public abstract class VirtualFile implements Comparable<VirtualFile>, Serializab
@Override
@Restricted(NoExternalUse.class)
public boolean isDescendant(String potentialChildRelativePath) throws IOException {
if (potentialChildRelativePath.equals("") && cacheDescendant) {
if (potentialChildRelativePath.isEmpty() && cacheDescendant) {
return true;
}
@@ -181,7 +181,7 @@ public class HistoryPageFilter<T> {
break;
}
}
if (itemsToAdd.size() == 0) {
if (itemsToAdd.isEmpty()) {
// All builds are older than newerThan ?
hasDownPage = true;
} else {