Clover coverage report - PLT Utilities Test Coverage (plt-20120304-r5436)
Coverage timestamp: Sat Mar 3 2012 22:01:56 CST
file stats: LOC: 123   Methods: 9
NCLOC: 38   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ConditionMonitor.java 37.5% 24% 44.4% 31%
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 an arbitrary condition is satisfied.
 44    * Each time {@link #check} is invoked, the blocked threads check to see if the condition
 45    * has become {@code true}. Note that there is no way for this to occur "automatically" —
 46    * the only way the monitor can be made aware of a possible change is by an explicit call to {@code check()}.
 47    */
 48    public class ConditionMonitor implements Condition {
 49    private final Condition _condition;
 50   
 51    /** Create an unsignaled completion monitor. */
 52  1 public ConditionMonitor(Condition condition) { _condition = condition; }
 53   
 54    /** Returns whether the flag is currently set */
 55  3 public boolean isTrue() { return _condition.isTrue(); }
 56   
 57    /** Check the condition and, if it is now {@code true}, notify all blocked threads. */
 58  1 synchronized public void check() {
 59    // each thread will check the condition again, but we can optimize the case where the condition is false
 60    // by checking once here
 61  1 if (_condition.isTrue()) { this.notifyAll(); }
 62    }
 63   
 64    /** Ensures that the condition is true before continuing. Blocks if necessary. */
 65  3 synchronized public void ensureTrue() throws InterruptedException {
 66  2 while (!_condition.isTrue()) { this.wait(); }
 67    }
 68   
 69    /**
 70    * Ensures that the condition is true before continuing. Blocks if necessary; fails if the
 71    * the timeout is reached.
 72    * @param timeout Maximum wait time, in milliseconds.
 73    */
 74  0 public void ensureTrue(long timeout) throws InterruptedException, TimeoutException {
 75  0 ensureTrue(timeout, TimeUnit.MILLISECONDS);
 76    }
 77   
 78    /**
 79    * Ensures that the condition is true before continuing. Blocks if necessary; fails if the
 80    * the timeout is reached.
 81    * @param timeout Maximum wait time, in {@code unit} units.
 82    * @param unit Units for {@code timeout}.
 83    */
 84  0 public synchronized void ensureTrue(long timeout, TimeUnit unit) throws InterruptedException, TimeoutException {
 85  0 if (!_condition.isTrue()) {
 86  0 long timeoutTime = ConcurrentUtil.futureTimeNanos(timeout, unit);
 87  0 do { ConcurrentUtil.waitUntilNanos(this, timeoutTime); } while (!_condition.isTrue());
 88    }
 89    }
 90   
 91    /**
 92    * Tries to ensure that the condition is true before continuing. Blocks if necessary. If the wait is
 93    * interrupted, returns {@code false}.
 94    */
 95  0 public boolean attemptEnsureTrue() {
 96  0 try { ensureTrue(); return true; }
 97  0 catch (InterruptedException e) { return _condition.isTrue(); }
 98    }
 99   
 100    /**
 101    * Tries to ensure that the monitor has been signaled before continuing. Blocks if necessary. If the wait
 102    * is interrupted or the timeout is reached, returns {@code false}.
 103    * @param timeout Maximum wait time, in milliseconds.
 104    */
 105  0 public boolean attemptEnsureTrue(long timeout) {
 106  0 try { ensureTrue(timeout, TimeUnit.MILLISECONDS); return true; }
 107  0 catch (InterruptedException e) { return _condition.isTrue(); }
 108  0 catch (TimeoutException e) { return _condition.isTrue(); }
 109    }
 110   
 111    /**
 112    * Tries to ensure that the monitor has been signaled before continuing. Blocks if necessary. If the wait
 113    * is interrupted or the timeout is reached, returns {@code false}.
 114    * @param timeout Maximum wait time, in {@code unit} units.
 115    * @param unit Units for {@code timeout}.
 116    */
 117  0 public boolean attemptEnsureTrue(long timeout, TimeUnit unit) {
 118  0 try { ensureTrue(timeout, unit); return true; }
 119  0 catch (InterruptedException e) { return _condition.isTrue(); }
 120  0 catch (TimeoutException e) { return _condition.isTrue(); }
 121    }
 122   
 123    }