Skip to content

Commit

Permalink
Performance: String.isEmpty() is faster than .equals()
Browse files Browse the repository at this point in the history
  • Loading branch information
mkarg committed Oct 1, 2023
1 parent 8887053 commit 694dd56
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 11 deletions.
12 changes: 5 additions & 7 deletions src/main/java/com/beust/jcommander/ParameterDescription.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import com.beust.jcommander.validators.NoValidator;
import com.beust.jcommander.validators.NoValueValidator;

import static com.beust.jcommander.Strings.isStringEmpty;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.*;
Expand Down Expand Up @@ -74,26 +76,22 @@ private ResourceBundle findResourceBundle(Object o) {
ResourceBundle result = null;

Parameters p = o.getClass().getAnnotation(Parameters.class);
if (p != null && ! isEmpty(p.resourceBundle())) {
if (p != null && ! isStringEmpty(p.resourceBundle())) {
result = ResourceBundle.getBundle(p.resourceBundle(), Locale.getDefault());
} else {
com.beust.jcommander.ResourceBundle a = o.getClass().getAnnotation(
com.beust.jcommander.ResourceBundle.class);
if (a != null && ! isEmpty(a.value())) {
if (a != null && ! isStringEmpty(a.value())) {
result = ResourceBundle.getBundle(a.value(), Locale.getDefault());
}
}

return result;
}

private boolean isEmpty(String s) {
return s == null || "".equals(s);
}

private void initDescription(String description, String descriptionKey, String[] names) {
this.description = description;
if (! "".equals(descriptionKey)) {
if (! isStringEmpty(descriptionKey)) {
if (bundle != null) {
this.description = bundle.getString(descriptionKey);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/beust/jcommander/Strings.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
public class Strings {

public static boolean isStringEmpty(String s) {
return s == null || "".equals(s);
return s == null || s.isEmpty();
}

public static boolean startsWith(String s, String with, boolean isCaseSensitive) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ public void appendAllParametersDetails(StringBuilder out, int indentCount, Strin

if (pd.isDynamicParameter()) {
String syntax = "(syntax: " + parameter.names()[0] + "key" + parameter.getAssignment() + "value)";
description += (description.length() == 0 ? "" : " ") + syntax;
description += (description.isEmpty() ? "" : " ") + syntax;
}

if (def != null && !pd.isHelp()) {
String displayedDef = Strings.isStringEmpty(def.toString()) ? "<empty string>" : def.toString();
String defaultText = "(default: " + (parameter.password() ? "********" : displayedDef) + ")";
description += (description.length() == 0 ? "" : " ") + defaultText;
description += (description.isEmpty() ? "" : " ") + defaultText;
}
Class<?> type = pd.getParameterized().getType();

Expand All @@ -94,7 +94,7 @@ public void appendAllParametersDetails(StringBuilder out, int indentCount, Strin
// of an enum field is empty in ParameterDescription#init(..)
if (!description.contains("Options: " + valueList)) {
String possibleValues = "(values: " + valueList + ")";
description += (description.length() == 0 ? "" : " ") + possibleValues;
description += (description.isEmpty() ? "" : " ") + possibleValues;
}
}

Expand Down

0 comments on commit 694dd56

Please sign in to comment.