Optimize ExitStatus#addExitDescription

Optimize ExitStatus#addExitDescription by reducing string allocations
through:

- avoid string allocation when the current description is empty
- avoid string allocation when the given description is empty
- avoid intermediate string allocation by allocating the correct buffer size

Issue #3979
This commit is contained in:
Philippe Marschall
2021-08-23 17:23:28 +02:00
committed by Mahmoud Ben Hassine
parent 8c7fb5ca02
commit b38226e810

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2022 the original author or authors.
* Copyright 2006-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -246,18 +246,19 @@ public class ExitStatus implements Serializable, Comparable<ExitStatus> {
* description.
*/
public ExitStatus addExitDescription(String description) {
StringBuilder buffer = new StringBuilder();
boolean changed = StringUtils.hasText(description) && !exitDescription.equals(description);
if (StringUtils.hasText(exitDescription)) {
buffer.append(exitDescription);
if (changed) {
if (StringUtils.hasText(description) && !exitDescription.equals(description)) {
StringBuilder buffer = new StringBuilder(description.length() + 2 + exitDescription.length());
buffer.append(exitDescription);
buffer.append("; ");
buffer.append(description);
return new ExitStatus(exitCode, buffer.toString());
}
return this;
}
if (changed) {
buffer.append(description);
else {
return new ExitStatus(exitCode, description);
}
return new ExitStatus(exitCode, buffer.toString());
}
/**