// ==========================================================================
// $Id: TimeFormatter.java,v 1.2 2002/09/09 09:24:34 hitoshi Exp $
// Time Formatter
// ==========================================================================
// 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.2 $
// $Date: 2002/09/09 09:24:34 $
// $Author: hitoshi $
// ==========================================================================
package de.mpg.mpisb.timerclock;

import  java.util.*;

public class TimeFormatter
{
  /**
   * constructor
   */
  public TimeFormatter()
  {
    this.mFormatter = new Vector();
    this.initFormat();
  }
  
  /** 
   * initialize formats 
   */
  public void initFormat()
  {
    this.mFormatter = new Vector();
    // all output
    this.mFormatter.add(new TimeFormattable() {
	public String format(long t){
	  String sign = "";
	  if(t < 0)
	  {
	    sign = "-";
	  }
	  t = Math.abs(t);
	  long [] trep = calcMSecToTime(t);
	  String r = sign + 
	    TimeFormattable.zeroP(trep[0], 2) + ":" + 
	    TimeFormattable.zeroP(trep[1], 2) + ":" +    
	    TimeFormattable.zeroP(trep[2], 2) + ":" + 
	    TimeFormattable.zeroP(trep[3], 3);
	  return(r);
	}
	public String getDescription(){
	  return("HH:MM:SS:MS, all output");
	}
      });
    // minutes and seconds
    this.mFormatter.add(new TimeFormattable() {
	public String format(long t){
	  String sign = "";
	  if(t < 0)
	  {
	    sign = "-";
	  }
	  t = Math.abs(t);
	  long [] trep = calcMSecToTime(t);
	  String r = sign + 
	    TimeFormattable.zeroP(trep[1], 2) + ":" + 
	    TimeFormattable.zeroP(trep[2], 2);
	  return(r);
	}
	public String getDescription(){
	  return("MM:SS, minutes and seconds");
	}
      });
    // minutes and seconds but when time is more than an hour, display it.
    this.mFormatter.add(new TimeFormattable() {
	public String format(long t){
	  String sign = "";
	  if(t < 0)
	  {
	    sign = "-";
	  }
	  t = Math.abs(t);
	  long [] trep = calcMSecToTime(t);
	  if(trep[0] == 0)
	  {
	    String r = sign +
	      TimeFormattable.zeroP(trep[1], 2) + ":" + 
	      TimeFormattable.zeroP(trep[2], 2);
	    return(r);
	  }
	  else
	  {
	    String r = sign + 
	      TimeFormattable.zeroP(trep[0], 2) + ":" + 
	      TimeFormattable.zeroP(trep[1], 2) + ":" + 
	      TimeFormattable.zeroP(trep[2], 2);
	    return(r);
	  }
	}
	public String getDescription(){
	  return("(HH:)MM:SS, minutes and seconds " +
		 "but if it needed, it shows hours.");
	}
      });
    this.setFormatIndex(2);
  }
  /** 
   * add format
   *   @param tf timer format 
   *   @see TimerFormat
   */
  public void addFormat(TimeFormattable tf)
  {
    this.mFormatter.add(tf);
  }
  /** 
   * peek format as Vector
   *   @return vector of tfs
   *   @see TimerFormat
   */
  public Vector peekFormatVec()
  {
    return(this.mFormatter);
  }
  /** 
   * set format index
   */
  public void setFormatIndex(int idx)
  {
    if((idx < 0) || (this.mFormatter.size() <= idx))
    {
      throw new RuntimeException("illegal format index");
    }
    this.mFormatterIdx  = idx;
    this.mCurrentFormat = (TimeFormattable)(this.mFormatter.get(this.mFormatterIdx));
  }
  /** 
   * get format index
   */
  public int getFormatIndex()
  {
    return(this.mFormatterIdx);
  }

  /**
   * get formatted string with current format
   */
  public String getFormattedString(long t)
  {
    return(this.mCurrentFormat.format(t));
  }

  /*
   * millisec -> time 
   */
  public static long [] calcMSecToTime(long timespan)
  {
    long [] ret = new long[4];
    ret[0]	= (timespan / 3600000) % 24; // hour
    ret[1]  	= (timespan / 60000)   % 60; // min
    ret[2]     	= (timespan / 1000)    % 60; // sec
    ret[3] 	=  timespan % 1000; 	     // millisec
    return(ret);
  }

  /**
   * time -> millisec
   */
  public static long calcTimeToMsec(long hour, long min, long sec, long millsec)
  {
    return((3600000 * hour) + (60000 * min) + (sec * 1000) + millsec);
  }
  /**
   * time -> millisec : array version
   */
  public static long calcTimeToMsec(long [] t)
  {
    return(calcTimeToMsec(t[0], t[1], t[2], t[3]));
  }

  // private variables
  private Vector 	  mFormatter	 = null;
  private int    	  mFormatterIdx	 = 0;
  private TimeFormattable mCurrentFormat = null;
}
