Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/jdk8/FlowableMapOptional.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.jdk8;
15
16 import java.util.*;
17
18 import org.reactivestreams.Subscriber;
19
20 import io.reactivex.rxjava3.core.Flowable;
21 import io.reactivex.rxjava3.functions.Function;
22 import io.reactivex.rxjava3.internal.subscribers.*;
23 import io.reactivex.rxjava3.operators.ConditionalSubscriber;
24
25 /**
26 * Map the upstream values into an Optional and emit its value if any.
27 * @param <T> the upstream element type
28 * @param <R> the output element type
29 * @since 3.0.0
30 */
31 public final class FlowableMapOptional<T, R> extends Flowable<R> {
32
33 final Flowable<T> source;
34
35 final Function<? super T, Optional<? extends R>> mapper;
36
37 public FlowableMapOptional(Flowable<T> source, Function<? super T, Optional<? extends R>> mapper) {
38 this.source = source;
39 this.mapper = mapper;
40 }
41
42 @Override
43 protected void subscribeActual(Subscriber<? super R> s) {
44 if (s instanceof ConditionalSubscriber) {
45 source.subscribe(new MapOptionalConditionalSubscriber<>((ConditionalSubscriber<? super R>)s, mapper));
46 } else {
47 source.subscribe(new MapOptionalSubscriber<>(s, mapper));
48 }
49 }
50
51 static final class MapOptionalSubscriber<T, R> extends BasicFuseableSubscriber<T, R>
52 implements ConditionalSubscriber<T> {
53
54 final Function<? super T, Optional<? extends R>> mapper;
55
56 MapOptionalSubscriber(Subscriber<? super R> downstream, Function<? super T, Optional<? extends R>> mapper) {
57 super(downstream);
58 this.mapper = mapper;
59 }
60
61 @Override
62 public void onNext(T t) {
63 if (!tryOnNext(t)) {
64 upstream.request(1);
65 }
66 }
67
68 @Override
69 public boolean tryOnNext(T t) {
70 if (done) {
71 return true;
72 }
73
74 if (sourceMode != NONE) {
75 downstream.onNext(null);
76 return true;
77 }
78
79 Optional<? extends R> result;
80 try {
81 result = Objects.requireNonNull(mapper.apply(t), "The mapper returned a null Optional");
82 } catch (Throwable ex) {
83 fail(ex);
84 return true;
85 }
86
87 if (result.isPresent()) {
88 downstream.onNext(result.get());
89 return true;
90 }
91 return false;
92 }
93
94 @Override
95 public int requestFusion(int mode) {
96 return transitiveBoundaryFusion(mode);
97 }
98
99 @Override
100 public R poll() throws Throwable {
101 for (;;) {
102 T item = qs.poll();
103 if (item == null) {
104 return null;
105 }
106 Optional<? extends R> result = Objects.requireNonNull(mapper.apply(item), "The mapper returned a null Optional");
107 if (result.isPresent()) {
108 return result.get();
109 }
110 if (sourceMode == ASYNC) {
111 qs.request(1);
112 }
113 }
114 }
115 }
116
117 static final class MapOptionalConditionalSubscriber<T, R> extends BasicFuseableConditionalSubscriber<T, R> {
118
119 final Function<? super T, Optional<? extends R>> mapper;
120
121 MapOptionalConditionalSubscriber(ConditionalSubscriber<? super R> downstream, Function<? super T, Optional<? extends R>> mapper) {
122 super(downstream);
123 this.mapper = mapper;
124 }
125
126 @Override
127 public void onNext(T t) {
128 if (!tryOnNext(t)) {
129 upstream.request(1);
130 }
131 }
132
133 @Override
134 public boolean tryOnNext(T t) {
135 if (done) {
136 return true;
137 }
138
139 if (sourceMode != NONE) {
140 downstream.onNext(null);
141 return true;
142 }
143
144 Optional<? extends R> result;
145 try {
146 result = Objects.requireNonNull(mapper.apply(t), "The mapper returned a null Optional");
147 } catch (Throwable ex) {
148 fail(ex);
149 return true;
150 }
151
152 if (result.isPresent()) {
153 return downstream.tryOnNext(result.get());
154 }
155 return false;
156 }
157
158 @Override
159 public int requestFusion(int mode) {
160 return transitiveBoundaryFusion(mode);
161 }
162
163 @Override
164 public R poll() throws Throwable {
165 for (;;) {
166 T item = qs.poll();
167 if (item == null) {
168 return null;
169 }
170 Optional<? extends R> result = Objects.requireNonNull(mapper.apply(item), "The mapper returned a null Optional");
171 if (result.isPresent()) {
172 return result.get();
173 }
174 if (sourceMode == ASYNC) {
175 qs.request(1);
176 }
177 }
178 }
179 }
180 }