//package uk.ac.lancs.unix.cotton.tetris;

/**
 * A generic class to provide a callback at regular intervals.
 * The class to receive the callbacks must implement AlarmUser.
 *@see AlarmUser
 */
public class Alarm extends Thread
{
  int delay;
  AlarmUser client;
  boolean keepRunning;
 
  /**
   * The constructor for the class.
   *@param delay The time between callbacks.
   *@param client The class to perform the callbacks on.
   */
  Alarm(int delay, AlarmUser client)
  {
    this.delay=delay;
    this.client=client;
    setDaemon(true);
  }

  /**
   * Repeatedly waits <code>delay</code> miliseconds then calls <code>client.alarmCall</code>.
   */
  public void run()
  {
    keepRunning=true;
    while(keepRunning)
    {
      synchronized(this)
      {
        try
        {
          wait(delay);
        }catch(InterruptedException e) {}
      }
      client.alarmCall();
    }
  }

  /**
   * Called when the alarm has out-lived it's usefulness.
   */
  public void stopAlarming()
  {
    keepRunning=false;
  }

  /**
   * Reset the clock.
   */
  public void resetClock()
  {
    synchronized(this)
    {
      notify();
    }
  }
}

