Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8092150

Add WORKER_STATE_COMPLETED event type and Worker.State.isCompleted() method

XMLWordPrintable

      When using javafx.concurrent.Task, I often want to perform an action when the Task is completed, regardless of whether it succeeded or failed.

      I suggest to add new EventType WORKER_STATE_COMPLETED as the common supertype for WORKER_STATE_SUCCEEDED, WORKER_STATE_FAILED, WORKER_STATE_CANCELLED.
      This would allow to write

          task.addEventHandler(WORKER_STATE_COMPLETED, e -> handle(e));

      instead of

          task.addEventHandler(ANY, e -> {
              if(e.getEventType() == WORKER_STATE_SUCCEEDED
                      || e.getEventType() == WORKER_STATE_FAILED
                      || e.getEventType() == WORKER_STATE_CANCELLED) {
                  handle(e);
              }
          });


      Furthermore, I suggest to add method Worker.State.isCompleted() that reutrns true for SUCCEEDED, FAILED, CANCELLED, and returns false otherwise.
      This would allow to write

          if(state.isCompleted()) {
              doSomething();
          }

      instead of

          switch(state) {
              case SUCCEEDED: // fall through
              case FAILED: // fall through
              case CANCELLED:
                  doSomething();
                  break;
              default: // do nothing
          }

            Unassigned Unassigned
            tmikula Tomas Mikula
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Imported: