Class CompositeDisposable
java.lang.Object
io.reactivex.rxjava4.disposables.CompositeDisposable
- All Implemented Interfaces:
Disposable, DisposableContainer, DisposableStreamerCancellation, StreamerCancellation
public final class CompositeDisposable
extends Object
implements Disposable, DisposableContainer, DisposableStreamerCancellation
A disposable container that can hold onto multiple other
Disposables and
offers O(1) time complexity for add(Disposable), remove(Disposable) and delete(Disposable)
operations.-
Constructor Summary
ConstructorsConstructorDescriptionCreates an emptyCompositeDisposable.CompositeDisposable(@NonNull Disposable... disposables) Creates aCompositeDisposablewith the given array of initialDisposableelements.CompositeDisposable(@NonNull Iterable<? extends Disposable> disposables) -
Method Summary
Modifier and TypeMethodDescriptionbooleanadd(@NonNull Disposable disposable) Adds aDisposableto this container or disposes it if the container has been disposed.booleanaddAll(@NonNull Disposable... disposables) Atomically adds the given array ofDisposables to the container or disposes them all if the container has been disposed.voidclear()Atomically clears the container, then disposes all the previously containedDisposables.booleandelete(@NonNull Disposable disposable) Removes (but does not dispose) the givenDisposableif it is part of this container.derive()Create a derived sub-cancellation management interface that can get disposed by this cancellation management object but disposing the sub-cancellation manager does not dispose this currentStreamerCancellationinstance..voiddispose()Dispose the resource, the operation should be idempotent.booleanReturns true if this resource has been disposed.booleanremove(@NonNull Disposable disposable) Removes and disposes the givenDisposableif it is part of this container.voidreset()Removes all containedDisposables without disposing them, making this container fresh.intsize()Returns the number of currently heldDisposables.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface Disposable
asAutoCloseableModifier and TypeMethodDescriptiondefault @NonNull AutoCloseableWraps thisDisposableinto anAutoCloseableinstance that can be used with try-with-resources constructs.
-
Constructor Details
-
CompositeDisposable
public CompositeDisposable()Creates an emptyCompositeDisposable. -
CompositeDisposable
Creates aCompositeDisposablewith the given array of initialDisposableelements.- Parameters:
disposables- the array ofDisposables to start with- Throws:
NullPointerException- ifdisposablesor any of its array items isnull
-
CompositeDisposable
- Parameters:
disposables- theIterablesequence ofDisposableto start with- Throws:
NullPointerException- ifdisposablesor any of its items isnull
-
-
Method Details
-
dispose
public void dispose()Description copied from interface:DisposableDispose the resource, the operation should be idempotent.- Specified by:
disposein interfaceDisposable
-
isDisposed
public boolean isDisposed()Description copied from interface:DisposableReturns true if this resource has been disposed.- Specified by:
isDisposedin interfaceDisposable- Specified by:
isDisposedin interfaceStreamerCancellation- Returns:
- true if this resource has been disposed
-
add
Adds aDisposableto this container or disposes it if the container has been disposed.- Specified by:
addin interfaceDisposableContainer- Specified by:
addin interfaceStreamerCancellation- Parameters:
disposable- theDisposableto add, notnull- Returns:
trueif successful,falseif this container has been disposed- Throws:
NullPointerException- ifdisposableisnull
-
addAll
Atomically adds the given array ofDisposables to the container or disposes them all if the container has been disposed.- Parameters:
disposables- the array ofDisposables- Returns:
trueif the operation was successful,falseif the container has been disposed- Throws:
NullPointerException- ifdisposablesor any of its array items isnull
-
remove
Removes and disposes the givenDisposableif it is part of this container.- Specified by:
removein interfaceDisposableContainer- Specified by:
removein interfaceStreamerCancellation- Parameters:
disposable- the disposable to remove and dispose, notnull- Returns:
trueif the operation was successful- Throws:
NullPointerException- ifdisposableisnull
-
delete
Removes (but does not dispose) the givenDisposableif it is part of this container.- Specified by:
deletein interfaceDisposableContainer- Specified by:
deletein interfaceStreamerCancellation- Parameters:
disposable- the disposable to remove, notnull- Returns:
trueif the operation was successful- Throws:
NullPointerException- ifdisposableisnull
-
clear
public void clear()Atomically clears the container, then disposes all the previously containedDisposables.- Specified by:
clearin interfaceDisposableContainer
-
size
public int size()Returns the number of currently heldDisposables.- Returns:
- the number of currently held
Disposables
-
reset
public void reset()Description copied from interface:DisposableContainerRemoves all containedDisposables without disposing them, making this container fresh.- Specified by:
resetin interfaceDisposableContainer
-
derive
Description copied from interface:StreamerCancellationCreate a derived sub-cancellation management interface that can get disposed by this cancellation management object but disposing the sub-cancellation manager does not dispose this currentStreamerCancellationinstance..- Specified by:
derivein interfaceStreamerCancellation- Returns:
- the derived sub-cancellation management object
-