antichess
Class GameTimer

java.lang.Object
  extended by java.util.TimerTask
      extended by antichess.GameTimer
All Implemented Interfaces:
Runnable

public class GameTimer
extends TimerTask

The GameTimer stores a time that ticks down when GameTimer is running. GameTimer also stores a list of GameTimerListeners that it can notify of its time.

Author:
nlharr
Specification Fields

Constructor Summary
GameTimer(long startTime, long interval)
          Creates a GameTimer with a start time and current time of startTime
GameTimer(long startTime, long currentTime, long interval)
          Creates a GameTimer with a start time of startTime, but a current time of currentTime
 
Method Summary
 void addTimerListener(GameTimerListener listener)
           
 long getStartTime()
           
 long getTime()
           
 boolean isRunning()
           
 void notifyTimeUpdate()
          Calls refreshTimer on all the listeners
 void run()
          Refreshes the time for all the listeners
 void setTime(long newTime)
           
 void start()
          Starts the timer
 void stop()
          Stops the timer
 
Methods inherited from class java.util.TimerTask
cancel, scheduledExecutionTime
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

GameTimer

public GameTimer(long startTime,
                 long interval)
Creates a GameTimer with a start time and current time of startTime

Parameters:
startTime - is the start time measured in milliseconds interval is the interval between updates to the listeners of this timer if interval is 0 the listeners are not updated
Throws:
IllegalArgumentException - if interval < 0 or startTimer < 0

GameTimer

public GameTimer(long startTime,
                 long currentTime,
                 long interval)
Creates a GameTimer with a start time of startTime, but a current time of currentTime

Parameters:
startTime - : the start time measured in milliseconds
currentTime - : the remaining time in milliseconds
interval - : the time between updates if interval is 0 the listeners are not updated
Throws:
IllegalArgumentException - if interval < 0 or startTimer < 0 or currentTime < 0
Method Detail

start

public void start()
Starts the timer

Effects:
isRunning = true
Modifies:
isRunning

stop

public void stop()
Stops the timer

Effects:
isRunning = false, if isRunning timeleft -=the interval since the first start() after the last stop()
Modifies:
isRunning, timeLeft

getStartTime

public long getStartTime()
Returns:
startTime

getTime

public long getTime()
Returns:
timeLeft

addTimerListener

public void addTimerListener(GameTimerListener listener)
Requires:
listener != null
Effects:
adds listener to listenerList
Modifies:
listenerList

isRunning

public boolean isRunning()
Returns:
isRunning

notifyTimeUpdate

public void notifyTimeUpdate()
Calls refreshTimer on all the listeners


run

public void run()
Refreshes the time for all the listeners

Specified by:
run in interface Runnable
Specified by:
run in class TimerTask

setTime

public void setTime(long newTime)
Effects:
if (!isRunning) timeLeft = newTime if newTime < 0 newTime = 0
Modifies:
timeLeft