Class ReferenceKeeper

java.lang.Object
org.apache.sis.referencing.factory.ReferenceKeeper

final class ReferenceKeeper extends Object
Most recently used objects stored or accessed in ConcurrentAuthorityFactory.findPool, retained by strong references for preventing too early garbage collection.

Design notes

  • Elements in this collection are generally not in ConcurrentAuthorityFactory.cache because they are "foreigner" objects, possibly created by different authorities. We have to maintain them in a separated collection.
  • We have to be careful about the references kept in this object. The purpose is to prevent garbage collection, so Object.equals(Object) is not the appropriate contract for deciding which elements to put. For example, a call to Map.put(key, value) may update the value without replacing the key if an entry already exists in the map, in which case the instance that is protected against garbage collection is not the intended one.
  • We tried to use LinkedHashMap as a LRU map in a previous version. It was not really simpler because of above-cited issue with object identities.
Since:
1.1
Version:
1.1
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private org.opengis.referencing.IdentifiedObject[]
    The objects to retain by strong reference.
    private static final int
    Number of references to keep.
    private static final long
    Time to wait before to remove entries from this map.
    private boolean
    Whether a cleaner task has already been registered for removing oldest entries.
    private int
    Index of the last element stored in the cache array.
    private long[]
    Time where object references were stored in this object.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs an initially empty instance.
  • Method Summary

    Modifier and Type
    Method
    Description
    private void
    Invoked in a background thread for clearing expired entries.
    (package private) final void
    markAsUsed(org.opengis.referencing.IdentifiedObject object)
    Retains the given object by strong reference for a limited amount of time.
    private void
    Registers a task to be executed later for removing expired entries.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • CAPACITY

      private static final int CAPACITY
      Number of references to keep. Should be relatively small because this class implementation is not designed for large collections. Note that the number of unique entries may be lower because this class does not try to avoid duplicated references.
      See Also:
    • EXPIRATION_TIME

      private static final long EXPIRATION_TIME
      Time to wait before to remove entries from this map. Current value is 5 minutes.
      See Also:
    • cache

      private org.opengis.referencing.IdentifiedObject[] cache
      The objects to retain by strong reference. May contains duplicated values and null anywhere. This is used as a cyclic queue. We use an array instead of LinkedHashMap for more control on which instance is retained (objet identity matter, not just object equality).
    • timestamps

      private long[] timestamps
      Time where object references were stored in this object. Used for finding which references expired.
    • indexOfLast

      private int indexOfLast
      Index of the last element stored in the cache array.
    • hasCleanerTask

      private boolean hasCleanerTask
      Whether a cleaner task has already been registered for removing oldest entries.
  • Constructor Details

    • ReferenceKeeper

      ReferenceKeeper()
      Constructs an initially empty instance.
  • Method Details

    • markAsUsed

      final void markAsUsed(org.opengis.referencing.IdentifiedObject object)
      Retains the given object by strong reference for a limited amount of time.
      Parameters:
      object - the object to temporarily retain by strong reference.
    • scheduleCleanerTask

      private void scheduleCleanerTask(long now)
      Registers a task to be executed later for removing expired entries. It is caller responsibility to verify that this method should be invoked.
      Parameters:
      now - value of System.nanoTime().
    • clearExpiredEntries

      private void clearExpiredEntries()
      Invoked in a background thread for clearing expired entries. This will allow the garbage collector to remove the entries from ConcurrentAuthorityFactory.findPool if not used elsewhere.