de.myfoo.commonj.timers
Class FooTimer

java.lang.Object
  extended by de.myfoo.commonj.timers.FooTimer
All Implemented Interfaces:
commonj.timers.Timer
Direct Known Subclasses:
FixedDelayTimer, FixedRateTimer, OneShotTimer

public abstract class FooTimer
extends java.lang.Object
implements commonj.timers.Timer

A FooTimer is returned when a TimerListener is scheduled using one of the FooTimerManager.schedule methods. It allows retrieving information about the scheduled TimerListener and allows cancelling it.

Version:
1.0
Author:
Kelly

Field Summary
protected  long period
           
protected  long scheduledExcecutionTime
           
protected  boolean stopped
           
 
Constructor Summary
FooTimer(long startTime, long period, commonj.timers.TimerListener listener)
          Creates a new instance of FooTimer.
 
Method Summary
 boolean cancel()
          This cancels the timer and all future TimerListener invocations and may be called during the TimerListener.timerExpired method.
protected abstract  void computeNextExecutionTime()
          Compute the next exceution time for this timer.
 void execute()
          Execute the timer listner.
 long getPeriod()
          Return the period used to compute the time this timer will repeat.
 long getScheduledExecutionTime()
          Returns the next absolute scheduled execution time in milliseconds.
 commonj.timers.TimerListener getTimerListener()
          Returns the application-supplied TimerListener associated with this Timer.
 boolean isCancelled()
          Getter for cancelled
 boolean isExpired()
          Check if this timer is expired and needs to be fired.
 void stop()
          Stop the timer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

stopped

protected boolean stopped

scheduledExcecutionTime

protected long scheduledExcecutionTime

period

protected long period
Constructor Detail

FooTimer

public FooTimer(long startTime,
                long period,
                commonj.timers.TimerListener listener)
Creates a new instance of FooTimer.

Parameters:
startTime - start time
period - execution period
listener - the timer listener for this timer.
Method Detail

computeNextExecutionTime

protected abstract void computeNextExecutionTime()
Compute the next exceution time for this timer.


execute

public void execute()
Execute the timer listner.


cancel

public boolean cancel()
This cancels the timer and all future TimerListener invocations and may be called during the TimerListener.timerExpired method.

CancelTimerListener.timerCancel events may be called concurrently with any TimerListener.timerExpired methods. Proper thread synchronization techiniques must be employed to ensure consistency.

Once a Timer is cancelled an application must not use the Timer instance again.

Specified by:
cancel in interface commonj.timers.Timer
Returns:
true if this prevented the next execution of this timer. false if this was already cancelled or had already expired in the one shot case.
See Also:
Timer.cancel()

getTimerListener

public commonj.timers.TimerListener getTimerListener()
                                              throws java.lang.IllegalStateException
Returns the application-supplied TimerListener associated with this Timer.

Specified by:
getTimerListener in interface commonj.timers.Timer
Returns:
The TimerListener associated with the timer.
Throws:
java.lang.IllegalStateException - if the TimerManager has been stopped.
See Also:
Timer.getTimerListener()

getScheduledExecutionTime

public long getScheduledExecutionTime()
                               throws java.lang.IllegalStateException
Returns the next absolute scheduled execution time in milliseconds.

If invoked while a TimerListener is running, the return value is the scheduled execution time of the current TimerListener execution.

If the timer has been suspended, the time reflects the most recently-calculated execution time prior to being suspended.

Specified by:
getScheduledExecutionTime in interface commonj.timers.Timer
Returns:
the time in milliseconds at which the TimerListener is scheduled to run next.
Throws:
java.lang.IllegalStateException - if the TimerManager has been stopped.
See Also:
Timer.getScheduledExecutionTime()

getPeriod

public long getPeriod()
Return the period used to compute the time this timer will repeat. A value of zero indicates that the timer is non-repeating.

Specified by:
getPeriod in interface commonj.timers.Timer
Returns:
the period in milliseconds between timer executions.
See Also:
Timer.getPeriod()

isCancelled

public boolean isCancelled()
Getter for cancelled

Returns:
Returns the cancelled.

stop

public void stop()
Stop the timer.


isExpired

public boolean isExpired()
Check if this timer is expired and needs to be fired.

Returns:
true if timer is expired.