edu.rice.cs.plt.lambda
Class CachedThunk<R>

java.lang.Object
  extended by edu.rice.cs.plt.lambda.CachedThunk<R>
All Implemented Interfaces:
ResolvingThunk<R>, Thunk<R>

public class CachedThunk<R>
extends java.lang.Object
implements ResolvingThunk<R>

A thunk that caches the result of a nested thunk on the first invocation of value(). Unlike LazyThunk, the cached result can be invalidated by invoking reset(); the next invocation of value() will again delegate to the nested thunk (the trade-off for this behavior is that the wrapped thunk cannot be discarded for the life of this object). If an exception occurs during evaluation, no result is cached.

Evaluation is thread-safe: locking guarantees that the nested thunk will never be evaluated (and terminate normally) twice without an intervening reset() invocation. Thus, if two threads invoke value() simultaneously (and a result is not cached), one will block until the other resolves the nested thunk.

See Also:
LazyThunk, DelayedThunk, LazyRunnable

Constructor Summary
CachedThunk(Thunk<? extends R> value)
           
 
Method Summary
 Option<R> cachedValue()
          Combines isResolved() and value() in an atomic operation (avoiding inconsistencies due to a concurrent reset()): if a value is cached, return it; otherwise, return "none".
 boolean isResolved()
          Test whether the thunk is in a "resolved" state.
static
<R> CachedThunk<R>
make(Thunk<? extends R> value)
           
 void reset()
           
 R value()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CachedThunk

public CachedThunk(Thunk<? extends R> value)
Method Detail

value

public R value()
Specified by:
value in interface Thunk<R>

reset

public void reset()

isResolved

public boolean isResolved()
Description copied from interface: ResolvingThunk
Test whether the thunk is in a "resolved" state. If true, an invocation of value will return promptly and without exception.

Specified by:
isResolved in interface ResolvingThunk<R>

cachedValue

public Option<R> cachedValue()
Combines isResolved() and value() in an atomic operation (avoiding inconsistencies due to a concurrent reset()): if a value is cached, return it; otherwise, return "none".


make

public static <R> CachedThunk<R> make(Thunk<? extends R> value)