Class CompositeDisposable

java.lang.Object
io.reactivex.rxjava3.disposables.CompositeDisposable
All Implemented Interfaces:
Disposable, DisposableContainer

public final class CompositeDisposable extends Object implements Disposable, DisposableContainer
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.
  • Field Details

  • Constructor Details

    • CompositeDisposable

      public CompositeDisposable()
      Creates an empty CompositeDisposable.
    • CompositeDisposable

      public CompositeDisposable(@NonNull @NonNull Disposable... disposables)
      Creates a CompositeDisposable with the given array of initial Disposable elements.
      Parameters:
      disposables - the array of Disposables to start with
      Throws:
      NullPointerException - if disposables or any of its array items is null
    • CompositeDisposable

      public CompositeDisposable(@NonNull @NonNull Iterable<? extends Disposable> disposables)
      Creates a CompositeDisposable with the given Iterable sequence of initial Disposable elements.
      Parameters:
      disposables - the Iterable sequence of Disposable to start with
      Throws:
      NullPointerException - if disposables or any of its items is null
  • Method Details

    • dispose

      public void dispose()
      Description copied from interface: Disposable
      Dispose the resource, the operation should be idempotent.
      Specified by:
      dispose in interface Disposable
    • isDisposed

      public boolean isDisposed()
      Description copied from interface: Disposable
      Returns true if this resource has been disposed.
      Specified by:
      isDisposed in interface Disposable
      Returns:
      true if this resource has been disposed
    • add

      public boolean add(@NonNull @NonNull Disposable disposable)
      Adds a Disposable to this container or disposes it if the container has been disposed.
      Specified by:
      add in interface DisposableContainer
      Parameters:
      disposable - the Disposable to add, not null
      Returns:
      true if successful, false if this container has been disposed
      Throws:
      NullPointerException - if disposable is null
    • addAll

      public boolean addAll(@NonNull @NonNull Disposable... disposables)
      Atomically adds the given array of Disposables to the container or disposes them all if the container has been disposed.
      Parameters:
      disposables - the array of Disposables
      Returns:
      true if the operation was successful, false if the container has been disposed
      Throws:
      NullPointerException - if disposables or any of its array items is null
    • remove

      public boolean remove(@NonNull @NonNull Disposable disposable)
      Removes and disposes the given Disposable if it is part of this container.
      Specified by:
      remove in interface DisposableContainer
      Parameters:
      disposable - the disposable to remove and dispose, not null
      Returns:
      true if the operation was successful
      Throws:
      NullPointerException - if disposable is null
    • delete

      public boolean delete(@NonNull @NonNull Disposable disposable)
      Removes (but does not dispose) the given Disposable if it is part of this container.
      Specified by:
      delete in interface DisposableContainer
      Parameters:
      disposable - the disposable to remove, not null
      Returns:
      true if the operation was successful
      Throws:
      NullPointerException - if disposable is null
    • clear

      public void clear()
      Atomically clears the container, then disposes all the previously contained Disposables.
    • size

      public int size()
      Returns the number of currently held Disposables.
      Returns:
      the number of currently held Disposables
    • dispose

      Dispose the contents of the OpenHashSet by suppressing non-fatal Throwables till the end.
      Parameters:
      set - the OpenHashSet to dispose elements of