Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use isEmpty() where possible #559

Merged
merged 1 commit into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions core/src/main/java/org/kohsuke/stapler/AncestorImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public String getUrl() {
public String getRestOfUrl() {
StringBuilder buf = new StringBuilder();
for (int i = index; i < tokens.length; i++) {
if (buf.length() > 0) {
if (!buf.isEmpty()) {
buf.append('/');
}
buf.append(tokens[i]);
Expand Down Expand Up @@ -104,12 +104,12 @@ public String getFullUrl() {
public String getRelativePath() {
StringBuilder buf = new StringBuilder();
for (int i = index + (endsWithSlash ? 0 : 1); i < tokens.length; i++) {
if (buf.length() > 0) {
if (!buf.isEmpty()) {
buf.append('/');
}
buf.append("..");
}
if (buf.length() == 0) {
if (buf.isEmpty()) {
buf.append('.');
}
return buf.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ private void visitLocalVariable(String name, int index) {
}

private String getResult() {
return result.length() != 0 ? result.substring(1) : "";
return !result.isEmpty() ? result.substring(1) : "";
}

private boolean isDebugInfoPresent() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ public String[] loadConstructorParamNames() {
s.close();

String v = p.getProperty("constructor");
if (v.length() == 0) {
if (v.isEmpty()) {
return new String[0];
}
return v.split(",");
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/kohsuke/stapler/Header.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class HandlerImpl extends AnnotationHandler<Header> {
public Object parse(StaplerRequest request, Header a, Class type, String parameterName)
throws ServletException {
String name = a.value();
if (name.length() == 0) {
if (name.isEmpty()) {
name = parameterName;
}
if (name == null) {
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/kohsuke/stapler/MetaClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ private void registerDoToken(KlassDescriptor<?> node) {

for (String name : names) {
final Function ff = f.contextualize(new WebMethodContext(name));
if (name.length() == 0) {
if (name.isEmpty()) {
dispatchers.add(new IndexDispatcher(ff));
} else {
final boolean isAccepted = filteredFunctions.contains(f);
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/org/kohsuke/stapler/QueryParameter.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class HandlerImpl extends AnnotationHandler<QueryParameter> {
public Object parse(StaplerRequest request, QueryParameter a, Class type, String parameterName)
throws ServletException {
String name = a.value();
if (name.length() == 0) {
if (name.isEmpty()) {
name = parameterName;
}
if (name == null) {
Expand All @@ -74,7 +74,7 @@ public Object parse(StaplerRequest request, QueryParameter a, Class type, String
if (a.required() && value == null) {
throw new ServletException("Required Query parameter " + name + " is missing");
}
if (a.fixEmpty() && value != null && value.length() == 0) {
if (a.fixEmpty() && value != null && value.isEmpty()) {
value = null;
}
return convert(type, value);
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/kohsuke/stapler/RequestImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -1234,7 +1234,7 @@ public JSONObject getSubmittedForm() throws ServletException {
isSubmission = !getParameterMap().isEmpty();
}

if (p == null || p.length() == 0) {
if (p == null || p.isEmpty()) {
// no data submitted
try {
StaplerResponse rsp = Stapler.getCurrentResponse();
Expand Down
8 changes: 4 additions & 4 deletions core/src/main/java/org/kohsuke/stapler/Stapler.java
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ protected void service(HttpServletRequest req, HttpServletResponse rsp) throws S
}

String lowerPath = servletPath.toLowerCase(Locale.ENGLISH);
if (servletPath.length() != 0 && !lowerPath.startsWith("/web-inf") && !lowerPath.startsWith("/meta-inf")) {
if (!servletPath.isEmpty() && !lowerPath.startsWith("/web-inf") && !lowerPath.startsWith("/meta-inf")) {
// getResource requires '/' prefix (and resin insists on that, too) but servletPath can be empty string
// (JENKINS-879)
// so make sure servletPath is at least length 1 before calling getResource()
Expand Down Expand Up @@ -634,7 +634,7 @@ boolean serveStaticResource(
Matcher m = RANGE_SPEC.matcher(range);
if (m.matches()) {
long s = Long.parseLong(m.group(1));
long e = m.group(2).length() > 0
long e = !m.group(2).isEmpty()
? Long.parseLong(m.group(2)) + 1 // range set is inclusive
: contentLength; // unspecified value means "all the way to the end"
e = Math.min(e, contentLength);
Expand Down Expand Up @@ -1129,7 +1129,7 @@ public static Stapler getCurrent() {
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 if (r.get(i).equals("..")) {
Expand Down Expand Up @@ -1158,7 +1158,7 @@ static String canonicalPath(String path) {
buf.append(token);
}
// translation: if (path.endsWith("/") && !buf.endsWith("/"))
if (path.endsWith("/") && (buf.length() == 0 || buf.charAt(buf.length() - 1) != '/')) {
if (path.endsWith("/") && (buf.isEmpty() || buf.charAt(buf.length() - 1) != '/')) {
buf.append('/');
}
return buf.toString();
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/kohsuke/stapler/TokenList.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public int countRemainingTokens() {
public String toString() {
StringBuilder buf = new StringBuilder();
for (int i = 0; i < tokens.length; i++) {
if (buf.length() > 0) {
if (!buf.isEmpty()) {
buf.append('/');
}
if (i == idx) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ private String adjustName() {
}

/*package*/ static String makeXmlName(String name) {
if (name.length() == 0) {
if (name.isEmpty()) {
name = "_";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private void write(ExecutableElement c) {
try {
StringBuilder buf = new StringBuilder();
for (VariableElement p : c.getParameters()) {
if (buf.length() > 0) {
if (!buf.isEmpty()) {
buf.append(',');
}
buf.append(p.getSimpleName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public SourceVersion getSupportedSourceVersion() {
private void write(ExecutableElement m) throws IOException {
StringBuilder buf = new StringBuilder();
for (VariableElement p : m.getParameters()) {
if (buf.length() > 0) {
if (!buf.isEmpty()) {
buf.append(',');
}
buf.append(p.getSimpleName());
Expand Down
2 changes: 1 addition & 1 deletion core/src/test/java/org/kohsuke/stapler/StaplerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void testToFileOnWindows() throws Exception {
new URL("file:/c:/" + path));

assertToFile(
path.length() == 0 ? "\\\\vboxsvr" : "\\\\vboxsvr\\" + path.replace("%20", " "),
path.isEmpty() ? "\\\\vboxsvr" : "\\\\vboxsvr\\" + path.replace("%20", " "),
new URL("file://vboxsvr/" + path),
new URL("file:////vboxsvr/" + path));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ public void run(JellyContext context, XMLOutput output) throws JellyTagException
* a jelly tag that needs evaluation?
*/
private boolean isTag(QName name) {
return name.getNamespaceURI().length() > 0;
return !name.getNamespaceURI().isEmpty();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public Adjunct get(String name) throws IOException {
*/
public void doDynamic(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
String path = req.getRestOfPath();
if (path.length() == 0) {
if (path.isEmpty()) {
throw HttpResponses.error(
HttpServletResponse.SC_NOT_FOUND, new IllegalArgumentException("No adjunct provided"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public InternationalizedStringExpression(ResourceBundle resourceBundle, String t
List<Expression> args = new ArrayList<>();
key = text.substring(0, idx);
text = text.substring(idx + 1); // at this point text="arg,arg)"
while (text.length() > 0) {
while (!text.isEmpty()) {
String token = tokenize(text);
args.add(JellyClassLoaderTearOff.EXPRESSION_FACTORY.createExpression(token));
text = text.substring(token.length() + 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public String getFormatString(Locale locale, String key) {
for (int i = 0; i < suffixes.length; i++) {
String suffix = suffixes[i];
String msg = get(suffix).getProperty(key);
if (msg != null && msg.length() > 0) {
if (msg != null && !msg.isEmpty()) {
// ignore a definition without value, because stapler:i18n generates
// value-less definitions
return msg;
Expand All @@ -111,7 +111,7 @@ public String getFormatString(Locale locale, String key) {
public String getFormatStringWithoutDefaulting(Locale locale, String key) {
for (String s : toStrings(locale)) {
String msg = get(s).getProperty(key);
if (msg != null && msg.length() > 0) {
if (msg != null && !msg.isEmpty()) {
return msg;
}
}
Expand Down Expand Up @@ -161,7 +161,7 @@ protected Properties get(String key) {
throw new UncheckedIOException("Failed to load " + url + ": " + e, e);
}

resources.put(key, wrapUp(key.length() > 0 ? key.substring(1) : "", props));
resources.put(key, wrapUp(!key.isEmpty() ? key.substring(1) : "", props));
return props;
}

Expand Down