Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableFromSupplier.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.flowable;
15
16 import org.reactivestreams.Subscriber;
17
18 import io.reactivex.rxjava3.core.Flowable;
19 import io.reactivex.rxjava3.exceptions.Exceptions;
20 import io.reactivex.rxjava3.functions.Supplier;
21 import io.reactivex.rxjava3.internal.subscriptions.DeferredScalarSubscription;
22 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
23
24 import java.util.Objects;
25
26 /**
27 * Call a Supplier for each incoming Subscriber and signal the returned value or the thrown exception.
28 * @param <T> the value type and element type returned by the supplier and the flow
29 * @since 3.0.0
30 */
31 public final class FlowableFromSupplier<T> extends Flowable<T> implements Supplier<T> {
32
33 final Supplier<? extends T> supplier;
34
35 public FlowableFromSupplier(Supplier<? extends T> supplier) {
36 this.supplier = supplier;
37 }
38
39 @Override
40 public void subscribeActual(Subscriber<? super T> s) {
41 DeferredScalarSubscription<T> deferred = new DeferredScalarSubscription<>(s);
42 s.onSubscribe(deferred);
43
44 T t;
45 try {
46 t = Objects.requireNonNull(supplier.get(), "The supplier returned a null value");
47 } catch (Throwable ex) {
48 Exceptions.throwIfFatal(ex);
49 if (deferred.isCancelled()) {
50 RxJavaPlugins.onError(ex);
51 } else {
52 s.onError(ex);
53 }
54 return;
55 }
56
57 deferred.complete(t);
58 }
59
60 @Override
61 public T get() throws Throwable {
62 return Objects.requireNonNull(supplier.get(), "The supplier returned a null value");
63 }
64 }