T - the resource typepublic final class SerialResource<T> extends java.util.concurrent.atomic.AtomicReference<java.lang.Object> implements Disposable
This resource container disposable helps in avoiding the wrapping of other resources into Disposables.
Note that since the implementation leaks the methods of AtomicReference, one must be careful to only call setResource and dispose on it. All other methods may lead to undefined behavior and should be used by internal means only.
| Constructor and Description |
|---|
SerialResource(Consumer<? super T> disposer)
Constructor with a custom disposer callback.
|
SerialResource(Consumer<? super T> disposer,
T initialResource)
Constructor with a custom disposer callback and the initial resource
|
| Modifier and Type | Method and Description |
|---|---|
void |
dispose()
Dispose the resource, the operation should be idempotent.
|
T |
getResource()
Returns the current held resource or null if no resource
is set or the container has been disposed.
|
boolean |
isDisposed()
Returns true if this resource has been disposed.
|
boolean |
setResource(T newResource)
Atomically replaces the current resource with the new resource but doesn't call the disposer
for it.
|
public SerialResource(Consumer<? super T> disposer)
disposer - public boolean setResource(T newResource)
newResource - the new resource to replace the old onepublic void dispose()
Disposabledispose in interface Disposablepublic T getResource()
public boolean isDisposed()