public static interface Disposable.Composite extends Disposable
Disposablethat 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
|Modifier and Type||Method and Description|
Adds the given collection of Disposables to the container or disposes them all if the container has been disposed.
Atomically mark the container as
Indicates if the container has already been disposed.
Returns the number of currently held Disposables.
boolean add(Disposable d)
Disposableto this container, if it is not
disposed. Otherwise d is disposed immediately.
default boolean addAll(Collection<? extends Disposable> ds)
ds- the collection of Disposables
disposed, clear it and then dispose all the previously contained Disposables. From there on the container cannot be reused, as
addAll(Collection)methods will immediately return false.
Note that if that is the case, attempts to add new disposable to it via
addAll(Collection) will be rejected.
boolean remove(Disposable d)
Disposablefrom 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 (probably guarded by a check that this method returned true, meaning the
disposable was actually in the container).