001/*
002 * Copyright 2008-2017 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright (C) 2015-2017 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    final String lowerName = toLowerCase(name);
173
174    if (lowerName.equals("canceled_before_starting"))
175    {
176      return CANCELED_BEFORE_STARTING;
177    }
178    else if (lowerName.equals("completed_successfully"))
179    {
180      return COMPLETED_SUCCESSFULLY;
181    }
182    else if (lowerName.equals("completed_with_errors"))
183    {
184      return COMPLETED_WITH_ERRORS;
185    }
186    else if (lowerName.equals("disabled"))
187    {
188      return DISABLED;
189    }
190    else if (lowerName.equals("running"))
191    {
192      return RUNNING;
193    }
194    else if (lowerName.equals("stopped_by_administrator"))
195    {
196      return STOPPED_BY_ADMINISTRATOR;
197    }
198    else if (lowerName.equals("stopped_by_error"))
199    {
200      return STOPPED_BY_ERROR;
201    }
202    else if (lowerName.equals("stopped_by_shutdown"))
203    {
204      return STOPPED_BY_SHUTDOWN;
205    }
206    else if (lowerName.equals("unscheduled"))
207    {
208      return UNSCHEDULED;
209    }
210    else if (lowerName.equals("waiting_on_dependency"))
211    {
212      return WAITING_ON_DEPENDENCY;
213    }
214    else if (lowerName.equals("waiting_on_start_time"))
215    {
216      return WAITING_ON_START_TIME;
217    }
218    else
219    {
220      return null;
221    }
222  }
223
224
225
226  /**
227   * Indicates whether this task state indicates that the task has not yet
228   * started running.
229   *
230   * @return  {@code true} if this task state indicates that the task has not
231   *          yet started, or {@code false} if not.
232   */
233  public boolean isPending()
234  {
235    switch (this)
236    {
237      case DISABLED:
238      case UNSCHEDULED:
239      case WAITING_ON_DEPENDENCY:
240      case WAITING_ON_START_TIME:
241        return true;
242      default:
243        return false;
244    }
245  }
246
247
248
249  /**
250   * Indicates whether this task state indicates that the task is currently
251   * running.
252   *
253   * @return  {@code true} if this task state indicates that the task is
254   *          currently running, or {@code false} if not.
255   */
256  public boolean isRunning()
257  {
258    return (this == RUNNING);
259  }
260
261
262
263  /**
264   * Indicates whether this task state indicates that the task has completed all
265   * of the processing that it will do.
266   *
267   * @return  {@code true} if this task state indicates that the task has
268   *          completed all of the processing that it will do, or {@code false}
269   *          if not.
270   */
271  public boolean isCompleted()
272  {
273    return (! (isPending() || isRunning()));
274  }
275
276
277
278  /**
279   * Retrieves a string representation of this task state.
280   *
281   * @return  A string representation of this task state.
282   */
283  @Override()
284  public String toString()
285  {
286    return name;
287  }
288}