001/**
002 * Copyright (c) 2004-2011 QOS.ch
003 * All rights reserved.
004 *
005 * Permission is hereby granted, free  of charge, to any person obtaining
006 * a  copy  of this  software  and  associated  documentation files  (the
007 * "Software"), to  deal in  the Software without  restriction, including
008 * without limitation  the rights to  use, copy, modify,  merge, publish,
009 * distribute,  sublicense, and/or sell  copies of  the Software,  and to
010 * permit persons to whom the Software  is furnished to do so, subject to
011 * the following conditions:
012 *
013 * The  above  copyright  notice  and  this permission  notice  shall  be
014 * included in all copies or substantial portions of the Software.
015 *
016 * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
017 * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
018 * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
019 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
020 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
021 * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
022 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023 *
024 */
025package org.slf4j.profiler;
026
027/**
028 * A very basic {@link TimeInstrument} which can be started and stopped
029 * once and only once.
030 * 
031 * @author Ceki Gülcü
032 *
033 */
034public class StopWatch implements TimeInstrument {
035
036    private String name;
037    private long startTime;
038    private long stopTime;
039    TimeInstrumentStatus status;
040
041    public StopWatch(String name) {
042        start(name);
043    }
044
045    StopWatch(StopWatch original) {
046        this.name = original.name;
047        this.startTime = original.startTime;
048        this.stopTime = original.stopTime;
049        this.status = original.status;
050    }
051
052    public void start(String name) {
053        this.name = name;
054        startTime = System.nanoTime();
055        status = TimeInstrumentStatus.STARTED;
056    }
057
058    public String getName() {
059        return name;
060    }
061
062    public TimeInstrument stop() {
063        if (status == TimeInstrumentStatus.STOPPED) {
064            return this;
065        }
066        return stop(System.nanoTime());
067    }
068
069    public StopWatch stop(long stopTime) {
070        this.status = TimeInstrumentStatus.STOPPED;
071        this.stopTime = stopTime;
072        return this;
073    }
074
075    @Override
076    public String toString() {
077        StringBuilder buf = new StringBuilder();
078        buf.append("StopWatch [");
079        buf.append(name);
080        buf.append("] ");
081
082        switch (status) {
083        case STARTED:
084            buf.append("STARTED");
085            break;
086        case STOPPED:
087            buf.append("elapsed time: ");
088            buf.append(Util.durationInDurationUnitsAsStr(elapsedTime(), DurationUnit.MICROSECOND));
089            break;
090        default:
091            throw new IllegalStateException("Status " + status + " is not expected");
092        }
093        return buf.toString();
094    }
095
096    public final long elapsedTime() {
097        if (status == TimeInstrumentStatus.STARTED) {
098            return 0;
099        } else {
100            return stopTime - startTime;
101        }
102    }
103
104    public TimeInstrumentStatus getStatus() {
105        return status;
106    }
107
108    public void print() {
109        System.out.println(toString());
110    }
111
112    public void log() {
113        throw new UnsupportedOperationException("A stopwatch instance does not know how to log");
114    }
115
116}