| 1 |
/* |
| 2 |
* Copyright (c) 2016-present, RxJava Contributors. |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 |
* compliance with the License. You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 |
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See |
| 11 |
* the License for the specific language governing permissions and limitations under the License. |
| 12 |
*/ |
| 13 |
|
| 14 |
package io.reactivex.rxjava3.disposables; |
| 15 |
|
| 16 |
import java.util.*; |
| 17 |
|
| 18 |
import io.reactivex.rxjava3.annotations.*; |
| 19 |
import io.reactivex.rxjava3.exceptions.*; |
| 20 |
import io.reactivex.rxjava3.internal.util.*; |
| 21 |
|
| 22 |
/** |
| 23 |
* A disposable container that can hold onto multiple other {@link Disposable}s and |
| 24 |
* offers <em>O(1)</em> time complexity for {@link #add(Disposable)}, {@link #remove(Disposable)} and {@link #delete(Disposable)} |
| 25 |
* operations. |
| 26 |
*/ |
| 27 |
public final class CompositeDisposable implements Disposable, DisposableContainer { |
| 28 |
|
| 29 |
OpenHashSet<Disposable> resources; |
| 30 |
|
| 31 |
volatile boolean disposed; |
| 32 |
|
| 33 |
/** |
| 34 |
* Creates an empty {@code CompositeDisposable}. |
| 35 |
*/ |
| 36 |
public CompositeDisposable() { |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Creates a {@code CompositeDisposable} with the given array of initial {@link Disposable} elements. |
| 41 |
* @param disposables the array of {@code Disposable}s to start with |
| 42 |
* @throws NullPointerException if {@code disposables} or any of its array items is {@code null} |
| 43 |
*/ |
| 44 |
public CompositeDisposable(@NonNull Disposable... disposables) { |
| 45 |
Objects.requireNonNull(disposables, "disposables is null"); |
| 46 |
this.resources = new OpenHashSet<>(disposables.length + 1); |
| 47 |
for (Disposable d : disposables) { |
| 48 |
Objects.requireNonNull(d, "A Disposable in the disposables array is null"); |
| 49 |
this.resources.add(d); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Creates a {@code CompositeDisposable} with the given {@link Iterable} sequence of initial {@link Disposable} elements. |
| 55 |
* @param disposables the {@code Iterable} sequence of {@code Disposable} to start with |
| 56 |
* @throws NullPointerException if {@code disposables} or any of its items is {@code null} |
| 57 |
*/ |
| 58 |
public CompositeDisposable(@NonNull Iterable<? extends Disposable> disposables) { |
| 59 |
Objects.requireNonNull(disposables, "disposables is null"); |
| 60 |
this.resources = new OpenHashSet<>(); |
| 61 |
for (Disposable d : disposables) { |
| 62 |
Objects.requireNonNull(d, "A Disposable item in the disposables sequence is null"); |
| 63 |
this.resources.add(d); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
@Override |
| 68 |
public void dispose() { |
| 69 |
if (disposed) { |
| 70 |
return; |
| 71 |
} |
| 72 |
OpenHashSet<Disposable> set; |
| 73 |
synchronized (this) { |
| 74 |
if (disposed) { |
| 75 |
return; |
| 76 |
} |
| 77 |
disposed = true; |
| 78 |
set = resources; |
| 79 |
resources = null; |
| 80 |
} |
| 81 |
|
| 82 |
dispose(set); |
| 83 |
} |
| 84 |
|
| 85 |
@Override |
| 86 |
public boolean isDisposed() { |
| 87 |
return disposed; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Adds a {@link Disposable} to this container or disposes it if the |
| 92 |
* container has been disposed. |
| 93 |
* @param disposable the {@code Disposable} to add, not {@code null} |
| 94 |
* @return {@code true} if successful, {@code false} if this container has been disposed |
| 95 |
* @throws NullPointerException if {@code disposable} is {@code null} |
| 96 |
*/ |
| 97 |
@Override |
| 98 |
public boolean add(@NonNull Disposable disposable) { |
| 99 |
Objects.requireNonNull(disposable, "disposable is null"); |
| 100 |
if (!disposed) { |
| 101 |
synchronized (this) { |
| 102 |
if (!disposed) { |
| 103 |
OpenHashSet<Disposable> set = resources; |
| 104 |
if (set == null) { |
| 105 |
set = new OpenHashSet<>(); |
| 106 |
resources = set; |
| 107 |
} |
| 108 |
set.add(disposable); |
| 109 |
return true; |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
disposable.dispose(); |
| 114 |
return false; |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Atomically adds the given array of {@link Disposable}s to the container or |
| 119 |
* disposes them all if the container has been disposed. |
| 120 |
* @param disposables the array of {@code Disposable}s |
| 121 |
* @return {@code true} if the operation was successful, {@code false} if the container has been disposed |
| 122 |
* @throws NullPointerException if {@code disposables} or any of its array items is {@code null} |
| 123 |
*/ |
| 124 |
public boolean addAll(@NonNull Disposable... disposables) { |
| 125 |
Objects.requireNonNull(disposables, "disposables is null"); |
| 126 |
if (!disposed) { |
| 127 |
synchronized (this) { |
| 128 |
if (!disposed) { |
| 129 |
OpenHashSet<Disposable> set = resources; |
| 130 |
if (set == null) { |
| 131 |
set = new OpenHashSet<>(disposables.length + 1); |
| 132 |
resources = set; |
| 133 |
} |
| 134 |
for (Disposable d : disposables) { |
| 135 |
Objects.requireNonNull(d, "A Disposable in the disposables array is null"); |
| 136 |
set.add(d); |
| 137 |
} |
| 138 |
return true; |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
for (Disposable d : disposables) { |
| 143 |
d.dispose(); |
| 144 |
} |
| 145 |
return false; |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Removes and disposes the given {@link Disposable} if it is part of this |
| 150 |
* container. |
| 151 |
* @param disposable the disposable to remove and dispose, not {@code null} |
| 152 |
* @return {@code true} if the operation was successful |
| 153 |
* @throws NullPointerException if {@code disposable} is {@code null} |
| 154 |
*/ |
| 155 |
@Override |
| 156 |
public boolean remove(@NonNull Disposable disposable) { |
| 157 |
if (delete(disposable)) { |
| 158 |
disposable.dispose(); |
| 159 |
return true; |
| 160 |
} |
| 161 |
return false; |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Removes (but does not dispose) the given {@link Disposable} if it is part of this |
| 166 |
* container. |
| 167 |
* @param disposable the disposable to remove, not {@code null} |
| 168 |
* @return {@code true} if the operation was successful |
| 169 |
* @throws NullPointerException if {@code disposable} is {@code null} |
| 170 |
*/ |
| 171 |
@Override |
| 172 |
public boolean delete(@NonNull Disposable disposable) { |
| 173 |
Objects.requireNonNull(disposable, "disposable is null"); |
| 174 |
if (disposed) { |
| 175 |
return false; |
| 176 |
} |
| 177 |
synchronized (this) { |
| 178 |
if (disposed) { |
| 179 |
return false; |
| 180 |
} |
| 181 |
|
| 182 |
OpenHashSet<Disposable> set = resources; |
| 183 |
if (set == null || !set.remove(disposable)) { |
| 184 |
return false; |
| 185 |
} |
| 186 |
} |
| 187 |
return true; |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* Atomically clears the container, then disposes all the previously contained {@link Disposable}s. |
| 192 |
*/ |
| 193 |
public void clear() { |
| 194 |
if (disposed) { |
| 195 |
return; |
| 196 |
} |
| 197 |
OpenHashSet<Disposable> set; |
| 198 |
synchronized (this) { |
| 199 |
if (disposed) { |
| 200 |
return; |
| 201 |
} |
| 202 |
|
| 203 |
set = resources; |
| 204 |
resources = null; |
| 205 |
} |
| 206 |
|
| 207 |
dispose(set); |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Returns the number of currently held {@link Disposable}s. |
| 212 |
* @return the number of currently held {@code Disposable}s |
| 213 |
*/ |
| 214 |
public int size() { |
| 215 |
if (disposed) { |
| 216 |
return 0; |
| 217 |
} |
| 218 |
synchronized (this) { |
| 219 |
if (disposed) { |
| 220 |
return 0; |
| 221 |
} |
| 222 |
OpenHashSet<Disposable> set = resources; |
| 223 |
return set != null ? set.size() : 0; |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
/** |
| 228 |
* Dispose the contents of the {@link OpenHashSet} by suppressing non-fatal |
| 229 |
* {@link Throwable}s till the end. |
| 230 |
* @param set the {@code OpenHashSet} to dispose elements of |
| 231 |
*/ |
| 232 |
void dispose(@Nullable OpenHashSet<Disposable> set) { |
| 233 |
if (set == null) { |
| 234 |
return; |
| 235 |
} |
| 236 |
List<Throwable> errors = null; |
| 237 |
Object[] array = set.keys(); |
| 238 |
for (Object o : array) { |
| 239 |
if (o instanceof Disposable) { |
| 240 |
try { |
| 241 |
((Disposable) o).dispose(); |
| 242 |
} catch (Throwable ex) { |
| 243 |
Exceptions.throwIfFatal(ex); |
| 244 |
if (errors == null) { |
| 245 |
errors = new ArrayList<>(); |
| 246 |
} |
| 247 |
errors.add(ex); |
| 248 |
} |
| 249 |
} |
| 250 |
} |
| 251 |
if (errors != null) { |
| 252 |
if (errors.size() == 1) { |
| 253 |
throw ExceptionHelper.wrapOrThrow(errors.get(0)); |
| 254 |
} |
| 255 |
throw new CompositeException(errors); |
| 256 |
} |
| 257 |
} |
| 258 |
} |