Clover coverage report - PLT Utilities Test Coverage (plt-20120304-r5436)
Coverage timestamp: Sat Mar 3 2012 22:01:56 CST
file stats: LOC: 96   Methods: 7
NCLOC: 29   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
CachedThunk.java 50% 73.3% 71.4% 69.2%
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.lambda;
 36   
 37    import edu.rice.cs.plt.tuple.Option;
 38   
 39    /**
 40    * <p>A thunk that caches the result of a nested thunk on the first invocation of {@code value()}.
 41    * Unlike {@link LazyThunk}, the cached result can be invalidated by invoking {@link #reset}; the
 42    * next invocation of {@code value()} will again delegate to the nested thunk (the trade-off for
 43    * this behavior is that the wrapped thunk cannot be discarded for the life of this object).
 44    * If an exception occurs during evaluation, no result is cached.</p>
 45    *
 46    * <p>Evaluation is thread-safe: locking guarantees that the nested thunk will never be evaluated (and
 47    * terminate normally) twice without an intervening {@code reset()} invocation. Thus, if two threads
 48    * invoke {@code value()} simultaneously (and a result is not cached), one will block until
 49    * the other resolves the nested thunk.</p>
 50    *
 51    * @see LazyThunk
 52    * @see DelayedThunk
 53    * @see LazyRunnable
 54    */
 55    public class CachedThunk<R> implements ResolvingThunk<R> {
 56   
 57    private final Thunk<? extends R> _thunk;
 58    private volatile Option<R> _val;
 59   
 60  12 public CachedThunk(Thunk<? extends R> value) {
 61  12 _thunk = value;
 62  12 _val = Option.none();
 63    }
 64   
 65  9 public R value() {
 66  9 Option<R> v = _val; // get a local copy in case there's a concurrent reset
 67  9 if (v.isNone()) { return resolve(); }
 68  0 else { return v.unwrap(); }
 69    }
 70   
 71  9 public synchronized void reset() {
 72    // *must* synchronize -- if we try to reset while resolve() is half-done, the reset may appear
 73    // to have never happened
 74  9 _val = Option.none();
 75    }
 76   
 77  0 public boolean isResolved() { return _val.isSome(); }
 78   
 79    /**
 80    * Combines {@link #isResolved} and {@link #value} in an atomic operation (avoiding inconsistencies
 81    * due to a concurrent {@link #reset}): if a value is cached, return it; otherwise, return "none".
 82    */
 83  0 public Option<R> cachedValue() { return _val; }
 84   
 85  9 private synchronized R resolve() {
 86  9 if (_val.isNone()) { // verify that the result is still unresolved now that we have a lock
 87  9 R result = _thunk.value();
 88  9 _val = Option.some(result);
 89  9 return result;
 90    }
 91  0 else { return _val.unwrap(); }
 92    }
 93   
 94  12 public static <R> CachedThunk<R> make(Thunk<? extends R> value) { return new CachedThunk<R>(value); }
 95   
 96    }