Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/jdk8/SingleMapOptional.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.jdk8;
15
16 import java.util.*;
17
18 import io.reactivex.rxjava3.core.*;
19 import io.reactivex.rxjava3.disposables.Disposable;
20 import io.reactivex.rxjava3.exceptions.Exceptions;
21 import io.reactivex.rxjava3.functions.Function;
22 import io.reactivex.rxjava3.internal.disposables.DisposableHelper;
23
24 /**
25 * Maps the success value to an {@link Optional} and emits its non-empty value or completes.
26 *
27 * @param <T> the upstream success value type
28 * @param <R> the result value type
29 * @since 3.0.0
30 */
31 public final class SingleMapOptional<T, R> extends Maybe<R> {
32
33 final Single<T> source;
34
35 final Function<? super T, Optional<? extends R>> mapper;
36
37 public SingleMapOptional(Single<T> source, Function<? super T, Optional<? extends R>> mapper) {
38 this.source = source;
39 this.mapper = mapper;
40 }
41
42 @Override
43 protected void subscribeActual(MaybeObserver<? super R> observer) {
44 source.subscribe(new MapOptionalSingleObserver<>(observer, mapper));
45 }
46
47 static final class MapOptionalSingleObserver<T, R> implements SingleObserver<T>, Disposable {
48
49 final MaybeObserver<? super R> downstream;
50
51 final Function<? super T, Optional<? extends R>> mapper;
52
53 Disposable upstream;
54
55 MapOptionalSingleObserver(MaybeObserver<? super R> downstream, Function<? super T, Optional<? extends R>> mapper) {
56 this.downstream = downstream;
57 this.mapper = mapper;
58 }
59
60 @Override
61 public void dispose() {
62 Disposable d = this.upstream;
63 this.upstream = DisposableHelper.DISPOSED;
64 d.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(this.upstream, d)) {
75 this.upstream = d;
76
77 downstream.onSubscribe(this);
78 }
79 }
80
81 @Override
82 public void onSuccess(T value) {
83 Optional<? extends R> v;
84
85 try {
86 v = Objects.requireNonNull(mapper.apply(value), "The mapper returned a null item");
87 } catch (Throwable ex) {
88 Exceptions.throwIfFatal(ex);
89 downstream.onError(ex);
90 return;
91 }
92
93 if (v.isPresent()) {
94 downstream.onSuccess(v.get());
95 } else {
96 downstream.onComplete();
97 }
98 }
99
100 @Override
101 public void onError(Throwable e) {
102 downstream.onError(e);
103 }
104 }
105 }