001    package railo.runtime.timer;
002    
003    /**
004     * Implementation of a simple Stopwatch
005     */
006    public class Stopwatch {
007            
008            private long start;
009            private int count=0;
010            private long total=0;
011            boolean isRunning;
012            
013            /**
014             * start the watch
015             */
016            public void start() {
017                    isRunning=true;
018                    start=System.currentTimeMillis();
019            }
020            
021            /**
022             * stops the watch
023             * @return returns the current time or 0 if watch not was running
024             */
025            public long stop() {
026                    if(isRunning) {
027                            long time=System.currentTimeMillis()-start;
028                            total+=time;
029                            count++;
030                            isRunning=false;
031                            return time;
032                            
033                    }
034                    return 0;
035            }
036            
037            /**
038             * @return returns the current time or 0 if watch is not running
039             */
040            public long time() {
041                    if(isRunning)return System.currentTimeMillis()-start;
042                    return 0;
043            }
044            
045            /**
046             * @return returns the total elapsed time
047             */
048            public long totalTime() {
049                    return total+time();
050            }
051            
052            /**
053             * @return returns how many start and stop was making
054             */
055            public int count() {
056                    return count;
057            }
058            /**
059             * resets the stopwatch
060             */
061            public void reset() {
062                    start=0;
063                    count=0;
064                    total=0;
065                    isRunning=false;
066            }
067    }