JAL-3878 Add methods to get and remove workers by their calc name.
authorMateusz Warowny <mmzwarowny@dundee.ac.uk>
Tue, 23 Nov 2021 13:50:59 +0000 (14:50 +0100)
committerMateusz Warowny <mmzwarowny@dundee.ac.uk>
Tue, 23 Nov 2021 13:50:59 +0000 (14:50 +0100)
src/jalview/api/AlignCalcManagerI2.java
src/jalview/workers/AlignCalcManager2.java

index 0bd6c3c..36bcda2 100644 (file)
@@ -32,7 +32,13 @@ public interface AlignCalcManagerI2
    * {@ink Class#equals(Object)} rather than {@code instanceof}.
    */
   List<AlignCalcWorkerI> getWorkersOfClass(Class<? extends AlignCalcWorkerI> cls);
-  
+
+  /**
+   * Returns the list of all workers for the given name or an empty list
+   * if there are none.
+   */
+  List<AlignCalcWorkerI> getWorkersForName(String name);
+
   /**
    * Removes the worker from the scheduler. It does not cancel workers
    * already scheduled for execution.
@@ -50,7 +56,12 @@ public interface AlignCalcManagerI2
   default void removeWorkersForAnnotation(AlignmentAnnotation annot) {
     removeWorkerForAnnotation(annot);
   }
-  
+
+  /**
+   * Removes all workers with matching name.
+   */
+  void removeWorkersForName(String name);
+
   /**
    * Removes all workers of a given class. The classes are compared using
    * {@link Class#equals(Object)}. 
index 8d4796d..5c25668 100644 (file)
@@ -312,6 +312,20 @@ public class AlignCalcManager2 implements AlignCalcManagerI2
   }
 
   @Override
+  public List<AlignCalcWorkerI> getWorkersForName(String name)
+  {
+    List<AlignCalcWorkerI> collected = new ArrayList<>();
+    for (var worker : getWorkers())
+    {
+      if (worker.getCalcName().equals(name))
+      {
+        collected.add(worker);
+      }
+    }
+    return collected;
+  }
+
+  @Override
   public void removeWorker(AlignCalcWorkerI worker)
   {
     if (worker.isDeletable())
@@ -351,6 +365,21 @@ public class AlignCalcManager2 implements AlignCalcManagerI2
   }
 
   @Override
+  public void removeWorkersForName(String name)
+  {
+    synchronized (registered)
+    {
+      for (var worker : getWorkers())
+      {
+        if (worker.getCalcName().equals(name))
+        {
+          removeWorker(worker);
+        }
+      }
+    }
+  }
+
+  @Override
   public void disableWorker(AlignCalcWorkerI worker)
   {
     // Null pointer check might be needed