Skip to content

Commit

Permalink
Use Java 17 language features
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkEWaite committed Oct 24, 2024
1 parent 97cd890 commit d727a2e
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 36 deletions.
4 changes: 2 additions & 2 deletions src/main/java/jenkins/advancedqueue/JobGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ public int getPriority() {
@Deprecated
@CheckForNull
public String getView() {
if (jobGroupStrategy instanceof ViewBasedJobInclusionStrategy) {
return ((ViewBasedJobInclusionStrategy) jobGroupStrategy).getViewName();
if (jobGroupStrategy instanceof ViewBasedJobInclusionStrategy strategy) {
return strategy.getViewName();

Check warning on line 171 in src/main/java/jenkins/advancedqueue/JobGroup.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 170-171 are not covered by tests
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,8 @@ public JobGroup getJobGroup(@NonNull PriorityConfigurationCallback priorityCallb
}

private boolean isJobInView(Job<?, ?> job, View view) {
if (view instanceof ViewGroup) {
return isJobInViewGroup(job, (ViewGroup) view);
if (view instanceof ViewGroup group) {
return isJobInViewGroup(job, group);

Check warning on line 275 in src/main/java/jenkins/advancedqueue/PriorityConfiguration.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 274-275 are not covered by tests
} else {
return view.contains((TopLevelItem) job);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ PriorityConfigurationCallback getPriority(
@NonNull ExecutorStepExecution.PlaceholderTask task,
@NonNull PriorityConfigurationCallback priorityCallback) {
Queue.Task ownerTask = task.getOwnerTask();
if (ownerTask instanceof Job<?, ?>) {
Job<?, ?> job = (Job<?, ?>) ownerTask;
if (ownerTask instanceof Job<?, ?> job) {

Check warning on line 28 in src/main/java/jenkins/advancedqueue/PriorityConfigurationPlaceholderTaskHelper.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 28 is not covered by tests
ItemInfo itemInfo = QueueItemCache.get().getItem(job.getName());
if (itemInfo != null) {
priorityCallback.setPrioritySelection(itemInfo.getPriority());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,12 @@ public static String getJobGroupName(DecisionLogger decisionLogger, Job<?, ?> jo
ItemGroup<?> parent = job.getParent();
decisionLogger.addDecisionLog(2, "Checking for Cloudbees Folder inclusion ...");
while (parent != null) {
if (parent instanceof AbstractFolder) {
AbstractFolder folder = (AbstractFolder) parent;
if (parent instanceof AbstractFolder<?> folder) {
decisionLogger.addDecisionLog(3, "Evaluating Folder [" + folder.getFullName() + "] ...");
DescribableList<AbstractFolderProperty<?>, AbstractFolderPropertyDescriptor> properties =
folder.getProperties();
for (AbstractFolderProperty<?> property : properties) {
if (property instanceof JobInclusionFolderProperty) {
JobInclusionFolderProperty incProperty = (JobInclusionFolderProperty) property;
if (property instanceof JobInclusionFolderProperty incProperty) {
if (incProperty.isUseJobGroup()) {
String name = incProperty.getJobGroupName();
decisionLogger.addDecisionLog(4, "JobGroup is enabled, with JobGroup [" + name + "] ...");
Expand All @@ -60,8 +58,8 @@ public static String getJobGroupName(DecisionLogger decisionLogger, Job<?, ?> jo
}
}
}
if (parent instanceof TopLevelItem) {
parent = ((TopLevelItem) parent).getParent();
if (parent instanceof TopLevelItem item) {
parent = item.getParent();

Check warning on line 62 in src/main/java/jenkins/advancedqueue/jobinclusion/strategy/FolderPropertyLoader.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 47-62 are not covered by tests
} else {
parent = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ public static ListBoxModel getPropertyBasesJobGroups() {
ListBoxModel strategies = new ListBoxModel();
for (JobGroup jobGroup : jobGroups) {
JobInclusionStrategy inclusionStrategy = jobGroup.getJobGroupStrategy();
if (inclusionStrategy instanceof PropertyBasedJobInclusionStrategy) {
strategies.add(((PropertyBasedJobInclusionStrategy) inclusionStrategy).getName());
if (inclusionStrategy instanceof PropertyBasedJobInclusionStrategy strategy) {
strategies.add(strategy.getName());

Check warning on line 117 in src/main/java/jenkins/advancedqueue/jobinclusion/strategy/PropertyBasedJobInclusionStrategy.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 116-117 are not covered by tests
}
}
return strategies;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ public ListBoxModel getListViewItems() {
private void addViews(String parent, ListBoxModel items, Collection<View> views) {
for (View view : views) {
items.add(parent + view.getDisplayName(), parent + view.getViewName());
if (view instanceof ViewGroup) {
addViews(parent + view.getDisplayName() + "/", items, ((ViewGroup) view).getViews());
if (view instanceof ViewGroup group) {
addViews(parent + view.getDisplayName() + "/", items, group.getViews());

Check warning on line 72 in src/main/java/jenkins/advancedqueue/jobinclusion/strategy/ViewBasedJobInclusionStrategy.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 71-72 are not covered by tests
}
}
}
Expand Down Expand Up @@ -203,8 +203,8 @@ private boolean isJobInView(Job<?, ?> job, View view) {
return true;
}
// Then try to iterate over the ViewGroup (Nested View)
if (view instanceof ViewGroup) {
return isJobInViewGroup(job, (ViewGroup) view);
if (view instanceof ViewGroup group) {
return isJobInViewGroup(job, group);
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ public JobPropertyStrategy() {}

@CheckForNull
private Integer getPriorityInternal(Queue.Item item) {
if (item.task instanceof Job<?, ?>) {
Job<?, ?> job = (Job<?, ?>) item.task;
if (item.task instanceof Job<?, ?> job) {

Check warning on line 54 in src/main/java/jenkins/advancedqueue/priority/strategy/JobPropertyStrategy.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 54 is not covered by tests
PriorityJobProperty priorityProperty = job.getProperty(PriorityJobProperty.class);
if (priorityProperty != null && priorityProperty.getUseJobPriority()) {
return priorityProperty.priority;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,21 @@ public void sortNotWaitingItems(List<? extends Queue.NotWaitingItem> items) {
//
if (items.size() > 0 && LOGGER.isLoggable(Level.FINER)) {
StringBuilder queueStr = new StringBuilder(items.get(0).getClass().getName());
queueStr.append(" Queue:\n"
+ "+----------------------------------------------------------------------+\n"
+ "| Item Id | Job Name | Priority | Weight |\n"
+ "+----------------------------------------------------------------------+\n");
queueStr.append(
"""
Queue:
+----------------------------------------------------------------------+
| Item Id | Job Name | Priority | Weight |
+----------------------------------------------------------------------+
""");
for (Queue.NotWaitingItem item : items) {
ItemInfo itemInfo = QueueItemCache.get().getItem(item.getId());
String jobName = itemInfo.getJobName();
if (jobName.length() > 21) {
jobName = jobName.substring(0, 9) + "..." + jobName.substring(jobName.length() - 9);
}
queueStr.append(String.format(
"| %10d | %20s | %8d | %20.5f |%n",
item.getId(), jobName, itemInfo.getPriority(), itemInfo.getWeight()));
queueStr.append("| %10d | %20s | %8d | %20.5f |%n"
.formatted(item.getId(), jobName, itemInfo.getPriority(), itemInfo.getWeight()));

Check warning on line 109 in src/main/java/jenkins/advancedqueue/sorter/AdvancedQueueSorter.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 95-109 are not covered by tests
}
queueStr.append("+----------------------------------------------------------------------+");
LOGGER.log(Level.FINER, queueStr.toString());
Expand Down
10 changes: 4 additions & 6 deletions src/main/java/jenkins/advancedqueue/sorter/ItemInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public PriorityConfigurationCallback setPrioritySelection(
}

public PriorityConfigurationCallback addDecisionLog(int indent, String log) {
this.decisionLog.add(String.format("%" + ((indent + 1) * 2) + "s%s", "", log));
this.decisionLog.add(("%" + ((indent + 1) * 2) + "s%s").formatted("", log));
return this;
}

Expand Down Expand Up @@ -162,8 +162,7 @@ public int compareTo(ItemInfo o) {

@Override
public boolean equals(Object obj) {
if (obj instanceof ItemInfo) {
ItemInfo itemInfo = (ItemInfo) obj;
if (obj instanceof ItemInfo itemInfo) {

Check warning on line 165 in src/main/java/jenkins/advancedqueue/sorter/ItemInfo.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 165 is not covered by tests
return compareTo(itemInfo) == 0;
}
return false;
Expand All @@ -190,9 +189,8 @@ public String toString() {
if (priorityStrategy != null) {
reason = priorityStrategy.getDescriptor().getDisplayName();
}
return String.format(
"Id: %s, JobName: %s, jobGroupId: %s, reason: %s, priority: %s, weight: %s, status: %s",
itemId, jobName, jobGroupId, reason, priority, weight, itemStatus);
return "Id: %s, JobName: %s, jobGroupId: %s, reason: %s, priority: %s, weight: %s, status: %s"
.formatted(itemId, jobName, jobGroupId, reason, priority, weight, itemStatus);
}

public String getDescisionLog() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ private void maintainCache() {
final PendingItem pi = it.next();
final Executable e = pi.workUnit.getExecutable();

if (e instanceof Run) {
startedItems.put(new StartedItem(pi.itemInfo.getJobName(), ((Run<?, ?>) e).getNumber()), pi.itemInfo);
if (e instanceof Run<?, ?> run) {
startedItems.put(new StartedItem(pi.itemInfo.getJobName(), run.getNumber()), pi.itemInfo);
it.remove();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ private void assertIncreasingWeight(float initialWeight) {
for (int i = 0; i < 10; ++i) {
float newWeight = new FQStrategy().getWeightToUse(1, previousWeight);
Assert.assertTrue(
String.format("New weight %s should be greater than previous weight %s", newWeight, previousWeight),
"New weight %s should be greater than previous weight %s".formatted(newWeight, previousWeight),
newWeight > previousWeight);
previousWeight = newWeight;
}
Expand Down

0 comments on commit d727a2e

Please sign in to comment.