Class BasicRecycler<R extends RecyclableInterface<R,P>,P extends RecyclerRequestInterface>
- java.lang.Object
-
- net.haesleinhuepf.clij.coremem.rgc.FreeableBase
-
- net.haesleinhuepf.clij.coremem.recycling.BasicRecycler<R,P>
-
- Type Parameters:
R- Recyclable typeP- Request type (P stands for request Parameter)
- All Implemented Interfaces:
RecyclerInterface<R,P>,Freeable
public class BasicRecycler<R extends RecyclableInterface<R,P>,P extends RecyclerRequestInterface> extends FreeableBase implements RecyclerInterface<R,P>, Freeable
Basic Recycler. The basic recycler uses a single queue to hold recycled objects. When a request cannot be satisfied by any recycled objects, then these objects are released and new objects are created according to the request.- Author:
- royer
-
-
Constructor Summary
Constructors Constructor Description BasicRecycler(RecyclableFactoryInterface<R,P> pRecyclableFactory, int pMaximumNumberOfObjects)Instanciates a basic recycler with a given recyclable factory and maximum number of objects.BasicRecycler(RecyclableFactoryInterface<R,P> pRecyclableFactory, int pMaximumNumberOfLiveObjects, int pMaximumNumberOfAvailableObjects, boolean pAutoFree)Instanciates a basic recycler with a given recyclable factory and maximum number of live and available objects.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddListener(RecyclerListenerInterface pRecyclerListener)Adds a recycler listener to this recycler.voidclearLive()Clears the list of live objects and frees them if the 'autofree' flag is setvoidclearReleased()Clears the list of available objects and frees them if the 'autofree' flag is set.longcomputeAvailableMemorySizeInBytes()Returns the calculated total size in bytes of available objects.longcomputeLiveMemorySizeInBytes()Returns the calculated total size in bytes of live objects.longensurePreallocated(long pNumberofPrealocatedRecyclablesNeeded, P pRecyclerRequest)Ensures that a given number of recyclable objects are available.voidfree()Free this object, which means that the underlying ressources are released.Rget(boolean pWaitForLiveObjectToComeBack, long pWaitTime, TimeUnit pTimeUnit, P pRecyclerRequest)Requests a recyclable object (all other get methods delegate to this one).TimeUnitgetAvailableQueueTimeUnit()Returns the available queue time unitlonggetAvailableQueueWaitTime()Returns the waiting time for the available queue.intgetMaxNumberOfAvailableObjects()Returns the maximum number of available objects allowed.intgetMaxNumberOfLiveObjects()Returns the maximum number of live objects allowed.intgetNumberOfAvailableObjects()Returns the current number of recyclable objects available for recycling.longgetNumberOfFailedRequests()Returns the number of failed requests.intgetNumberOfLiveObjects()Returns the current number of live objects - these are objects that are curently in use and thus not available for recycling.RgetOrFail(P pRecyclerRequest)Attempts to get a recyclable object from this recycler for a given request.RgetOrWait(long pWaitTime, TimeUnit pTimeUnit, P pRecyclerRequest)Waits for a given amount of time to return a recyclable object given a request.booleanisFree()Returns true if the object has been freed, which usually means that the underlying ressources are not longer available.voidprintDebugInfo()BELOW ARE DEBUG METHODS:voidrelease(R pRecyclable)Releases the given objectvoidremoveListener(RecyclerListenerInterface pRecyclerListener)Remioves a recycler listener to this recycler.voidsetAvailableQueueWaitingTime(long pAvailableQueueWaitTime, TimeUnit pTimeUnit)Sets the waiting time for the available queue.-
Methods inherited from class net.haesleinhuepf.clij.coremem.rgc.FreeableBase
complainIfFreed
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface net.haesleinhuepf.clij.coremem.rgc.Freeable
complainIfFreed
-
-
-
-
Constructor Detail
-
BasicRecycler
public BasicRecycler(RecyclableFactoryInterface<R,P> pRecyclableFactory, int pMaximumNumberOfObjects)
Instanciates a basic recycler with a given recyclable factory and maximum number of objects. This number of objects is split evenly between live and available objects.- Parameters:
pRecyclableFactory- factory capable of instanciating recyclablespMaximumNumberOfObjects- maximum number of objects (live + availables)
-
BasicRecycler
public BasicRecycler(RecyclableFactoryInterface<R,P> pRecyclableFactory, int pMaximumNumberOfLiveObjects, int pMaximumNumberOfAvailableObjects, boolean pAutoFree)
Instanciates a basic recycler with a given recyclable factory and maximum number of live and available objects. A flag determines whether released objects should also be freed.- Parameters:
pRecyclableFactory- factory capable of instanciating recyclablespMaximumNumberOfLiveObjects- maximum number of live objectspMaximumNumberOfAvailableObjects- maximum number of available objectspAutoFree- true free released/cleared objects.
-
-
Method Detail
-
setAvailableQueueWaitingTime
public void setAvailableQueueWaitingTime(long pAvailableQueueWaitTime, TimeUnit pTimeUnit)Sets the waiting time for the available queue. A long integer and time unit must be provided.- Parameters:
pAvailableQueueWaitTime- timepTimeUnit- time unit
-
getAvailableQueueWaitTime
public long getAvailableQueueWaitTime()
Returns the waiting time for the available queue.- Returns:
- witing time, call getAvailableQueueTimeUnit() to get the corresponding time unit.
-
getAvailableQueueTimeUnit
public TimeUnit getAvailableQueueTimeUnit()
Returns the available queue time unit- Returns:
- available queue time unit
-
addListener
public void addListener(RecyclerListenerInterface pRecyclerListener)
Description copied from interface:RecyclerInterfaceAdds a recycler listener to this recycler.- Specified by:
addListenerin interfaceRecyclerInterface<R extends RecyclableInterface<R,P>,P extends RecyclerRequestInterface>- Parameters:
pRecyclerListener- recycler listener
-
removeListener
public void removeListener(RecyclerListenerInterface pRecyclerListener)
Description copied from interface:RecyclerInterfaceRemioves a recycler listener to this recycler.- Specified by:
removeListenerin interfaceRecyclerInterface<R extends RecyclableInterface<R,P>,P extends RecyclerRequestInterface>- Parameters:
pRecyclerListener- recycler listener
-
ensurePreallocated
public long ensurePreallocated(long pNumberofPrealocatedRecyclablesNeeded, P pRecyclerRequest)Description copied from interface:RecyclerInterfaceEnsures that a given number of recyclable objects are available.- Specified by:
ensurePreallocatedin interfaceRecyclerInterface<R extends RecyclableInterface<R,P>,P extends RecyclerRequestInterface>- Parameters:
pNumberofPrealocatedRecyclablesNeeded- number of preallocated recyclablespRecyclerRequest- request to use for instanciating the recyclable objects- Returns:
- effective number of recyclable objects allocated.
-
getOrWait
public R getOrWait(long pWaitTime, TimeUnit pTimeUnit, P pRecyclerRequest)
Description copied from interface:RecyclerInterfaceWaits for a given amount of time to return a recyclable object given a request.- Specified by:
getOrWaitin interfaceRecyclerInterface<R extends RecyclableInterface<R,P>,P extends RecyclerRequestInterface>- Parameters:
pWaitTime- wait timepTimeUnit- wait time unitpRecyclerRequest- request- Returns:
- null if fails to get a new recyclable object before timeout
-
getOrFail
public R getOrFail(P pRecyclerRequest)
Description copied from interface:RecyclerInterfaceAttempts to get a recyclable object from this recycler for a given request. This call might fail in which case a null reference is returned.- Specified by:
getOrFailin interfaceRecyclerInterface<R extends RecyclableInterface<R,P>,P extends RecyclerRequestInterface>- Parameters:
pRecyclerRequest- request- Returns:
- null if failed to get a recyclable.
-
get
public R get(boolean pWaitForLiveObjectToComeBack, long pWaitTime, TimeUnit pTimeUnit, P pRecyclerRequest)
Description copied from interface:RecyclerInterfaceRequests a recyclable object (all other get methods delegate to this one).- Specified by:
getin interfaceRecyclerInterface<R extends RecyclableInterface<R,P>,P extends RecyclerRequestInterface>- Parameters:
pWaitForLiveObjectToComeBack- as it names suggest, this flag causes this call to wait until an object is available.pWaitTime- wait timepTimeUnit- wait time unitpRecyclerRequest- request- Returns:
- null if fails or timeout
-
release
public void release(R pRecyclable)
Description copied from interface:RecyclerInterfaceReleases the given object- Specified by:
releasein interfaceRecyclerInterface<R extends RecyclableInterface<R,P>,P extends RecyclerRequestInterface>- Parameters:
pRecyclable- recyclable object to release
-
getMaxNumberOfLiveObjects
public int getMaxNumberOfLiveObjects()
Description copied from interface:RecyclerInterfaceReturns the maximum number of live objects allowed.- Specified by:
getMaxNumberOfLiveObjectsin interfaceRecyclerInterface<R extends RecyclableInterface<R,P>,P extends RecyclerRequestInterface>- Returns:
- maximum number of live objects allowed
-
getMaxNumberOfAvailableObjects
public int getMaxNumberOfAvailableObjects()
Description copied from interface:RecyclerInterfaceReturns the maximum number of available objects allowed.- Specified by:
getMaxNumberOfAvailableObjectsin interfaceRecyclerInterface<R extends RecyclableInterface<R,P>,P extends RecyclerRequestInterface>- Returns:
- max number of available objects
-
getNumberOfLiveObjects
public int getNumberOfLiveObjects()
Description copied from interface:RecyclerInterfaceReturns the current number of live objects - these are objects that are curently in use and thus not available for recycling.- Specified by:
getNumberOfLiveObjectsin interfaceRecyclerInterface<R extends RecyclableInterface<R,P>,P extends RecyclerRequestInterface>- Returns:
- number of live objects
-
getNumberOfAvailableObjects
public int getNumberOfAvailableObjects()
Description copied from interface:RecyclerInterfaceReturns the current number of recyclable objects available for recycling.- Specified by:
getNumberOfAvailableObjectsin interfaceRecyclerInterface<R extends RecyclableInterface<R,P>,P extends RecyclerRequestInterface>- Returns:
- number of available recyclable objects
-
getNumberOfFailedRequests
public long getNumberOfFailedRequests()
Description copied from interface:RecyclerInterfaceReturns the number of failed requests.- Specified by:
getNumberOfFailedRequestsin interfaceRecyclerInterface<R extends RecyclableInterface<R,P>,P extends RecyclerRequestInterface>- Returns:
- number of failed requests
-
computeLiveMemorySizeInBytes
public long computeLiveMemorySizeInBytes()
Description copied from interface:RecyclerInterfaceReturns the calculated total size in bytes of live objects.- Specified by:
computeLiveMemorySizeInBytesin interfaceRecyclerInterface<R extends RecyclableInterface<R,P>,P extends RecyclerRequestInterface>- Returns:
- total siee of live objects in bytes
-
computeAvailableMemorySizeInBytes
public long computeAvailableMemorySizeInBytes()
Description copied from interface:RecyclerInterfaceReturns the calculated total size in bytes of available objects.- Specified by:
computeAvailableMemorySizeInBytesin interfaceRecyclerInterface<R extends RecyclableInterface<R,P>,P extends RecyclerRequestInterface>- Returns:
- calculated total size in bytes
-
clearReleased
public void clearReleased()
Description copied from interface:RecyclerInterfaceClears the list of available objects and frees them if the 'autofree' flag is set.- Specified by:
clearReleasedin interfaceRecyclerInterface<R extends RecyclableInterface<R,P>,P extends RecyclerRequestInterface>
-
clearLive
public void clearLive()
Description copied from interface:RecyclerInterfaceClears the list of live objects and frees them if the 'autofree' flag is set- Specified by:
clearLivein interfaceRecyclerInterface<R extends RecyclableInterface<R,P>,P extends RecyclerRequestInterface>
-
free
public void free()
Description copied from interface:FreeableFree this object, which means that the underlying ressources are released.
-
isFree
public boolean isFree()
Description copied from interface:FreeableReturns true if the object has been freed, which usually means that the underlying ressources are not longer available.
-
printDebugInfo
public void printDebugInfo()
BELOW ARE DEBUG METHODS:- Specified by:
printDebugInfoin interfaceRecyclerInterface<R extends RecyclableInterface<R,P>,P extends RecyclerRequestInterface>
-
-