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