Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/single/SingleToObservable.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 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.observers.DeferredScalarDisposable;
20
21 /**
22 * Wraps a Single and exposes it as an Observable.
23 *
24 * @param <T> the value type
25 */
26 public final class SingleToObservable<T> extends Observable<T> {
27
28 final SingleSource<? extends T> source;
29
30 public SingleToObservable(SingleSource<? extends T> source) {
31 this.source = source;
32 }
33
34 @Override
35 public void subscribeActual(final Observer<? super T> observer) {
36 source.subscribe(create(observer));
37 }
38
39 /**
40 * Creates a {@link SingleObserver} wrapper around a {@link Observer}.
41 * <p>History: 2.0.1 - experimental
42 * @param <T> the value type
43 * @param downstream the downstream {@code Observer} to talk to
44 * @return the new SingleObserver instance
45 * @since 2.2
46 */
47 public static <T> SingleObserver<T> create(Observer<? super T> downstream) {
48 return new SingleToObservableObserver<>(downstream);
49 }
50
51 static final class SingleToObservableObserver<T>
52 extends DeferredScalarDisposable<T>
53 implements SingleObserver<T> {
54
55 private static final long serialVersionUID = 3786543492451018833L;
56 Disposable upstream;
57
58 SingleToObservableObserver(Observer<? super T> downstream) {
59 super(downstream);
60 }
61
62 @Override
63 public void onSubscribe(Disposable d) {
64 if (DisposableHelper.validate(this.upstream, d)) {
65 this.upstream = d;
66
67 downstream.onSubscribe(this);
68 }
69 }
70
71 @Override
72 public void onSuccess(T value) {
73 complete(value);
74 }
75
76 @Override
77 public void onError(Throwable e) {
78 error(e);
79 }
80
81 @Override
82 public void dispose() {
83 super.dispose();
84 upstream.dispose();
85 }
86
87 }
88 }