| 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.mixed; |
| 15 |
|
| 16 |
import io.reactivex.rxjava3.core.*; |
| 17 |
import io.reactivex.rxjava3.exceptions.Exceptions; |
| 18 |
import io.reactivex.rxjava3.functions.*; |
| 19 |
import io.reactivex.rxjava3.internal.disposables.EmptyDisposable; |
| 20 |
import io.reactivex.rxjava3.internal.operators.maybe.MaybeToObservable; |
| 21 |
import io.reactivex.rxjava3.internal.operators.single.SingleToObservable; |
| 22 |
|
| 23 |
import java.util.Objects; |
| 24 |
|
| 25 |
/** |
| 26 |
* Utility class to extract a value from a scalar source reactive type, |
| 27 |
* map it to a 0-1 type then subscribe the output type's consumer to it, |
| 28 |
* saving on the overhead of the regular subscription channel. |
| 29 |
* <p>History: 2.1.11 - experimental |
| 30 |
* @since 2.2 |
| 31 |
*/ |
| 32 |
final class ScalarXMapZHelper { |
| 33 |
|
| 34 |
private ScalarXMapZHelper() { |
| 35 |
throw new IllegalStateException("No instances!"); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Try subscribing to a {@link CompletableSource} mapped from |
| 40 |
* a scalar source (which implements {@link Supplier}). |
| 41 |
* @param <T> the upstream value type |
| 42 |
* @param source the source reactive type ({@code Flowable} or {@code Observable}) |
| 43 |
* possibly implementing {@link Supplier}. |
| 44 |
* @param mapper the function that turns the scalar upstream value into a |
| 45 |
* {@link CompletableSource} |
| 46 |
* @param observer the consumer to subscribe to the mapped {@link CompletableSource} |
| 47 |
* @return true if a subscription did happen and the regular path should be skipped |
| 48 |
*/ |
| 49 |
static <T> boolean tryAsCompletable(Object source, |
| 50 |
Function<? super T, ? extends CompletableSource> mapper, |
| 51 |
CompletableObserver observer) { |
| 52 |
if (source instanceof Supplier) { |
| 53 |
@SuppressWarnings("unchecked") |
| 54 |
Supplier<T> supplier = (Supplier<T>) source; |
| 55 |
CompletableSource cs = null; |
| 56 |
try { |
| 57 |
T item = supplier.get(); |
| 58 |
if (item != null) { |
| 59 |
cs = Objects.requireNonNull(mapper.apply(item), "The mapper returned a null CompletableSource"); |
| 60 |
} |
| 61 |
} catch (Throwable ex) { |
| 62 |
Exceptions.throwIfFatal(ex); |
| 63 |
EmptyDisposable.error(ex, observer); |
| 64 |
return true; |
| 65 |
} |
| 66 |
|
| 67 |
if (cs == null) { |
| 68 |
EmptyDisposable.complete(observer); |
| 69 |
} else { |
| 70 |
cs.subscribe(observer); |
| 71 |
} |
| 72 |
return true; |
| 73 |
} |
| 74 |
return false; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Try subscribing to a {@link MaybeSource} mapped from |
| 79 |
* a scalar source (which implements {@link Supplier}). |
| 80 |
* @param <T> the upstream value type |
| 81 |
* @param <R> the downstream value type |
| 82 |
* @param source the source reactive type ({@code Flowable} or {@code Observable}) |
| 83 |
* possibly implementing {@link Supplier}. |
| 84 |
* @param mapper the function that turns the scalar upstream value into a |
| 85 |
* {@link MaybeSource} |
| 86 |
* @param observer the consumer to subscribe to the mapped {@link MaybeSource} |
| 87 |
* @return true if a subscription did happen and the regular path should be skipped |
| 88 |
*/ |
| 89 |
static <T, R> boolean tryAsMaybe(Object source, |
| 90 |
Function<? super T, ? extends MaybeSource<? extends R>> mapper, |
| 91 |
Observer<? super R> observer) { |
| 92 |
if (source instanceof Supplier) { |
| 93 |
@SuppressWarnings("unchecked") |
| 94 |
Supplier<T> supplier = (Supplier<T>) source; |
| 95 |
MaybeSource<? extends R> cs = null; |
| 96 |
try { |
| 97 |
T item = supplier.get(); |
| 98 |
if (item != null) { |
| 99 |
cs = Objects.requireNonNull(mapper.apply(item), "The mapper returned a null MaybeSource"); |
| 100 |
} |
| 101 |
} catch (Throwable ex) { |
| 102 |
Exceptions.throwIfFatal(ex); |
| 103 |
EmptyDisposable.error(ex, observer); |
| 104 |
return true; |
| 105 |
} |
| 106 |
|
| 107 |
if (cs == null) { |
| 108 |
EmptyDisposable.complete(observer); |
| 109 |
} else { |
| 110 |
cs.subscribe(MaybeToObservable.create(observer)); |
| 111 |
} |
| 112 |
return true; |
| 113 |
} |
| 114 |
return false; |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Try subscribing to a {@link SingleSource} mapped from |
| 119 |
* a scalar source (which implements {@link Supplier}). |
| 120 |
* @param <T> the upstream value type |
| 121 |
* @param <R> the downstream value type |
| 122 |
* @param source the source reactive type ({@code Flowable} or {@code Observable}) |
| 123 |
* possibly implementing {@link Supplier}. |
| 124 |
* @param mapper the function that turns the scalar upstream value into a |
| 125 |
* {@link SingleSource} |
| 126 |
* @param observer the consumer to subscribe to the mapped {@link SingleSource} |
| 127 |
* @return true if a subscription did happen and the regular path should be skipped |
| 128 |
*/ |
| 129 |
static <T, R> boolean tryAsSingle(Object source, |
| 130 |
Function<? super T, ? extends SingleSource<? extends R>> mapper, |
| 131 |
Observer<? super R> observer) { |
| 132 |
if (source instanceof Supplier) { |
| 133 |
@SuppressWarnings("unchecked") |
| 134 |
Supplier<T> supplier = (Supplier<T>) source; |
| 135 |
SingleSource<? extends R> cs = null; |
| 136 |
try { |
| 137 |
T item = supplier.get(); |
| 138 |
if (item != null) { |
| 139 |
cs = Objects.requireNonNull(mapper.apply(item), "The mapper returned a null SingleSource"); |
| 140 |
} |
| 141 |
} catch (Throwable ex) { |
| 142 |
Exceptions.throwIfFatal(ex); |
| 143 |
EmptyDisposable.error(ex, observer); |
| 144 |
return true; |
| 145 |
} |
| 146 |
|
| 147 |
if (cs == null) { |
| 148 |
EmptyDisposable.complete(observer); |
| 149 |
} else { |
| 150 |
cs.subscribe(SingleToObservable.create(observer)); |
| 151 |
} |
| 152 |
return true; |
| 153 |
} |
| 154 |
return false; |
| 155 |
} |
| 156 |
} |