001    // ============================================== 
002    //                                                                                              
003    // Module-Imaging 1.0, 05-2006                  
004    // +++++++++++++++++++++++++++++++++++++                        
005    //                                                                                              
006    // Andreas Trappe       - idea, concept
007    // Chi Vu Huu           - concept, development
008    //
009    // $Revision: 12159 $ $Date: 2007-07-27 11:21:09 +0200 (Fr, 27 Jul 2007) $ 
010    // ============================================== 
011    
012    package org.mycore.services.imaging;
013    
014    /**
015     * A class to help benchmark code It simulates a real stop watch
016     */
017    public class Stopwatch {
018    
019        private long startTime = -1;
020    
021        private long stopTime = -1;
022    
023        private boolean running = false;
024    
025        public Stopwatch start() {
026            startTime = System.currentTimeMillis();
027            running = true;
028            return this;
029        }
030    
031        public Stopwatch stop() {
032            stopTime = System.currentTimeMillis();
033            running = false;
034            return this;
035        }
036    
037        /**
038         * returns elapsed time in milliseconds if the watch has never been started
039         * then return zero
040         */
041        public long getElapsedTime() {
042            if (startTime == -1) {
043                return 0;
044            }
045            if (running) {
046                return System.currentTimeMillis() - startTime;
047            } else {
048                return stopTime - startTime;
049            }
050        }
051    
052        public Stopwatch reset() {
053            startTime = -1;
054            stopTime = -1;
055            running = false;
056            return this;
057        }
058    }