Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernize string operations #618

Merged
merged 3 commits into from
Jan 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,7 @@ public static void proceed(
@Override
public void expand(@NonNull EnvVars env) {
final LinkedHashMap<String, String> variables = new LinkedHashMap<>();
final String resourceNames =
lockedResources.keySet().stream().collect(Collectors.joining(","));
final String resourceNames = String.join(",", lockedResources.keySet());
variables.put(variable, resourceNames);
int index = 0;
for (Entry<String, List<LockableResourceProperty>> lockResourceEntry :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,9 +358,9 @@ public List<LockableResource> fromNames(final List<String> names, final boolean

// ---------------------------------------------------------------------------
private String getStack() {
StringBuffer buf = new StringBuffer();
StringBuilder buf = new StringBuilder();
for (StackTraceElement st : Thread.currentThread().getStackTrace()) {
buf.append("\n" + st);
buf.append("\n").append(st);
}
return buf.toString();
}
Expand Down Expand Up @@ -1213,7 +1213,7 @@ private List<LockableResource> getFreeResourcesWithLabel(
// ---------------------------------------------------------------------------
// for debug purpose
private String getCauses(List<LockableResource> resources) {
StringBuffer buf = new StringBuffer();
StringBuilder buf = new StringBuilder();
int currentSize = 0;
for (LockableResource resource : resources) {
String cause = resource.getLockCauseDetail();
Expand All @@ -1224,7 +1224,7 @@ private String getCauses(List<LockableResource> resources) {
buf.append("\n ...");
break;
}
buf.append("\n " + cause);
buf.append("\n ").append(cause);

final String queueCause = getQueueCause(resource);
if (!queueCause.isEmpty()) {
Expand All @@ -1237,7 +1237,7 @@ private String getCauses(List<LockableResource> resources) {
// ---------------------------------------------------------------------------
// for debug purpose
private String getQueueCause(final LockableResource resource) {
Map<Run<?, ?>, Integer> usage = new HashMap<Run<?, ?>, Integer>();
Map<Run<?, ?>, Integer> usage = new HashMap<>();

for (QueuedContextStruct entry : this.queuedContexts) {

Expand All @@ -1263,16 +1263,20 @@ private String getQueueCause(final LockableResource resource) {
usage.put(build, count);
}

StringBuffer buf = new StringBuffer();
StringBuilder buf = new StringBuilder();
int currentSize = 0;
for (Map.Entry<Run<?, ?>, Integer> entry : usage.entrySet()) {
Run<?, ?> build = entry.getKey();
int count = entry.getValue();

if (build != null && count > 0) {
currentSize++;
buf.append("\n Queued " + count + " time(s) by build " + " " + build.getFullDisplayName() + " "
+ ModelHyperlinkNote.encodeTo(build));
buf.append("\n Queued ")
.append(count)
.append(" time(s) by build ")
.append(build.getFullDisplayName())
.append(" ")
.append(ModelHyperlinkNote.encodeTo(build));

if (currentSize >= enabledCausesCount) {
buf.append("\n ...");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@
String label = Util.fixEmptyAndTrim(labelName);
String script = Util.fixEmptyAndTrim(resourceMatchScript);

if (number == null || number.equals("") || number.trim().equals("0")) {
if (number == null || number.isEmpty() || number.trim().equals("0")) {

Check warning on line 239 in src/main/java/org/jenkins/plugins/lockableresources/RequiredResourcesProperty.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 239 is only partially covered, 5 branches are missing
return FormValidation.ok();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,14 +279,14 @@ public int getAssignedResourceAmount(String labelString) {
// ---------------------------------------------------------------------------
private void informPerformanceIssue() {
String method = Thread.currentThread().getStackTrace()[2].getMethodName();
StringBuffer buf = new StringBuffer();
StringBuilder buf = new StringBuilder();
for (StackTraceElement st : Thread.currentThread().getStackTrace()) {
buf.append("\n" + st);
buf.append("\n").append(st);
}
LOGGER.warning("lockable-resources-plugin: The method "
+ method
+ " has been deprecated due performance issues. When you see this message, please inform plugin developers:"
+ buf.toString());
+ buf);
}

// ---------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ public void pressure(final int resourcesCount) throws Exception {
// create resources
LOGGER.info("Create resources with labels");
for (int i = 1; i <= resourcesCount; i++) {
lrm.createResourceWithLabel("resourceA_" + Integer.toString(i), "label1 label2");
lrm.createResourceWithLabel("resourceAA_" + Integer.toString(i), "label");
lrm.createResourceWithLabel("resourceAAA_" + Integer.toString(i), "label1");
lrm.createResourceWithLabel("resourceAAAA_" + Integer.toString(i), "(=%/!(/)?$/ HH( RU))");
lrm.createResourceWithLabel("resourceA_" + i, "label1 label2");
lrm.createResourceWithLabel("resourceAA_" + i, "label");
lrm.createResourceWithLabel("resourceAAA_" + i, "label1");
lrm.createResourceWithLabel("resourceAAAA_" + i, "(=%/!(/)?$/ HH( RU))");
}

// define groovy script used by our test jobs
Expand Down Expand Up @@ -137,14 +137,14 @@ public void pressure(final int resourcesCount) throws Exception {
// create more resources until the first job has been started
LOGGER.info("Create more resources");
for (int i = 1; i <= resourcesCount; i++) {
lrm.createResourceWithLabel("resourceB_" + Integer.toString(i), "label1");
lrm.createResourceWithLabel("resourceB_" + i, "label1");
}

// create jenkins nodes. All shall be mirrored to resources
LOGGER.info("Create jenkins nodes");
for (int i = 1; i <= nodesCount; i++) {
j.createSlave("AgentAAA_" + i, "label label1 label2", null);
lrm.createResourceWithLabel("resourceC_" + Integer.toString(i), "label1");
lrm.createResourceWithLabel("resourceC_" + i, "label1");
j.createSlave("AGENT_BBB_" + i, null, null);
}

Expand All @@ -156,7 +156,7 @@ public void pressure(final int resourcesCount) throws Exception {
// create more resources until the first job has been started
LOGGER.info("Additional resources");
for (int i = 1; i <= resourcesCount; i++) {
lrm.createResourceWithLabel("resourceD_" + Integer.toString(i), "label1");
lrm.createResourceWithLabel("resourceD_" + i, "label1");
}

// create more jenkins nodes to make more chaos
Expand Down