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 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 Details

    • 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 ConcurrentMap<Integer,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 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 Lock cleanupLock
      Lock to prevent concurrent cleanup of the map.
    • hashCodeCollisionCounter

      private final AtomicInteger hashCodeCollisionCounter
  • Constructor Details

    • 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 the fetch(Object) method will simply return its argument. To enable the cache, set this property to "true" (case insensitive).
  • Method Details

    • 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(Object p, Object q)