1 22 package org.jboss.util; 23 24 import java.io.Serializable ; 25 26 32 public class StopWatch 33 implements Serializable , Cloneable 34 { 35 36 protected long total = 0; 37 38 39 protected long start = -1; 40 41 42 protected long stop = -1; 43 44 45 protected int count = 0; 46 47 48 protected boolean running = false; 49 50 53 public StopWatch() {} 54 55 60 public StopWatch(final boolean running) 61 { 62 if (running) start(); 63 } 64 65 70 public void start(final boolean reset) 71 { 72 if (!running) { 73 if (reset) reset(); 74 start = System.currentTimeMillis(); 75 running = true; 76 } 77 } 78 79 82 public void start() 83 { 84 start(false); 85 } 86 87 92 public long stop() 93 { 94 long lap = 0; 95 96 if (running) { 97 count++; 98 stop = System.currentTimeMillis(); 99 lap = stop - start; 100 total += lap; 101 running = false; 102 } 103 104 return lap; 105 } 106 107 110 public void reset() 111 { 112 start = -1; 113 stop = -1; 114 total = 0; 115 count = 0; 116 running = false; 117 } 118 119 124 public int getLapCount() 125 { 126 return count; 127 } 128 129 134 public long getLapTime() 135 { 136 if (start == -1) { 137 return 0; 138 } 139 else if (running) { 140 return System.currentTimeMillis() - start; 141 } 142 else { 143 return stop - start; 144 } 145 } 146 147 152 public long getAverageLapTime() 153 { 154 return (count == 0) ? 0 : getLapTime() / getLapCount(); 155 } 156 157 162 public long getTime() 163 { 164 if (start == -1) { 165 return 0; 166 } 167 else if (running) { 168 return total + System.currentTimeMillis() - start; 169 } 170 else { 171 return total; 172 } 173 } 174 175 180 public boolean isRunning() 181 { 182 return running; 183 } 184 185 188 public String toString() 189 { 190 StringBuffer buff = new StringBuffer (); 191 192 if (running) { 193 formatElapsedTime(buff, getTime()); 195 196 if (count >= 1) { 198 buff.append(", count=").append(count); 199 buff.append(", current="); 200 formatElapsedTime(buff, getLapTime()); 201 } 202 } 203 else { 204 formatElapsedTime(buff, getTime()); 206 207 if (count > 1) { 209 buff.append(", count=").append(count); 210 buff.append(", average="); 211 formatElapsedTime(buff, getAverageLapTime()); 212 } 213 } 214 215 return buff.toString(); 216 } 217 218 private void formatElapsedTime(final StringBuffer buff, final long lapsed) 219 { 220 long m = lapsed / 60000; 221 if (m != 0) { 222 buff.append(m).append("m:"); 223 } 224 225 long s = (lapsed - 60000 * m) / 1000; 226 if (s != 0) { 227 buff.append(s).append("s:"); 228 } 229 230 long ms = (lapsed - 60000 * m - 1000 * s); 232 buff.append(ms).append("ms"); 233 } 234 235 240 public Object clone() 241 { 242 try { 243 return super.clone(); 244 } 245 catch (CloneNotSupportedException e) { 246 throw new InternalError (); 247 } 248 } 249 250 251 255 258 private static class Wrapper 259 extends StopWatch 260 { 261 protected StopWatch watch; 262 263 public Wrapper(final StopWatch watch) { 264 this.watch = watch; 265 } 266 267 public void start(final boolean reset) { 268 watch.start(reset); 269 } 270 271 public void start() { 272 watch.start(); 273 } 274 275 public long stop() { 276 return watch.stop(); 277 } 278 279 public void reset() { 280 watch.reset(); 281 } 282 283 public long getLapTime() { 284 return watch.getLapTime(); 285 } 286 287 public long getAverageLapTime() { 288 return watch.getAverageLapTime(); 289 } 290 291 public int getLapCount() { 292 return watch.getLapCount(); 293 } 294 295 public long getTime() { 296 return watch.getTime(); 297 } 298 299 public boolean isRunning() { 300 return watch.isRunning(); 301 } 302 303 public String toString() { 304 return watch.toString(); 305 } 306 } 307 308 314 public static StopWatch makeSynchronized(final StopWatch watch) 315 { 316 return new Wrapper(watch) 317 { 318 public synchronized void start(final boolean reset) { 319 this.watch.start(reset); 320 } 321 322 public synchronized void start() { 323 this.watch.start(); 324 } 325 326 public synchronized long stop() { 327 return this.watch.stop(); 328 } 329 330 public synchronized void reset() { 331 this.watch.reset(); 332 } 333 334 public synchronized long getLapTime() { 335 return this.watch.getLapTime(); 336 } 337 338 public synchronized long getAverageLapTime() { 339 return this.watch.getAverageLapTime(); 340 } 341 342 public synchronized int getLapCount() { 343 return this.watch.getLapCount(); 344 } 345 346 public synchronized long getTime() { 347 return this.watch.getTime(); 348 } 349 350 public synchronized boolean isRunning() { 351 return this.watch.isRunning(); 352 } 353 354 public synchronized String toString() { 355 return this.watch.toString(); 356 } 357 }; 358 } 359 } 360 | Popular Tags |