Debian Codegraph @

src/main/java/io/reactivex/rxjava3/disposables/SerialDisposable.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.disposables;
15
16 import java.util.concurrent.atomic.AtomicReference;
17
18 import io.reactivex.rxjava3.annotations.Nullable;
19 import io.reactivex.rxjava3.internal.disposables.DisposableHelper;
20
21 /**
22 * A Disposable container that allows atomically updating/replacing the contained
23 * Disposable with another Disposable, disposing the old one when updating plus
24 * handling the disposition when the container itself is disposed.
25 */
26 public final class SerialDisposable implements Disposable {
27 final AtomicReference<Disposable> resource;
28
29 /**
30 * Constructs an empty SerialDisposable.
31 */
32 public SerialDisposable() {
33 this.resource = new AtomicReference<>();
34 }
35
36 /**
37 * Constructs a SerialDisposable with the given initial Disposable instance.
38 * @param initialDisposable the initial Disposable instance to use, null allowed
39 */
40 public SerialDisposable(@Nullable Disposable initialDisposable) {
41 this.resource = new AtomicReference<>(initialDisposable);
42 }
43
44 /**
45 * Atomically: set the next disposable on this container and dispose the previous
46 * one (if any) or dispose next if the container has been disposed.
47 * @param next the Disposable to set, may be null
48 * @return true if the operation succeeded, false if the container has been disposed
49 * @see #replace(Disposable)
50 */
51 public boolean set(@Nullable Disposable next) {
52 return DisposableHelper.set(resource, next);
53 }
54
55 /**
56 * Atomically: set the next disposable on this container but don't dispose the previous
57 * one (if any) or dispose next if the container has been disposed.
58 * @param next the Disposable to set, may be null
59 * @return true if the operation succeeded, false if the container has been disposed
60 * @see #set(Disposable)
61 */
62 public boolean replace(@Nullable Disposable next) {
63 return DisposableHelper.replace(resource, next);
64 }
65
66 /**
67 * Returns the currently contained Disposable or null if this container is empty.
68 * @return the current Disposable, may be null
69 */
70 @Nullable
71 public Disposable get() {
72 Disposable d = resource.get();
73 if (d == DisposableHelper.DISPOSED) {
74 return Disposable.disposed();
75 }
76 return d;
77 }
78
79 @Override
80 public void dispose() {
81 DisposableHelper.dispose(resource);
82 }
83
84 @Override
85 public boolean isDisposed() {
86 return DisposableHelper.isDisposed(resource.get());
87 }
88 }