|
Components |
|
| |||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
com.sas.util.Alarm
public class Alarm
A timed alarm which fires events at regular intervals.
This object allows you to set an alarm at a specified
time in the future. When the time expires, the alarm will
send an AlarmEvent
to all AlarmListener
s.
The alarm can auto-repeat indefinitely or a fixed number of times.
For example, an Alarm
object may be used in a document editor
to support an autosave operation. An alarm can be created to
fire at five minute intervals (5 * Alarm.ONE_MINUTE
)
and the alarm listener can be an object which performs the
autosave if the document has been modified since the last
autosave.
Although Alarm
s have a run()
method, they do not implement java.lang.Runnable
since the Alarm
implementation is expected to provide the alarm thread.
All time units are measured in milliseconds.
If an alarm is serialized, it saves its properties and when it is deserialized,
the alarm will automatically start itself if it was running when serialized,
and if the current time is still less than the stop time. However,
the alarm does not store its AlarmEvent
listeners when serialized,
so you must call addAlarmListener
after deserializing an alarm from
a serial stream.
If an alarm is created in a design time environment (such as in an
IDE), the count
property is not decremented when the
alarm fires. This preserves the count
property
for use at run time, when the Alarm
is deserialized
in a running application.
For example, if you wish to create an Alarm which fires events for a listener at 24:00 intervals, starting at midnight, you can use the following:
Alarm alarm = new Alarm(listener, com.sas.util.CurrentDate.millisUntilMidnight(null), Alarm.ONE_DAY, (int) 0);Other unit time intervals are available from
TimerInterface
,
which Alarm
implements by way of AlarmInterface
.
Properties (properties defined in TimerInterface)
Nested Class Summary | |
---|---|
protected class |
Alarm.AlarmThread
|
Field Summary | |
---|---|
protected Alarm.AlarmThread |
alarmThread
A separate thread used to implement the alarm timeout. |
protected int |
count
Number of times to auto-repeat before stopping. |
protected long |
delay
How long to actually delay before triggering this alarm each time it is started. |
protected long |
internalDelay
How long until the next alarm will fire. |
protected long |
interval
Duration of the auto-repeat interval, in milliseconds. |
protected long |
startTime
Time alarm was last started |
protected long |
stopTime
Time alarm was last stopped |
protected long |
timeLastFired
Keep track of the time this alarm last fired, in milliseconds since the Epoch. |
Fields inherited from interface com.sas.util.TimeIntervals |
---|
ONE_DAY, ONE_HOUR, ONE_MINUTE, ONE_SECOND, ONE_WEEK |
Constructor Summary | |
---|---|
Alarm()
Default no-arg constructor. |
|
Alarm(AlarmListener listener,
java.util.Calendar startDateTime,
java.util.Calendar stopDateTime,
long intervalMillis)
Construct and start an alarm which will fire at the time specified by the start date/time, and recur at the specified interval until the stop date/time. |
|
Alarm(AlarmListener listener,
java.util.Calendar startDateTime,
long intervalMillis,
int count)
Construct an alarm which will fire at the time specified by the java.util.Calendar
and recur at the specified interval. |
|
Alarm(AlarmListener listener,
long millisecondDelay,
long intervalMillis,
int count)
Create and start an alarm which begins firing AlarmEvent s
millisecondDelay milliseconds from now,
repeating every intervalMillis milliseconds,
until it has fired count times (or indefinitely if
count is 0). |
|
Alarm(AlarmListener listener,
long startTimeMillis,
long stopTimeMillis,
long intervalMillis)
Construct and start an alarm which will fire at the time specified by the startTimeMillis, and recur at the specified interval until the stopTimeMillis. |
Method Summary | |
---|---|
void |
addAlarmListener(AlarmListener listener)
Add a listener for the event. |
protected int |
decrementCount()
Decrement the count, but only at run time, not at design time. |
protected void |
fireAlarm(AlarmEvent event)
Send an alarm event to all listeners. |
int |
getCount()
Get the number of times this alarm will fire before stopping. |
long |
getDelay()
Get the amount of time this alarm will wait before firing following a start() . |
long |
getDuration()
Return the number of milliseconds this alarm has been running. |
static com.sas.beans.ExtendedBeanInfo |
getExtendedBeanInfo()
|
long |
getInterval()
Return this alarm's interval, which measures the time interval at which this alarm repeats. |
long |
getStartTime()
Retrieve the start time |
long |
getStopTime()
Retrieve the stop time |
protected void |
init(AlarmListener listener,
long millisecondDelay,
long intervalMillis,
int alarmCount)
|
boolean |
isRunning()
Test if an alarm is running. |
protected void |
onStop()
Respond to a stop. |
void |
removeAlarmListener(AlarmListener listener)
Remove a listener for the event. |
protected void |
reset()
Reset this alarm. |
protected void |
resetCount()
Reset the count based on a start/stop interval. |
void |
setCount(int alarmCount)
Set the number of times this alarm will fire. |
void |
setDelay(long millisecondDelay)
Set the initial amount of time this alarm will wait before first firing when started. |
void |
setInterval(long intervalMillis)
Set the interval at which this alarm fires. |
void |
setRunning(boolean running)
Set the state of this alarm to be running or stopped. |
void |
setStartTime(long startTimeMillis)
Set this alarm's start time, the time when this alarm will begin firing alarm events. |
void |
setStopTime(long stopTimeMillis)
Set the timer's stop time. |
void |
start()
Start this alarm. |
void |
stop()
Stop this alarm if it is running. |
java.lang.String |
toString()
Return a string representation of the alarm. |
protected void |
triggerAlarm()
Trigger this alarm. |
Field Detail |
---|
protected long interval
protected long timeLastFired
protected long delay
protected transient long internalDelay
protected long startTime
protected long stopTime
protected int count
protected transient Alarm.AlarmThread alarmThread
Constructor Detail |
---|
public Alarm()
public Alarm(AlarmListener listener, long millisecondDelay, long intervalMillis, int count)
AlarmEvent
s
millisecondDelay
milliseconds from now,
repeating every intervalMillis
milliseconds,
until it has fired count
times (or indefinitely if
count
is 0).
Note that this constructor is unfortunately very similar to
new Alarm(AlarmListener listener, long startTimeMillis, long
stopTimeMillis, long intervalMillis)
and differs only in the type of the last argument, so be careful
to use the right constructor.
listener
- an initial listener for the AlarmEvent
. More may be added later,
but the Alarm may fire between Alarm
construction and later
calls to addAlarmListener
. This listener is added to the listener
list immediately, before the alarm is scheduled.millisecondDelay
- the amount of time to delay before firing the alarm
after a start()
. This can differ from interval
.
For example, if you want an alarm to fire at a fixed time
each day starting at some day in the future,
millisecondDelay will be the number of milliseconds
until that time, and intervalMillis will be 24 hours
(Alarm.ONE_DAY
, or 24 * 60 * 60 * 1000).intervalMillis
- if greater than 0, then automatically reschedule
the alarm this many time units after it goes
off.count
- the number of times the alarm will fire before stopping
automatically. This is only honored if intervalMillis is non-zero.
If count is 0, the alarm will recur indefinitely. Otherwise,
count is decremented each time the alarm fires.
If you restart an expired alarm, you must reset the count
if you want a limited number of alarm events. Note: count
does not decrement if this alarm is created in a design time environment.public Alarm(AlarmListener listener, long startTimeMillis, long stopTimeMillis, long intervalMillis)
Note that this constructor is unfortunately very similar to
new Alarm(AlarmListener listener, long millisecondDelay, long intervalMillis, int count)
and differs only in the type of the last argument, so be careful
to use the right constructor.
listener
- an initial listener for the AlarmEvent
. More may be added later,
but the alarm may fire between alarm construction and later
calls to addAlarmListener
. This listener is added to the listener
list immediately, before the alarm is scheduled.startTimeMillis
- the time, in milliseconds since the Epoch, when the
alarm should start. Use 0 to start immediately, or
System.currentTimeMillis() + millisecondDelay
to start at a specific delay.stopTimeMillis
- the time, in milliseconds since the Epoch, when the
alarm should stop.
The alarm will try fire at intervals up to and including the stop time, but
the alarm will not fire after stopTimeMillis + intervalMillis/2
.
If stopTimeMillis == 0 and intervalMillis is non-zero, the alarm repeats indefinitely.intervalMillis
- if greater than 0, then automatically reschedule
the alarm this many time units after it goes
off, until the stop time has passed.public Alarm(AlarmListener listener, java.util.Calendar startDateTime, long intervalMillis, int count)
java.util.Calendar
and recur at the specified interval.
Note that this constructor is unfortunately very similar to
new Alarm(AlarmListener listener, Calendar startDateTime, Calendar stopDateTime, long intervalMillis)
and differs only in the type of the last argument, so be careful
to use the right constructor.
listener
- an initial listener for the AlarmEvent
. More may be added later,
but the alarm may fire between alarm construction and later
calls to addAlarmListener
. This listener is added to the listener
list immediately, before the alarm is scheduled.startDateTime
- the java.util.Calendar
time when the
alarm should start. Use null to start immediately.intervalMillis
- if greater than 0, then automatically reschedule
the alarm this many time units after it goes
off.count
- the number of times the alarm will fire before stopping
automatically. This is only honored if intervalMillis is non-zero.
If count is 0, the alarm will recur indefinitely. Otherwise,
count is decremented each time the alarm fires.
If you restart an expired alarm, you must reset the count
if you want a limited number of alarm events. Note: count
does not decrement if this alarm is created in a design time environment.public Alarm(AlarmListener listener, java.util.Calendar startDateTime, java.util.Calendar stopDateTime, long intervalMillis)
Note that this constructor is unfortunately very similar to
new Alarm(AlarmListener listener, Calendar startDateTime, long intervalMillis, int count)
and differs only in the type of the last argument, so be careful
to use the right constructor.
listener
- an initial listener for the AlarmEvent
. More may be added later,
but the alarm may fire between alarm construction and later
calls to addAlarmListener
. This listener is added to the listener
list immediately, before the alarm is scheduled.startDateTime
- the java.util.Calendar
time when the
alarm should start. Use null to start immediately.stopDateTime
- The time at which the alarm should stop.
The alarm will try fire at intervals up to and including the stop time, but
the alarm will not fire after stop time + intervalMillis/2.
If stopDateTime is null and intervalMillis is non-zero, the alarm repeats indefinitely.intervalMillis
- if greater than 0, then automatically reschedule
the alarm this many time units after it goes
off, until the stop Time has passed.Method Detail |
---|
public static com.sas.beans.ExtendedBeanInfo getExtendedBeanInfo()
protected void init(AlarmListener listener, long millisecondDelay, long intervalMillis, int alarmCount)
protected void resetCount()
getCount()
public long getStartTime()
getStartTime
in interface TimerInterface
stop()
,
start()
public void setStartTime(long startTimeMillis)
java.util.Calendar
to milliseconds,
you can use Util.toMillis(java.util.Calendar)
setStartTime
in interface AlarmInterface
startTimeMillis
- the time the timer should start, in number of milliseconds since
the Epochstart()
public long getStopTime()
getStopTime
in interface TimerInterface
stop()
,
start()
public void setStopTime(long stopTimeMillis)
The count is reset to the number of intervals between the start time and stop time (if not running) or the number of remaining intervals between the current time and the stop time.
setStopTime
in interface AlarmInterface
stopTimeMillis
- the time the timer should stop, in number of milliseconds since
the Epoch. If stopTimeMillis and count are 0
and interval is greater than zero, the timer will repeat indefinitely.stop()
,
start()
public boolean isRunning()
isRunning
in interface TimerInterface
true
if stop() has not been called since start() was last
called and this alarm has not reached it set stop time.
false
otherwise.stop()
,
start()
public final void setRunning(boolean running)
setRunning
in interface TimerInterface
running
- the new running state. If this is
the same as the current state, nothing happens.
Otherwise, if true
, then call start()
, else call stop()
.
This method is an alias for calling start()
and stop()
,
and is therefore final
.
If you extend the Alarm
class, you should override those
methods to change behavior.stop()
,
start()
public long getDuration()
getDuration
in interface TimerInterface
public long getInterval()
getInterval
in interface AlarmInterface
setInterval(long)
public void setInterval(long intervalMillis)
ONE_HOUR
to ONE_SECOND
;
you don't want to have to wait up to an hour for the new
interval to take effect.
If the new interval is larger
than the current interval and this alarm is running,
this alarm will still fire as previously scheduled, then
assume the new interval.
setInterval
in interface AlarmInterface
intervalMillis
- the new alarm interval, in milliseconds.
Setting the interval to 0 or a negative integer has the effect of
stopping the alarm.getInterval()
,
stop()
,
start()
public void setDelay(long millisecondDelay)
java.util.Calendar
,
you can use Util.millisUntil(java.util.Calendar)
setDelay
in interface AlarmInterface
millisecondDelay
- this alarm delay, in milliseconds.public void setCount(int alarmCount)
setCount
in interface AlarmInterface
alarmCount
- the number of times this alarm will fire.
alarmCount is valid only if the interval is non-zero.public int getCount()
getCount
in interface AlarmInterface
public long getDelay()
start()
. This delay is not reset to 0 after
a stop()
.
getDelay
in interface AlarmInterface
public void start()
start
in interface TimerInterface
stop()
,
setRunning(boolean)
public void stop()
stop
in interface TimerInterface
start()
,
setRunning(boolean)
protected void onStop()
"running"
and "stopTime"
properties.
public void addAlarmListener(AlarmListener listener)
AlarmEvent
event.
addAlarmListener
in interface AlarmInterface
addAlarmListener
in interface AlarmSource
listener
- an object which handles AlarmEvent
events
the listener is not added a second time if it already exists
in the list of listeners for this event.AlarmSource
public void removeAlarmListener(AlarmListener listener)
AlarmEvent
event. Nothing
happens if the listener is not in the list of listeners for this event.
removeAlarmListener
in interface AlarmInterface
removeAlarmListener
in interface AlarmSource
listener
- an object which handles AlarmEvent
eventsAlarmSource
protected void reset()
timeLastFired
,
the variable which record the time the alarm was last fired.
protected void fireAlarm(AlarmEvent event)
event
- an AlarmEvent
to send to the listeners.protected int decrementCount()
protected void triggerAlarm()
AlarmEvent
and then reschedule this alarm if it is recurring.
public java.lang.String toString()
toString
in class java.lang.Object
|
Components |
|
| |||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |