Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableScalarXMap.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.flowable;
15
16 import org.reactivestreams.*;
17
18 import io.reactivex.rxjava3.core.Flowable;
19 import io.reactivex.rxjava3.exceptions.Exceptions;
20 import io.reactivex.rxjava3.functions.*;
21 import io.reactivex.rxjava3.internal.subscriptions.*;
22 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
23
24 import java.util.Objects;
25
26 /**
27 * Utility classes to work with scalar-sourced XMap operators (where X == { flat, concat, switch }).
28 */
29 public final class FlowableScalarXMap {
30
31 /** Utility class. */
32 private FlowableScalarXMap() {
33 throw new IllegalStateException("No instances!");
34 }
35
36 /**
37 * Tries to subscribe to a possibly Supplier source's mapped Publisher.
38 * @param <T> the input value type
39 * @param <R> the output value type
40 * @param source the source Publisher
41 * @param subscriber the subscriber
42 * @param mapper the function mapping a scalar value into a Publisher
43 * @return true if successful, false if the caller should continue with the regular path.
44 */
45 @SuppressWarnings("unchecked")
46 public static <T, R> boolean tryScalarXMapSubscribe(Publisher<T> source,
47 Subscriber<? super R> subscriber,
48 Function<? super T, ? extends Publisher<? extends R>> mapper) {
49 if (source instanceof Supplier) {
50 T t;
51
52 try {
53 t = ((Supplier<T>)source).get();
54 } catch (Throwable ex) {
55 Exceptions.throwIfFatal(ex);
56 EmptySubscription.error(ex, subscriber);
57 return true;
58 }
59
60 if (t == null) {
61 EmptySubscription.complete(subscriber);
62 return true;
63 }
64
65 Publisher<? extends R> r;
66
67 try {
68 r = Objects.requireNonNull(mapper.apply(t), "The mapper returned a null Publisher");
69 } catch (Throwable ex) {
70 Exceptions.throwIfFatal(ex);
71 EmptySubscription.error(ex, subscriber);
72 return true;
73 }
74
75 if (r instanceof Supplier) {
76 R u;
77
78 try {
79 u = ((Supplier<R>)r).get();
80 } catch (Throwable ex) {
81 Exceptions.throwIfFatal(ex);
82 EmptySubscription.error(ex, subscriber);
83 return true;
84 }
85
86 if (u == null) {
87 EmptySubscription.complete(subscriber);
88 return true;
89 }
90 subscriber.onSubscribe(new ScalarSubscription<>(subscriber, u));
91 } else {
92 r.subscribe(subscriber);
93 }
94
95 return true;
96 }
97 return false;
98 }
99
100 /**
101 * Maps a scalar value into a Publisher and emits its values.
102 *
103 * @param <T> the scalar value type
104 * @param <U> the output value type
105 * @param value the scalar value to map
106 * @param mapper the function that gets the scalar value and should return
107 * a Publisher that gets streamed
108 * @return the new Flowable instance
109 */
110 public static <T, U> Flowable<U> scalarXMap(final T value, final Function<? super T, ? extends Publisher<? extends U>> mapper) {
111 return RxJavaPlugins.onAssembly(new ScalarXMapFlowable<>(value, mapper));
112 }
113
114 /**
115 * Maps a scalar value to a Publisher and subscribes to it.
116 *
117 * @param <T> the scalar value type
118 * @param <R> the mapped Publisher's element type.
119 */
120 static final class ScalarXMapFlowable<T, R> extends Flowable<R> {
121
122 final T value;
123
124 final Function<? super T, ? extends Publisher<? extends R>> mapper;
125
126 ScalarXMapFlowable(T value,
127 Function<? super T, ? extends Publisher<? extends R>> mapper) {
128 this.value = value;
129 this.mapper = mapper;
130 }
131
132 @SuppressWarnings("unchecked")
133 @Override
134 public void subscribeActual(Subscriber<? super R> s) {
135 Publisher<? extends R> other;
136 try {
137 other = Objects.requireNonNull(mapper.apply(value), "The mapper returned a null Publisher");
138 } catch (Throwable e) {
139 Exceptions.throwIfFatal(e);
140 EmptySubscription.error(e, s);
141 return;
142 }
143 if (other instanceof Supplier) {
144 R u;
145
146 try {
147 u = ((Supplier<R>)other).get();
148 } catch (Throwable ex) {
149 Exceptions.throwIfFatal(ex);
150 EmptySubscription.error(ex, s);
151 return;
152 }
153
154 if (u == null) {
155 EmptySubscription.complete(s);
156 return;
157 }
158 s.onSubscribe(new ScalarSubscription<>(s, u));
159 } else {
160 other.subscribe(s);
161 }
162 }
163 }
164 }