Skip to content

Commit

Permalink
Reducing number of StringBuilder::toString conversions to improve eff…
Browse files Browse the repository at this point in the history
…iciency and readability
  • Loading branch information
mkarg committed Dec 1, 2024
1 parent 81af743 commit 97faa56
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 26 deletions.
16 changes: 8 additions & 8 deletions src/main/java/com/beust/jcommander/DefaultUsageFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public DefaultUsageFormatter(JCommander commander) {
public final void usage(String commandName) {
StringBuilder sb = new StringBuilder();
usage(commandName, sb);
commander.getConsole().print(sb.toString());
commander.getConsole().println(sb);
}

/**
Expand Down Expand Up @@ -153,7 +153,7 @@ public void appendMainLine(StringBuilder out, boolean hasOptions, boolean hasCom
if (commander.getMainParameter() != null && commander.getMainParameter().getDescription() != null) {
mainLine.append(" ").append(commander.getMainParameter().getDescription().getDescription());
}
wrapDescription(out, indentCount, mainLine.toString());
wrapDescription(out, indentCount, mainLine);
out.append('\n');
}

Expand Down Expand Up @@ -286,7 +286,7 @@ public void appendCommands(StringBuilder out, int indentCount, int descriptionIn
firstCommand = false;
}
JCommander.ProgramName progName = commands.getKey();
String dispName = progName.getDisplayName();
var dispName = progName.getDisplayName();
String commandDescription = Optional.ofNullable(getCommandDescription(progName.getName()))
.map(desc -> s(6) + desc)
.orElse("");
Expand Down Expand Up @@ -352,9 +352,9 @@ public String getCommandDescription(String commandName) {
* description}. If the first line needs to be indented prepend the
* correct number of spaces to {@code description}.
*/
public void wrapDescription(StringBuilder out, int indent, int currentLineIndent, String description) {
public void wrapDescription(StringBuilder out, int indent, int currentLineIndent, CharSequence description) {
int max = commander.getColumnSize();
String[] words = description.split(" ");
String[] words = description.toString().split(" ");
int current = currentLineIndent;

for (int i = 0; i < words.length; i++) {
Expand Down Expand Up @@ -385,7 +385,7 @@ public void wrapDescription(StringBuilder out, int indent, int currentLineIndent
* correct number of spaces to {@code description}.
* @see #wrapDescription(StringBuilder, int, int, String)
*/
public void wrapDescription(StringBuilder out, int indent, String description) {
public void wrapDescription(StringBuilder out, int indent, CharSequence description) {
wrapDescription(out, indent, 0, description);
}

Expand All @@ -404,13 +404,13 @@ public static String getI18nString(ResourceBundle bundle, String key, String def
*
* @return count-many spaces
*/
public static String s(int count) {
public static CharSequence s(int count) {
StringBuilder result = new StringBuilder();

for (int i = 0; i < count; i++) {
result.append(" ");
}
return result.toString();
return result;
}

/**
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/com/beust/jcommander/JCommander.java
Original file line number Diff line number Diff line change
Expand Up @@ -1096,7 +1096,7 @@ public void setProgramName(String name, String... aliases) {
public void usage() {
StringBuilder sb = new StringBuilder();
usageFormatter.usage(sb);
getConsole().print(sb.toString());
getConsole().print(sb);
}

/**
Expand All @@ -1105,7 +1105,7 @@ public void usage() {
public void usage(String commandName) {
StringBuilder sb = new StringBuilder();
usageFormatter.usage(commandName, sb);
getConsole().print(sb.toString());
getConsole().print(sb);
}

/**
Expand Down Expand Up @@ -1566,13 +1566,13 @@ public String getParsedAlias() {
/**
* @return n spaces
*/
private String s(int count) {
private CharSequence s(int count) {
StringBuilder result = new StringBuilder();
for (int i = 0; i < count; i++) {
result.append(" ");
}

return result.toString();
return result;
}

/**
Expand Down Expand Up @@ -1636,15 +1636,15 @@ public String getDisplayName() {
StringBuilder sb = new StringBuilder();
sb.append(name);
if (!aliases.isEmpty()) {
sb.append("(");
sb.append('(');
Iterator<String> aliasesIt = aliases.iterator();
while (aliasesIt.hasNext()) {
sb.append(aliasesIt.next());
if (aliasesIt.hasNext()) {
sb.append(",");
sb.append(',');
}
}
sb.append(")");
sb.append(')');
}
return sb.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ public Path convert(String value) {
try {
return Paths.get(value);
} catch (InvalidPathException e) {
String encoded = escapeUnprintable(value);
String encoded = escapeUnprintable(value).toString();
throw new ParameterException(getErrorString(encoded, "a path"));
}
}

private static String escapeUnprintable(String value) {
private static CharSequence escapeUnprintable(String value) {
StringBuilder bldr = new StringBuilder();
for (char c: value.toCharArray()) {
if (c < ' ') {
Expand All @@ -53,6 +53,6 @@ private static String escapeUnprintable(String value) {
bldr.append(c);
}
}
return bldr.toString();
return bldr;
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/beust/jcommander/internal/Console.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

public interface Console {

void print(String msg);
void print(CharSequence msg);

void println(String msg);
void println(CharSequence msg);

char[] readPassword(boolean echoInput);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ public DefaultConsole() {
this.target = System.out;
}

public void print(String msg) {
public void print(CharSequence msg) {
target.print(msg);
}

public void println(String msg) {
public void println(CharSequence msg) {
target.println(msg);
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/beust/jcommander/internal/JDK6Console.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ public JDK6Console(Object console) throws Exception {
writer = (PrintWriter) writerMethod.invoke(console);
}

public void print(String msg) {
public void print(CharSequence msg) {
writer.print(msg);
}

public void println(String msg) {
public void println(CharSequence msg) {
writer.println(msg);
}

Expand Down
4 changes: 2 additions & 2 deletions src/test/java/com/beust/jcommander/StringBuilderConsole.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ public StringBuilderConsole(StringBuilder output) {
}

@Override
public void print(String msg) {
public void print(CharSequence msg) {
output.append(msg);
}

@Override
public void println(String msg) {
public void println(CharSequence msg) {
print(msg);
output.append('\n');
}
Expand Down

0 comments on commit 97faa56

Please sign in to comment.