Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/disposables/ArrayCompositeDisposable.java

raw
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.internal.disposables;
15
16 import java.util.concurrent.atomic.AtomicReferenceArray;
17
18 import io.reactivex.rxjava3.disposables.Disposable;
19
20 /**
21 * A composite disposable with a fixed number of slots.
22 *
23 * <p>Note that since the implementation leaks the methods of AtomicReferenceArray, one must be
24 * careful to only call setResource, replaceResource and dispose on it. All other methods may lead to undefined behavior
25 * and should be used by internal means only.
26 */
27 public final class ArrayCompositeDisposable extends AtomicReferenceArray<Disposable> implements Disposable {
28
29 private static final long serialVersionUID = 2746389416410565408L;
30
31 public ArrayCompositeDisposable(int capacity) {
32 super(capacity);
33 }
34
35 /**
36 * Sets the resource at the specified index and disposes the old resource.
37 * @param index the index of the resource to set
38 * @param resource the new resource
39 * @return true if the resource has ben set, false if the composite has been disposed
40 */
41 public boolean setResource(int index, Disposable resource) {
42 for (;;) {
43 Disposable o = get(index);
44 if (o == DisposableHelper.DISPOSED) {
45 resource.dispose();
46 return false;
47 }
48 if (compareAndSet(index, o, resource)) {
49 if (o != null) {
50 o.dispose();
51 }
52 return true;
53 }
54 }
55 }
56
57 /**
58 * Replaces the resource at the specified index and returns the old resource.
59 * @param index the index of the resource to replace
60 * @param resource the new resource
61 * @return the old resource, can be null
62 */
63 public Disposable replaceResource(int index, Disposable resource) {
64 for (;;) {
65 Disposable o = get(index);
66 if (o == DisposableHelper.DISPOSED) {
67 resource.dispose();
68 return null;
69 }
70 if (compareAndSet(index, o, resource)) {
71 return o;
72 }
73 }
74 }
75
76 @Override
77 public void dispose() {
78 if (get(0) != DisposableHelper.DISPOSED) {
79 int s = length();
80 for (int i = 0; i < s; i++) {
81 Disposable o = get(i);
82 if (o != DisposableHelper.DISPOSED) {
83 o = getAndSet(i, DisposableHelper.DISPOSED);
84 if (o != DisposableHelper.DISPOSED && o != null) {
85 o.dispose();
86 }
87 }
88 }
89 }
90 }
91
92 @Override
93 public boolean isDisposed() {
94 return get(0) == DisposableHelper.DISPOSED;
95 }
96 }