001/*
002 * Copyright 2008-2018 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright (C) 2015-2018 Ping Identity Corporation
007 *
008 * This program is free software; you can redistribute it and/or modify
009 * it under the terms of the GNU General Public License (GPLv2 only)
010 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
011 * as published by the Free Software Foundation.
012 *
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016 * GNU General Public License for more details.
017 *
018 * You should have received a copy of the GNU General Public License
019 * along with this program; if not, see <http://www.gnu.org/licenses>.
020 */
021package com.unboundid.ldap.sdk.unboundidds.tasks;
022
023
024
025import com.unboundid.util.ThreadSafety;
026import com.unboundid.util.ThreadSafetyLevel;
027
028import static com.unboundid.util.StaticUtils.*;
029
030
031
032/**
033 * This class defines a task state, which provides information about the current
034 * state of processing for a scheduled task.
035 * <BR>
036 * <BLOCKQUOTE>
037 *   <B>NOTE:</B>  This class, and other classes within the
038 *   {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only
039 *   supported for use against Ping Identity, UnboundID, and Alcatel-Lucent 8661
040 *   server products.  These classes provide support for proprietary
041 *   functionality or for external specifications that are not considered stable
042 *   or mature enough to be guaranteed to work in an interoperable way with
043 *   other types of LDAP servers.
044 * </BLOCKQUOTE>
045 */
046@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
047public enum TaskState
048{
049  /**
050   * The task state that indicates that the task was canceled before it started
051   * running.
052   */
053  CANCELED_BEFORE_STARTING("canceled_before_starting"),
054
055
056
057  /**
058   * The task state that indicates that the task has completed successfully.
059   */
060  COMPLETED_SUCCESSFULLY("completed_successfully"),
061
062
063
064  /**
065   * The task state that indicates that the task has completed but with one or
066   * more errors.
067   */
068  COMPLETED_WITH_ERRORS("completed_with_errors"),
069
070
071
072  /**
073   * The task state that indicates that the task has been disabled.
074   */
075  DISABLED("disabled"),
076
077
078
079  /**
080   * The task state that indicates that the task is running.
081   */
082  RUNNING("running"),
083
084
085
086  /**
087   * The task state that indicates that the task was forced to stop running when
088   * it was canceled by an administrator.
089   */
090  STOPPED_BY_ADMINISTRATOR("stopped_by_administrator"),
091
092
093
094  /**
095   * The task state that indicates that the task was forced to stop running when
096   * it encountered an unrecoverable error.
097   */
098  STOPPED_BY_ERROR("stopped_by_error"),
099
100
101
102  /**
103   * The task state that indicates that the task was forced to stop running when
104   * the task scheduler was shut down.
105   */
106  STOPPED_BY_SHUTDOWN("stopped_by_shutdown"),
107
108
109
110  /**
111   * The task state that indicates that the task has not yet been scheduled.
112   */
113  UNSCHEDULED("unscheduled"),
114
115
116
117  /**
118   * The task state that indicates that the task has one or more unsatisfied
119   * dependencies.
120   */
121  WAITING_ON_DEPENDENCY("waiting_on_dependency"),
122
123
124
125  /**
126   * The task state that indicates that the task is waiting on the start time to
127   * arrive.
128   */
129  WAITING_ON_START_TIME("waiting_on_start_time");
130
131
132
133  // The name of this failed dependency action.
134  private final String name;
135
136
137
138  /**
139   * Creates a new task state with the specified name.
140   *
141   * @param  name  The name of the task state to create.
142   */
143  TaskState(final String name)
144  {
145    this.name = name;
146  }
147
148
149
150  /**
151   * Retrieves the name of this task state.
152   *
153   * @return  The name of this task state.
154   */
155  public String getName()
156  {
157    return name;
158  }
159
160
161
162  /**
163   * Retrieves the task state with the specified name.
164   *
165   * @param  name  The name of the task state to retrieve.
166   *
167   * @return  The requested task state, or {@code null} if there is no state
168   *          with the given name.
169   */
170  public static TaskState forName(final String name)
171  {
172    switch (toLowerCase(name))
173    {
174      case "canceledbeforestarting":
175      case "canceled-before-starting":
176      case "canceled_before_starting":
177        return CANCELED_BEFORE_STARTING;
178      case "completedsuccessfully":
179      case "completed-successfully":
180      case "completed_successfully":
181        return COMPLETED_SUCCESSFULLY;
182      case "completedwitherrors":
183      case "completed-with-errors":
184      case "completed_with_errors":
185        return COMPLETED_WITH_ERRORS;
186      case "disabled":
187        return DISABLED;
188      case "running":
189        return RUNNING;
190      case "stoppedbyadministrator":
191      case "stopped-by-administrator":
192      case "stopped_by_administrator":
193        return STOPPED_BY_ADMINISTRATOR;
194      case "stoppedbyerror":
195      case "stopped-by-error":
196      case "stopped_by_error":
197        return STOPPED_BY_ERROR;
198      case "stoppedbyshutdown":
199      case "stopped-by-shutdown":
200      case "stopped_by_shutdown":
201        return STOPPED_BY_SHUTDOWN;
202      case "unscheduled":
203        return UNSCHEDULED;
204      case "waitingondependency":
205      case "waiting-on-dependency":
206      case "waiting_on_dependency":
207        return WAITING_ON_DEPENDENCY;
208      case "waitingonstarttime":
209      case "waiting-on-start-time":
210      case "waiting_on_start_time":
211        return WAITING_ON_START_TIME;
212      default:
213        return null;
214    }
215  }
216
217
218
219  /**
220   * Indicates whether this task state indicates that the task has not yet
221   * started running.
222   *
223   * @return  {@code true} if this task state indicates that the task has not
224   *          yet started, or {@code false} if not.
225   */
226  public boolean isPending()
227  {
228    switch (this)
229    {
230      case DISABLED:
231      case UNSCHEDULED:
232      case WAITING_ON_DEPENDENCY:
233      case WAITING_ON_START_TIME:
234        return true;
235      default:
236        return false;
237    }
238  }
239
240
241
242  /**
243   * Indicates whether this task state indicates that the task is currently
244   * running.
245   *
246   * @return  {@code true} if this task state indicates that the task is
247   *          currently running, or {@code false} if not.
248   */
249  public boolean isRunning()
250  {
251    return (this == RUNNING);
252  }
253
254
255
256  /**
257   * Indicates whether this task state indicates that the task has completed all
258   * of the processing that it will do.
259   *
260   * @return  {@code true} if this task state indicates that the task has
261   *          completed all of the processing that it will do, or {@code false}
262   *          if not.
263   */
264  public boolean isCompleted()
265  {
266    return (! (isPending() || isRunning()));
267  }
268
269
270
271  /**
272   * Retrieves a string representation of this task state.
273   *
274   * @return  A string representation of this task state.
275   */
276  @Override()
277  public String toString()
278  {
279    return name;
280  }
281}