content: /*
   * Copyright (c) 2024, Alibaba Cloud;
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *    http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package com.aliyun.dataworks.migrationx.domain.dataworks.dolphinscheduler.v3.enums;
  
  import java.util.HashMap;
  import java.util.Map;
  
  public enum TaskExecutionStatus {
  
      SUBMITTED_SUCCESS(0, "submit success"),
      RUNNING_EXECUTION(1, "running"),
      PAUSE(3, "pause"),
      FAILURE(6, "failure"),
      SUCCESS(7, "success"),
      NEED_FAULT_TOLERANCE(8, "need fault tolerance"),
      KILL(9, "kill"),
      DELAY_EXECUTION(12, "delay execution"),
      FORCED_SUCCESS(13, "forced success"),
      DISPATCH(17, "dispatch"),
  
      ;
  
      private static final Map<Integer, TaskExecutionStatus> CODE_MAP = new HashMap<>();
      private static final int[] NEED_FAILOVER_STATES = new int[]{
              SUBMITTED_SUCCESS.getCode(),
              DISPATCH.getCode(),
              RUNNING_EXECUTION.getCode(),
              DELAY_EXECUTION.getCode(),
      };
  
      static {
          for (TaskExecutionStatus executionStatus : TaskExecutionStatus.values()) {
              CODE_MAP.put(executionStatus.getCode(), executionStatus);
          }
      }
  
      /**
       * Get <code>TaskExecutionStatus</code> by code, if the code is invalidated will throw {@link IllegalArgumentException}.
       */
      public static TaskExecutionStatus of(int code) {
          TaskExecutionStatus taskExecutionStatus = CODE_MAP.get(code);
          if (taskExecutionStatus == null) {
              throw new IllegalArgumentException(String.format("The task execution status code: %s is invalidated",
                      code));
          }
          return taskExecutionStatus;
      }
  
      public boolean isRunning() {
          return this == RUNNING_EXECUTION;
      }
  
      public boolean isSuccess() {
          return this == TaskExecutionStatus.SUCCESS;
      }
  
      public boolean isForceSuccess() {
          return this == TaskExecutionStatus.FORCED_SUCCESS;
      }
  
      public boolean isKill() {
          return this == TaskExecutionStatus.KILL;
      }
  
      public boolean isFailure() {
          return this == TaskExecutionStatus.FAILURE || this == NEED_FAULT_TOLERANCE;
      }
  
      public boolean isPause() {
          return this == TaskExecutionStatus.PAUSE;
      }
  
      public boolean isFinished() {
          return isSuccess() || isKill() || isFailure() || isPause();
      }
  
      public boolean isNeedFaultTolerance() {
          return this == NEED_FAULT_TOLERANCE;
      }
  
      public static int[] getNeedFailoverWorkflowInstanceState() {
          return NEED_FAILOVER_STATES;
      }
  
      public boolean shouldFailover() {
          return SUBMITTED_SUCCESS == this
                  || DISPATCH == this
                  || RUNNING_EXECUTION == this
                  || DELAY_EXECUTION == this;
      }
  
      private final int code;
      private final String desc;
  
      TaskExecutionStatus(int code, String desc) {
          this.code = code;
          this.desc = desc;
      }
  
      public int getCode() {
          return code;
      }
  
      public String getDesc() {
          return desc;
      }
  
      @Override
      public String toString() {
          return "TaskExecutionStatus{" + "code=" + code + ", desc='" + desc + '\'' + '}';
      }
  }
  