Debian Codegraph @

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