Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/single/SingleFlatMapNotification.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 java.util.Objects;
17 import java.util.concurrent.atomic.AtomicReference;
18
19 import io.reactivex.rxjava3.core.*;
20 import io.reactivex.rxjava3.disposables.Disposable;
21 import io.reactivex.rxjava3.exceptions.*;
22 import io.reactivex.rxjava3.functions.*;
23 import io.reactivex.rxjava3.internal.disposables.DisposableHelper;
24
25 /**
26 * Maps a value into a SingleSource and relays its signal.
27 *
28 * @param <T> the source value type
29 * @param <R> the result value type
30 * @since 3.0.0
31 */
32 public final class SingleFlatMapNotification<T, R> extends Single<R> {
33
34 final SingleSource<T> source;
35
36 final Function<? super T, ? extends SingleSource<? extends R>> onSuccessMapper;
37
38 final Function<? super Throwable, ? extends SingleSource<? extends R>> onErrorMapper;
39
40 public SingleFlatMapNotification(SingleSource<T> source,
41 Function<? super T, ? extends SingleSource<? extends R>> onSuccessMapper,
42 Function<? super Throwable, ? extends SingleSource<? extends R>> onErrorMapper) {
43 this.source = source;
44 this.onSuccessMapper = onSuccessMapper;
45 this.onErrorMapper = onErrorMapper;
46 }
47
48 @Override
49 protected void subscribeActual(SingleObserver<? super R> observer) {
50 source.subscribe(new FlatMapSingleObserver<>(observer, onSuccessMapper, onErrorMapper));
51 }
52
53 static final class FlatMapSingleObserver<T, R>
54 extends AtomicReference<Disposable>
55 implements SingleObserver<T>, Disposable {
56
57 private static final long serialVersionUID = 4375739915521278546L;
58
59 final SingleObserver<? super R> downstream;
60
61 final Function<? super T, ? extends SingleSource<? extends R>> onSuccessMapper;
62
63 final Function<? super Throwable, ? extends SingleSource<? extends R>> onErrorMapper;
64
65 Disposable upstream;
66
67 FlatMapSingleObserver(SingleObserver<? super R> actual,
68 Function<? super T, ? extends SingleSource<? extends R>> onSuccessMapper,
69 Function<? super Throwable, ? extends SingleSource<? extends R>> onErrorMapper) {
70 this.downstream = actual;
71 this.onSuccessMapper = onSuccessMapper;
72 this.onErrorMapper = onErrorMapper;
73 }
74
75 @Override
76 public void dispose() {
77 DisposableHelper.dispose(this);
78 upstream.dispose();
79 }
80
81 @Override
82 public boolean isDisposed() {
83 return DisposableHelper.isDisposed(get());
84 }
85
86 @Override
87 public void onSubscribe(Disposable d) {
88 if (DisposableHelper.validate(this.upstream, d)) {
89 this.upstream = d;
90
91 downstream.onSubscribe(this);
92 }
93 }
94
95 @Override
96 public void onSuccess(T value) {
97 SingleSource<? extends R> source;
98
99 try {
100 source = Objects.requireNonNull(onSuccessMapper.apply(value), "The onSuccessMapper returned a null SingleSource");
101 } catch (Throwable ex) {
102 Exceptions.throwIfFatal(ex);
103 downstream.onError(ex);
104 return;
105 }
106
107 if (!isDisposed()) {
108 source.subscribe(new InnerObserver());
109 }
110 }
111
112 @Override
113 public void onError(Throwable e) {
114 SingleSource<? extends R> source;
115
116 try {
117 source = Objects.requireNonNull(onErrorMapper.apply(e), "The onErrorMapper returned a null SingleSource");
118 } catch (Throwable ex) {
119 Exceptions.throwIfFatal(ex);
120 downstream.onError(new CompositeException(e, ex));
121 return;
122 }
123
124 if (!isDisposed()) {
125 source.subscribe(new InnerObserver());
126 }
127 }
128
129 final class InnerObserver implements SingleObserver<R> {
130
131 @Override
132 public void onSubscribe(Disposable d) {
133 DisposableHelper.setOnce(FlatMapSingleObserver.this, d);
134 }
135
136 @Override
137 public void onSuccess(R value) {
138 downstream.onSuccess(value);
139 }
140
141 @Override
142 public void onError(Throwable e) {
143 downstream.onError(e);
144 }
145 }
146 }
147 }