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