Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/maybe/MaybeToObservable.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 io.reactivex.rxjava3.core.*;
17 import io.reactivex.rxjava3.disposables.Disposable;
18 import io.reactivex.rxjava3.internal.disposables.DisposableHelper;
19 import io.reactivex.rxjava3.internal.fuseable.HasUpstreamMaybeSource;
20 import io.reactivex.rxjava3.internal.observers.DeferredScalarDisposable;
21
22 /**
23 * Wraps a MaybeSource and exposes it as an Observable, relaying signals in a backpressure-aware manner
24 * and composes cancellation through.
25 *
26 * @param <T> the value type
27 */
28 public final class MaybeToObservable<T> extends Observable<T> implements HasUpstreamMaybeSource<T> {
29
30 final MaybeSource<T> source;
31
32 public MaybeToObservable(MaybeSource<T> source) {
33 this.source = source;
34 }
35
36 @Override
37 public MaybeSource<T> source() {
38 return source;
39 }
40
41 @Override
42 protected void subscribeActual(Observer<? super T> observer) {
43 source.subscribe(create(observer));
44 }
45
46 /**
47 * Creates a {@link MaybeObserver} wrapper around a {@link Observer}.
48 * <p>History: 2.1.11 - experimental
49 * @param <T> the value type
50 * @param downstream the downstream {@code Observer} to talk to
51 * @return the new MaybeObserver instance
52 * @since 2.2
53 */
54 public static <T> MaybeObserver<T> create(Observer<? super T> downstream) {
55 return new MaybeToObservableObserver<>(downstream);
56 }
57
58 static final class MaybeToObservableObserver<T> extends DeferredScalarDisposable<T>
59 implements MaybeObserver<T> {
60
61 private static final long serialVersionUID = 7603343402964826922L;
62
63 Disposable upstream;
64
65 MaybeToObservableObserver(Observer<? super T> downstream) {
66 super(downstream);
67 }
68
69 @Override
70 public void onSubscribe(Disposable d) {
71 if (DisposableHelper.validate(this.upstream, d)) {
72 this.upstream = d;
73
74 downstream.onSubscribe(this);
75 }
76 }
77
78 @Override
79 public void onSuccess(T value) {
80 complete(value);
81 }
82
83 @Override
84 public void onError(Throwable e) {
85 error(e);
86 }
87
88 @Override
89 public void onComplete() {
90 complete();
91 }
92
93 @Override
94 public void dispose() {
95 super.dispose();
96 upstream.dispose();
97 }
98 }
99 }