package org.yi.happy.metric;

/**
 * I am a simple timer.
 */
public class SimpleTimer {

    /**
     * create started.
     */
    public SimpleTimer() {
        startTime = System.currentTimeMillis();
        stopTime = startTime - 1;
    }

    /**
     * when the timer started.
     */
    private long startTime;

    /**
     * when the timer was stopped. (initially: startTime - 1)
     */
    private long stopTime;

    /**
     * stop the timer. A timer may only be stopped once.
     */
    public void stop() {
        if (stopTime >= startTime) {
            throw new IllegalStateException("stop may only be called once");
        }
        stopTime = System.currentTimeMillis();
    }

    /**
     * get the time on the timer. if the timer has not been stopped get the time
     * from when it was started to now. if the timer has been stopped get the
     * time between when it was started and when it was stopped.
     * 
     * @return the elapsed time in ms
     */
    public long getTime() {
        if (stopTime < startTime) {
            return System.currentTimeMillis() - startTime;
        }
        return stopTime - startTime;
    }
}

@@ -1157,11 +1157,139 @@
 e;%0A    %7D
+%0A%0A    @Override%0A    public String toString() %7B%0A        return %22%5B%22 + getClass().getName() + %22: time=%22 + getTime() + %22 ms%5D%22;%0A    %7D
 %0A%7D%0A

Format simple timers when they are printed.