X
- The type of exception that operations on the object can throw.public abstract class PoolableObject<X extends Exception> extends Object
release()
from other methods, e.g. a Closeable
's close
method.
Instances of this class 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.
Modifier | Constructor and Description |
---|---|
protected |
PoolableObject()
Creates a new poolable object.
|
Modifier and Type | Method and Description |
---|---|
protected void |
addReference(Object reference)
Adds a reference to this object.
|
protected boolean |
isEnabled(LogLevel level)
Returns whether or not logging at a specific level is enabled.
|
protected void |
logEvent(LogLevel level,
String message)
Logs a custom event for this object.
|
protected void |
logEvent(LogLevel level,
Supplier<String> messageSupplier)
Logs a custom event for this object.
|
protected void |
logEvent(String message)
Logs a custom event for this object at debug level.
|
protected void |
logEvent(Supplier<String> messageSupplier)
Logs a custom event for this object at debug level.
|
protected void |
release()
Releases this object.
|
protected abstract void |
releaseResources()
Releases any resources associated with this object.
|
protected void |
removeReference(Object reference)
Removes a reference to this object.
|
protected abstract boolean |
validate()
Checks whether or not this object is still valid.
|
protected final void addReference(Object reference)
InputStream
or OutputStream
. These should be added as reference,
and removed when they are no longer needed (e.g. when they are closed).reference
- The non-null
reference to add.NullPointerException
- If the given reference is null
.protected final void removeReference(Object reference) throws X extends Exception
releaseResources()
will be called instead.reference
- The non-null
reference to remove.NullPointerException
- If the given reference is null
.X
- If an exception is thrown when calling releaseResources()
.X extends Exception
addReference(Object)
protected abstract boolean validate()
Pool.acquire()
or Pool.acquireNow()
.
They will also have their resources released.true
if this object is still valid, or false
otherwise.protected abstract void releaseResources() throws X extends Exception
protected void release() throws X extends Exception
releaseResources()
will be called instead.X
- If an exception is thrown when calling releaseResources()
.X extends Exception
protected final void logEvent(String message)
logEvent(Supplier)
instead.
Note: this method will use the logger of the Pool
that manages this object. If this object is not pooled, this method will do nothing.
message
- The event message.protected final void logEvent(LogLevel level, String message)
logEvent(LogLevel, Supplier)
instead.
Note: this method will use the logger of the Pool
that manages this object. If this object is not pooled, this method will do nothing.
level
- The log level to use.message
- The event message.protected final void logEvent(Supplier<String> messageSupplier)
Note: this method will use the logger of the Pool
that manages this object. If this object is not pooled, this method will do nothing.
messageSupplier
- A supplier for the event message.protected final void logEvent(LogLevel level, Supplier<String> messageSupplier)
Note: this method will use the logger of the Pool
that manages this object. If this object is not pooled, this method will do nothing.
level
- The log level to use.messageSupplier
- A supplier for the event message.protected final boolean isEnabled(LogLevel level)
Note: this method will use the logger of the Pool
that manages this object. If this object is not pooled, this method will return
false
.
level
- The level to check.true
if logging at the given level is enabled, or false
otherwise.Copyright © 2022. All rights reserved.