Skip to content

Commit

Permalink
(chores) tooling: use more adequately sized buffers
Browse files Browse the repository at this point in the history
Signed-off-by: Otavio R. Piske <[email protected]>
  • Loading branch information
orpiske committed Aug 25, 2024
1 parent c2e6af4 commit 956c65b
Show file tree
Hide file tree
Showing 19 changed files with 28 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static String sanitizeDescription(String javadoc, boolean summary) {
}

// lets just use what java accepts as identifiers
StringBuilder sb = new StringBuilder();
StringBuilder sb = new StringBuilder(javadoc.length());

// split into lines
String[] lines = javadoc.split("\n");
Expand Down Expand Up @@ -101,7 +101,7 @@ public static String sanitizeDescription(String javadoc, boolean summary) {
s = s.replaceAll("\\{@\\w+\\s#?([\\w.#(\\d,)]+)}", "$1");

// create a new line
StringBuilder cb = new StringBuilder();
StringBuilder cb = new StringBuilder(s.length() + 16);
for (char c : s.toCharArray()) {
if (Character.isJavaIdentifierPart(c) || VALID_CHARS.indexOf(c) != -1) {
cb.append(c);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public static String between(String text, String after, String before) {
* @return as a title
*/
public static String asTitle(String name) {
StringBuilder sb = new StringBuilder();
StringBuilder sb = new StringBuilder(name.length() + 16);
for (char c : name.toCharArray()) {
boolean upper = Character.isUpperCase(c);
boolean first = sb.isEmpty();
Expand Down Expand Up @@ -170,7 +170,7 @@ public static String before(String text, String before) {
}

public static String indentCollection(String indent, Collection<String> list) {
StringBuilder sb = new StringBuilder();
StringBuilder sb = new StringBuilder(256);
for (String text : list) {
sb.append(indent).append(text);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public static String quote(String value) {
char c = value.charAt(i);
if (c == '"' || c == '\\') {
if (sb == null) {
sb = new StringBuilder();
sb = new StringBuilder(value.length() + 8);
sb.append("\"");
sb.append("\\");
sb.append(c);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public String toString() {
if (parameters.length == 0 && boundType == BoundType.Extends && clazz == Object.class) {
return "?";
}
StringBuilder sb = new StringBuilder();
StringBuilder sb = new StringBuilder(128);
if (boundType == BoundType.Extends) {
sb.append("? extends ");
} else if (boundType == BoundType.Super) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ public String printClass() {
}

public String printClass(boolean innerClassesLast) {
StringBuilder sb = new StringBuilder();
StringBuilder sb = new StringBuilder(4096);

Set<String> imports = new TreeSet<>(Comparator.comparing(JavaClass::importOrder));
imports.addAll(this.imports);
Expand Down Expand Up @@ -311,7 +311,7 @@ private void printClass(boolean innerClassesLast, StringBuilder sb, String inden

}

StringBuilder sb2 = new StringBuilder();
StringBuilder sb2 = new StringBuilder(4096);
sb2.append(indent);
if (isPublic) {
sb2.append("public ");
Expand Down Expand Up @@ -464,7 +464,7 @@ private void printMethod(StringBuilder sb, String indent, Method method) {
sb.append(" {");
}
} else {
StringBuilder sb2 = new StringBuilder();
StringBuilder sb2 = new StringBuilder(2048);
sb2.append(indent);
if (method.isPublic) {
sb2.append("public ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ private void generateMetaInfConfigurer(String fqn, String targetFqn, File resour
String pn = targetFqn.substring(0, pos);
String en = targetFqn.substring(pos + 1);

StringBuilder w = new StringBuilder();
StringBuilder w = new StringBuilder(256);
w.append("# ").append(GENERATED_MSG).append("\n");
w.append("class=").append(pn).append(".").append(en).append("Configurer").append("\n");
String fileName = "META-INF/services/org/apache/camel/configurer/" + fqn;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public String xmlEncode(String str) {
}

public String javadoc(String indent, String doc) {
StringBuilder sb = new StringBuilder();
StringBuilder sb = new StringBuilder(doc.length() * 2);
sb.append("/**\n");
int len = 78 - indent.length();
String rem = xmlEncode(doc);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ public String xmlEncode(String str) {
}

public String javadoc(String indent, String doc) {
StringBuilder sb = new StringBuilder();
StringBuilder sb = new StringBuilder(doc.length() * 2);
List<String> lines = formatJavadocOrCommentStringAsList(doc, indent);
if (!lines.isEmpty()) {
sb.append("/**\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -665,8 +665,9 @@ private boolean filterOutOption(ComponentModel component, BaseOptionModel option
}

public String getDocumentationWithNotes(BaseOptionModel option) {
StringBuilder sb = new StringBuilder();
sb.append(option.getDescription());
String description = option.getDescription();
StringBuilder sb = new StringBuilder(description.length() * 64);
sb.append(description);

if (!Strings.isNullOrEmpty(option.getDefaultValueNote())) {
if (sb.charAt(sb.length() - 1) != '.') {
Expand Down Expand Up @@ -1680,7 +1681,7 @@ protected void generatePropertyConfigurer(
}

protected void generateMetaInfConfigurer(String name, String fqn) {
StringBuilder w = new StringBuilder();
StringBuilder w = new StringBuilder(256);

w.append("# ").append(GENERATED_MSG).append("\n");
w.append("class=").append(fqn).append("\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public int prepareDataFormat() throws MojoExecutionException {

Map<String, String> javaTypes = new HashMap<>();

StringBuilder buffer = new StringBuilder();
StringBuilder buffer = new StringBuilder(256);
int count = 0;

File f = new File(project.getBasedir(), "target/classes");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public int prepareLanguage() throws MojoExecutionException {

Map<String, String> javaTypes = new HashMap<>();

StringBuilder buffer = new StringBuilder();
StringBuilder buffer = new StringBuilder(256);
int count = 0;

File f = new File(project.getBasedir(), "target/classes");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
}

if (!models.isEmpty()) {
StringBuilder sb = new StringBuilder();
StringBuilder sb = new StringBuilder(256);
sb.append("# ").append(GENERATED_MSG).append(NL);
for (String name : models) {
sb.append(name).append(NL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
}

private static String evaluateTemplate(final String templateName, final MainModel model) throws MojoExecutionException {
StringBuilder sb = new StringBuilder();
StringBuilder sb = new StringBuilder(256);

try (InputStream templateStream = UpdateReadmeMojo.class.getClassLoader().getResourceAsStream(templateName)) {
String template = PackageHelper.loadText(templateStream);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ public int hashCode() {
}

public String asString(String pad) {
StringBuilder sb = new StringBuilder();
StringBuilder sb = new StringBuilder(256);
sb.append(pad).append("<dependency>\n");
sb.append(pad).append(" <groupId>").append(groupId).append("</groupId>\n");
sb.append(pad).append(" <artifactId>").append(artifactId).append("</artifactId>\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static String dashToCamelCase(String text) {
return text;
}

StringBuilder sb = new StringBuilder();
StringBuilder sb = new StringBuilder(text.length());

for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
Expand All @@ -73,7 +73,7 @@ public static String camelCaseToDash(String text) {
return text;
}

StringBuilder answer = new StringBuilder();
StringBuilder answer = new StringBuilder(text.length());

Character prev = null;
Character next;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
}
for (String pval : pvals.split(",")) {
pval = sanitizeFileName(pval);
StringBuilder sb = new StringBuilder();
StringBuilder sb = new StringBuilder(256);
sb.append("# ").append(GENERATED_MSG).append(NL).append("class=").append(className).append(NL);
if (ServiceFactory.JDK_SERVICE.equals(sfa.value().asString())) {
updateResource(resourcesOutputDir.toPath(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ private boolean updateSensitiveHelperPatterns(File camelDir, Set<String> secrets
StringJoiner sb = new StringJoiner("\n");
boolean first = true;
for (String name : secrets) {
StringBuilder line = new StringBuilder();
StringBuilder line = new StringBuilder(name.length() + 32);
line.append(spaces52);
line.append("+ \"");
if (!first) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public static String extractJavaDoc(String sourceCode, MethodSource<?> ms) throw
String doc = sourceCode.substring(pos, pos + len);
LineNumberReader ln = new LineNumberReader(new StringReader(doc));
String line;
StringBuilder sb = new StringBuilder();
StringBuilder sb = new StringBuilder(256);
while ((line = ln.readLine()) != null) {
line = line.trim();
if (line.startsWith("/**") || line.startsWith("*/")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ public static Index readJandexIndexIgnoreMissing(MavenProject project, Log log)
}

public static String joinHeaderAndSource(String licenseHeader, String source) {
StringBuilder sb = new StringBuilder(licenseHeader);
StringBuilder sb = new StringBuilder(licenseHeader.length() + source.length() + 8);

sb.append(licenseHeader);
sb.append("\n");
sb.append(source);
return sb.toString();
Expand Down

0 comments on commit 956c65b

Please sign in to comment.