- java.lang.Object
-
- swim.concurrent.AbstractTimer
-
- All Implemented Interfaces:
Timer
,TimerFunction
public abstract class AbstractTimer extends Object implements Timer
Skeletal implementation of a statefulTimer
.
-
-
Field Summary
Fields Modifier and Type Field Description protected TimerContext
timerContext
-
Constructor Summary
Constructors Constructor Description AbstractTimer()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description boolean
cancel()
Cancels this timer to prevent it from executing.boolean
isScheduled()
Returnstrue
if this timer is currently scheduled to execute.void
reschedule(long millis)
Schedules this timer to execute aftermillis
milliseconds has elapsed.abstract void
runTimer()
Executes scheduled logic when thisTimer
fires.Schedule
schedule()
Returns theSchedule
to which this timer is bound.void
setTimerContext(TimerContext timerContext)
Sets the context used to manage thisTimer
.TimerContext
timerContext()
Returns the context used to manage thisTimer
.void
timerDidCancel()
Lifecycle callback invoked after thisTimer
is explicitly cancelled.void
timerWillSchedule(long millis)
Lifecycle callback invoked before thisTimer
is scheduled for execution.
-
-
-
Field Detail
-
timerContext
protected TimerContext timerContext
-
-
Method Detail
-
timerContext
public final TimerContext timerContext()
Description copied from interface:Timer
Returns the context used to manage thisTimer
. Returnsnull
if thisTimer
is not bound to aSchedule
.- Specified by:
timerContext
in interfaceTimer
-
setTimerContext
public void setTimerContext(TimerContext timerContext)
Description copied from interface:Timer
Sets the context used to manage thisTimer
. ATimerContext
is assigned when binding thisTimer
to aSchedule
.- Specified by:
setTimerContext
in interfaceTimer
-
runTimer
public abstract void runTimer()
Description copied from interface:Timer
Executes scheduled logic when thisTimer
fires.- Specified by:
runTimer
in interfaceTimer
- Specified by:
runTimer
in interfaceTimerFunction
-
timerWillSchedule
public void timerWillSchedule(long millis)
Description copied from interface:Timer
Lifecycle callback invoked before thisTimer
is scheduled for execution.- Specified by:
timerWillSchedule
in interfaceTimer
-
timerDidCancel
public void timerDidCancel()
Description copied from interface:Timer
Lifecycle callback invoked after thisTimer
is explicitly cancelled.- Specified by:
timerDidCancel
in interfaceTimer
-
schedule
public Schedule schedule()
Returns theSchedule
to which this timer is bound. Delegates to the assignedtimerContext
.- Throws:
TimerException
- iftimerContext
isnull
.
-
isScheduled
public boolean isScheduled()
Returnstrue
if this timer is currently scheduled to execute. Delegates to the assignedtimerContext
, if set; otherwise returnsfalse
.
-
reschedule
public void reschedule(long millis)
Schedules this timer to execute aftermillis
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 assignedtimerContext
.- Throws:
TimerException
- iftimerContext
isnull
, or ifmillis
is negative.
-
cancel
public boolean cancel()
Cancels this timer to prevent it from executing. Returnstrue
if this operation caused the cancellation of this timer; returnsfalse
if thisTimer
was not scheduled to execute.
-
-