From 35b2b1aee57c4fd7e50662c800795dcb875fb835 Mon Sep 17 00:00:00 2001 From: Markus KARG Date: Sun, 1 Dec 2024 14:47:05 +0000 Subject: [PATCH] Reducing number of StringBuilder::toString conversions to improve efficiency and readability --- .../beust/jcommander/DefaultUsageFormatter.java | 16 ++++++++-------- .../java/com/beust/jcommander/JCommander.java | 14 +++++++------- .../beust/jcommander/ParameterDescription.java | 4 ++-- .../jcommander/converters/PathConverter.java | 6 +++--- .../com/beust/jcommander/internal/Console.java | 4 ++-- .../jcommander/internal/DefaultConsole.java | 4 ++-- .../beust/jcommander/internal/JDK6Console.java | 4 ++-- 7 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/main/java/com/beust/jcommander/DefaultUsageFormatter.java b/src/main/java/com/beust/jcommander/DefaultUsageFormatter.java index 1d564d679..1a463c3f2 100644 --- a/src/main/java/com/beust/jcommander/DefaultUsageFormatter.java +++ b/src/main/java/com/beust/jcommander/DefaultUsageFormatter.java @@ -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); } /** @@ -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'); } @@ -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(""); @@ -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++) { @@ -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); } @@ -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; } /** diff --git a/src/main/java/com/beust/jcommander/JCommander.java b/src/main/java/com/beust/jcommander/JCommander.java index d67404c44..e482d008b 100644 --- a/src/main/java/com/beust/jcommander/JCommander.java +++ b/src/main/java/com/beust/jcommander/JCommander.java @@ -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); } /** @@ -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); } /** @@ -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; } /** @@ -1636,15 +1636,15 @@ public String getDisplayName() { StringBuilder sb = new StringBuilder(); sb.append(name); if (!aliases.isEmpty()) { - sb.append("("); + sb.append('('); Iterator aliasesIt = aliases.iterator(); while (aliasesIt.hasNext()) { sb.append(aliasesIt.next()); if (aliasesIt.hasNext()) { - sb.append(","); + sb.append(','); } } - sb.append(")"); + sb.append(')'); } return sb.toString(); } diff --git a/src/main/java/com/beust/jcommander/ParameterDescription.java b/src/main/java/com/beust/jcommander/ParameterDescription.java index fdba2de90..1067f6948 100644 --- a/src/main/java/com/beust/jcommander/ParameterDescription.java +++ b/src/main/java/com/beust/jcommander/ParameterDescription.java @@ -185,14 +185,14 @@ public Object getObject() { return object; } - public String getNames() { + public CharSequence getNames() { StringBuilder sb = new StringBuilder(); String[] names = wrappedParameter.names(); for (int i = 0; i < names.length; i++) { if (i > 0) sb.append(", "); sb.append(names[i]); } - return sb.toString(); + return sb; } public String getCategory() { diff --git a/src/main/java/com/beust/jcommander/converters/PathConverter.java b/src/main/java/com/beust/jcommander/converters/PathConverter.java index 92a1072ca..b6d34d2bb 100644 --- a/src/main/java/com/beust/jcommander/converters/PathConverter.java +++ b/src/main/java/com/beust/jcommander/converters/PathConverter.java @@ -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 < ' ') { @@ -53,6 +53,6 @@ private static String escapeUnprintable(String value) { bldr.append(c); } } - return bldr.toString(); + return bldr; } } diff --git a/src/main/java/com/beust/jcommander/internal/Console.java b/src/main/java/com/beust/jcommander/internal/Console.java index 95eafe16d..b91ce04a3 100644 --- a/src/main/java/com/beust/jcommander/internal/Console.java +++ b/src/main/java/com/beust/jcommander/internal/Console.java @@ -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); } diff --git a/src/main/java/com/beust/jcommander/internal/DefaultConsole.java b/src/main/java/com/beust/jcommander/internal/DefaultConsole.java index cc234f410..ecf33e640 100644 --- a/src/main/java/com/beust/jcommander/internal/DefaultConsole.java +++ b/src/main/java/com/beust/jcommander/internal/DefaultConsole.java @@ -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); } diff --git a/src/main/java/com/beust/jcommander/internal/JDK6Console.java b/src/main/java/com/beust/jcommander/internal/JDK6Console.java index 507a57559..08ee14f4f 100644 --- a/src/main/java/com/beust/jcommander/internal/JDK6Console.java +++ b/src/main/java/com/beust/jcommander/internal/JDK6Console.java @@ -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); }