Package org.apache.fop.fo.properties
Class PropertyCache<T>
- java.lang.Object
-
- org.apache.fop.fo.properties.PropertyCache<T>
-
- Type Parameters:
T
- The type of values that are cached
public final class PropertyCache<T> extends java.lang.Object
Thread-safe cache that minimizes the memory requirements by fetching an instance from the cache that is equal to the given one. Internally the instances are stored in WeakReferences in order to be reclaimed when they are no longer referenced.
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.concurrent.locks.Lock
cleanupLock
Lock to prevent concurrent cleanup of the map.private java.util.concurrent.atomic.AtomicInteger
hashCodeCollisionCounter
private static org.apache.commons.logging.Log
LOG
private java.util.concurrent.ConcurrentMap<java.lang.Integer,java.lang.ref.WeakReference<T>>
map
The underlying map that stores WeakReferences to the cached entries.private java.util.concurrent.atomic.AtomicInteger
putCounter
Counts the number of entries put in the map in order to periodically check and remove the entries whose referents have been reclaimed.private boolean
useCache
Determines if the cache is used based on the value of the system property org.apache.fop.fo.properties.use-cache
-
Constructor Summary
Constructors Constructor Description PropertyCache()
Creates a new cache.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private void
attemptCleanup()
private void
cleanReclaimedMapEntries()
private boolean
eq(java.lang.Object p, java.lang.Object q)
T
fetch(T obj)
Returns a cached version of the given object.
-
-
-
Field Detail
-
LOG
private static final org.apache.commons.logging.Log LOG
-
useCache
private final boolean useCache
Determines if the cache is used based on the value of the system property org.apache.fop.fo.properties.use-cache
-
map
private final java.util.concurrent.ConcurrentMap<java.lang.Integer,java.lang.ref.WeakReference<T>> map
The underlying map that stores WeakReferences to the cached entries. The map keys are the hashCode of the cached entries. The map values are a WeakRefence to the cached entries. When two cached entries have the same hash code, the last one is kept but this should be an exception case (otherwise the hashCode() method of T needs to be fixed).
-
putCounter
private final java.util.concurrent.atomic.AtomicInteger putCounter
Counts the number of entries put in the map in order to periodically check and remove the entries whose referents have been reclaimed.
-
cleanupLock
private final java.util.concurrent.locks.Lock cleanupLock
Lock to prevent concurrent cleanup of the map.
-
hashCodeCollisionCounter
private final java.util.concurrent.atomic.AtomicInteger hashCodeCollisionCounter
-
-
Constructor Detail
-
PropertyCache
public PropertyCache()
Creates a new cache. The "org.apache.fop.fo.properties.use-cache" system property is used to determine whether properties should actually be cached or not. If not, then thefetch(Object)
method will simply return its argument. To enable the cache, set this property to "true" (case insensitive).
-
-
Method Detail
-
fetch
public T fetch(T obj)
Returns a cached version of the given object. If the object is not yet in the cache, it will be added and then returned.- Parameters:
obj
- an object- Returns:
- a cached version of the object
-
attemptCleanup
private void attemptCleanup()
-
cleanReclaimedMapEntries
private void cleanReclaimedMapEntries()
-
eq
private boolean eq(java.lang.Object p, java.lang.Object q)
-
-