| 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.jdk8; |
| 15 |
|
| 16 |
import java.util.*; |
| 17 |
|
| 18 |
import io.reactivex.rxjava3.core.Observable; |
| 19 |
import io.reactivex.rxjava3.core.Observer; |
| 20 |
import io.reactivex.rxjava3.functions.Function; |
| 21 |
import io.reactivex.rxjava3.internal.observers.BasicFuseableObserver; |
| 22 |
|
| 23 |
/** |
| 24 |
* Map the upstream values into an Optional and emit its value if any. |
| 25 |
* @param <T> the upstream element type |
| 26 |
* @param <R> the output element type |
| 27 |
* @since 3.0.0 |
| 28 |
*/ |
| 29 |
public final class ObservableMapOptional<T, R> extends Observable<R> { |
| 30 |
|
| 31 |
final Observable<T> source; |
| 32 |
|
| 33 |
final Function<? super T, Optional<? extends R>> mapper; |
| 34 |
|
| 35 |
public ObservableMapOptional(Observable<T> source, Function<? super T, Optional<? extends R>> mapper) { |
| 36 |
this.source = source; |
| 37 |
this.mapper = mapper; |
| 38 |
} |
| 39 |
|
| 40 |
@Override |
| 41 |
protected void subscribeActual(Observer<? super R> observer) { |
| 42 |
source.subscribe(new MapOptionalObserver<>(observer, mapper)); |
| 43 |
} |
| 44 |
|
| 45 |
static final class MapOptionalObserver<T, R> extends BasicFuseableObserver<T, R> { |
| 46 |
|
| 47 |
final Function<? super T, Optional<? extends R>> mapper; |
| 48 |
|
| 49 |
MapOptionalObserver(Observer<? super R> downstream, Function<? super T, Optional<? extends R>> mapper) { |
| 50 |
super(downstream); |
| 51 |
this.mapper = mapper; |
| 52 |
} |
| 53 |
|
| 54 |
@Override |
| 55 |
public void onNext(T t) { |
| 56 |
if (done) { |
| 57 |
return; |
| 58 |
} |
| 59 |
|
| 60 |
if (sourceMode != NONE) { |
| 61 |
downstream.onNext(null); |
| 62 |
return; |
| 63 |
} |
| 64 |
|
| 65 |
Optional<? extends R> result; |
| 66 |
try { |
| 67 |
result = Objects.requireNonNull(mapper.apply(t), "The mapper returned a null Optional"); |
| 68 |
} catch (Throwable ex) { |
| 69 |
fail(ex); |
| 70 |
return; |
| 71 |
} |
| 72 |
|
| 73 |
if (result.isPresent()) { |
| 74 |
downstream.onNext(result.get()); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
@Override |
| 79 |
public int requestFusion(int mode) { |
| 80 |
return transitiveBoundaryFusion(mode); |
| 81 |
} |
| 82 |
|
| 83 |
@Override |
| 84 |
public R poll() throws Throwable { |
| 85 |
for (;;) { |
| 86 |
T item = qd.poll(); |
| 87 |
if (item == null) { |
| 88 |
return null; |
| 89 |
} |
| 90 |
Optional<? extends R> result = Objects.requireNonNull(mapper.apply(item), "The mapper returned a null Optional"); |
| 91 |
if (result.isPresent()) { |
| 92 |
return result.get(); |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
} |