001/*
002 * JPPF.
003 * Copyright (C) 2005-2018 JPPF Team.
004 * http://www.jppf.org
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.jppf.scheduling;
020
021import java.io.Serializable;
022import java.text.*;
023import java.util.Date;
024
025/**
026 * Instances of this class contain data used to setup a schedule.
027 * This includes duration, date, date format.
028 * @author Laurent Cohen
029 */
030public class JPPFSchedule implements Serializable {
031  /**
032   * Explicit serialVersionUID.
033   */
034  private static final long serialVersionUID = 1L;
035  /**
036   * Time in milliseconds, after which this task will be aborted.<br>
037   * A value of 0 or less indicates this task never times out.
038   */
039  private final long duration;
040  /**
041   * Schedule date as a string.
042   */
043  private final String date;
044  /**
045   * Schedule date format as a string.
046   */
047  private String format;
048  /**
049   * Format describing the schedule date.
050   */
051  private transient SimpleDateFormat dateFormat = null;
052
053  /**
054   * Initialize this schedule configuration with the specified duration.
055   * @param duration the duration in milliseconds.
056   */
057  public JPPFSchedule(final long duration) {
058    this.duration = duration;
059    this.date = null;
060    this.format = null;
061  }
062
063  /**
064   * Initialize this schedule configuration with the specified fixed date annd date format.
065   * @param date the schedule date provided as a string.
066   * @param format the format in which the date is expressed (including locale and time zone information),
067   * as specified in the description of {@link SimpleDateFormat}.
068   */
069  public JPPFSchedule(final String date, final String format) {
070    this.duration = 0L;
071    this.date = date;
072    this.format = format;
073  }
074
075  /**
076   * Get the duration for this configuration.
077   * The time at which the duration starts dependants on who is using it.
078   * For instance, for scheduling a job, it starts when the job is inserted into the job queue by the server.
079   * @return the timeout in milliseconds.
080   */
081  public long getDuration() {
082    return duration;
083  }
084
085  /**
086   * Get the scheduled date for this configuration.
087   * @return the date in string format.
088   */
089  public String getDate() {
090    return date;
091  }
092
093  /**
094   * Get the format of timeout date for this task.
095   * @return the date format as a string pattern.
096   */
097  public String getFormat() {
098    return format;
099  }
100
101  /**
102   * Convert this schedule to a {@link Date} object.
103   * @param startDate the starting date to use if the schedule is expressed as a duration.
104   * @return this schedule expressed as a {@link Date}.
105   * @throws ParseException if parsing using the simple date format fails.
106   */
107  public Date toDate(final long startDate) throws ParseException {
108    Date dt = null;
109    if ((date == null) || (format == null)) dt = new Date(startDate + duration);
110    else {
111      if (dateFormat == null) dateFormat = new SimpleDateFormat(format);
112      dt = dateFormat.parse(date);
113    }
114    return dt;
115  }
116
117  /**
118   * Convert this schedule to a long value.
119   * @param startDate the starting date to use if the schedule is expressed as a duration.
120   * @return this schedule expressed as a long.
121   * @throws ParseException if parsing using the simple date format fails.
122   */
123  public long toLong(final long startDate) throws ParseException {
124    long result = 0L;
125    if ((date == null) || (format == null)) result = startDate + duration;
126    else {
127      if (dateFormat == null) dateFormat = new SimpleDateFormat(format);
128      final Date dt = dateFormat.parse(date);
129      result = dt.getTime();
130    }
131    return result;
132  }
133
134  @Override
135  public String toString() {
136    final StringBuilder sb = new StringBuilder("schedule[");
137    if (date != null) sb.append("date=").append(date).append(", format=").append(format == null ? "null" : format);
138    else sb.append("delay=").append(duration);
139    sb.append(']');
140    return sb.toString();
141  }
142
143  /**
144   * Determine whether this schedule was initialized with a date or not.
145   * @return true if a date was specified when constructing this schedule, false otherwise.
146   */
147  public boolean hasDate() {
148    return (date != null) && (format != null);
149  }
150
151  /**
152   * Determine whether this schedule was initialized with a duration or not.
153   * @return true if a duration (or timeout) was specified when constructing this schedule, false otherwise.
154   */
155  public boolean hasDuration() {
156    return duration > 0;
157  }
158}