001/*
002 * JPPF.
003 * Copyright (C) 2005-2018 JPPF Team.
004 * http://www.jppf.org
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * 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, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.jppf.client;
020
021import java.util.*;
022
023/**
024 * Status of the connection between a client and a driver.
025 * @author Laurent Cohen
026 */
027public enum JPPFClientConnectionStatus {
028  /**
029   * The connection was just created.
030   */
031  NEW,
032  /**
033   * The connection was disconnected from the driver.
034   */
035  DISCONNECTED,
036  /**
037   * The connection is currently attempting to connect to the driver.
038   */
039  CONNECTING,
040  /**
041   * The connection is successfully connected to the driver.
042   */
043  ACTIVE,
044  /**
045   * The connection is currently executing a job.
046   */
047  EXECUTING,
048  /**
049   * The connection failed to (re)connect to the driver and no further attempt will be made.
050   */
051  FAILED,
052  /**
053   * The connection was closed by the application.
054   */
055  CLOSED;
056
057  /**
058   * The list of working statuses.
059   */
060  private static final List<JPPFClientConnectionStatus> WORKING_STATUSES = Collections.unmodifiableList(Arrays.asList(ACTIVE, EXECUTING));
061  /**
062   * The list of working statuses.
063   */
064  private static final List<JPPFClientConnectionStatus> TERMINATED_STATUSES = Collections.unmodifiableList(Arrays.asList(CLOSED, FAILED));
065
066  /**
067   * Determine whether this status is one of those specified as input.
068   * @param statuses the statuses to check against.
069   * @return {@code true} if this status is one of those specified as input, {@code false} otherwise.
070   */
071  public boolean isOneOf(final List<JPPFClientConnectionStatus> statuses) {
072    if (statuses == null) return false;
073    for (JPPFClientConnectionStatus status: statuses) {
074      if (this == status) return true;
075    }
076    return false;
077  }
078
079  /**
080   * Determine whether this status a working status, that is if it is either {@link #ACTIVE} or {@link #EXECUTING}.
081   * @return {@code true} if this status is a working status, {@code false} otherwise.
082   */
083  public boolean isWorkingStatus() {
084    return isOneOf(WORKING_STATUSES);
085  }
086
087  /**
088   * Determine whether this status a working status, that is if it is either {@link #FAILED} or {@link #CLOSED}.
089   * @return {@code true} if this status is a working status, {@code false} otherwise.
090   */
091  public boolean isTerminatedStatus() {
092    return isOneOf(TERMINATED_STATUSES);
093  }
094
095  /**
096   * Get the statuses indicating that a connection is in a working state.
097   * @return an array of {@link JPPFClientConnectionStatus} enum values.
098   */
099  public static JPPFClientConnectionStatus[] workingStatuses() {
100    return WORKING_STATUSES.toArray(new JPPFClientConnectionStatus[WORKING_STATUSES.size()]);
101  }
102
103  /**
104   * Get the statuses indicating that a connection is in a terminated state.
105   * @return an array of {@link JPPFClientConnectionStatus} enum values.
106   */
107  public static JPPFClientConnectionStatus[] terminatedStatuses() {
108    return TERMINATED_STATUSES.toArray(new JPPFClientConnectionStatus[TERMINATED_STATUSES.size()]);
109  }
110}