unsw.cse.mica.tools.timer
Class Timer

java.lang.Object
  extended by unsw.cse.mica.tools.timer.Timer
All Implemented Interfaces:
java.lang.Runnable

public class Timer
extends java.lang.Object
implements java.lang.Runnable

The Timer class provides the functionality for an agent that can provides events at specific times. Normally it is wrapped inside a TimerAgent to provide an agent capable of processing event request mobs and generating new mobs as events fire. As there are times when atighter coupling is needed between an agent and the timing functionality, the core functionality is kept separate from the TimerAgent interface. An example of this is in the TimerMicaBot, which uses its own internal timer to provide temporal functionality within FrameScript.

Author:
jhw

Constructor Summary
Timer(ITimerHandler handler)
          Create a new timer with the given handler
 
Method Summary
 void addEvent(TimerEvent event)
          Add the given event to the event list
 void cancelAllEvents()
          Cancel all the events currently in the queue.
 void cancelEvents(java.util.List names)
          Cancel all the events whose names are in the list given
 void init()
          Initialise the timer by starting the internal thread.
 void run()
          The main loop for the internal thread.
 void terminate()
          Terminate the timer.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Timer

public Timer(ITimerHandler handler)
Create a new timer with the given handler

Parameters:
handler -
Method Detail

init

public void init()
Initialise the timer by starting the internal thread.


terminate

public void terminate()
Terminate the timer. This method sets a flag and returns. The flag is an indication to the internal thread that it should terminate itself. This does not happen instantly but will occur soon (within around a second) of this call.


cancelAllEvents

public void cancelAllEvents()
Cancel all the events currently in the queue.


cancelEvents

public void cancelEvents(java.util.List names)
Cancel all the events whose names are in the list given

Parameters:
names - a list assumed to contain Strings, representing the names of the events that are to be cancelled.

addEvent

public void addEvent(TimerEvent event)
Add the given event to the event list

Parameters:
event -

run

public void run()
The main loop for the internal thread. Repeatedly looks for the next event to fire and sleeps till the appropriate time. If the event is ready to fire then fire it, otherwise re-evaluate as new events may have been added (causing the thread to be interrupted prematurely).

Specified by:
run in interface java.lang.Runnable