Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/single/SingleToFlowable.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.single;
15
16 import org.reactivestreams.Subscriber;
17
18 import io.reactivex.rxjava3.core.*;
19 import io.reactivex.rxjava3.disposables.Disposable;
20 import io.reactivex.rxjava3.internal.disposables.DisposableHelper;
21 import io.reactivex.rxjava3.internal.subscriptions.DeferredScalarSubscription;
22
23 /**
24 * Wraps a Single and exposes it as a Flowable.
25 *
26 * @param <T> the value type
27 */
28 public final class SingleToFlowable<T> extends Flowable<T> {
29
30 final SingleSource<? extends T> source;
31
32 public SingleToFlowable(SingleSource<? extends T> source) {
33 this.source = source;
34 }
35
36 @Override
37 public void subscribeActual(final Subscriber<? super T> s) {
38 source.subscribe(new SingleToFlowableObserver<T>(s));
39 }
40
41 static final class SingleToFlowableObserver<T> extends DeferredScalarSubscription<T>
42 implements SingleObserver<T> {
43
44 private static final long serialVersionUID = 187782011903685568L;
45
46 Disposable upstream;
47
48 SingleToFlowableObserver(Subscriber<? super T> downstream) {
49 super(downstream);
50 }
51
52 @Override
53 public void onSubscribe(Disposable d) {
54 if (DisposableHelper.validate(this.upstream, d)) {
55 this.upstream = d;
56
57 downstream.onSubscribe(this);
58 }
59 }
60
61 @Override
62 public void onSuccess(T value) {
63 complete(value);
64 }
65
66 @Override
67 public void onError(Throwable e) {
68 downstream.onError(e);
69 }
70
71 @Override
72 public void cancel() {
73 super.cancel();
74 upstream.dispose();
75 }
76 }
77 }