Clover coverage report - PLT Utilities Test Coverage (plt-20120304-r5436)
Coverage timestamp: Sat Mar 3 2012 22:01:56 CST
file stats: LOC: 103   Methods: 6
NCLOC: 35   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
Stopwatch.java 50% 73.7% 66.7% 67.7%
coverage coverage
 1    /*BEGIN_COPYRIGHT_BLOCK*
 2   
 3    PLT Utilities BSD License
 4   
 5    Copyright (c) 2007-2010 JavaPLT group at Rice University
 6    All rights reserved.
 7   
 8    Developed by: Java Programming Languages Team
 9    Rice University
 10    http://www.cs.rice.edu/~javaplt/
 11   
 12    Redistribution and use in source and binary forms, with or without modification, are permitted
 13    provided that the following conditions are met:
 14   
 15    - Redistributions of source code must retain the above copyright notice, this list of conditions
 16    and the following disclaimer.
 17    - Redistributions in binary form must reproduce the above copyright notice, this list of
 18    conditions and the following disclaimer in the documentation and/or other materials provided
 19    with the distribution.
 20    - Neither the name of the JavaPLT group, Rice University, nor the names of the library's
 21    contributors may be used to endorse or promote products derived from this software without
 22    specific prior written permission.
 23   
 24    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
 25    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 26    FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND
 27    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 28    DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 29    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 30    IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 31    OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 32   
 33    *END_COPYRIGHT_BLOCK*/
 34   
 35    package edu.rice.cs.plt.debug;
 36   
 37    import java.util.List;
 38    import java.util.LinkedList;
 39    import edu.rice.cs.plt.iter.IterUtil;
 40   
 41    /**
 42    * A simple timer based on {@link System#currentTimeMillis}. To support better performance, this class
 43    * is not thread-safe.
 44    */
 45    public class Stopwatch {
 46    private final List<Long> _splits;
 47    private boolean _running;
 48    private long _start;
 49   
 50    /** Create a new stopwatch with no split times and in a stopped state. */
 51  0 public Stopwatch() {
 52  0 _running = false;
 53  0 _splits = new LinkedList<Long>();
 54    }
 55   
 56    /**
 57    * Create a new stopwatch with no split times; if {@code startImmediately} is {@code true},
 58    * start the timer before returning.
 59    */
 60  2 public Stopwatch(boolean startImmediately) {
 61  2 _running = false;
 62  2 _splits = new LinkedList<Long>();
 63  2 if (startImmediately) start();
 64    }
 65   
 66    /**
 67    * Start the timer.
 68    * @throws IllegalStateException If the stopwatch is currently running.
 69    */
 70  4 public void start() {
 71  0 if (_running) { throw new IllegalStateException("Already running"); }
 72  4 _start = System.currentTimeMillis();
 73  4 _running = true;
 74    }
 75   
 76    /**
 77    * Record and return the number of milliseconds since {@code start()} was invoked.
 78    * @throws IllegalStateException If the stopwatch is not currently running.
 79    */
 80  4 public long split() {
 81  0 if (!_running) { throw new IllegalStateException("Not running"); }
 82  4 long result = System.currentTimeMillis() - _start;
 83  4 _splits.add(result);
 84  4 return result;
 85    }
 86   
 87    /**
 88    * Stop the timer; record and return the number of milliseconds since {@code start()} was invoked.
 89    * @throws IllegalStateException If the stopwatch is not currently running.
 90    */
 91  4 public long stop() {
 92  4 long result = split();
 93  4 _running = false;
 94  4 return result;
 95    }
 96   
 97    /**
 98    * Get a dynamically-updating view of all splits recorded by the stopwatch.
 99    * (Create a {@link IterUtil#snapshot} if the stopwatch is to be used while iterating over
 100    * this list.)
 101    */
 102  0 public Iterable<Long> splits() { return IterUtil.immutable(_splits); }
 103    }