Debian Codegraph @

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