001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.reef.runtime.common.driver;
020
021/**
022 * The status of the Driver.
023 */
024public enum DriverStatus {
025
026  PRE_INIT,
027  INIT,
028  RUNNING,
029  SHUTTING_DOWN,
030  FAILING;
031
032  /**
033   * Check if the driver is in process of shutting down (either gracefully or due to an error).
034   * @return true if the driver is shutting down (gracefully or otherwise).
035   */
036  public boolean isClosing() {
037    return this == SHUTTING_DOWN || this == FAILING;
038  }
039
040  /**
041   * Check whether a driver state transition from current state to a given one is legal.
042   * @param toStatus Destination state.
043   * @return true if transition is valid, false otherwise.
044   */
045  public boolean isLegalTransition(final DriverStatus toStatus) {
046
047    switch (this) {
048
049    case PRE_INIT:
050      switch (toStatus) {
051      case INIT:
052        return true;
053      default:
054        return false;
055      }
056
057    case INIT:
058      switch (toStatus) {
059      case RUNNING:
060        return true;
061      default:
062        return false;
063      }
064
065    case RUNNING:
066      switch (toStatus) {
067      case SHUTTING_DOWN:
068      case FAILING:
069        return true;
070      default:
071        return false;
072      }
073
074    case FAILING:
075    case SHUTTING_DOWN:
076      return false;
077
078    default:
079      throw new IllegalStateException("Unknown input state: " + this);
080    }
081  }
082}