From 574a2e35604d6f7e6bbc83f8a21521a5fa8735d5 Mon Sep 17 00:00:00 2001 From: Mateusz Warowny Date: Mon, 14 Mar 2022 15:51:44 +0100 Subject: [PATCH] JAL-3878 Move array index search to ArrayUtils --- src/jalview/util/ArrayUtils.java | 22 ++++++++++++++++++++ src/jalview/ws2/actions/AbstractPollableTask.java | 23 +++------------------ 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/src/jalview/util/ArrayUtils.java b/src/jalview/util/ArrayUtils.java index c05dac5..e141d07 100644 --- a/src/jalview/util/ArrayUtils.java +++ b/src/jalview/util/ArrayUtils.java @@ -20,6 +20,8 @@ */ package jalview.util; +import java.util.Objects; + public class ArrayUtils { /** @@ -44,4 +46,24 @@ public class ArrayUtils } } } + + /** + * Return the index of the first occurrence of the item in the array or -1 if + * the array doesn't contain the item. + * + * @param arr + * array to be searched + * @param item + * item to search for + * @return index of the first occurrence of the item or -1 if not found + */ + public static int indexOf(Object[] arr, Object item) + { + for (int i = 0; i < arr.length; i++) + { + if (Objects.equals(arr[i], item)) + return i; + } + return -1; + } } diff --git a/src/jalview/ws2/actions/AbstractPollableTask.java b/src/jalview/ws2/actions/AbstractPollableTask.java index d1db921..1e57556 100644 --- a/src/jalview/ws2/actions/AbstractPollableTask.java +++ b/src/jalview/ws2/actions/AbstractPollableTask.java @@ -10,6 +10,7 @@ import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; import jalview.bin.Cache; +import jalview.util.ArrayUtils; import jalview.ws.params.ArgumentI; import jalview.ws2.actions.api.TaskEventListener; import jalview.ws2.actions.api.TaskI; @@ -207,11 +208,11 @@ public abstract class AbstractPollableTask implements Task private void updateGlobalStatus() { JobStatus newStatus = taskStatus; - int currentPrecedence = getPrecedenceOf(newStatus); + int currentPrecedence = ArrayUtils.indexOf(statusPrecedence, newStatus); for (BaseJob job : jobs) { JobStatus status = job.getStatus(); - int jobPrecedence = getPrecedenceOf(status); + int jobPrecedence = ArrayUtils.indexOf(statusPrecedence, status); if (currentPrecedence < jobPrecedence) { currentPrecedence = jobPrecedence; @@ -240,24 +241,6 @@ public abstract class AbstractPollableTask implements Task JobStatus.SERVER_ERROR }; - /** - * @param status - * status to find the precedence of - * @return precedence of the status - */ - private static int getPrecedenceOf(JobStatus status) - { - final int len = statusPrecedence.length; - for (int i = 0; i < len; i++) - { - if (statusPrecedence[i] == status) - { - return i; - } - } - return -1; - } - @Override public void cancel() { -- 1.7.10.2