public class SingleCloseable extends java.lang.Object implements java.io.Closeable, Cancelable
This class might help in situations where decoupling closeables allows the referenced objects to be collected earlier by a GC.
The implementation is thread-safe.
| Modifier and Type | Field and Description |
|---|---|
protected java.util.concurrent.atomic.AtomicReference<java.io.Closeable> |
current
The reference holder.
|
protected static java.io.Closeable |
SENTINEL
The empty sentinel to know when we have
closed the previous instance and keep the
invariants of this SingleCloseable.
|
| Constructor and Description |
|---|
SingleCloseable() |
| Modifier and Type | Method and Description |
|---|---|
void |
close() |
void |
closeSilently()
Closes this container silently.
|
java.io.Closeable |
get() |
boolean |
isClosed() |
void |
set(java.io.Closeable c)
Sets the managed closeable if not already set.
|
@Nonnull protected final java.util.concurrent.atomic.AtomicReference<java.io.Closeable> current
@Nonnull protected static final java.io.Closeable SENTINEL
public void close()
throws java.io.IOException
close in interface java.io.Closeableclose in interface java.lang.AutoCloseablejava.io.IOExceptionpublic void set(@Nonnull
java.io.Closeable c)
c - the closeable to handle@Nullable public java.io.Closeable get()
public boolean isClosed()
isClosed in interface Cancelablepublic void closeSilently()