View Javadoc

1   // Copyright (C) 2005 - 2008 Philip Aston
2   // All rights reserved.
3   //
4   // This file is part of The Grinder software distribution. Refer to
5   // the file LICENSE which is part of The Grinder distribution for
6   // licensing details. The Grinder distribution is available on the
7   // Internet at http://grinder.sourceforge.net/
8   //
9   // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
10  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
11  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
12  // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
13  // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
14  // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
15  // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
16  // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
17  // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
18  // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
19  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
20  // OF THE POSSIBILITY OF SUCH DAMAGE.
21  
22  package net.grinder.engine.process;
23  
24  import net.grinder.common.UncheckedGrinderException;
25  
26  
27  /**
28   * Records time.
29   *
30   * <p>
31   * Strict state model, the {@link StopWatch} is either <em>running</em> or
32   * <em>not running</em>. If <em>running</em> then the only valid method is
33   * {@link #stop}. If <em>not running</em> then {@link #start},
34   * {@link #getTime}, {@link #add}, and {@link #reset} are all valid, but
35   * {@link #stop} is not.
36   * </p>
37   *
38   * <p>
39   * Calling a method that is not valid for the current state results in an
40   * unchecked {@link StopWatchStateException}; this is effectively an assertion.
41   * </p>
42   *
43   * @author Philip Aston
44   */
45  interface StopWatch {
46  
47    void start() throws StopWatchRunningException;
48  
49    void stop() throws StopWatchNotRunningException;
50  
51    void reset() throws StopWatchRunningException;
52  
53    long getTime() throws StopWatchRunningException;
54  
55    boolean isRunning();
56  
57    void add(StopWatch watch) throws StopWatchRunningException;
58  
59    /**
60     * {@link StopWatch} was in an invalid state for the method called.
61     */
62    abstract class StopWatchStateException extends UncheckedGrinderException {
63      public StopWatchStateException(String s) {
64        super(s);
65      }
66    }
67  
68    /**
69     * {@link StopWatch} was running.
70     */
71    class StopWatchRunningException extends StopWatchStateException {
72      public StopWatchRunningException(String s) {
73        super(s);
74      }
75    }
76  
77    /**
78     * {@link StopWatch} was not running.
79     */
80    class StopWatchNotRunningException extends StopWatchStateException {
81      public StopWatchNotRunningException(String s) {
82        super(s);
83      }
84    }
85  }