Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/maybe/MaybeUnsubscribeOn.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.operators.maybe;
15
16 import java.util.concurrent.atomic.AtomicReference;
17
18 import io.reactivex.rxjava3.core.*;
19 import io.reactivex.rxjava3.disposables.Disposable;
20 import io.reactivex.rxjava3.internal.disposables.DisposableHelper;
21
22 /**
23 * Makes sure a dispose() call from downstream happens on the specified scheduler.
24 *
25 * @param <T> the value type
26 */
27 public final class MaybeUnsubscribeOn<T> extends AbstractMaybeWithUpstream<T, T> {
28
29 final Scheduler scheduler;
30
31 public MaybeUnsubscribeOn(MaybeSource<T> source, Scheduler scheduler) {
32 super(source);
33 this.scheduler = scheduler;
34 }
35
36 @Override
37 protected void subscribeActual(MaybeObserver<? super T> observer) {
38 source.subscribe(new UnsubscribeOnMaybeObserver<>(observer, scheduler));
39 }
40
41 static final class UnsubscribeOnMaybeObserver<T> extends AtomicReference<Disposable>
42 implements MaybeObserver<T>, Disposable, Runnable {
43
44 private static final long serialVersionUID = 3256698449646456986L;
45
46 final MaybeObserver<? super T> downstream;
47
48 final Scheduler scheduler;
49
50 Disposable ds;
51
52 UnsubscribeOnMaybeObserver(MaybeObserver<? super T> actual, Scheduler scheduler) {
53 this.downstream = actual;
54 this.scheduler = scheduler;
55 }
56
57 @Override
58 public void dispose() {
59 Disposable d = getAndSet(DisposableHelper.DISPOSED);
60 if (d != DisposableHelper.DISPOSED) {
61 this.ds = d;
62 scheduler.scheduleDirect(this);
63 }
64 }
65
66 @Override
67 public void run() {
68 ds.dispose();
69 }
70
71 @Override
72 public boolean isDisposed() {
73 return DisposableHelper.isDisposed(get());
74 }
75
76 @Override
77 public void onSubscribe(Disposable d) {
78 if (DisposableHelper.setOnce(this, d)) {
79 downstream.onSubscribe(this);
80 }
81 }
82
83 @Override
84 public void onSuccess(T value) {
85 downstream.onSuccess(value);
86 }
87
88 @Override
89 public void onError(Throwable e) {
90 downstream.onError(e);
91 }
92
93 @Override
94 public void onComplete() {
95 downstream.onComplete();
96 }
97 }
98 }