Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/mixed/MaybeFlatMapPublisher.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.mixed;
15
16 import java.util.Objects;
17 import java.util.concurrent.atomic.*;
18
19 import org.reactivestreams.*;
20
21 import io.reactivex.rxjava3.core.*;
22 import io.reactivex.rxjava3.disposables.Disposable;
23 import io.reactivex.rxjava3.exceptions.Exceptions;
24 import io.reactivex.rxjava3.functions.Function;
25 import io.reactivex.rxjava3.internal.disposables.DisposableHelper;
26 import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper;
27
28 /**
29 * Maps the success value of a Maybe onto a Publisher and
30 * relays its signals to the downstream subscriber.
31 *
32 * @param <T> the success value type of the Maybe source
33 * @param <R> the result type of the Publisher and this operator
34 * @since 2.1.15
35 */
36 public final class MaybeFlatMapPublisher<T, R> extends Flowable<R> {
37
38 final MaybeSource<T> source;
39
40 final Function<? super T, ? extends Publisher<? extends R>> mapper;
41
42 public MaybeFlatMapPublisher(MaybeSource<T> source,
43 Function<? super T, ? extends Publisher<? extends R>> mapper) {
44 this.source = source;
45 this.mapper = mapper;
46 }
47
48 @Override
49 protected void subscribeActual(Subscriber<? super R> s) {
50 source.subscribe(new FlatMapPublisherSubscriber<>(s, mapper));
51 }
52
53 static final class FlatMapPublisherSubscriber<T, R>
54 extends AtomicReference<Subscription>
55 implements FlowableSubscriber<R>, MaybeObserver<T>, Subscription {
56
57 private static final long serialVersionUID = -8948264376121066672L;
58
59 final Subscriber<? super R> downstream;
60
61 final Function<? super T, ? extends Publisher<? extends R>> mapper;
62
63 Disposable upstream;
64
65 final AtomicLong requested;
66
67 FlatMapPublisherSubscriber(Subscriber<? super R> downstream, Function<? super T, ? extends Publisher<? extends R>> mapper) {
68 this.downstream = downstream;
69 this.mapper = mapper;
70 this.requested = new AtomicLong();
71 }
72
73 @Override
74 public void onNext(R t) {
75 downstream.onNext(t);
76 }
77
78 @Override
79 public void onError(Throwable t) {
80 downstream.onError(t);
81 }
82
83 @Override
84 public void onComplete() {
85 downstream.onComplete();
86 }
87
88 @Override
89 public void request(long n) {
90 SubscriptionHelper.deferredRequest(this, requested, n);
91 }
92
93 @Override
94 public void cancel() {
95 upstream.dispose();
96 SubscriptionHelper.cancel(this);
97 }
98
99 @Override
100 public void onSubscribe(Disposable d) {
101 if (DisposableHelper.validate(upstream, d)) {
102 this.upstream = d;
103 downstream.onSubscribe(this);
104 }
105 }
106
107 @Override
108 public void onSuccess(T t) {
109 Publisher<? extends R> p;
110
111 try {
112 p = Objects.requireNonNull(mapper.apply(t), "The mapper returned a null Publisher");
113 } catch (Throwable ex) {
114 Exceptions.throwIfFatal(ex);
115 downstream.onError(ex);
116 return;
117 }
118
119 if (get() != SubscriptionHelper.CANCELLED) {
120 p.subscribe(this);
121 }
122 }
123
124 @Override
125 public void onSubscribe(Subscription s) {
126 SubscriptionHelper.deferredSetOnce(this, requested, s);
127 }
128 }
129 }