Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/maybe/MaybeToFlowable.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 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.fuseable.HasUpstreamMaybeSource;
22 import io.reactivex.rxjava3.internal.subscriptions.DeferredScalarSubscription;
23
24 /**
25 * Wraps a MaybeSource and exposes it as a Flowable, relaying signals in a backpressure-aware manner
26 * and composes cancellation through.
27 *
28 * @param <T> the value type
29 */
30 public final class MaybeToFlowable<T> extends Flowable<T> implements HasUpstreamMaybeSource<T> {
31
32 final MaybeSource<T> source;
33
34 public MaybeToFlowable(MaybeSource<T> source) {
35 this.source = source;
36 }
37
38 @Override
39 public MaybeSource<T> source() {
40 return source;
41 }
42
43 @Override
44 protected void subscribeActual(Subscriber<? super T> s) {
45 source.subscribe(new MaybeToFlowableSubscriber<>(s));
46 }
47
48 static final class MaybeToFlowableSubscriber<T> extends DeferredScalarSubscription<T>
49 implements MaybeObserver<T> {
50
51 private static final long serialVersionUID = 7603343402964826922L;
52
53 Disposable upstream;
54
55 MaybeToFlowableSubscriber(Subscriber<? super T> downstream) {
56 super(downstream);
57 }
58
59 @Override
60 public void onSubscribe(Disposable d) {
61 if (DisposableHelper.validate(this.upstream, d)) {
62 this.upstream = d;
63
64 downstream.onSubscribe(this);
65 }
66 }
67
68 @Override
69 public void onSuccess(T value) {
70 complete(value);
71 }
72
73 @Override
74 public void onError(Throwable e) {
75 downstream.onError(e);
76 }
77
78 @Override
79 public void onComplete() {
80 downstream.onComplete();
81 }
82
83 @Override
84 public void cancel() {
85 super.cancel();
86 upstream.dispose();
87 }
88 }
89 }