Merge branch 'JAL-3878_ws-overhaul-3' into mmw/Release_2_12_ws_merge
[jalview.git] / src / jalview / ws2 / api / JobStatus.java
diff --git a/src/jalview/ws2/api/JobStatus.java b/src/jalview/ws2/api/JobStatus.java
new file mode 100644 (file)
index 0000000..3341a69
--- /dev/null
@@ -0,0 +1,71 @@
+package jalview.ws2.api;
+
+public enum JobStatus
+{
+  /** Job has invalid inputs and cannot be started. */
+  INVALID,
+  /** Job is created and ready for submission. */
+  READY,
+  /** Job has been submitted and awaits processing. */
+  SUBMITTED,
+  /** Job has been queued for execution */
+  QUEUED,
+  /** Job is running */
+  RUNNING,
+  /** Job has completed successfully. */
+  COMPLETED,
+  /** Job has finished with errors. */
+  FAILED,
+  /** Job has been cancelled by the user. */
+  CANCELLED,
+  /** Job cannot be processed due to server error. */
+  SERVER_ERROR,
+  /** Job status cannot be determined. */
+  UNKNOWN;
+
+  /**
+   * Returns true if the status corresponds to the job completed due to normal
+   * termination, error or cancellation.
+   * 
+   * @return {@value true} if status corresponds to a finished job.
+   */
+  public boolean isDone()
+  {
+    switch (this)
+    {
+    case INVALID:
+    case COMPLETED:
+    case FAILED:
+    case CANCELLED:
+    case SERVER_ERROR:
+      return true;
+    case READY:
+    case SUBMITTED:
+    case QUEUED:
+    case RUNNING:
+    case UNKNOWN:
+      return false;
+    default:
+      throw new AssertionError("non-exhaustive switch statement");
+    }
+  }
+
+  /**
+   * A precedence order of job statuses used to compute the overall task status.
+   */
+  public static final JobStatus[] statusPrecedence = {
+      JobStatus.INVALID, // all must be invalid for task to be invalid
+      JobStatus.COMPLETED, // all but invalid must be completed for task to be
+                           // completed
+      JobStatus.UNKNOWN, // unknown prevents successful completion but not
+                         // running or failure
+      JobStatus.READY,
+      JobStatus.SUBMITTED,
+      JobStatus.QUEUED,
+      JobStatus.RUNNING,
+      JobStatus.CANCELLED, // if any is terminated unsuccessfully, the task is
+                           // failed
+      JobStatus.FAILED,
+      JobStatus.SERVER_ERROR
+  };
+}