Debian Codegraph @

src/main/java/io/reactivex/rxjava3/disposables/ReferenceDisposable.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.Objects;
17 import java.util.concurrent.atomic.AtomicReference;
18
19 import io.reactivex.rxjava3.annotations.NonNull;
20
21 /**
22 * Base class for Disposable containers that manage some other type that
23 * has to be run when the container is disposed.
24 *
25 * @param <T> the type contained
26 */
27 abstract class ReferenceDisposable<T> extends AtomicReference<T> implements Disposable {
28
29 private static final long serialVersionUID = 6537757548749041217L;
30
31 ReferenceDisposable(T value) {
32 super(Objects.requireNonNull(value, "value is null"));
33 }
34
35 protected abstract void onDisposed(@NonNull T value);
36
37 @Override
38 public final void dispose() {
39 T value = get();
40 if (value != null) {
41 value = getAndSet(null);
42 if (value != null) {
43 onDisposed(value);
44 }
45 }
46 }
47
48 @Override
49 public final boolean isDisposed() {
50 return get() == null;
51 }
52 }