X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=support%2Fsrc%2Forg%2Fwamblee%2Fcache%2FCachedObject.java;h=787bc8703d015c685041729192ab9441b7e93172;hb=07cedd3f0730646ea35a7f668b3e1e872a4605d9;hp=8a4772f8fed557efd6d5c717b6a7d90325ded89a;hpb=fb4deb496257c78d4711aab48191dd9a0678798a;p=utils diff --git a/support/src/org/wamblee/cache/CachedObject.java b/support/src/org/wamblee/cache/CachedObject.java index 8a4772f8..787bc870 100644 --- a/support/src/org/wamblee/cache/CachedObject.java +++ b/support/src/org/wamblee/cache/CachedObject.java @@ -12,7 +12,7 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - */ + */ package org.wamblee.cache; @@ -21,91 +21,109 @@ import java.io.Serializable; import org.apache.log4j.Logger; /** - * Represents a cached object. The object is either retrieved from the cache if the cache - * has it, or a call back is invoked to get the object (and put it in the cache). + * Represents a cached object. The object is either retrieved from the cache if + * the cache has it, or a call back is invoked to get the object (and put it in + * the cache). */ public class CachedObject { - + private static final Logger LOGGER = Logger.getLogger(CachedObject.class); - + /** - * Callback invoked to compute an object if it was not found in the cache. - * @param Type of the object + * Callback invoked to compute an object if it was not found in the cache. + * + * @param + * Type of the object */ public static interface Computation { /** - * Gets the object. Called when the object is not in the cache. - * @param aObjectKey Id of the object in the cache. - * @return Object, must be non-null. + * Gets the object. Called when the object is not in the cache. + * + * @param aObjectKey + * Id of the object in the cache. + * @return Object, must be non-null. */ - Value getObject(Key aObjectKey); + Value getObject(Key aObjectKey); } - + /** - * Cache to use. + * Cache to use. */ private Cache _cache; - + /** - * Key of the object in the cache. + * Key of the object in the cache. */ - private KeyType _objectKey; - + private KeyType _objectKey; + /** - * Computation used to obtain the object if it is not found in the cache. + * Computation used to obtain the object if it is not found in the cache. */ - private Computation _computation; - + private Computation _computation; + /** - * Constructs the cached object. - * @param aCache Cache to use. - * @param aObjectKey Key of the object in the cache. - * @param aComputation Computation to get the object in case the object is not in the cache. + * Constructs the cached object. + * + * @param aCache + * Cache to use. + * @param aObjectKey + * Key of the object in the cache. + * @param aComputation + * Computation to get the object in case the object is not in the + * cache. */ - public CachedObject(Cache aCache, KeyType aObjectKey, Computation aComputation) { - _cache = aCache; - _objectKey = aObjectKey; - _computation = aComputation; + public CachedObject(Cache aCache, KeyType aObjectKey, + Computation aComputation) { + _cache = aCache; + _objectKey = aObjectKey; + _computation = aComputation; } - + /** - * Gets the object. Since the object is cached, different calls to this method may return - * different objects. - * @return Object. + * Gets the object. Since the object is cached, different calls to this + * method may return different objects. + * + * @return Object. */ - public ValueType get() { - ValueType object = (ValueType)_cache.get(_objectKey); // the used cache is thread safe. - if ( object == null ) { - // synchronize the computation to make sure that the object is only computed - // once when multiple concurrent threads detect that the entry must be - // recomputed. - synchronized (this) { - object = (ValueType)_cache.get(_objectKey); - if ( object == null ) { - // No other thread did a recomputation so we must do this now. - LOGGER.debug("Refreshing cache for '" + _objectKey + "'"); - object = _computation.getObject(_objectKey); - _cache.put(_objectKey, object); - } + public ValueType get() { + ValueType object = (ValueType) _cache.get(_objectKey); // the used + // cache is + // thread safe. + if (object == null) { + // synchronize the computation to make sure that the object is only + // computed + // once when multiple concurrent threads detect that the entry must + // be + // recomputed. + synchronized (this) { + object = (ValueType) _cache.get(_objectKey); + if (object == null) { + // No other thread did a recomputation so we must do this + // now. + LOGGER.debug("Refreshing cache for '" + _objectKey + "'"); + object = _computation.getObject(_objectKey); + _cache.put(_objectKey, object); + } } } - return object; + return object; } - + /** - * Invalidates the cache for the object so that it is recomputed the next - * time it is requested. - * + * Invalidates the cache for the object so that it is recomputed the next + * time it is requested. + * */ - public void invalidate() { + public void invalidate() { _cache.remove(_objectKey); } - + /** - * Gets the cache. - * @return Cache. + * Gets the cache. + * + * @return Cache. */ - public Cache getCache() { - return _cache; + public Cache getCache() { + return _cache; } }