// ==========================================================================
// $Id: TimerDowncount.java,v 1.3 2002/09/01 15:48:33 hitoshi Exp $
// Timer : down count timer 
// ==========================================================================
// Copyright (C) 2002 Yamauchi, Hitoshi
//
//   Max-Planck-Institut fuer Informatik AG4
//   Stuhlsatzenhausweg 85, 66123 Saarbruecken
//   http://www.mpi-sb.mpg.de/
// 
// This software can be redistributed only under GNU copyleft.
//
// @author Yamauchi Hitoshi »³Æâ ÀÆ
// ==========================================================================
// $Revision: 1.3 $
// $Date: 2002/09/01 15:48:33 $
// $Author: hitoshi $
// ==========================================================================
package de.mpg.mpisb.timerclock;

import  java.util.*;

public class TimerDowncount extends Timer
{
  /**
   * constructor
   */
  public TimerDowncount(long period)
  {
    this.mSW = new StopWatch();
    this.setPeriod(period);
  }
  
  /** 
   * reset timer
   */
  public void reset()
  {
    if(this.mSW.isRun())
    {
      this.mSW.stop();
    }
    this.mSW.reset();
  }

  /** 
   * start timer
   */
  public void start()
  {
    if(!(this.mSW.isRun()))
    {
      this.mSW.run();
    }
  }
  
  /** 
   * stop timer
   */
  public void stop()
  {
    if(this.mSW.isRun())
    {
      this.mSW.stop();    
    }
  }

  /**
   * get current display time 
   */
  public long getCurrentDisplayTime()
  {
    long accumElapsed = this.mSW.peekTime();
    long disptime = this.getPeriod() - accumElapsed;
    return(disptime);
  }

  /**
   * get current elapsed time 
   */
  public long getCurrentElapsedTime()
  {
    long accumElapsed = this.mSW.peekTime();
    return(accumElapsed);
  }

  /**
   * string representation
   */
  public String toString()
  {
    String s = "Period: "  + this.getPeriod() + 
      ", CurDispTime: "    + this.getCurrentDisplayTime() + 
      ", CurElapsedTime: " + this.getCurrentElapsedTime();
    return(s);
  }

  // private variables
  private StopWatch mSW;

  /*
   * main for test
   */
  public static void main(String []args)
  {
    try {
      Timer t = new TimerDowncount(10000);
      t.start();
      System.out.println("start");
      for(int i = 0; i < 10; i++)
      {
	System.out.println(t);
	Thread.sleep(400);
      }

      System.out.println("stop");      
      t.stop();
      for(int i = 0; i < 10; i++)
      {
	System.out.println(t);
	Thread.sleep(400);
      }

      t.start();
      System.out.println("start");
      while(true)
      {
	System.out.println(t);
	Thread.sleep(400);
      }

    } catch (InterruptedException e)
    {
      System.out.println(e);
    }
  } 
}
