Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/maybe/MaybeMap.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 upstream success value into some other value.
26 *
27 * @param <T> the upstream value type
28 * @param <R> the downstream value type
29 */
30 public final class MaybeMap<T, R> extends AbstractMaybeWithUpstream<T, R> {
31
32 final Function<? super T, ? extends R> mapper;
33
34 public MaybeMap(MaybeSource<T> source, Function<? super T, ? extends R> mapper) {
35 super(source);
36 this.mapper = mapper;
37 }
38
39 @Override
40 protected void subscribeActual(MaybeObserver<? super R> observer) {
41 source.subscribe(new MapMaybeObserver<T, R>(observer, mapper));
42 }
43
44 static final class MapMaybeObserver<T, R> implements MaybeObserver<T>, Disposable {
45
46 final MaybeObserver<? super R> downstream;
47
48 final Function<? super T, ? extends R> mapper;
49
50 Disposable upstream;
51
52 MapMaybeObserver(MaybeObserver<? super R> actual, Function<? super T, ? extends R> mapper) {
53 this.downstream = actual;
54 this.mapper = mapper;
55 }
56
57 @Override
58 public void dispose() {
59 Disposable d = this.upstream;
60 this.upstream = DisposableHelper.DISPOSED;
61 d.dispose();
62 }
63
64 @Override
65 public boolean isDisposed() {
66 return upstream.isDisposed();
67 }
68
69 @Override
70 public void onSubscribe(Disposable d) {
71 if (DisposableHelper.validate(this.upstream, d)) {
72 this.upstream = d;
73
74 downstream.onSubscribe(this);
75 }
76 }
77
78 @Override
79 public void onSuccess(T value) {
80 R v;
81
82 try {
83 v = Objects.requireNonNull(mapper.apply(value), "The mapper returned a null item");
84 } catch (Throwable ex) {
85 Exceptions.throwIfFatal(ex);
86 downstream.onError(ex);
87 return;
88 }
89
90 downstream.onSuccess(v);
91 }
92
93 @Override
94 public void onError(Throwable e) {
95 downstream.onError(e);
96 }
97
98 @Override
99 public void onComplete() {
100 downstream.onComplete();
101 }
102 }
103 }