org.wamblee.cache
Class ComputedValue<T>

java.lang.Object
  extended by org.wamblee.cache.ComputedValue<T>
Type Parameters:
T -

public class ComputedValue<T>
extends Object

Utility class to deal with recomputation of a certain value. The goal is to have only one thread at a time compute the value while other threads that simulateneously detect recomputation continue with the old value.

Author:
Erik Brakkee

Nested Class Summary
static interface ComputedValue.Computation<T>
          Computation
 
Constructor Summary
ComputedValue(Object aLock, ComputedValue.Computation aComputation)
          Constructs the compute guard
 
Method Summary
 void compute()
          Triggers computation of the value (if no other thread is currently computing the value).
 T get()
          Gets the current value of the object, recomputing it if the object is out of date.
 T getCached()
          Gets the currently cached value.
 void set(T aValue)
          Sets the value explicitly.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ComputedValue

public ComputedValue(Object aLock,
                     ComputedValue.Computation aComputation)
Constructs the compute guard

Parameters:
aLock - Lock to use during computation and to guard the value.
aComputation - Computation to use.
Method Detail

compute

public void compute()
Triggers computation of the value (if no other thread is currently computing the value).


get

public T get()
Gets the current value of the object, recomputing it if the object is out of date. This method ensures that only one thread at a time will do recomputations.

Returns:
Current value.

getCached

public T getCached()
Gets the currently cached value. No recomputation is performed.

Returns:
Cached value.

set

public void set(T aValue)
Sets the value explicitly.

Parameters:
aValue - value to set.


Copyright © 2011. All Rights Reserved.