| 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 io.reactivex.rxjava3.annotations.Nullable; |
| 17 |
import io.reactivex.rxjava3.core.*; |
| 18 |
import io.reactivex.rxjava3.functions.Function; |
| 19 |
import io.reactivex.rxjava3.internal.observers.BasicFuseableObserver; |
| 20 |
|
| 21 |
import java.util.Objects; |
| 22 |
|
| 23 |
public final class ObservableMap<T, U> extends AbstractObservableWithUpstream<T, U> { |
| 24 |
final Function<? super T, ? extends U> function; |
| 25 |
|
| 26 |
public ObservableMap(ObservableSource<T> source, Function<? super T, ? extends U> function) { |
| 27 |
super(source); |
| 28 |
this.function = function; |
| 29 |
} |
| 30 |
|
| 31 |
@Override |
| 32 |
public void subscribeActual(Observer<? super U> t) { |
| 33 |
source.subscribe(new MapObserver<T, U>(t, function)); |
| 34 |
} |
| 35 |
|
| 36 |
static final class MapObserver<T, U> extends BasicFuseableObserver<T, U> { |
| 37 |
final Function<? super T, ? extends U> mapper; |
| 38 |
|
| 39 |
MapObserver(Observer<? super U> actual, Function<? super T, ? extends U> mapper) { |
| 40 |
super(actual); |
| 41 |
this.mapper = mapper; |
| 42 |
} |
| 43 |
|
| 44 |
@Override |
| 45 |
public void onNext(T t) { |
| 46 |
if (done) { |
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
if (sourceMode != NONE) { |
| 51 |
downstream.onNext(null); |
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
U v; |
| 56 |
|
| 57 |
try { |
| 58 |
v = Objects.requireNonNull(mapper.apply(t), "The mapper function returned a null value."); |
| 59 |
} catch (Throwable ex) { |
| 60 |
fail(ex); |
| 61 |
return; |
| 62 |
} |
| 63 |
downstream.onNext(v); |
| 64 |
} |
| 65 |
|
| 66 |
@Override |
| 67 |
public int requestFusion(int mode) { |
| 68 |
return transitiveBoundaryFusion(mode); |
| 69 |
} |
| 70 |
|
| 71 |
@Nullable |
| 72 |
@Override |
| 73 |
public U poll() throws Throwable { |
| 74 |
T t = qd.poll(); |
| 75 |
return t != null ? Objects.requireNonNull(mapper.apply(t), "The mapper function returned a null value.") : null; |
| 76 |
} |
| 77 |
} |
| 78 |
} |