gizmoball
Class JavaUtilTimerClock

java.lang.Object
  extended by gizmoball.JavaUtilTimerClock
All Implemented Interfaces:
Clock

public final class JavaUtilTimerClock
extends Object
implements Clock

An implementation of the Clock interface using Timer. Each JavaUtilTimerClock object wraps a java.util.Timer object and uses that to dispatch the tasks that it is in charge of running.

JavaUtilTimerClock also has the extra bonus that it is a thread-safe class. Multiple threads may share a JavaUtilTimerClock object without requiring external synchronization.

Note that all the specification fields are inherited from Clock. For a canonical list of them, see the specifications for Clock.


Field Summary
static String rcsid
          Class revision identifier.
 
Constructor Summary
JavaUtilTimerClock(Board board, int interval)
          Constructs a new stopped Clock with the given Board and interval.
 
Method Summary
 void addBoardView(BoardView view)
          Adds a BoardView to the set of BoardViews to be redrawn by this Clock.
 boolean isRunning()
          Returns whether this Clock is running.
 void removeBoardView(BoardView view)
          Removes a BoardView from the set of BoardViews to be redrawn by this Clock.
 void start()
          Starts a Clock that is not currently running.
 void stop()
          Stops a Clock that is currently running.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

rcsid

public static final String rcsid
Class revision identifier. rcsid contains an RCS Id keyword string that may be used to identify which revision of JavaUtilTimerClock.java was used to generate an instance of JavaUtilTimerClock.class. To identify the revision of a JavaUtilTimerClock.class file, run "ident JavaUtilTimerClock.class" (ident is part of the RCS software package).

See Also:
Constant Field Values
Constructor Detail

JavaUtilTimerClock

public JavaUtilTimerClock(Board board,
                          int interval)
Constructs a new stopped Clock with the given Board and interval.

Parameters:
board - Board to be stepped
interval - length of this Clock's interval in milliseconds
Requires:
board != null and interval > 0
Effects:
  • this.interval = interval
  • this.board = board
  • this.boardViews = the empty set
  • this.isRunning = false
Method Detail

start

public void start()
Starts a Clock that is not currently running.

Note that this implementation relaxes the requires clause specified by Clock by allowing this method to be called even on already started JavaUtilTimerClocks. Such a call will be effectively a no-op.

Specified by:
start in interface Clock
Requires:
nothing
Effects:
this.isRunning = true

stop

public void stop()
Stops a Clock that is currently running.

Note that this implementation relaxes the requires clause specified by Clock by allowing this method to be called even on already stopped JavaUtilTimerClocks. Such a call will be effectively a no-op.

Beware that this method synchronously stops this Clock; it will wait for the last step and redraw cycle to finish before returning. Do not call this method from a thread that is holding locks on resources that might be needed to complete a Board step or the BoardView redraws.

Specified by:
stop in interface Clock
Requires:
nothing
Effects:
this.isRunning = false

isRunning

public boolean isRunning()
Returns whether this Clock is running. If this Clock is currently stepping its Board at its configured regular interval, then returns true; otherwise, if this Clock is stopped, returns false.

Specified by:
isRunning in interface Clock
Returns:
this.isRunning

addBoardView

public void addBoardView(BoardView view)
Adds a BoardView to the set of BoardViews to be redrawn by this Clock.

Note that this implementation of addBoardView has a less strict requires clause than specified by Clock. In particular, it is okay for this method to be called when this JavaUtilTimerClock is running; the addition of the BoardView will just be effective "soon," most likely during the next iteration of the step and redraw cycle. Also, it is okay for view to already have been previously added; in such a case, this method will be essentially a no-op.

Specified by:
addBoardView in interface Clock
Parameters:
view - BoardView to be added
Requires:
view != null
Effects:
this.boardViewspost = this.boardViewspre + { view }

removeBoardView

public void removeBoardView(BoardView view)
Removes a BoardView from the set of BoardViews to be redrawn by this Clock.

Note that this implementation of removeBoardView has a less strict requires clause than specified by Clock. In particular, it is okay for this method to be called when this JavaUtilTimerClock is running; the removal of the BoardView will just be effective "soon," most likely during the next iteration of the step and redraw cycle. Also, it is okay for view not to currently be in this.boardViews; in such a case, this method will be essentially a no-op.

Specified by:
removeBoardView in interface Clock
Parameters:
view - BoardView to be removed
Requires:
view != null
Effects:
this.boardViewspost = this.boardViewspre - { view }