Merge branch 'mmw/JAL-4199-web-services-testing' into mmw/bug/JAL-4241-annotation...
[jalview.git] / src / jalview / ws2 / actions / api / TaskEventListener.java
index 3dda2a4..b0bb4b6 100644 (file)
@@ -2,8 +2,9 @@ package jalview.ws2.actions.api;
 
 import java.util.List;
 
+import jalview.bin.Console;
 import jalview.ws2.api.JobStatus;
-import jalview.ws2.api.WebServiceJob;
+import jalview.ws2.api.WebServiceJobHandle;
 
 /**
  * The listener interface for receiving relevant job progress and state change
@@ -22,91 +23,131 @@ public interface TaskEventListener<T>
   /**
    * Invoked when the task has been started. The {@code subJobs} parameter
    * contains a complete list of sub-jobs for that run. Note that restartable
-   * tasks may invoke this method multiple times with different set of
-   * sub-jobs.
+   * tasks may invoke this method multiple times with different set of sub-jobs.
    * 
    * @param source
-   *    task this event originates from
+   *          task this event originates from
    * @param subJobs
-   *    list of sub-jobs for this run
+   *          list of sub-jobs for this run
    */
-  void taskStarted(TaskI<T> source, List<WebServiceJob> subJobs);
+  default void taskStarted(TaskI<T> source, List<? extends JobI> subJobs) {};
 
   /**
    * Invoked when the global task status has changed.
    * 
    * @param source
-   *    task this event originates from
+   *          task this event originates from
    * @param status
-   *    new task status
+   *          new task status
    */
-  void taskStatusChanged(TaskI<T> source, JobStatus status);
+  default void taskStatusChanged(TaskI<T> source, JobStatus status) {};
 
   /**
    * Invoked when the task has completed. If the task completed with a result,
-   * that result is passed in the call argument, otherwise, a {@code null}
-   * value is given.
+   * that result is passed in the call argument, otherwise, a {@code null} value
+   * is given.
    * 
    * @param source
-   *    task this event originates from
+   *          task this event originates from
    * @param result
-   *    computation result or null if result not present
+   *          computation result or null if result not present
    */
-  void taskCompleted(TaskI<T> source, T result);
+  default void taskCompleted(TaskI<T> source, T result) {};
 
   /**
    * Invoked when an unhandled exception has occurred during task execution.
    * 
    * @param source
-   *    task this event originates from
+   *          task this event originates from
    * @param e
-   *    exception
+   *          exception
    */
-  void taskException(TaskI<T> source, Exception e);
-
-  /**
-   * Invoked when the task had been restarted. This event is only applicable
-   * to restartable tasks and will precede each {@link #taskStarted} after
-   * the first one.
-   * 
-   * @param source
-   *    task this event originates from
-   */
-  void taskRestarted(TaskI<T> source);
+  default void taskException(TaskI<T> source, Exception e) {};
 
   /**
    * Invoked when the status of a sub-job has changed.
    * 
    * @param source
-   *    task this event originates form
+   *          task this event originates form
    * @param job
-   *    sub-job that has been updated
+   *          sub-job that has been updated
    * @param status
-   *    new job status
+   *          new job status
    */
-  void subJobStatusChanged(TaskI<T> source, WebServiceJob job, JobStatus status);
+  default void subJobStatusChanged(TaskI<T> source, JobI job, JobStatus status) {};
 
   /**
    * Invoked when a log string of the sub-job has changed.
    * 
    * @param source
-   *    task this event originates form
+   *          task this event originates form
    * @param job
-   *    sub-job that has been updated
+   *          sub-job that has been updated
    * @param log
-   *    new log string
+   *          new log string
    */
-  void subJobLogChanged(TaskI<T> source, WebServiceJob job, String log);
+  default void subJobLogChanged(TaskI<T> source, JobI job, String log) {};
 
   /**
    * Invoked when an error log string of the sub-job has changed.
    * 
    * @param source
-   *    task this event originates form
+   *          task this event originates form
    * @param job
-   *    sub-job that has been updated
+   *          sub-job that has been updated
    * @param log
-   *    new log string
+   *          new log string
    */
-  void subJobErrorLogChanged(TaskI<T> source, WebServiceJob job, String log);
+  default void subJobErrorLogChanged(TaskI<T> source, JobI job, String log) {};
+
+  @SuppressWarnings("rawtypes")
+  static final TaskEventListener NULL_LISTENER = new TaskEventListener()
+  {
+    @Override
+    public void taskStarted(TaskI source, List subJobs)
+    {
+      Console.info("task started with " + subJobs.size() + " jobs");
+    }
+
+    @Override
+    public void taskStatusChanged(TaskI source, JobStatus status)
+    {
+      Console.info("task status " + status);
+    }
+
+    @Override
+    public void taskCompleted(TaskI source, Object result)
+    {
+      Console.info("task completed");
+    }
+
+    @Override
+    public void taskException(TaskI source, Exception e)
+    {
+      Console.info("task failed", e);
+    }
+
+    @Override
+    public void subJobStatusChanged(TaskI source, JobI job,
+            JobStatus status)
+    {
+      Console.info("sub-job " + job.getInternalId() + " status " + status);
+    }
+
+    @Override
+    public void subJobLogChanged(TaskI source, JobI job, String log)
+    {
+    }
+
+    @Override
+    public void subJobErrorLogChanged(TaskI source, JobI job, String log)
+    {
+    }
+  };
+
+  @SuppressWarnings("unchecked")
+  static <T> TaskEventListener<T> nullListener()
+  {
+    return (TaskEventListener<T>) NULL_LISTENER;
+  }
 }