Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/mvsteenb/javauitransformer/xmltransformer/util/StopWatch.java


1   package com.mvsteenb.javauitransformer.xmltransformer.util;
2   
3   import java.util.Date;
4   
5   /**
6    * com.mvsteenb.javauitransformer.xmltransformer.util
7    *
8    * <p><b>About</b></p>
9    *
10   * <p>
11   *   This class is part of the JavaUIFormatter version @build.version@ (build #@build.number@) which was built on @build.date@.
12   * </p>
13   *
14   * <p><b>Description</b></p>
15   *
16   * Implements a stopwatch timer that can be used for performance testing
17   *
18   * <p><b>Free Software</b></p>
19   *
20   * <p>
21   *   Copyright (C) 2003 Mario Van Steenberghe
22   * </p>
23   *
24   * <p>
25   *   <small>
26   *     This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser
27   *     General Public License as published by the Free Software Foundation; either version 2.1 of the License, or
28   *     (at your option) any later version. This library is distributed in the hope that it will be useful,
29   *     but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
30   *     PURPOSE.  See the GNU Lesser General Public License for more details. You should have received a copy of the
31   *     GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc.,
32   *     59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
33   *   </small>
34   * </p>
35   *
36   * <p>
37   *   <small>
38   *     Please contact me at mario.vansteenberghe@pandora.be for more information.
39   *   </small>
40   * </p>
41   *
42   *   
43   * <p><b>Revision History</b></p>
44   *
45   * <p>
46   *   Oct 2, 2003 : mvsteenb : Initial Revision
47   * </p>
48   * 
49   */
50  
51  public class StopWatch {
52  
53    private Date startTime = null;
54  
55    /**
56     * Constructor
57     */
58  
59    public StopWatch() {
60  
61    }
62  
63    // ================================================================================================================ //
64    // public methods                                                                                                   //
65    // ================================================================================================================ //
66  
67    /**
68     * Starts the stopwatch
69     */
70  
71    public void start() throws StopWatchException {
72      if (startTime != null)
73        throw new StopWatchException("StopWatch already started : stop the stopwatch before restarting !");
74      startTime = new Date();
75    }
76  
77    /**
78     * Stops the stopwatch and returns elapsed time
79     */
80  
81    public long stop() throws StopWatchException {
82      if (startTime == null)
83        throw new StopWatchException("StopWatch not started : start the stopwatch before stopping !");
84      long res = TimeUtil.timeDifference(startTime, new Date());
85      startTime = null;
86      return res;
87    }
88  
89    /**
90     * Returns intermediate result without stopping watch
91     */
92  
93    public long intermediate() throws StopWatchException {
94      if (startTime == null)
95        throw new StopWatchException("StopWatch not started : start the stopwatch first !");
96      return TimeUtil.timeDifference(startTime, new Date());
97    }
98  
99  }