Clover coverage report - PLT Utilities Test Coverage (plt-20120304-r5436)
Coverage timestamp: Sat Mar 3 2012 22:01:56 CST
file stats: LOC: 144   Methods: 12
NCLOC: 54   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
CompletionMonitor.java 50% 47.1% 58.3% 50%
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.concurrent;
 36   
 37    import java.util.concurrent.TimeUnit;
 38    import java.util.concurrent.TimeoutException;
 39   
 40    import edu.rice.cs.plt.lambda.Condition;
 41   
 42    /**
 43    * Provides a convenient facility for blocking until a boolean flag is signaled. Typically, this is used
 44    * for communication in which one thread must complete a task before other threads can proceed.
 45    */
 46    public class CompletionMonitor implements Condition {
 47    private volatile boolean _signal;
 48   
 49    /** Create an unsignaled completion monitor. */
 50  15 public CompletionMonitor() { _signal = false; }
 51   
 52    /**
 53    * Create a completion monitor in the given initial state. If signaled is {@code true}, invocations of
 54    * {@link #ensureSignaled} will not block until {@link #reset} is invoked.
 55    */
 56  8 public CompletionMonitor(boolean signaled) { _signal = signaled; }
 57   
 58    /** Returns whether the flag is currently set */
 59  17 public boolean isSignaled() { return _signal; }
 60   
 61    /** Returns whether the flag is currently set */
 62  0 public boolean isTrue() { return _signal; }
 63   
 64    /** Revert to the unsignaled state */
 65  5 public void reset() { _signal = false; }
 66   
 67    /** Sets the state to signaled and alerts all blocked threads */
 68  21 public void signal() {
 69  21 if (!_signal) {
 70  21 synchronized (this) {
 71  21 if (!_signal) {
 72  21 _signal = true;
 73  21 this.notifyAll();
 74    }
 75    }
 76    }
 77    }
 78   
 79    /** Ensures that the monitor has been signaled before continuing. Blocks if necessary. */
 80  19 public void ensureSignaled() throws InterruptedException {
 81  19 if (!_signal) {
 82  15 synchronized (this) {
 83  15 while (!_signal) { this.wait(); }
 84    }
 85    }
 86    }
 87   
 88    /**
 89    * Ensures that the monitor has been signaled before continuing. Blocks if necessary; fails if the
 90    * the timeout is reached.
 91    * @param timeout Maximum wait time, in milliseconds.
 92    */
 93  0 public void ensureSignaled(long timeout) throws InterruptedException, TimeoutException {
 94  0 ensureSignaled(timeout, TimeUnit.MILLISECONDS);
 95    }
 96   
 97    /**
 98    * Ensures that the monitor has been signaled before continuing. Blocks if necessary; fails if the
 99    * the timeout is reached.
 100    * @param timeout Maximum wait time, in {@code unit} units.
 101    * @param unit Units for {@code timeout}.
 102    */
 103  0 public void ensureSignaled(long timeout, TimeUnit unit) throws InterruptedException, TimeoutException {
 104  0 if (!_signal) {
 105  0 long timeoutTime = ConcurrentUtil.futureTimeNanos(timeout, unit);
 106  0 synchronized (this) {
 107  0 while (!_signal) { ConcurrentUtil.waitUntilNanos(this, timeoutTime); }
 108    }
 109    }
 110    }
 111   
 112    /**
 113    * Tries to ensure that the monitor has been signaled before continuing. Blocks if necessary. If the wait
 114    * is interrupted, returns {@code false}.
 115    */
 116  5 public boolean attemptEnsureSignaled() {
 117  5 try { ensureSignaled(); return true; }
 118  0 catch (InterruptedException e) { return _signal; }
 119    }
 120   
 121    /**
 122    * Tries to ensure that the monitor has been signaled before continuing. Blocks if necessary. If the wait
 123    * is interrupted or the timeout is reached, returns {@code false}.
 124    * @param timeout Maximum wait time, in milliseconds.
 125    */
 126  0 public boolean attemptEnsureSignaled(long timeout) {
 127  0 try { ensureSignaled(timeout, TimeUnit.MILLISECONDS); return true; }
 128  0 catch (InterruptedException e) { return _signal; }
 129  0 catch (TimeoutException e) { return _signal; }
 130    }
 131   
 132    /**
 133    * Tries to ensure that the monitor has been signaled before continuing. Blocks if necessary. If the wait
 134    * is interrupted or the timeout is reached, returns {@code false}.
 135    * @param timeout Maximum wait time, in {@code unit} units.
 136    * @param unit Units for {@code timeout}.
 137    */
 138  0 public boolean attemptEnsureSignaled(long timeout, TimeUnit unit) {
 139  0 try { ensureSignaled(timeout, unit); return true; }
 140  0 catch (InterruptedException e) { return _signal; }
 141  0 catch (TimeoutException e) { return _signal; }
 142    }
 143   
 144    }