public static interface Disposable.Composite extends Disposable
Disposable
that is itself Disposable
. Accumulate
disposables and dispose them all in one go by using dispose()
. Using
the add(Disposable)
methods give ownership to the container, which is now
responsible for disposing them. You can however retake ownership of individual
elements by keeping a reference and using remove(Disposable)
, which puts
the responsibility of disposing said elements back in your hands. Note that once
disposed, the container cannot be reused and you will need a new Disposable.Composite
.Disposable.Composite, Disposable.Swap
Modifier and Type | Method and Description |
---|---|
boolean |
add(Disposable d)
Add a
Disposable to this container, if it is not disposed . |
default boolean |
addAll(Collection<? extends Disposable> ds)
Adds the given collection of Disposables to the container or disposes them
all if the container has been disposed.
|
void |
dispose()
Atomically mark the container as
disposed , clear it and then
dispose all the previously contained Disposables. |
boolean |
isDisposed()
Indicates if the container has already been disposed.
|
boolean |
remove(Disposable d)
Delete the
Disposable from this container, without disposing it. |
int |
size()
Returns the number of currently held Disposables.
|
boolean add(Disposable d)
Disposable
to this container, if it is not disposed
.
Otherwise d is disposed immediately.d
- the Disposable
to add.default boolean addAll(Collection<? extends Disposable> ds)
ds
- the collection of DisposablesDisposables.composite()
variants.void dispose()
disposed
, clear it and then
dispose all the previously contained Disposables. From there on the container cannot
be reused, as add(Disposable)
and addAll(Collection)
methods
will immediately return false.dispose
in interface Disposable
boolean isDisposed()
Note that if that is the case, attempts to add new disposable to it via
add(Disposable)
and addAll(Collection)
will be rejected.
isDisposed
in interface Disposable
boolean remove(Disposable d)
Disposable
from this container, without disposing it.
It becomes the responsibility of the caller to dispose the value themselves,
which they can do by a simple call to Disposable.dispose()
on said
value (probaby guarded by a check that this method returned true, meaning the
disposable was actually in the container).
d
- the Disposable
to remove.int size()