001/*
002 * JPPF.
003 * Copyright (C) 2005-2016 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  /**
033   * Explicit serialVersionUID.
034   */
035  private static final long serialVersionUID = 1L;
036  /**
037   * Time in milliseconds, after which this task will be aborted.<br>
038   * A value of 0 or less indicates this task never times out.
039   */
040  private final long duration;
041  /**
042   * Schedule date as a string.
043   */
044  private final String date;
045  /**
046   * Schedule date format as a string.
047   */
048  private String format;
049  /**
050   * Format describing the schedule date.
051   */
052  private transient SimpleDateFormat dateFormat = null;
053
054  /**
055   * Initialize this schedule configuration with the specified duration.
056   * @param duration the duration in milliseconds.
057   */
058  public JPPFSchedule(final long duration)
059  {
060    this.duration = duration;
061    this.date = null;
062    this.format = null;
063  }
064
065  /**
066   * Initialize this schedule configuration with the specified fixed date annd date format.
067   * @param date the schedule date provided as a string.
068   * @param format the format in which the date is expressed (including locale and time zone information),
069   * as specified in the description of {@link SimpleDateFormat}.
070   */
071  public JPPFSchedule(final String date, final String format)
072  {
073    this.duration = 0L;
074    this.date = date;
075    this.format = format;
076  }
077
078  /**
079   * Get the duration for this configuration.
080   * The time at which the duration starts dependants on who is using it.
081   * For instance, for scheduling a job, it starts when the job is inserted into the job queue by the server.
082   * @return the timeout in milliseconds.
083   */
084  public long getDuration()
085  {
086    return duration;
087  }
088
089  /**
090   * Get the scheduled date for this configuration.
091   * @return the date in string format.
092   */
093  public String getDate()
094  {
095    return date;
096  }
097
098  /**
099   * Get the format of timeout date for this task.
100   * @return the date format as a string pattern.
101   */
102  public String getFormat()
103  {
104    return format;
105  }
106
107  /**
108   * Convert this schedule to a {@link Date} object.
109   * @param startDate the starting date to use if the schedule is expressed as a duration.
110   * @return this schedule expressed as a {@link Date}.
111   * @throws ParseException if parsing using the simple date format fails.
112   */
113  public Date toDate(final long startDate) throws ParseException
114  {
115    Date dt = null;
116    if ((date == null) || (format == null)) dt = new Date(startDate + duration);
117    else
118    {
119      if (dateFormat == null) dateFormat = new SimpleDateFormat(format);
120      dt = dateFormat.parse(date);
121    }
122    return dt;
123  }
124
125  /**
126   * Convert this schedule to a long value.
127   * @param startDate the starting date to use if the schedule is expressed as a duration.
128   * @return this schedule expressed as a long.
129   * @throws ParseException if parsing using the simple date format fails.
130   */
131  public long toLong(final long startDate) throws ParseException
132  {
133    long result = 0L;
134    if ((date == null) || (format == null)) result = startDate + duration;
135    else
136    {
137      if (dateFormat == null) dateFormat = new SimpleDateFormat(format);
138      Date dt = dateFormat.parse(date);
139      result = dt.getTime();
140    }
141    return result;
142  }
143
144  @Override
145  public String toString()
146  {
147    StringBuilder sb = new StringBuilder("schedule[");
148    if (date != null) sb.append("date=").append(date).append(", format=").append(format == null ? "null" : format);
149    else sb.append("delay=").append(duration);
150    sb.append(']');
151    return sb.toString();
152  }
153
154  /**
155   * Determine whether this schedule was initialized with a date or not.
156   * @return true if a date was specified when constructing this schedule, false otherwise.
157   */
158  public boolean hasDate()
159  {
160    return (date != null) && (format != null);
161  }
162
163  /**
164   * Determine whether this schedule was initialized with a duration or not.
165   * @return true if a duration (or timeout) was specified when constructing this schedule, false otherwise.
166   */
167  public boolean hasDuration()
168  {
169    return duration > 0;
170  }
171}