001/* ---------------------------------------------------------------------------- 002 The Kiwi Toolkit - A Java Class Library 003 Copyright (C) 1998-2004 Mark A. Lindner 004 005 This library is free software; you can redistribute it and/or 006 modify it under the terms of the GNU General Public License as 007 published by the Free Software Foundation; either version 2 of the 008 License, or (at your option) any later version. 009 010 This library is distributed in the hope that it will be useful, 011 but WITHOUT ANY WARRANTY; without even the implied warranty of 012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 General Public License for more details. 014 015 You should have received a copy of the GNU General Public License 016 along with this library; if not, write to the Free Software 017 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 018 02111-1307, USA. 019 020 The author may be contacted at: mark_a_lindner@yahoo.com 021 ---------------------------------------------------------------------------- 022 $Log: IntervalTimer.java,v $ 023 Revision 1.5 2004/05/05 21:22:45 markl 024 Comment header updates. 025 026 Revision 1.4 2003/01/19 09:42:39 markl 027 Javadoc & comment header updates. 028 029 Revision 1.3 2001/03/12 05:43:34 markl 030 Javadoc cleanup. 031 032 Revision 1.2 2001/03/12 02:57:40 markl 033 Source code cleanup. 034 035 Revision 1.1 1999/04/23 07:33:14 markl 036 Initial revision 037 ---------------------------------------------------------------------------- 038*/ 039 040package kiwi.util; 041 042/** A class that represents an interval timer. The timer can be started, 043 * stopped, resumed, and cleared, much like a stopwatch. 044 * 045 * @author Mark Lindner 046 */ 047 048public class IntervalTimer implements Resource 049 { 050 private long start = 0, stop = 0; 051 private long time = 0, accum_time = 0; 052 private boolean running = false; 053 private String name = null; 054 055 /** Construct a new <code>IntervalTimer</code>. 056 */ 057 058 public IntervalTimer() 059 { 060 } 061 062 /** Get the name for this timer. 063 * 064 * @return The timer's name. 065 */ 066 067 public synchronized String getName() 068 { 069 return(name); 070 } 071 072 /** Set the name for this timer. 073 * 074 * @param name The new name for the timer. 075 */ 076 077 public synchronized void setName(String name) 078 { 079 this.name = name; 080 } 081 082 /** Determine if the timer is currently running. Note that a timer is 083 * not <i>running</i> in the sense that a thread is running; no code 084 * is being executed while the timer is running. 085 * 086 * @return <code>true</code> if the timer is running and <code>false</code> 087 * otherwise. 088 */ 089 090 public synchronized boolean isRunning() 091 { 092 return(running); 093 } 094 095 /** Start the timer. If the timer is already running, this method does 096 * nothing. 097 */ 098 099 public synchronized void start() 100 { 101 if(running) 102 return; 103 104 clear(); 105 start = System.currentTimeMillis(); 106 running = true; 107 } 108 109 /** Stop the timer. If the timer is not running, this method does nothing. 110 */ 111 112 public synchronized void stop() 113 { 114 if(!running) 115 return; 116 117 stop = System.currentTimeMillis(); 118 time = (stop - start); 119 accum_time += time; 120 running = false; 121 } 122 123 /** Resume a stopped timer. If the timer is running, this method does 124 * nothing. 125 */ 126 127 public synchronized void resume() 128 { 129 if(running) 130 return; 131 132 start = System.currentTimeMillis(); 133 running = true; 134 } 135 136 /** Clear a timer. Regardless of whether the timer is running or not, it 137 * is stopped and the accumulated time is reset to 0. 138 */ 139 140 public synchronized void clear() 141 { 142 start = 0; 143 accum_time = 0; 144 running = false; 145 } 146 147 /** Get the accumulated time for the timer. Time accumulates while the 148 * timer is running, but not while it is stopped. 149 * 150 * @return The amount of time that this timer has accumulated, in 151 * microseconds, or <code>-1</code> if the timer is currently running. 152 */ 153 154 public synchronized long getTime() 155 { 156 if(running) 157 return(-1); 158 159 return(accum_time); 160 } 161 162 /** Create a string representation of the timer, which includes its name 163 * and accumulated time. 164 */ 165 166 public synchronized String toString() 167 { 168 return("Timer " + getName() + ": " + (running ? "(running)" 169 : getTime() + " ms")); 170 } 171 172 /** Reserve the timer. This method clears the timer. */ 173 174 public void reserve() 175 { 176 clear(); 177 } 178 179 /** Release the timer. This method clears the timer. */ 180 181 public void release() 182 { 183 clear(); 184 } 185 186 } 187 188/* end of source file */