Adrian,
you forgot to account for the case where the GC removes the instance in
the worst possible moment and WeakReference.get() returns null.
On 20.07.2007 16:33:34 Adrian Cumiskey wrote:
> Index: src/java/org/apache/fop/fo/properties/PropertyCache.java
> ===================================================================
> --- src/java/org/apache/fop/fo/properties/PropertyCache.java (revision
> 555659)
> +++ src/java/org/apache/fop/fo/properties/PropertyCache.java (working copy)
> @@ -19,6 +19,7 @@
>
> package org.apache.fop.fo.properties;
>
> +import java.lang.ref.WeakReference;
> import java.util.Collections;
> import java.util.Map;
> import java.util.WeakHashMap;
> @@ -39,16 +40,16 @@
> * Checks if the given property is present in the cache - if so, returns
> * a reference to the cached value. Otherwise the given object is added
> * to the cache and returned.
> - * @param obj
> + * @param prop a property
> * @return the cached instance
> */
> public Property fetch(Property prop) {
> -
> - Property cacheEntry = (Property) propCache.get(prop);
> - if (cacheEntry != null) {
> + WeakReference ref = (WeakReference) propCache.get(prop);
> + if (ref != null) {
> + Property cacheEntry = (Property)ref.get();
> return cacheEntry;
> } else {
> - propCache.put(prop, prop);
> + propCache.put(prop, new WeakReference(prop));
> return prop;
> }
> }
Jeremias Maerki