| 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.observable; |
| 15 |
|
| 16 |
import java.util.Objects; |
| 17 |
import java.util.concurrent.atomic.AtomicInteger; |
| 18 |
|
| 19 |
import io.reactivex.rxjava3.annotations.Nullable; |
| 20 |
import io.reactivex.rxjava3.core.*; |
| 21 |
import io.reactivex.rxjava3.exceptions.Exceptions; |
| 22 |
import io.reactivex.rxjava3.functions.*; |
| 23 |
import io.reactivex.rxjava3.internal.disposables.EmptyDisposable; |
| 24 |
import io.reactivex.rxjava3.operators.QueueDisposable; |
| 25 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 26 |
|
| 27 |
/** |
| 28 |
* Utility classes to work with scalar-sourced XMap operators (where X == { flat, concat, switch }). |
| 29 |
*/ |
| 30 |
public final class ObservableScalarXMap { |
| 31 |
|
| 32 |
/** Utility class. */ |
| 33 |
private ObservableScalarXMap() { |
| 34 |
throw new IllegalStateException("No instances!"); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Tries to subscribe to a possibly Supplier source's mapped ObservableSource. |
| 39 |
* @param <T> the input value type |
| 40 |
* @param <R> the output value type |
| 41 |
* @param source the source ObservableSource |
| 42 |
* @param observer the subscriber |
| 43 |
* @param mapper the function mapping a scalar value into an ObservableSource |
| 44 |
* @return true if successful, false if the caller should continue with the regular path. |
| 45 |
*/ |
| 46 |
@SuppressWarnings("unchecked") |
| 47 |
public static <T, R> boolean tryScalarXMapSubscribe(ObservableSource<T> source, |
| 48 |
Observer<? super R> observer, |
| 49 |
Function<? super T, ? extends ObservableSource<? extends R>> mapper) { |
| 50 |
if (source instanceof Supplier) { |
| 51 |
T t; |
| 52 |
|
| 53 |
try { |
| 54 |
t = ((Supplier<T>)source).get(); |
| 55 |
} catch (Throwable ex) { |
| 56 |
Exceptions.throwIfFatal(ex); |
| 57 |
EmptyDisposable.error(ex, observer); |
| 58 |
return true; |
| 59 |
} |
| 60 |
|
| 61 |
if (t == null) { |
| 62 |
EmptyDisposable.complete(observer); |
| 63 |
return true; |
| 64 |
} |
| 65 |
|
| 66 |
ObservableSource<? extends R> r; |
| 67 |
|
| 68 |
try { |
| 69 |
r = Objects.requireNonNull(mapper.apply(t), "The mapper returned a null ObservableSource"); |
| 70 |
} catch (Throwable ex) { |
| 71 |
Exceptions.throwIfFatal(ex); |
| 72 |
EmptyDisposable.error(ex, observer); |
| 73 |
return true; |
| 74 |
} |
| 75 |
|
| 76 |
if (r instanceof Supplier) { |
| 77 |
R u; |
| 78 |
|
| 79 |
try { |
| 80 |
u = ((Supplier<R>)r).get(); |
| 81 |
} catch (Throwable ex) { |
| 82 |
Exceptions.throwIfFatal(ex); |
| 83 |
EmptyDisposable.error(ex, observer); |
| 84 |
return true; |
| 85 |
} |
| 86 |
|
| 87 |
if (u == null) { |
| 88 |
EmptyDisposable.complete(observer); |
| 89 |
return true; |
| 90 |
} |
| 91 |
ScalarDisposable<R> sd = new ScalarDisposable<>(observer, u); |
| 92 |
observer.onSubscribe(sd); |
| 93 |
sd.run(); |
| 94 |
} else { |
| 95 |
r.subscribe(observer); |
| 96 |
} |
| 97 |
|
| 98 |
return true; |
| 99 |
} |
| 100 |
return false; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Maps a scalar value into an Observable and emits its values. |
| 105 |
* |
| 106 |
* @param <T> the scalar value type |
| 107 |
* @param <U> the output value type |
| 108 |
* @param value the scalar value to map |
| 109 |
* @param mapper the function that gets the scalar value and should return |
| 110 |
* an ObservableSource that gets streamed |
| 111 |
* @return the new Observable instance |
| 112 |
*/ |
| 113 |
public static <T, U> Observable<U> scalarXMap(T value, |
| 114 |
Function<? super T, ? extends ObservableSource<? extends U>> mapper) { |
| 115 |
return RxJavaPlugins.onAssembly(new ScalarXMapObservable<>(value, mapper)); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Maps a scalar value to an ObservableSource and subscribes to it. |
| 120 |
* |
| 121 |
* @param <T> the scalar value type |
| 122 |
* @param <R> the mapped ObservableSource's element type. |
| 123 |
*/ |
| 124 |
static final class ScalarXMapObservable<T, R> extends Observable<R> { |
| 125 |
|
| 126 |
final T value; |
| 127 |
|
| 128 |
final Function<? super T, ? extends ObservableSource<? extends R>> mapper; |
| 129 |
|
| 130 |
ScalarXMapObservable(T value, |
| 131 |
Function<? super T, ? extends ObservableSource<? extends R>> mapper) { |
| 132 |
this.value = value; |
| 133 |
this.mapper = mapper; |
| 134 |
} |
| 135 |
|
| 136 |
@SuppressWarnings("unchecked") |
| 137 |
@Override |
| 138 |
public void subscribeActual(Observer<? super R> observer) { |
| 139 |
ObservableSource<? extends R> other; |
| 140 |
try { |
| 141 |
other = Objects.requireNonNull(mapper.apply(value), "The mapper returned a null ObservableSource"); |
| 142 |
} catch (Throwable e) { |
| 143 |
Exceptions.throwIfFatal(e); |
| 144 |
EmptyDisposable.error(e, observer); |
| 145 |
return; |
| 146 |
} |
| 147 |
if (other instanceof Supplier) { |
| 148 |
R u; |
| 149 |
|
| 150 |
try { |
| 151 |
u = ((Supplier<R>)other).get(); |
| 152 |
} catch (Throwable ex) { |
| 153 |
Exceptions.throwIfFatal(ex); |
| 154 |
EmptyDisposable.error(ex, observer); |
| 155 |
return; |
| 156 |
} |
| 157 |
|
| 158 |
if (u == null) { |
| 159 |
EmptyDisposable.complete(observer); |
| 160 |
return; |
| 161 |
} |
| 162 |
ScalarDisposable<R> sd = new ScalarDisposable<>(observer, u); |
| 163 |
observer.onSubscribe(sd); |
| 164 |
sd.run(); |
| 165 |
} else { |
| 166 |
other.subscribe(observer); |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Represents a Disposable that signals one onNext followed by an onComplete. |
| 173 |
* |
| 174 |
* @param <T> the value type |
| 175 |
*/ |
| 176 |
public static final class ScalarDisposable<T> |
| 177 |
extends AtomicInteger |
| 178 |
implements QueueDisposable<T>, Runnable { |
| 179 |
|
| 180 |
private static final long serialVersionUID = 3880992722410194083L; |
| 181 |
|
| 182 |
final Observer<? super T> observer; |
| 183 |
|
| 184 |
final T value; |
| 185 |
|
| 186 |
static final int START = 0; |
| 187 |
static final int FUSED = 1; |
| 188 |
static final int ON_NEXT = 2; |
| 189 |
static final int ON_COMPLETE = 3; |
| 190 |
|
| 191 |
public ScalarDisposable(Observer<? super T> observer, T value) { |
| 192 |
this.observer = observer; |
| 193 |
this.value = value; |
| 194 |
} |
| 195 |
|
| 196 |
@Override |
| 197 |
public boolean offer(T value) { |
| 198 |
throw new UnsupportedOperationException("Should not be called!"); |
| 199 |
} |
| 200 |
|
| 201 |
@Override |
| 202 |
public boolean offer(T v1, T v2) { |
| 203 |
throw new UnsupportedOperationException("Should not be called!"); |
| 204 |
} |
| 205 |
|
| 206 |
@Nullable |
| 207 |
@Override |
| 208 |
public T poll() { |
| 209 |
if (get() == FUSED) { |
| 210 |
lazySet(ON_COMPLETE); |
| 211 |
return value; |
| 212 |
} |
| 213 |
return null; |
| 214 |
} |
| 215 |
|
| 216 |
@Override |
| 217 |
public boolean isEmpty() { |
| 218 |
return get() != FUSED; |
| 219 |
} |
| 220 |
|
| 221 |
@Override |
| 222 |
public void clear() { |
| 223 |
lazySet(ON_COMPLETE); |
| 224 |
} |
| 225 |
|
| 226 |
@Override |
| 227 |
public void dispose() { |
| 228 |
set(ON_COMPLETE); |
| 229 |
} |
| 230 |
|
| 231 |
@Override |
| 232 |
public boolean isDisposed() { |
| 233 |
return get() == ON_COMPLETE; |
| 234 |
} |
| 235 |
|
| 236 |
@Override |
| 237 |
public int requestFusion(int mode) { |
| 238 |
if ((mode & SYNC) != 0) { |
| 239 |
lazySet(FUSED); |
| 240 |
return SYNC; |
| 241 |
} |
| 242 |
return NONE; |
| 243 |
} |
| 244 |
|
| 245 |
@Override |
| 246 |
public void run() { |
| 247 |
if (get() == START && compareAndSet(START, ON_NEXT)) { |
| 248 |
observer.onNext(value); |
| 249 |
if (get() == ON_NEXT) { |
| 250 |
lazySet(ON_COMPLETE); |
| 251 |
observer.onComplete(); |
| 252 |
} |
| 253 |
} |
| 254 |
} |
| 255 |
} |
| 256 |
} |