package org.jboss.util;
import java.io.Serializable;
public class StopWatch
implements Serializable, Cloneable
{
static final long serialVersionUID = 4628094303187435707L;
protected long total = 0;
protected long start = -1;
protected long stop = -1;
protected int count = 0;
protected boolean running = false;
public StopWatch() {}
public StopWatch(final boolean running)
{
if (running) start();
}
public void start(final boolean reset)
{
if (!running) {
if (reset) reset();
start = System.currentTimeMillis();
running = true;
}
}
public void start()
{
start(false);
}
public long stop()
{
long lap = 0;
if (running) {
count++;
stop = System.currentTimeMillis();
lap = stop - start;
total += lap;
running = false;
}
return lap;
}
public void reset()
{
start = -1;
stop = -1;
total = 0;
count = 0;
running = false;
}
public int getLapCount()
{
return count;
}
public long getLapTime()
{
if (start == -1) {
return 0;
}
else if (running) {
return System.currentTimeMillis() - start;
}
else {
return stop - start;
}
}
public long getAverageLapTime()
{
return (count == 0) ? 0 : getLapTime() / getLapCount();
}
public long getTime()
{
if (start == -1) {
return 0;
}
else if (running) {
return total + System.currentTimeMillis() - start;
}
else {
return total;
}
}
public boolean isRunning()
{
return running;
}
public String toString()
{
StringBuffer buff = new StringBuffer();
if (running) {
formatElapsedTime(buff, getTime());
if (count >= 1) {
buff.append(", count=").append(count);
buff.append(", current=");
formatElapsedTime(buff, getLapTime());
}
}
else {
formatElapsedTime(buff, getTime());
if (count > 1) {
buff.append(", count=").append(count);
buff.append(", average=");
formatElapsedTime(buff, getAverageLapTime());
}
}
return buff.toString();
}
private void formatElapsedTime(final StringBuffer buff, final long lapsed)
{
long m = lapsed / 60000;
if (m != 0) {
buff.append(m).append("m:");
}
long s = (lapsed - 60000 * m) / 1000;
if (s != 0) {
buff.append(s).append("s:");
}
long ms = (lapsed - 60000 * m - 1000 * s);
buff.append(ms).append("ms");
}
public Object clone()
{
try {
return super.clone();
}
catch (CloneNotSupportedException e) {
throw new InternalError();
}
}
private static class Wrapper
extends StopWatch
{
protected StopWatch watch;
public Wrapper(final StopWatch watch) {
this.watch = watch;
}
public void start(final boolean reset) {
watch.start(reset);
}
public void start() {
watch.start();
}
public long stop() {
return watch.stop();
}
public void reset() {
watch.reset();
}
public long getLapTime() {
return watch.getLapTime();
}
public long getAverageLapTime() {
return watch.getAverageLapTime();
}
public int getLapCount() {
return watch.getLapCount();
}
public long getTime() {
return watch.getTime();
}
public boolean isRunning() {
return watch.isRunning();
}
public String toString() {
return watch.toString();
}
}
public static StopWatch makeSynchronized(final StopWatch watch)
{
return new Wrapper(watch)
{
public synchronized void start(final boolean reset) {
this.watch.start(reset);
}
public synchronized void start() {
this.watch.start();
}
public synchronized long stop() {
return this.watch.stop();
}
public synchronized void reset() {
this.watch.reset();
}
public synchronized long getLapTime() {
return this.watch.getLapTime();
}
public synchronized long getAverageLapTime() {
return this.watch.getAverageLapTime();
}
public synchronized int getLapCount() {
return this.watch.getLapCount();
}
public synchronized long getTime() {
return this.watch.getTime();
}
public synchronized boolean isRunning() {
return this.watch.isRunning();
}
public synchronized String toString() {
return this.watch.toString();
}
};
}
}