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