Class AbstractTimer

java.lang.Object
swim.concurrent.AbstractTimer
All Implemented Interfaces:
Timer, TimerFunction

public abstract class AbstractTimer extends Object implements Timer
Skeletal implementation of a stateful Timer.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected TimerContext
    Context used to manage this Timer; null if this Timer is not bound to a Schedule.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Cancels this timer to prevent it from executing.
    boolean
    Returns true if this timer is currently scheduled to execute.
    void
    reschedule(long millis)
    Schedules this timer to execute after millis milliseconds has elapsed.
    abstract void
    Executes scheduled logic when this Timer fires.
    Returns the Schedule to which this timer is bound.
    void
    Sets the context used to manage this Timer.
    Returns the context used to manage this Timer.
    void
    Lifecycle callback invoked after this Timer is explicitly cancelled.
    void
    timerWillSchedule(long millis)
    Lifecycle callback invoked before this Timer is scheduled for execution.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • timerContext

      protected TimerContext timerContext
      Context used to manage this Timer; null if this Timer is not bound to a Schedule.
  • Constructor Details

    • AbstractTimer

      public AbstractTimer()
  • Method Details

    • timerContext

      public final TimerContext timerContext()
      Description copied from interface: Timer
      Returns the context used to manage this Timer. Returns null if this Timer is not bound to a Schedule.
      Specified by:
      timerContext in interface Timer
    • setTimerContext

      public void setTimerContext(TimerContext timerContext)
      Description copied from interface: Timer
      Sets the context used to manage this Timer. A TimerContext is assigned when binding this Timer to a Schedule.
      Specified by:
      setTimerContext in interface Timer
    • runTimer

      public abstract void runTimer()
      Description copied from interface: Timer
      Executes scheduled logic when this Timer fires.
      Specified by:
      runTimer in interface Timer
      Specified by:
      runTimer in interface TimerFunction
    • timerWillSchedule

      public void timerWillSchedule(long millis)
      Description copied from interface: Timer
      Lifecycle callback invoked before this Timer is scheduled for execution.
      Specified by:
      timerWillSchedule in interface Timer
    • timerDidCancel

      public void timerDidCancel()
      Description copied from interface: Timer
      Lifecycle callback invoked after this Timer is explicitly cancelled.
      Specified by:
      timerDidCancel in interface Timer
    • schedule

      public Schedule schedule()
      Returns the Schedule to which this timer is bound. Delegates to the assigned timerContext.
      Throws:
      TimerException - if timerContext is null.
    • isScheduled

      public boolean isScheduled()
      Returns true if this timer is currently scheduled to execute. Delegates to the assigned timerContext, if set; otherwise returns false.
    • reschedule

      public void reschedule(long millis)
      Schedules this timer to execute after millis milliseconds has elapsed. If this timer is currently scheduled, it will not execute at its previously scheduled time; it will only execute at the newly scheduled time. Delegates to the assigned timerContext.
      Throws:
      TimerException - if timerContext is null, or if millis is negative.
    • cancel

      public boolean cancel()
      Cancels this timer to prevent it from executing. Returns true if this operation caused the cancellation of this timer; returns false if this Timer was not scheduled to execute.