Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/maybe/MaybeDematerialize.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.maybe;
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 MaybeDematerialize<T, R> extends AbstractMaybeWithUpstream<T, R> {
33
34 final Function<? super T, Notification<R>> selector;
35
36 public MaybeDematerialize(Maybe<T> source, Function<? super T, Notification<R>> selector) {
37 super(source);
38 this.selector = selector;
39 }
40
41 @Override
42 protected void subscribeActual(MaybeObserver<? super R> observer) {
43 source.subscribe(new DematerializeObserver<>(observer, selector));
44 }
45
46 static final class DematerializeObserver<T, R> implements MaybeObserver<T>, Disposable {
47
48 final MaybeObserver<? super R> downstream;
49
50 final Function<? super T, Notification<R>> selector;
51
52 Disposable upstream;
53
54 DematerializeObserver(MaybeObserver<? super R> downstream,
55 Function<? super T, Notification<R>> selector) {
56 this.downstream = downstream;
57 this.selector = selector;
58 }
59
60 @Override
61 public void dispose() {
62 upstream.dispose();
63 }
64
65 @Override
66 public boolean isDisposed() {
67 return upstream.isDisposed();
68 }
69
70 @Override
71 public void onSubscribe(Disposable d) {
72 if (DisposableHelper.validate(upstream, d)) {
73 upstream = d;
74 downstream.onSubscribe(this);
75 }
76 }
77
78 @Override
79 public void onSuccess(T t) {
80 Notification<R> notification;
81
82 try {
83 notification = Objects.requireNonNull(selector.apply(t), "The selector returned a null Notification");
84 } catch (Throwable ex) {
85 Exceptions.throwIfFatal(ex);
86 downstream.onError(ex);
87 return;
88 }
89 if (notification.isOnNext()) {
90 downstream.onSuccess(notification.getValue());
91 } else if (notification.isOnComplete()) {
92 downstream.onComplete();
93 } else {
94 downstream.onError(notification.getError());
95 }
96 }
97
98 @Override
99 public void onError(Throwable e) {
100 downstream.onError(e);
101 }
102
103 @Override
104 public void onComplete() {
105 downstream.onComplete();
106 }
107 }
108 }