Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableDematerialize.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.*;
19 import io.reactivex.rxjava3.exceptions.Exceptions;
20 import io.reactivex.rxjava3.functions.Function;
21 import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper;
22 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
23
24 import java.util.Objects;
25
26 public final class FlowableDematerialize<T, R> extends AbstractFlowableWithUpstream<T, R> {
27
28 final Function<? super T, ? extends Notification<R>> selector;
29
30 public FlowableDematerialize(Flowable<T> source, Function<? super T, ? extends Notification<R>> selector) {
31 super(source);
32 this.selector = selector;
33 }
34
35 @Override
36 protected void subscribeActual(Subscriber<? super R> subscriber) {
37 source.subscribe(new DematerializeSubscriber<>(subscriber, selector));
38 }
39
40 static final class DematerializeSubscriber<T, R> implements FlowableSubscriber<T>, Subscription {
41
42 final Subscriber<? super R> downstream;
43
44 final Function<? super T, ? extends Notification<R>> selector;
45
46 boolean done;
47
48 Subscription upstream;
49
50 DematerializeSubscriber(Subscriber<? super R> downstream, Function<? super T, ? extends Notification<R>> selector) {
51 this.downstream = downstream;
52 this.selector = selector;
53 }
54
55 @Override
56 public void onSubscribe(Subscription s) {
57 if (SubscriptionHelper.validate(this.upstream, s)) {
58 this.upstream = s;
59 downstream.onSubscribe(this);
60 }
61 }
62
63 @Override
64 public void onNext(T item) {
65 if (done) {
66 if (item instanceof Notification) {
67 Notification<?> notification = (Notification<?>)item;
68 if (notification.isOnError()) {
69 RxJavaPlugins.onError(notification.getError());
70 }
71 }
72 return;
73 }
74
75 Notification<R> notification;
76
77 try {
78 notification = Objects.requireNonNull(selector.apply(item), "The selector returned a null Notification");
79 } catch (Throwable ex) {
80 Exceptions.throwIfFatal(ex);
81 upstream.cancel();
82 onError(ex);
83 return;
84 }
85 if (notification.isOnError()) {
86 upstream.cancel();
87 onError(notification.getError());
88 } else if (notification.isOnComplete()) {
89 upstream.cancel();
90 onComplete();
91 } else {
92 downstream.onNext(notification.getValue());
93 }
94 }
95
96 @Override
97 public void onError(Throwable t) {
98 if (done) {
99 RxJavaPlugins.onError(t);
100 return;
101 }
102 done = true;
103
104 downstream.onError(t);
105 }
106
107 @Override
108 public void onComplete() {
109 if (done) {
110 return;
111 }
112 done = true;
113
114 downstream.onComplete();
115 }
116
117 @Override
118 public void request(long n) {
119 upstream.request(n);
120 }
121
122 @Override
123 public void cancel() {
124 upstream.cancel();
125 }
126 }
127 }