Class CompositeDisposable
java.lang.Object
io.reactivex.rxjava4.disposables.CompositeDisposable
- All Implemented Interfaces:
Disposable, DisposableContainer, AutoCloseable
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.-
Nested Class Summary
Nested classes/interfaces inherited from interface DisposableContainer
DisposableContainer.NeverDisposableContainerModifier and TypeInterfaceDescriptionstatic final recordImplementation of a never disposable container. -
Field Summary
Fields inherited from interface DisposableContainer
NEVERModifier and TypeFieldDescriptionstatic final DisposableContainerThe container implementation that just ignores everything, for cases where the dispose signal has no side-effects to work with. -
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.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
closeModifier and TypeMethodDescriptiondefault voidclose()Dispose the resource, the operation should be idempotent.Methods inherited from interface DisposableContainer
register, subscribeModifier and TypeMethodDescriptiondefault DisposableRegisters aDisposablewith this container so that it can be removed and disposed via a simpleDisposable.dispose()call to the returned Disposable.default DisposableRegisters aDisposablewith this container so that it can be deleted, not disposed via a simpleDisposable.dispose()call to the returned Disposable.
-
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- 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- 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- 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- 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
-