From c161179fa24cc9cf71bb9bbbfccfa62d1762de1a Mon Sep 17 00:00:00 2001 From: Mark Waite Date: Sun, 20 Oct 2024 17:01:56 -0600 Subject: [PATCH] Use Java 17 language features --- .../java/jenkins/advancedqueue/JobGroup.java | 4 ++-- .../advancedqueue/PriorityConfiguration.java | 4 ++-- ...iorityConfigurationPlaceholderTaskHelper.java | 3 +-- .../strategy/FolderPropertyLoader.java | 10 ++++------ .../PropertyBasedJobInclusionStrategy.java | 4 ++-- .../strategy/ViewBasedJobInclusionStrategy.java | 8 ++++---- .../priority/strategy/JobPropertyStrategy.java | 3 +-- .../sorter/AdvancedQueueSorter.java | 16 +++++++++------- .../jenkins/advancedqueue/sorter/ItemInfo.java | 10 ++++------ .../sorter/StartedJobItemCache.java | 4 ++-- .../sorter/strategy/FQStrategyTest.java | 2 +- 11 files changed, 32 insertions(+), 36 deletions(-) diff --git a/src/main/java/jenkins/advancedqueue/JobGroup.java b/src/main/java/jenkins/advancedqueue/JobGroup.java index 15202053..4213c278 100644 --- a/src/main/java/jenkins/advancedqueue/JobGroup.java +++ b/src/main/java/jenkins/advancedqueue/JobGroup.java @@ -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(); } return null; } diff --git a/src/main/java/jenkins/advancedqueue/PriorityConfiguration.java b/src/main/java/jenkins/advancedqueue/PriorityConfiguration.java index 257ea404..bd38e637 100644 --- a/src/main/java/jenkins/advancedqueue/PriorityConfiguration.java +++ b/src/main/java/jenkins/advancedqueue/PriorityConfiguration.java @@ -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); } else { return view.contains((TopLevelItem) job); } diff --git a/src/main/java/jenkins/advancedqueue/PriorityConfigurationPlaceholderTaskHelper.java b/src/main/java/jenkins/advancedqueue/PriorityConfigurationPlaceholderTaskHelper.java index 73f23a78..6ebc00a8 100644 --- a/src/main/java/jenkins/advancedqueue/PriorityConfigurationPlaceholderTaskHelper.java +++ b/src/main/java/jenkins/advancedqueue/PriorityConfigurationPlaceholderTaskHelper.java @@ -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) { ItemInfo itemInfo = QueueItemCache.get().getItem(job.getName()); if (itemInfo != null) { priorityCallback.setPrioritySelection(itemInfo.getPriority()); diff --git a/src/main/java/jenkins/advancedqueue/jobinclusion/strategy/FolderPropertyLoader.java b/src/main/java/jenkins/advancedqueue/jobinclusion/strategy/FolderPropertyLoader.java index 501fc9ce..ef78c3d4 100644 --- a/src/main/java/jenkins/advancedqueue/jobinclusion/strategy/FolderPropertyLoader.java +++ b/src/main/java/jenkins/advancedqueue/jobinclusion/strategy/FolderPropertyLoader.java @@ -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, 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 + "] ..."); @@ -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(); } else { parent = null; } diff --git a/src/main/java/jenkins/advancedqueue/jobinclusion/strategy/PropertyBasedJobInclusionStrategy.java b/src/main/java/jenkins/advancedqueue/jobinclusion/strategy/PropertyBasedJobInclusionStrategy.java index 80465fa3..1039df87 100644 --- a/src/main/java/jenkins/advancedqueue/jobinclusion/strategy/PropertyBasedJobInclusionStrategy.java +++ b/src/main/java/jenkins/advancedqueue/jobinclusion/strategy/PropertyBasedJobInclusionStrategy.java @@ -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()); } } return strategies; diff --git a/src/main/java/jenkins/advancedqueue/jobinclusion/strategy/ViewBasedJobInclusionStrategy.java b/src/main/java/jenkins/advancedqueue/jobinclusion/strategy/ViewBasedJobInclusionStrategy.java index dd2e07cc..ee5ea50c 100644 --- a/src/main/java/jenkins/advancedqueue/jobinclusion/strategy/ViewBasedJobInclusionStrategy.java +++ b/src/main/java/jenkins/advancedqueue/jobinclusion/strategy/ViewBasedJobInclusionStrategy.java @@ -68,8 +68,8 @@ public ListBoxModel getListViewItems() { private void addViews(String parent, ListBoxModel items, Collection 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()); } } } @@ -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; } diff --git a/src/main/java/jenkins/advancedqueue/priority/strategy/JobPropertyStrategy.java b/src/main/java/jenkins/advancedqueue/priority/strategy/JobPropertyStrategy.java index f3b7fe3b..b02c2e97 100644 --- a/src/main/java/jenkins/advancedqueue/priority/strategy/JobPropertyStrategy.java +++ b/src/main/java/jenkins/advancedqueue/priority/strategy/JobPropertyStrategy.java @@ -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) { PriorityJobProperty priorityProperty = job.getProperty(PriorityJobProperty.class); if (priorityProperty != null && priorityProperty.getUseJobPriority()) { return priorityProperty.priority; diff --git a/src/main/java/jenkins/advancedqueue/sorter/AdvancedQueueSorter.java b/src/main/java/jenkins/advancedqueue/sorter/AdvancedQueueSorter.java index dfe810cf..c07d2ac4 100644 --- a/src/main/java/jenkins/advancedqueue/sorter/AdvancedQueueSorter.java +++ b/src/main/java/jenkins/advancedqueue/sorter/AdvancedQueueSorter.java @@ -92,19 +92,21 @@ public void sortNotWaitingItems(List 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())); } queueStr.append("+----------------------------------------------------------------------+"); LOGGER.log(Level.FINER, queueStr.toString()); diff --git a/src/main/java/jenkins/advancedqueue/sorter/ItemInfo.java b/src/main/java/jenkins/advancedqueue/sorter/ItemInfo.java index fdc3a7e4..fc8f5c02 100644 --- a/src/main/java/jenkins/advancedqueue/sorter/ItemInfo.java +++ b/src/main/java/jenkins/advancedqueue/sorter/ItemInfo.java @@ -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; } @@ -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) { return compareTo(itemInfo) == 0; } return false; @@ -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() { diff --git a/src/main/java/jenkins/advancedqueue/sorter/StartedJobItemCache.java b/src/main/java/jenkins/advancedqueue/sorter/StartedJobItemCache.java index 8ec891e1..3a8c10de 100644 --- a/src/main/java/jenkins/advancedqueue/sorter/StartedJobItemCache.java +++ b/src/main/java/jenkins/advancedqueue/sorter/StartedJobItemCache.java @@ -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(); } } diff --git a/src/test/java/jenkins/advancedqueue/sorter/strategy/FQStrategyTest.java b/src/test/java/jenkins/advancedqueue/sorter/strategy/FQStrategyTest.java index 148c204a..05f72fd2 100644 --- a/src/test/java/jenkins/advancedqueue/sorter/strategy/FQStrategyTest.java +++ b/src/test/java/jenkins/advancedqueue/sorter/strategy/FQStrategyTest.java @@ -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; }