X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=support%2Fgeneral%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Fcache%2FCachedObject.java;h=bdabd51795a92787893ed87c862827a8eb754586;hb=f4d446b84fa1a38ed83cd157f79fdb8233822145;hp=f844c728e9b69d18c176ed3afc95133d223fe243;hpb=246a6a94ac725ea202a79591a69530014bf1da5f;p=utils diff --git a/support/general/src/main/java/org/wamblee/cache/CachedObject.java b/support/general/src/main/java/org/wamblee/cache/CachedObject.java index f844c728..bdabd517 100644 --- a/support/general/src/main/java/org/wamblee/cache/CachedObject.java +++ b/support/general/src/main/java/org/wamblee/cache/CachedObject.java @@ -1,52 +1,40 @@ /* * Copyright 2005 the original author or authors. - * + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * 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; +import org.apache.log4j.Logger; + 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). * * @author Erik Brakkee + * + * @param DOCUMENT ME! + * @param DOCUMENT ME! */ 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 + * DOCUMENT ME! */ - 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. - */ - Value getObject(Key aObjectKey); - } + private static final Logger LOGGER = Logger.getLogger(CachedObject.class); /** * Cache to use. @@ -59,13 +47,14 @@ public class CachedObject computation; - /** +/** * Constructs the cached object. - * + * * @param aCache * Cache to use. * @param aObjectKey @@ -75,22 +64,23 @@ public class CachedObject aCache, KeyType aObjectKey, - Computation aComputation) { - cache = aCache; - objectKey = aObjectKey; - computation = aComputation; + 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. - * + * 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. + // cache is + // thread safe. + if (object == null) { // synchronize the computation to make sure that the object is only // computed @@ -99,6 +89,7 @@ public class CachedObject + * 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. + */ + Value getObject(Key aObjectKey); + } }