Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/maybe/MaybeFromSingle.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.HasUpstreamSingleSource;
20
21 /**
22 * Wrap a Single into a Maybe.
23 *
24 * @param <T> the value type
25 */
26 public final class MaybeFromSingle<T> extends Maybe<T> implements HasUpstreamSingleSource<T> {
27
28 final SingleSource<T> source;
29
30 public MaybeFromSingle(SingleSource<T> source) {
31 this.source = source;
32 }
33
34 @Override
35 public SingleSource<T> source() {
36 return source;
37 }
38
39 @Override
40 protected void subscribeActual(MaybeObserver<? super T> observer) {
41 source.subscribe(new FromSingleObserver<>(observer));
42 }
43
44 static final class FromSingleObserver<T> implements SingleObserver<T>, Disposable {
45 final MaybeObserver<? super T> downstream;
46
47 Disposable upstream;
48
49 FromSingleObserver(MaybeObserver<? super T> downstream) {
50 this.downstream = downstream;
51 }
52
53 @Override
54 public void dispose() {
55 upstream.dispose();
56 upstream = DisposableHelper.DISPOSED;
57 }
58
59 @Override
60 public boolean isDisposed() {
61 return upstream.isDisposed();
62 }
63
64 @Override
65 public void onSubscribe(Disposable d) {
66 if (DisposableHelper.validate(this.upstream, d)) {
67 this.upstream = d;
68
69 downstream.onSubscribe(this);
70 }
71 }
72
73 @Override
74 public void onSuccess(T value) {
75 upstream = DisposableHelper.DISPOSED;
76 downstream.onSuccess(value);
77 }
78
79 @Override
80 public void onError(Throwable e) {
81 upstream = DisposableHelper.DISPOSED;
82 downstream.onError(e);
83 }
84 }
85 }