Class PoolableObject<X extends Exception>
- java.lang.Object
 - 
- com.github.robtimus.pool.PoolableObject<X>
 
 
- 
- Type Parameters:
 X- The type of exception that operations on the object can throw.
- Direct Known Subclasses:
 ObjectWrapper
public abstract class PoolableObject<X extends Exception> extends Object
An object that can be pooled. Note that the functionality to return the object back to the pool it was acquired from is not part of its public API. This allows implementations to callrelease()from other methods, e.g. aCloseable'sclosemethod.Instances of this class or sub classes are not expected to be thread-safe. Once acquired from a pool, they should usually be used in only one thread until they are released and returned back to the pool.
- Author:
 - Rob Spoor
 
 
- 
- 
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfacePoolableObject.Reference<X extends Exception>A reference to aPoolableObject. 
- 
Constructor Summary
Constructors Modifier Constructor Description protectedPoolableObject()Creates a new poolable object. 
- 
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected PoolableObject.Reference<X>addReference()Adds a reference to this object.protected booleanisEnabled(LogLevel level)Returns whether or not logging at a specific level is enabled.protected voidlogEvent(LogLevel level, String message)Logs a custom event for this object.protected voidlogEvent(LogLevel level, Supplier<String> messageSupplier)Logs a custom event for this object.protected voidlogEvent(String message)Logs a custom event for this object at debug level.protected voidlogEvent(Supplier<String> messageSupplier)Logs a custom event for this object at debug level.protected voidrelease()Releases this object.protected abstract voidreleaseResources()Releases any resources associated with this object.protected abstract booleanvalidate()Checks whether or not this object is still valid. 
 - 
 
- 
- 
Method Detail
- 
addReference
protected final PoolableObject.Reference<X> addReference()
Adds a reference to this object. An object will only be returned to the pool it was acquired from if all references to the object are removed. This allows objects to return other (closeable) objects likeInputStreamorOutputStream. For such objects, a reference should be added using this method, and the returned value should be removed or closed when the objects are no longer needed (e.g. when they are closed).- Returns:
 - An object representing the added reference.
 - Since:
 - 2.0
 
 
- 
validate
protected abstract boolean validate()
Checks whether or not this object is still valid. Invalid object will be removed from the pool instead of being returned fromPool.acquire()orPool.acquireNow(). They will also have their resources released.- Returns:
 trueif this object is still valid, orfalseotherwise.
 
- 
releaseResources
protected abstract void releaseResources() throws X extends ExceptionReleases any resources associated with this object. 
- 
release
protected void release() throws X extends ExceptionReleases this object. If no more references remain, this object will be returned to the pool it was acquired from. If this object is not associated with a pool,releaseResources()will be called instead.- Throws:
 X- If an exception is thrown when callingreleaseResources().X extends Exception
 
- 
logEvent
protected final void logEvent(String message)
Logs a custom event for this object at debug level. The message should preferably be a compile-time constant; for calculated messages, uselogEvent(Supplier)instead.Note: this method will use the logger of the
Poolthat manages this object. If this object is not pooled, this method will do nothing.- Parameters:
 message- The event message.
 
- 
logEvent
protected final void logEvent(LogLevel level, String message)
Logs a custom event for this object. The message should preferably be a compile-time constant; for calculated messages, uselogEvent(LogLevel, Supplier)instead.Note: this method will use the logger of the
Poolthat manages this object. If this object is not pooled, this method will do nothing.- Parameters:
 level- The log level to use.message- The event message.
 
- 
logEvent
protected final void logEvent(Supplier<String> messageSupplier)
Logs a custom event for this object at debug level.Note: this method will use the logger of the
Poolthat manages this object. If this object is not pooled, this method will do nothing.- Parameters:
 messageSupplier- A supplier for the event message.
 
- 
logEvent
protected final void logEvent(LogLevel level, Supplier<String> messageSupplier)
Logs a custom event for this object.Note: this method will use the logger of the
Poolthat manages this object. If this object is not pooled, this method will do nothing.- Parameters:
 level- The log level to use.messageSupplier- A supplier for the event message.
 
- 
isEnabled
protected final boolean isEnabled(LogLevel level)
Returns whether or not logging at a specific level is enabled. This can be used to perform conditional configuration, like adding logging listeners conditionally.Note: this method will use the logger of the
Poolthat manages this object. If this object is not pooled, this method will returnfalse.- Parameters:
 level- The level to check.- Returns:
 trueif logging at the given level is enabled, orfalseotherwise.
 
 - 
 
 -