Package com.automation_pros.odva.util
Class SizedSoftCache<T>
- java.lang.Object
-
- com.automation_pros.odva.util.SizedSoftCache<T>
-
- Type Parameters:
T-
- Direct Known Subclasses:
ByteBuffersSoftCache,BytesSoftCache
public abstract class SizedSoftCache<T> extends java.lang.ObjectCommon code for a simple class that caches object that have an associated size
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringFullNameprotected java.lang.Stringidentityprotected intminimumprotected java.util.Map<java.lang.Integer,SoftCache<T>>sizedCachesstatic org.slf4j.LoggersLoggerprotected inttraceIndex
-
Constructor Summary
Constructors Modifier Constructor Description protectedSizedSoftCache(int traceIndex, int minimum)protectedSizedSoftCache(int traceIndex, java.lang.String identity, int minimum)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description Tallocate(int desiredSize)Obtain a new or recycled object of the given type with capacity no less than the specified desired size.protected abstract Tcreate(int size)When the cache runs empty, further allocations must create new objects.protected abstract intdynamicEach(int size)Implementations must provide a function that estimates the GC impact of an instance of a given size.voidfree(T recycle)Return an object of the given type for caching.intnormalizedSize(int requestSize)To maximize re-use, requested sizes are rounded up to the next larger power of two, or the next larger value that is 3 * a power of two.protected abstract booleanqualified(T recycle)An item returned from a user might have been changed in a way that makes it unsuitable for reuse.protected abstract intsizeOf(T recycle)When an object is freed for recycling, its size must be examined to determine the appropriate nested cache to use, and whether it exactly matches that cache.
-
-
-
Method Detail
-
dynamicEach
protected abstract int dynamicEach(int size)
Implementations must provide a function that estimates the GC impact of an instance of a given size.- Parameters:
size-- Returns:
-
normalizedSize
public int normalizedSize(int requestSize)
To maximize re-use, requested sizes are rounded up to the next larger power of two, or the next larger value that is 3 * a power of two. But never less than 2.Like so: 2, 3, 4, 6, 8, 12, 16, 24, 32, 48, 64, 96, 128, 192, 256...
- Parameters:
requestSize-- Returns:
-
qualified
protected abstract boolean qualified(T recycle)
An item returned from a user 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:
recycle- 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(int size)
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(int desiredSize)
Obtain a new or recycled object of the given type with capacity no less than the specified desired size. Collect timing stats by normalized size on allocation pace to maintain approximately five seconds of buffered objects.- Returns:
-
sizeOf
protected abstract int sizeOf(T recycle)
When an object is freed for recycling, its size must be examined to determine the appropriate nested cache to use, and whether it exactly matches that cache.- Parameters:
recycle-- Returns:
- The actual size of the object to recycle.
-
free
public void free(T recycle)
Return an object of the given type for caching.- Parameters:
recycle-
-
-