/**
 * Copyright (c) 2002, Peace Technology, Inc.
 * $Author:Roytman, Alex$
 * $Revision$
 * $Date$
 * $NoKeywords$
 */

package com.peacetech.webtools.tomcat.dbcp;

import java.util.Date;
/**
 * Compatibility layer with java.util.Timer to avoid JDK 1.3 dependency
 */
public interface TimerService {
  /**
   * Schedules the specified task for execution after the specified delay.
   *
   * @param task  task to be scheduled.
   * @param delay delay in milliseconds before task is to be executed.
   * @throws IllegalArgumentException if <tt>delay</tt> is negative, or
   *         <tt>delay + System.currentTimeMillis()</tt> is negative.
   * @throws IllegalStateException if task was already scheduled or
   *         cancelled, or timer was cancelled.
   */
  public void schedule(Runnable task, long delay);

  /**
   * Schedules the specified task for execution at the specified time.  If
   * the time is in the past, the task is scheduled for immediate execution.
   *
   * @param task task to be scheduled.
   * @param time time at which task is to be executed.
   * @throws IllegalArgumentException if <tt>time.getTime()</tt> is negative.
   * @throws IllegalStateException if task was already scheduled or
   *         cancelled, timer was cancelled, or timer thread terminated.
   */
  public void schedule(Runnable task, Date time);

  /**
   * Schedules the specified task for repeated <i>fixed-delay execution</i>,
   * beginning after the specified delay.  Subsequent executions take place
   * at approximately regular intervals separated by the specified period.
   *
   * <p>In fixed-delay execution, each execution is scheduled relative to
   * the actual execution time of the previous execution.  If an execution
   * is delayed for any reason (such as garbage collection or other
   * background activity), subsequent executions will be delayed as well.
   * In the long run, the frequency of execution will generally be slightly
   * lower than the reciprocal of the specified period (assuming the system
   * clock underlying <tt>Object.wait(long)</tt> is accurate).
   *
   * <p>Fixed-delay execution is appropriate for recurring activities
   * that require "smoothness."  In other words, it is appropriate for
   * activities where it is more important to keep the frequency accurate
   * in the short run than in the long run.  This includes most animation
   * tasks, such as blinking a cursor at regular intervals.  It also includes
   * tasks wherein regular activity is performed in response to human
   * input, such as automatically repeating a character as long as a key
   * is held down.
   *
   * @param task   task to be scheduled.
   * @param delay  delay in milliseconds before task is to be executed.
   * @param period time in milliseconds between successive task executions.
   * @throws IllegalArgumentException if <tt>delay</tt> is negative, or
   *         <tt>delay + System.currentTimeMillis()</tt> is negative.
   * @throws IllegalStateException if task was already scheduled or
   *         cancelled, timer was cancelled, or timer thread terminated.
   */
  public void schedule(Runnable task, long delay, long period);

  /**
   * Schedules the specified task for repeated <i>fixed-delay execution</i>,
   * beginning at the specified time. Subsequent executions take place at
   * approximately regular intervals, separated by the specified period.
   *
   * <p>In fixed-delay execution, each execution is scheduled relative to
   * the actual execution time of the previous execution.  If an execution
   * is delayed for any reason (such as garbage collection or other
   * background activity), subsequent executions will be delayed as well.
   * In the long run, the frequency of execution will generally be slightly
   * lower than the reciprocal of the specified period (assuming the system
   * clock underlying <tt>Object.wait(long)</tt> is accurate).
   *
   * <p>Fixed-delay execution is appropriate for recurring activities
   * that require "smoothness."  In other words, it is appropriate for
   * activities where it is more important to keep the frequency accurate
   * in the short run than in the long run.  This includes most animation
   * tasks, such as blinking a cursor at regular intervals.  It also includes
   * tasks wherein regular activity is performed in response to human
   * input, such as automatically repeating a character as long as a key
   * is held down.
   *
   * @param task   task to be scheduled.
   * @param firstTime First time at which task is to be executed.
   * @param period time in milliseconds between successive task executions.
   * @throws IllegalArgumentException if <tt>time.getTime()</tt> is negative.
   * @throws IllegalStateException if task was already scheduled or
   *         cancelled, timer was cancelled, or timer thread terminated.
   */
  public void schedule(Runnable task, Date firstTime, long period);

  /**
   * Schedules the specified task for repeated <i>fixed-rate execution</i>,
   * beginning after the specified delay.  Subsequent executions take place
   * at approximately regular intervals, separated by the specified period.
   *
   * <p>In fixed-rate execution, each execution is scheduled relative to the
   * scheduled execution time of the initial execution.  If an execution is
   * delayed for any reason (such as garbage collection or other background
   * activity), two or more executions will occur in rapid succession to
   * "catch up."  In the long run, the frequency of execution will be
   * exactly the reciprocal of the specified period (assuming the system
   * clock underlying <tt>Object.wait(long)</tt> is accurate).
   *
   * <p>Fixed-rate execution is appropriate for recurring activities that
   * are sensitive to <i>absolute</i> time, such as ringing a chime every
   * hour on the hour, or running scheduled maintenance every day at a
   * particular time.  It is also appropriate for for recurring activities
   * where the total time to perform a fixed number of executions is
   * important, such as a countdown timer that ticks once every second for
   * ten seconds.  Finally, fixed-rate execution is appropriate for
   * scheduling multiple repeating timer tasks that must remain synchronized
   * with respect to one another.
   *
   * @param task   task to be scheduled.
   * @param delay  delay in milliseconds before task is to be executed.
   * @param period time in milliseconds between successive task executions.
   * @throws IllegalArgumentException if <tt>delay</tt> is negative, or
   *         <tt>delay + System.currentTimeMillis()</tt> is negative.
   * @throws IllegalStateException if task was already scheduled or
   *         cancelled, timer was cancelled, or timer thread terminated.
   */
  public void scheduleAtFixedRate(Runnable task, long delay, long period);

  /**
   * Schedules the specified task for repeated <i>fixed-rate execution</i>,
   * beginning at the specified time. Subsequent executions take place at
   * approximately regular intervals, separated by the specified period.
   *
   * <p>In fixed-rate execution, each execution is scheduled relative to the
   * scheduled execution time of the initial execution.  If an execution is
   * delayed for any reason (such as garbage collection or other background
   * activity), two or more executions will occur in rapid succession to
   * "catch up."  In the long run, the frequency of execution will be
   * exactly the reciprocal of the specified period (assuming the system
   * clock underlying <tt>Object.wait(long)</tt> is accurate).
   *
   * <p>Fixed-rate execution is appropriate for recurring activities that
   * are sensitive to <i>absolute</i> time, such as ringing a chime every
   * hour on the hour, or running scheduled maintenance every day at a
   * particular time.  It is also appropriate for for recurring activities
   * where the total time to perform a fixed number of executions is
   * important, such as a countdown timer that ticks once every second for
   * ten seconds.  Finally, fixed-rate execution is appropriate for
   * scheduling multiple repeating timer tasks that must remain synchronized
   * with respect to one another.
   *
   * @param task   task to be scheduled.
   * @param firstTime First time at which task is to be executed.
   * @param period time in milliseconds between successive task executions.
   * @throws IllegalArgumentException if <tt>time.getTime()</tt> is negative.
   * @throws IllegalStateException if task was already scheduled or
   *         cancelled, timer was cancelled, or timer thread terminated.
   */
  public void scheduleAtFixedRate(Runnable task, Date firstTime, long period);

  /**
   * Terminates this timer, discarding any currently scheduled tasks.
   * Does not interfere with a currently executing task (if it exists).
   * Once a timer has been terminated, its execution thread terminates
   * gracefully, and no more tasks may be scheduled on it.
   *
   * <p>Note that calling this method from within the run method of a
   * timer task that was invoked by this timer absolutely guarantees that
   * the ongoing task execution is the last task execution that will ever
   * be performed by this timer.
   *
   * <p>This method may be called repeatedly; the second and subsequent
   * calls have no effect.
   */
  public void cancel();

  /**
   * terminats task. If task is instance of TimerTask it's cancel method is invoked
   * otherwise TimerTask associated with task instance looked up in HashMap and its
   * cancel() method get invoked
   * @param task to be canceled
   */
  public void cancelTask(Object task);

}
