Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableMap.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.Subscriber;
17
18 import io.reactivex.rxjava3.annotations.Nullable;
19 import io.reactivex.rxjava3.core.Flowable;
20 import io.reactivex.rxjava3.functions.Function;
21 import io.reactivex.rxjava3.internal.subscribers.*;
22 import io.reactivex.rxjava3.operators.ConditionalSubscriber;
23
24 import java.util.Objects;
25
26 public final class FlowableMap<T, U> extends AbstractFlowableWithUpstream<T, U> {
27 final Function<? super T, ? extends U> mapper;
28 public FlowableMap(Flowable<T> source, Function<? super T, ? extends U> mapper) {
29 super(source);
30 this.mapper = mapper;
31 }
32
33 @Override
34 protected void subscribeActual(Subscriber<? super U> s) {
35 if (s instanceof ConditionalSubscriber) {
36 source.subscribe(new MapConditionalSubscriber<T, U>((ConditionalSubscriber<? super U>)s, mapper));
37 } else {
38 source.subscribe(new MapSubscriber<T, U>(s, mapper));
39 }
40 }
41
42 static final class MapSubscriber<T, U> extends BasicFuseableSubscriber<T, U> {
43 final Function<? super T, ? extends U> mapper;
44
45 MapSubscriber(Subscriber<? super U> actual, Function<? super T, ? extends U> mapper) {
46 super(actual);
47 this.mapper = mapper;
48 }
49
50 @Override
51 public void onNext(T t) {
52 if (done) {
53 return;
54 }
55
56 if (sourceMode != NONE) {
57 downstream.onNext(null);
58 return;
59 }
60
61 U v;
62
63 try {
64 v = Objects.requireNonNull(mapper.apply(t), "The mapper function returned a null value.");
65 } catch (Throwable ex) {
66 fail(ex);
67 return;
68 }
69 downstream.onNext(v);
70 }
71
72 @Override
73 public int requestFusion(int mode) {
74 return transitiveBoundaryFusion(mode);
75 }
76
77 @Nullable
78 @Override
79 public U poll() throws Throwable {
80 T t = qs.poll();
81 return t != null ? Objects.requireNonNull(mapper.apply(t), "The mapper function returned a null value.") : null;
82 }
83 }
84
85 static final class MapConditionalSubscriber<T, U> extends BasicFuseableConditionalSubscriber<T, U> {
86 final Function<? super T, ? extends U> mapper;
87
88 MapConditionalSubscriber(ConditionalSubscriber<? super U> actual, Function<? super T, ? extends U> function) {
89 super(actual);
90 this.mapper = function;
91 }
92
93 @Override
94 public void onNext(T t) {
95 if (done) {
96 return;
97 }
98
99 if (sourceMode != NONE) {
100 downstream.onNext(null);
101 return;
102 }
103
104 U v;
105
106 try {
107 v = Objects.requireNonNull(mapper.apply(t), "The mapper function returned a null value.");
108 } catch (Throwable ex) {
109 fail(ex);
110 return;
111 }
112 downstream.onNext(v);
113 }
114
115 @Override
116 public boolean tryOnNext(T t) {
117 if (done) {
118 return true;
119 }
120
121 if (sourceMode != NONE) {
122 downstream.tryOnNext(null);
123 return true;
124 }
125
126 U v;
127
128 try {
129 v = Objects.requireNonNull(mapper.apply(t), "The mapper function returned a null value.");
130 } catch (Throwable ex) {
131 fail(ex);
132 return true;
133 }
134 return downstream.tryOnNext(v);
135 }
136
137 @Override
138 public int requestFusion(int mode) {
139 return transitiveBoundaryFusion(mode);
140 }
141
142 @Nullable
143 @Override
144 public U poll() throws Throwable {
145 T t = qs.poll();
146 return t != null ? Objects.requireNonNull(mapper.apply(t), "The mapper function returned a null value.") : null;
147 }
148 }
149
150 }