| 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.*; |
| 19 |
import io.reactivex.rxjava3.exceptions.Exceptions; |
| 20 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 21 |
|
| 22 |
/** |
| 23 |
* Allows lifting operators into a chain of Publishers. |
| 24 |
* |
| 25 |
* <p>By having a concrete Publisher as lift, operator fusing can now identify |
| 26 |
* both the source and the operation inside it via casting, unlike the lambda version of this. |
| 27 |
* |
| 28 |
* @param <T> the upstream value type |
| 29 |
* @param <R> the downstream parameter type |
| 30 |
*/ |
| 31 |
public final class FlowableLift<R, T> extends AbstractFlowableWithUpstream<T, R> { |
| 32 |
/** The actual operator. */ |
| 33 |
final FlowableOperator<? extends R, ? super T> operator; |
| 34 |
|
| 35 |
public FlowableLift(Flowable<T> source, FlowableOperator<? extends R, ? super T> operator) { |
| 36 |
super(source); |
| 37 |
this.operator = operator; |
| 38 |
} |
| 39 |
|
| 40 |
@Override |
| 41 |
public void subscribeActual(Subscriber<? super R> s) { |
| 42 |
try { |
| 43 |
Subscriber<? super T> st = operator.apply(s); |
| 44 |
|
| 45 |
if (st == null) { |
| 46 |
throw new NullPointerException("Operator " + operator + " returned a null Subscriber"); |
| 47 |
} |
| 48 |
|
| 49 |
source.subscribe(st); |
| 50 |
} catch (NullPointerException e) { // NOPMD |
| 51 |
throw e; |
| 52 |
} catch (Throwable e) { |
| 53 |
Exceptions.throwIfFatal(e); |
| 54 |
// can't call onError because no way to know if a Subscription has been set or not |
| 55 |
// can't call onSubscribe because the call might have set a Subscription already |
| 56 |
RxJavaPlugins.onError(e); |
| 57 |
|
| 58 |
NullPointerException npe = new NullPointerException("Actually not, but can't throw other exceptions due to RS"); |
| 59 |
npe.initCause(e); |
| 60 |
throw npe; |
| 61 |
} |
| 62 |
} |
| 63 |
} |