Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/single/SingleDematerialize.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.single;
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
22 import java.util.Objects;
23
24 /**
25 * Maps the success value of the source to a Notification, then
26 * maps it back to the corresponding signal type.
27 * <p>History: 2.2.4 - experimental
28 * @param <T> the element type of the source
29 * @param <R> the element type of the Notification and result
30 * @since 3.0.0
31 */
32 public final class SingleDematerialize<T, R> extends Maybe<R> {
33
34 final Single<T> source;
35
36 final Function<? super T, Notification<R>> selector;
37
38 public SingleDematerialize(Single<T> source, Function<? super T, Notification<R>> selector) {
39 this.source = source;
40 this.selector = selector;
41 }
42
43 @Override
44 protected void subscribeActual(MaybeObserver<? super R> observer) {
45 source.subscribe(new DematerializeObserver<>(observer, selector));
46 }
47
48 static final class DematerializeObserver<T, R> implements SingleObserver<T>, Disposable {
49
50 final MaybeObserver<? super R> downstream;
51
52 final Function<? super T, Notification<R>> selector;
53
54 Disposable upstream;
55
56 DematerializeObserver(MaybeObserver<? super R> downstream,
57 Function<? super T, Notification<R>> selector) {
58 this.downstream = downstream;
59 this.selector = selector;
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 onSubscribe(Disposable d) {
74 if (DisposableHelper.validate(upstream, d)) {
75 upstream = d;
76 downstream.onSubscribe(this);
77 }
78 }
79
80 @Override
81 public void onSuccess(T t) {
82 Notification<R> notification;
83
84 try {
85 notification = Objects.requireNonNull(selector.apply(t), "The selector returned a null Notification");
86 } catch (Throwable ex) {
87 Exceptions.throwIfFatal(ex);
88 downstream.onError(ex);
89 return;
90 }
91 if (notification.isOnNext()) {
92 downstream.onSuccess(notification.getValue());
93 } else if (notification.isOnComplete()) {
94 downstream.onComplete();
95 } else {
96 downstream.onError(notification.getError());
97 }
98 }
99
100 @Override
101 public void onError(Throwable e) {
102 downstream.onError(e);
103 }
104 }
105 }