/* 
 * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved. 
 *  
 * Project: OpenSubsystems 
 *  
 * $Id: MyTimer.java,v 1.5 2007/01/07 06:14:00 bastafidli Exp $ 
 *  
 * This program is free software; you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; version 2 of the License.  
 *  
 * This program is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 * GNU General Public License for more details. 
 *  
 * You should have received a copy of the GNU General Public License 
 * along with this program; if not, write to the Free Software 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA  
 */ 
 
 
/** 
 * Class encapsulating timer functionality. 
 *  
 * @version $Id: MyTimer.java,v 1.5 2007/01/07 06:14:00 bastafidli Exp $ 
 * @author Miro Halas 
 * @code.reviewer Miro Halas 
 * @code.reviewed Initial revision 
 */ 
public class MyTimer 
{ 
   // Attributes /////////////////////////////////////////////////////////////// 
    
   /** 
    * Remeber start time here. 
    */ 
   private long m_lStartTime; 
    
   /** 
    * Remeber stop time here. 
    */ 
   private long m_lStopTime; 
 
   // Constructors ///////////////////////////////////////////////////////////// 
    
   /** 
    * Default constructor. 
    * Starts counting fro the moment it is cosntructed. 
    */ 
   public MyTimer( 
   ) 
   { 
      reset(); 
   } 
 
   // Logic //////////////////////////////////////////////////////////////////// 
    
   /** 
    * @return long - start time; 
    */ 
   public long getStartTime() 
   { 
      return m_lStartTime; 
   } 
 
   /** 
    * @return - stop time; 
    */ 
   public long getStopTime() 
   { 
      return m_lStopTime; 
   } 
 
   /** 
    * Reset the counter and start counting from scratch. 
    */ 
   public void reset( 
   ) 
   { 
      m_lStartTime = System.currentTimeMillis(); 
      m_lStopTime = 0; 
   } 
 
   /** 
    * Stop the timer. 
    */ 
   public void stop( 
   ) 
   { 
      m_lStopTime = System.currentTimeMillis(); 
   } 
 
   /** 
    * Get timer duration (the timer doesn't stop) in milliseconds. 
    *  
    * @return long - difference between stop and start time. 
    */ 
   public long getDuration( 
   ) 
   { 
      long lStopTime; 
 
      if (m_lStopTime == 0) 
      { 
         lStopTime = System.currentTimeMillis(); 
      } 
      else 
      { 
         lStopTime = m_lStopTime; 
      } 
 
      return lStopTime - m_lStartTime; 
   } 
 
   /** 
    * Print the state of the timer without stopping it. 
    * @return String - timing information 
    */ 
   public String toString( 
   ) 
   { 
      long lTotalMS   = getDuration(); 
      long lMS        = lTotalMS % 1000; 
      long lTotalSecs = lTotalMS / 1000; 
      long lSecs      = lTotalSecs % 60; 
      long lTotalMins = lTotalSecs / 60; 
      long lMinutes   = lTotalMins % 60; 
      long lHours     = lTotalMins / 60; 
      StringBuffer sbBuffer = new StringBuffer(); 
 
      if (lHours > 0) 
      { 
         sbBuffer.append(lHours); 
         sbBuffer.append(":"); 
         sbBuffer.append(lMinutes); 
         sbBuffer.append(":"); 
         sbBuffer.append(lSecs); 
         sbBuffer.append("."); 
         sbBuffer.append(lMS); 
      } 
      else if (lMinutes > 0) 
      { 
         sbBuffer.append(lMinutes); 
         sbBuffer.append(":"); 
         sbBuffer.append(lSecs); 
         sbBuffer.append("."); 
         sbBuffer.append(lMS); 
      } 
      else if (lSecs > 0) 
      { 
         sbBuffer.append(lSecs); 
         sbBuffer.append("."); 
         sbBuffer.append(lMS); 
         sbBuffer.append(" seconds"); 
      } 
      else 
      { 
         sbBuffer.append(lMS); 
         sbBuffer.append(" ms"); 
      } 
       
      return sbBuffer.toString(); 
   } 
} 
 
    
     
     
     
  
  |