Class ReferenceKeeper
java.lang.Object
org.apache.sis.referencing.factory.ReferenceKeeper
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.cachebecause 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 toMap.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
LinkedHashMapas 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
FieldsModifier and TypeFieldDescriptionprivate org.opengis.referencing.IdentifiedObject[]The objects to retain by strong reference.private static final intNumber of references to keep.private static final longTime to wait before to remove entries from this map.private booleanWhether a cleaner task has already been registered for removing oldest entries.private intIndex of the last element stored in thecachearray.private long[]Time where object references were stored in this object. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate voidInvoked in a background thread for clearing expired entries.(package private) final voidmarkAsUsed(org.opengis.referencing.IdentifiedObject object) Retains the given object by strong reference for a limited amount of time.private voidscheduleCleanerTask(long now) Registers a task to be executed later for removing expired entries.
-
Field Details
-
CAPACITY
private static final int CAPACITYNumber 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_TIMETime to wait before to remove entries from this map. Current value is 5 minutes.- See Also:
-
cache
private org.opengis.referencing.IdentifiedObject[] cacheThe objects to retain by strong reference. May contains duplicated values andnullanywhere. This is used as a cyclic queue. We use an array instead ofLinkedHashMapfor more control on which instance is retained (objet identity matter, not just object equality). -
timestamps
private long[] timestampsTime where object references were stored in this object. Used for finding which references expired. -
indexOfLast
private int indexOfLastIndex of the last element stored in thecachearray. -
hasCleanerTask
private boolean hasCleanerTaskWhether 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 ofSystem.nanoTime().
-
clearExpiredEntries
private void clearExpiredEntries()Invoked in a background thread for clearing expired entries. This will allow the garbage collector to remove the entries fromConcurrentAuthorityFactory.findPoolif not used elsewhere.
-