Package io.permazen.util
Class CloseableRefs<C extends Closeable>
java.lang.Object
io.permazen.util.CloseableRefs<C>
- Type Parameters:
C
- target object type
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected void
finalize()
Check that the reference count is zero before finalization.protected Logger
Get theLogger
to use for logging errors.Get the underlying target instance, which must not yet be closed.protected void
Handle an exception being thrown byCloseable.close()
when closing the target.void
ref()
Increment reference count.int
refs()
Get current reference count.void
unref()
Decrement reference count.
-
Field Details
-
target
-
-
Constructor Details
-
CloseableRefs
Constructor.Initially the reference count is set to one.
- Parameters:
target
- target instance
-
-
Method Details
-
getTarget
Get the underlying target instance, which must not yet be closed.The returned value should not be closed; that will happen automatically when the reference count goes to zero.
- Returns:
- the underlying
Closeable
- Throws:
IllegalStateException
- if reference count is already zeroed
-
ref
public void ref()Increment reference count.- Throws:
IllegalStateException
- if reference count is already zeroed
-
unref
public void unref()Decrement reference count.- Throws:
IllegalStateException
- if reference count is already zeroed
-
refs
public int refs()Get current reference count.- Returns:
- the current number of references to this instance
-
handleCloseException
Handle an exception being thrown byCloseable.close()
when closing the target.The implementation in
CloseableRefs
just logs the error.- Parameters:
e
- exception thrown when closing the target object
-
getLogger
Get theLogger
to use for logging errors.The implementation returns the logger associated with
this.target.getClass()
. -
finalize
Check that the reference count is zero before finalization.If not, that means there was a leak and so the underlying
Closeable
is closed and an error is logged.
-