Package io.reactivex.rxjava3.disposables
Class CompositeDisposable
- java.lang.Object
-
- io.reactivex.rxjava3.disposables.CompositeDisposable
-
- All Implemented Interfaces:
Disposable,DisposableContainer
public final class CompositeDisposable extends java.lang.Object implements Disposable, DisposableContainer
A disposable container that can hold onto multiple otherDisposables and offers O(1) time complexity foradd(Disposable),remove(Disposable)anddelete(Disposable)operations.
-
-
Field Summary
Fields Modifier and Type Field Description (package private) booleandisposed(package private) OpenHashSet<Disposable>resources
-
Constructor Summary
Constructors Constructor Description CompositeDisposable()Creates an emptyCompositeDisposable.CompositeDisposable(@NonNull Disposable... disposables)Creates aCompositeDisposablewith the given array of initialDisposableelements.CompositeDisposable(@NonNull java.lang.Iterable<? extends Disposable> disposables)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanadd(@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.(package private) voiddispose(@Nullable OpenHashSet<Disposable> set)Dispose the contents of theOpenHashSetby suppressing non-fatalThrowables till the end.booleanisDisposed()Returns true if this resource has been disposed.booleanremove(@NonNull Disposable disposable)Removes and disposes the givenDisposableif it is part of this container.intsize()Returns the number of currently heldDisposables.
-
-
-
Field Detail
-
resources
OpenHashSet<Disposable> resources
-
disposed
volatile boolean disposed
-
-
Constructor Detail
-
CompositeDisposable
public CompositeDisposable()
Creates an emptyCompositeDisposable.
-
CompositeDisposable
public CompositeDisposable(@NonNull @NonNull Disposable... disposables)
Creates aCompositeDisposablewith the given array of initialDisposableelements.- Parameters:
disposables- the array ofDisposables to start with- Throws:
java.lang.NullPointerException- ifdisposablesor any of its array items isnull
-
CompositeDisposable
public CompositeDisposable(@NonNull @NonNull java.lang.Iterable<? extends Disposable> disposables)
- Parameters:
disposables- theIterablesequence ofDisposableto start with- Throws:
java.lang.NullPointerException- ifdisposablesor any of its items isnull
-
-
Method Detail
-
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
public boolean add(@NonNull @NonNull Disposable disposable)
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:
java.lang.NullPointerException- ifdisposableisnull
-
addAll
public boolean addAll(@NonNull @NonNull Disposable... disposables)
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:
java.lang.NullPointerException- ifdisposablesor any of its array items isnull
-
remove
public boolean remove(@NonNull @NonNull Disposable disposable)
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:
java.lang.NullPointerException- ifdisposableisnull
-
delete
public boolean delete(@NonNull @NonNull Disposable disposable)
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:
java.lang.NullPointerException- ifdisposableisnull
-
clear
public void clear()
Atomically clears the container, then disposes all the previously containedDisposables.
-
size
public int size()
Returns the number of currently heldDisposables.- Returns:
- the number of currently held
Disposables
-
dispose
void dispose(@Nullable @Nullable OpenHashSet<Disposable> set)
Dispose the contents of theOpenHashSetby suppressing non-fatalThrowables till the end.- Parameters:
set- theOpenHashSetto dispose elements of
-
-