001/*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License").  You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2009 Sun Microsystems, Inc.
025 *      Portions Copyright 2015 ForgeRock AS.
026 */
027package org.opends.guitools.controlpanel.datamodel;
028
029import java.util.Date;
030
031/** The class to be used to describe the task schedule. */
032public class ScheduleType
033{
034  /** The different type of schedules. */
035  public enum Type
036  {
037    /** Launch now. */
038    LAUNCH_NOW,
039    /** Launch later in a specific date. */
040    LAUNCH_LATER,
041    /** Launch periodically. */
042    LAUNCH_PERIODICALLY
043  }
044
045  private Type type;
046  private Date launchLaterDate;
047  private String cronValue;
048  private String toString;
049  private int hashCode;
050
051  private ScheduleType()
052  {
053  }
054
055  /**
056   * Returns a schedule instance that launches the task now.
057   * @return a schedule instance that launches the task now.
058   */
059  public static ScheduleType createLaunchNow()
060  {
061    ScheduleType schedule = new ScheduleType();
062    schedule.type = Type.LAUNCH_NOW;
063    schedule.toString = schedule.calculateToString();
064    schedule.hashCode = schedule.calculateHashCode();
065    return schedule;
066  }
067
068  /**
069   * Returns a schedule instance that launches the task at a given date.
070   * @param date the Date at which the task must be launched.
071   * @return a schedule instance that launches the task at a given date.
072   */
073  public static ScheduleType createLaunchLater(Date date)
074  {
075    ScheduleType schedule = new ScheduleType();
076    schedule.type = Type.LAUNCH_LATER;
077    schedule.launchLaterDate = date;
078    schedule.toString = schedule.calculateToString();
079    schedule.hashCode = schedule.calculateHashCode();
080    return schedule;
081  }
082
083  /**
084   * Returns a schedule instance that launches the task using a cron schedule.
085   * @param cron the String containing the cron schedule.
086   * @return a schedule instance that launches the task using a cron schedule.
087   */
088  public static ScheduleType createCron(String cron)
089  {
090    ScheduleType schedule = new ScheduleType();
091    schedule.type = Type.LAUNCH_PERIODICALLY;
092    schedule.cronValue = cron;
093    schedule.toString = schedule.calculateToString();
094    schedule.hashCode = schedule.calculateHashCode();
095    return schedule;
096  }
097
098  /**
099   * Returns the type of the schedule.
100   * @return the type of the schedule.
101   */
102  public Type getType()
103  {
104    return type;
105  }
106
107  /**
108   * Returns the date on which the task will be launched.
109   * @return the date on which the task will be launched.
110   */
111  public Date getLaunchLaterDate()
112  {
113    return launchLaterDate;
114  }
115
116  /**
117   * Returns the CRON String representation of the schedule.
118   * @return the CRON String representation of the schedule.
119   */
120  public String getCronValue()
121  {
122    return cronValue;
123  }
124
125  /** {@inheritDoc} */
126  public boolean equals(Object o)
127  {
128    if (o == this)
129    {
130      return true;
131    }
132    return o != null
133        && toString().equals(o.toString());
134  }
135
136  /** {@inheritDoc} */
137  public String toString()
138  {
139    return toString;
140  }
141
142  /** {@inheritDoc} */
143  public int hashCode()
144  {
145    return hashCode;
146  }
147
148  /**
149   * Calculates the hashCode.
150   * To be called after the calculateToString is called.
151   * @return the value of the hashCode.
152   */
153  private int calculateHashCode()
154  {
155    return 32 + toString.hashCode();
156  }
157
158  private String calculateToString()
159  {
160    switch (type)
161    {
162    case LAUNCH_NOW:
163      return "Schedule Type: Launch Now";
164    case LAUNCH_LATER:
165      return "Schedule Type: Launch Later at date " + launchLaterDate;
166    case LAUNCH_PERIODICALLY:
167      return "Schedule Type: periodical schedule " + cronValue;
168    default:
169      throw new RuntimeException("Invalid type: " + type);
170    }
171  }
172}