Package com.automation_pros.odva.util
Class SoftCache<T>
- java.lang.Object
-
- com.automation_pros.odva.util.SoftCache<T>
-
- Type Parameters:
T- The concrete data type this cache tracks.
public abstract class SoftCache<T> extends java.lang.ObjectCommon code for a simple cache based on Soft References. As items are retrieved from the garbage collector, implementations can validate or reset instances to reusable condition, or signal non-reusability.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected classSoftCache.NonReturnedprotected classSoftCache.NonReturnedSumprotected classSoftCache.Returned
-
Field Summary
Fields Modifier and Type Field Description protected longallocationCostprotected static java.util.WeakHashMap<SoftCache<?>,java.lang.String>cachesprotected intcapacityprotected longcreationCostprotected longcreationsprotected longcreations0protected inteachstatic java.lang.StringFullNameprotected java.lang.Stringidentityprotected intmaxQueuedprotected intminQueuedprotected java.util.concurrent.ConcurrentHashMap<java.lang.Integer,SoftCache.NonReturned>nonReturnsprotected static java.lang.Threadprunerprotected longrecycleCostprotected longrecyclesprotected longrecycles0protected java.util.concurrent.ConcurrentLinkedDeque<SoftCache.Returned>returnedprotected java.util.concurrent.ConcurrentHashMap<java.lang.Integer,SoftCache.Returned>returnedMapprotected intsizeEstimatestatic org.slf4j.LoggersLoggerprotected inttraceIndex
-
Constructor Summary
Constructors Modifier Constructor Description protectedSoftCache(int each, int traceIndex)protectedSoftCache(int each, int traceIndex, java.lang.String identity)Implementations can provide a more concise and/or more informative string to use in place of the simple class name.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description Tallocate()Obtain a new or recycled object of the given type.protected abstract Tcreate()When the cache runs empty, further allocations must create new objects.voidfree(T recycle)Return an object of the given type for caching.protected java.lang.String[]prune(long nonReturnThreshold)protected abstract booleanqualified(T recycled)An item returned from the garbage collector might have been changed in a way that makes it unsuitable for reuse.static voidshutdown()protected voidstop()
-
-
-
Field Detail
-
sLogger
public static final org.slf4j.Logger sLogger
-
FullName
public static final java.lang.String FullName
-
caches
protected static final java.util.WeakHashMap<SoftCache<?>,java.lang.String> caches
-
pruner
protected static java.lang.Thread pruner
-
nonReturns
protected final java.util.concurrent.ConcurrentHashMap<java.lang.Integer,SoftCache.NonReturned> nonReturns
-
returnedMap
protected final java.util.concurrent.ConcurrentHashMap<java.lang.Integer,SoftCache.Returned> returnedMap
-
returned
protected final java.util.concurrent.ConcurrentLinkedDeque<SoftCache.Returned> returned
-
each
protected final int each
-
traceIndex
protected final int traceIndex
-
identity
protected final java.lang.String identity
-
capacity
protected int capacity
-
sizeEstimate
protected volatile int sizeEstimate
-
recycles0
protected long recycles0
-
recycles
protected long recycles
-
recycleCost
protected volatile long recycleCost
-
creations0
protected long creations0
-
creations
protected long creations
-
creationCost
protected volatile long creationCost
-
minQueued
protected int minQueued
-
maxQueued
protected int maxQueued
-
allocationCost
protected volatile long allocationCost
-
-
Method Detail
-
shutdown
public static void shutdown()
-
prune
protected java.lang.String[] prune(long nonReturnThreshold)
-
stop
protected void stop()
-
qualified
protected abstract boolean qualified(T recycled)
An item returned from the garbage collector might have been changed in a way that makes it unsuitable for reuse. This method gives the implementation a chance to verify that and/or make corrections.- Parameters:
recycled- The subject item that is to be placed back in this cache's fast access list.- Returns:
- True if ready for re-use, false if to be discarded.
-
create
protected abstract T create()
When the cache runs empty, further allocations must create new objects. Since the queue is based on soft references, the garbage collector can throw some away at will.- Returns:
-
allocate
public T allocate()
Obtain a new or recycled object of the given type. Collect timing stats on allocation pace to maintain approximately five seconds of buffered objects.- Returns:
-
free
public void free(T recycle)
Return an object of the given type for caching.- Parameters:
recycle-
-
-