public abstract class Timer extends AsyncEvent implements ActiveEvent<Timer,TimeDispatcher>
Clock
. This class defines basic functionality
available to all timers. Applications will generally use either
PeriodicTimer
to create an event that is fired repeatedly at
regular intervals, or OneShotTimer
for an event that just
fires once at a specific time. A timer is always associated with at
least one Clock
, which provides the basic facilities of
something that ticks along following some time line (realtime,
CPU-time, user-time, simulation-time, etc.). All timers are created
disabled and do nothing until start()
is
called.Modifier | Constructor and Description |
---|---|
protected |
Timer(HighResolutionTime<?> time,
AsyncEventHandler handler,
TimeDispatcher dispatcher)
Creates a timer that fires according to the given
time based
on the Clock associated with time and is dispatched
by the specified dispatcher . |
protected |
Timer(HighResolutionTime<?> time,
Clock clock,
AsyncEventHandler handler)
Creates a timer that fires according to the given
time ,
which must be based on the supplied Clock clock (if
any), and is handled by the specified AsyncEventHandler
handler . |
Modifier and Type | Method and Description |
---|---|
void |
bindTo(java.lang.String happening)
Deprecated.
RTSJ 2.0
|
ReleaseParameters<?> |
createReleaseParameters()
Creates a
ReleaseParameters object appropriate to the
timing characteristics of this event. |
void |
destroy()
Deprecated.
since RTSJ 2.0
|
void |
disable()
Disables this timer, preventing it from firing.
|
void |
enable()
Re-enables this timer after it has been disabled.
|
Clock |
getClock()
Obtains the instance of
Clock on which this timer is based. |
TimeDispatcher |
getDispatcher()
Obtain the current dispatcher for this event.
|
AbsoluteTime |
getEffectiveStartTime()
Returns a newly-created time representing the time when the timer
actually started, or when the timer has been rescheduled, the
effective start time after the reschedule.
|
AbsoluteTime |
getEffectiveStartTime(AbsoluteTime dest)
Updates
dest to represent the time when the timer actually
started, or when the timer has been rescheduled, the effective start
time after the reschedule. |
AbsoluteTime |
getFireTime()
Gets the time at which this
Timer is expected to fire. |
AbsoluteTime |
getFireTime(AbsoluteTime dest)
Gets the time at which this
Timer is expected to fire. |
HighResolutionTime<?> |
getStart()
Gets the start time of this
Timer . |
boolean |
handledBy(AsyncEventHandler handler)
Determines whether or not the handler given as the parameter is
associated with
this . |
boolean |
isActive()
Determines the activation state of this happening,
i.e., it has been started.
|
boolean |
isRunning()
Determines if
this is active and is
enabled such that when the given time occurs it will
fire the event. |
void |
reschedule(HighResolutionTime<?> time)
Changes the scheduled time for this event.
|
TimeDispatcher |
setDispatcher(TimeDispatcher dispatcher)
Change the current dispatcher for this event.
|
void |
start()
Starts this timer.
|
void |
start(boolean disabled)
Starts this timer.
|
void |
start(boolean disabled,
PhasingPolicy phasingPolicy)
Starts the timer with the specified
PhasingPolicy and the
specified disabled state. |
void |
start(PhasingPolicy phasingPolicy)
Starts the timer with the specified
PhasingPolicy . |
boolean |
stop()
Stops a timer when it is active and changes its state
to inactive and disabled.
|
addHandler, fire, removeHandler, setHandler, unbindTo
addHandler, handledBy, hasHandlers, removeHandler, setHandler
protected Timer(HighResolutionTime<?> time, AsyncEventHandler handler, TimeDispatcher dispatcher) throws StaticIllegalArgumentException, StaticUnsupportedOperationException, IllegalAssignmentError
time
based
on the Clock
associated with time
and is dispatched
by the specified dispatcher
.time
- The parameter used to determine when to fire the event.
A time
value of null
is equivalent to a
RelativeTime
of 0, and in this case
the Timer
fires
immediately upon a call to start()
.handler
- The default handler
to use for this event.
When null
, no handler
is associated with the
timer and nothing will happen when this event fires unless a
handler
is subsequently associated with the timer
using the addHandler()
or setHandler()
method.dispatcher
- The object used to interface between
this timer
and its associated clock. When
null
, the system default dispatcher is used.StaticIllegalArgumentException
- when time
is a
negative RelativeTime
value.StaticUnsupportedOperationException
- when time
has a
Chronograph
is not a clock
.IllegalAssignmentError
- when this Timer
cannot hold references to
handler
and clock
.protected Timer(HighResolutionTime<?> time, Clock clock, AsyncEventHandler handler) throws StaticIllegalArgumentException, StaticUnsupportedOperationException, IllegalAssignmentError
time
,
which must be based on the supplied Clock
clock
(if
any), and is handled by the specified AsyncEventHandler
handler
. The system default dispatcher will be used.
This constructor is slated for deprecation in a future release, and
a constructor that does not receive a Clock
argument should
be used in preference.
time
- The parameter used to determine when to fire the event.
A time
value of null
is equivalent to a
RelativeTime
of 0, and in this case
the Timer
fires immediately upon a call to start()
.clock
- The clock on which to base this timer.
When null
, the clock associated with time
is used.handler
- The default handler
to use for this event.
When null
, no handler
is associated with the
timer and nothing will happen when this event fires unless a
handler
is subsequently associated with the timer
using the addHandler()
or setHandler()
method.StaticIllegalArgumentException
- when time
is a
negative RelativeTime
value or the supplied
clock
is not the Clock
associated with
time
.StaticUnsupportedOperationException
- when time
has a
Chronograph
that is not an instance of
Clock
.IllegalAssignmentError
- when this Timer
cannot hold references to handler
and clock
.public Clock getClock() throws StaticIllegalStateException
Clock
on which this timer
is based.Clock
associated with this
Timer
.StaticIllegalStateException
- when this Timer
has been destroyed.public HighResolutionTime<?> getStart()
Timer
. Note that the start time
uses copy semantics, so changes made to the value returned by this
method do not affect the start time of this Timer
.Timer
, ensuring the content has
the original values.public AbsoluteTime getEffectiveStartTime() throws StaticIllegalStateException, java.lang.ArithmeticException
this
actually started.StaticIllegalStateException
- when the timer is not active or has
been destroyed.java.lang.ArithmeticException
- when the result does not fit in the
normalized format.public AbsoluteTime getEffectiveStartTime(AbsoluteTime dest) throws StaticIllegalStateException, java.lang.ArithmeticException
dest
to represent the time when the timer actually
started, or when the timer has been rescheduled, the effective start
time after the reschedule.
When dest
is null
, behaves as if
getEffectiveStartTime()
had been called.dest
- An object used to store the time this
actually started.StaticIllegalStateException
- when the timer is not active or has
been destroyed.java.lang.ArithmeticException
- when the result does not fit in the
normalized format.public AbsoluteTime getFireTime() throws StaticIllegalStateException, java.lang.ArithmeticException
Timer
is expected to fire.
When the Timer
is disabled, the returned
time is that of the skipping or the firing.
When the Timer
is not-active, it throws
StaticIllegalStateException
.this
is expected to
fire (release handlers or skip), in a newly allocated AbsoluteTime
object.
When the timer has been created or re-scheduled
(see reschedule(javax.realtime.HighResolutionTime<?>)
)
using an instance of
RelativeTime
for its time parameter, then it will return
the sum of the current time and the RelativeTime
remaining time before the timer is expected to fire/skip.
The clock association of the returned time is the clock
on which this
timer is based.java.lang.ArithmeticException
- when the result does not fit
in the normalized format.StaticIllegalStateException
- when this Timer
has been destroyed, or when it is not-active.public AbsoluteTime getFireTime(AbsoluteTime dest) throws StaticIllegalStateException, java.lang.ArithmeticException
Timer
is expected to fire.
When the Timer
is disabled, the returned
time is that of the skipping or the firing.
When the Timer
is not-active it throws
StaticIllegalStateException
.dest
- The instance of AbsoluteTime
which will be updated in place and returned.
The clock association of the dest
parameter
is ignored.
When dest
is null
, a new object
is allocated for the result.AbsoluteTime
passed as parameter, with time values representing
the absolute time at which this
is expected to
fire (release its handlers or skip).
When the dest
parameter is null
,
the result is returned in a newly allocated object.
When the timer has been created or rescheduled
(see reschedule(javax.realtime.HighResolutionTime<?>)
)
using an instance of
RelativeTime
for its time parameter then it will return
the sum of the current time and the RelativeTime
remaining time before the timer is expected to fire.
The clock association of the returned time is the clock
on which this
timer is based.java.lang.ArithmeticException
- when the result does not fit
in the normalized format.StaticIllegalStateException
- when this Timer
has been destroyed, or when it is not-active.public TimeDispatcher getDispatcher()
getDispatcher
in interface ActiveEvent<Timer,TimeDispatcher>
getDispatcher
in interface Releasable<Timer,TimeDispatcher>
public TimeDispatcher setDispatcher(TimeDispatcher dispatcher)
dispatcher
is null
, the default dispatcher is restored.setDispatcher
in interface ActiveEvent<Timer,TimeDispatcher>
public boolean isActive()
isActive
in interface ActiveEvent<Timer,TimeDispatcher>
true
when active, false
otherwise.public boolean isRunning() throws StaticIllegalStateException
this
is active and is
enabled such that when the given time occurs it will
fire the event. Given the Timer
current state it answers
the question "Is firing expected?".isRunning
in interface ActiveEvent<Timer,TimeDispatcher>
isRunning
in class AsyncBaseEvent
true
when the timer is active and
enabled; otherwise
false
, when the timer has either not been started,
it has been started but it is disabled,
or it has been started and is now stopped.StaticIllegalStateException
- when this Timer
has been destroyed.public boolean handledBy(AsyncEventHandler handler) throws StaticIllegalStateException
AsyncEvent
this
.handledBy
in class AsyncEvent
handler
- An event handler to be added to the Timertrue
when handler
is associated with
this
, otherwise false
.StaticIllegalStateException
- when this Timer
has been
destroyed.public ReleaseParameters<?> createReleaseParameters() throws StaticIllegalStateException
ReleaseParameters
object appropriate to the
timing characteristics of this event.
The default is the most pessimistic: AperiodicParameters
.
This is typically called by code that is setting up a
handler
for
this event that will fill in the parts of the release parameters
for which it has values, e.g. cost.createReleaseParameters
in class AsyncBaseEvent
ReleaseParameters
object.StaticIllegalStateException
- when this Timer
has been
destroyed.public void enable() throws StaticIllegalStateException
disable()
.)
When the Timer
is already enabled,
this method does nothing.
When the Timer
is not-active,
this method does nothing.enable
in interface ActiveEvent<Timer,TimeDispatcher>
enable
in class AsyncBaseEvent
StaticIllegalStateException
- when this Timer
has been destroyed.public void disable() throws StaticIllegalStateException
RelativeTime
for its time parameter continues
to count while it is disabled, and no changes take place in
a disabled timer created using an instance of
AbsoluteTime
. In both cases the potential firing is
simply masked, or skipped.
When the timer is subsequently re-enabled
before its fire time or(?) it is
enabled when its fire time occurs, then it will fire.
It is important to note that this method does not delay
the time before a possible firing.
For example, when the timer is set to fire at time
42 and the disable()
is called at time 30 and
enable()
is called at time 40 the firing will occur
at time 42 (not time 52). These semantics imply
also that firings are not queued. Using the above example,
when enable was called at time 43 no firing will occur,
since at time 42 this
was disabled.
When the Timer
is already disabled,
whether it is active or inactive,
this method does nothing.disable
in interface ActiveEvent<Timer,TimeDispatcher>
disable
in class AsyncBaseEvent
StaticIllegalStateException
- when this Timer
has been destroyed.public void start() throws StaticIllegalStateException
start
in interface ActiveEvent<Timer,TimeDispatcher>
StaticIllegalStateException
- when this Timer
has been destroyed,
or when this timer is already active.public void start(boolean disabled) throws StaticIllegalStateException
disabled
is true
starts the
timer making it active in a disabled state.
When disabled
is false
this method behaves
like the start()
method.start
in interface ActiveEvent<Timer,TimeDispatcher>
disabled
- When true
, the timer will be
active but disabled after it is started.
When false
this method behaves
like the start()
method.StaticIllegalStateException
- when this Timer
has been destroyed,
or when this timer is active.public void start(PhasingPolicy phasingPolicy) throws LateStartException, StaticIllegalArgumentException
PhasingPolicy
.phasingPolicy
- Determines what happens when the start is too late.LateStartException
- when this method is called after its absolute
start time and the phasingPolicy
is
PhasingPolicy.STRICT_PHASING
.StaticIllegalArgumentException
- when the start time of this timer
is not an absolute time, or phasingPolicy
is
null
or, when this in not a periodic timer,
ADJUST_FORWARD
or ADJUST_BACKWARD
.public void start(boolean disabled, PhasingPolicy phasingPolicy) throws LateStartException, StaticIllegalArgumentException
PhasingPolicy
and the
specified disabled state.disabled
- It determines the mode of start: true
for
enabled and false
for disabled for consistency with
start(boolean)
.phasingPolicy
- It determines what happens when the start is too late.LateStartException
- when this method is called after its absolute
start time and the phasingPolicy
is
PhasingPolicy.STRICT_PHASING
.StaticIllegalArgumentException
- when the start time of this timer
is not an absolute time, or phasingPolicy
is
null
or, when this in not a periodic timer,
ADJUST_FORWARD
or ADJUST_BACKWARD
.public boolean stop() throws StaticIllegalStateException
stop
in interface ActiveEvent<Timer,TimeDispatcher>
true
when this
was
enabled and false
otherwise.StaticIllegalStateException
- when this Timer
has been destroyed.public void reschedule(HighResolutionTime<?> time) throws StaticIllegalStateException, StaticIllegalArgumentException
AbsoluteTime
or a RelativeTime
for its argument, and the
Timer
will behave as if created using that type
for its time
parameter.
The rescheduling will take place between the invocation and
the return of the method.
Note that while the scheduled time is changed as described above,
the rescheduling itself is applied only on the first firing
(or on the first skipping when disabled) of a timer's
activation. When reschedule
is invoked after the
current activation timer's firing, then the rescheduled
time
will be effective only upon the next
start
or startDisabled
command
(which may need to be preceded by a stop
command).
When reschedule
is invoked with a
RelativeTime
time
on an
active timer before its first firing/skipping, then
the rescheduled firing/skipping time
is
relative to the time of invocation.
time
- The time to reschedule for this event firing.
When time
is null
, the previous
time is still the time used for the Timer
firing.StaticIllegalArgumentException
- when time
is a
negative RelativeTime
value.StaticIllegalStateException
- when this Timer
has been
destroyed.@Deprecated public void destroy() throws StaticIllegalStateException
this
from counting or comparing
when active, removes from it all the
associated handlers if any, and releases as many of its
resources as possible back to the system.
Every method invoked on a Timer
that has been destroyed
will throw StaticIllegalStateException
.StaticIllegalStateException
- when this Timer
has been
destroyed.@Deprecated public void bindTo(java.lang.String happening) throws StaticUnsupportedOperationException
AsyncEvent
, not to special subclasses.bindTo
in class AsyncEvent
happening
- to which to bindStaticUnsupportedOperationException
- when bindTo
is called on a Timer
.