|
| 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 | +package io.reactivex.rxjava3.internal.jdk8; |
| 14 | + |
| 15 | +import java.util.concurrent.CompletionStage; |
| 16 | +import java.util.concurrent.atomic.AtomicReference; |
| 17 | +import java.util.function.BiConsumer; |
| 18 | + |
| 19 | +import org.reactivestreams.Subscriber; |
| 20 | + |
| 21 | +import io.reactivex.rxjava3.core.Flowable; |
| 22 | +import io.reactivex.rxjava3.internal.subscriptions.DeferredScalarSubscription; |
| 23 | + |
| 24 | +/** |
| 25 | + * Wrap a CompletionStage and signal its outcome. |
| 26 | + * @param <T> the element type |
| 27 | + * @since 3.0.0 |
| 28 | + */ |
| 29 | +public final class FlowableFromCompletionStage<T> extends Flowable<T> { |
| 30 | + |
| 31 | + final CompletionStage<T> stage; |
| 32 | + |
| 33 | + public FlowableFromCompletionStage(CompletionStage<T> stage) { |
| 34 | + this.stage = stage; |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + protected void subscribeActual(Subscriber<? super T> s) { |
| 39 | + // We need an indirection because one can't detach from a whenComplete |
| 40 | + // and cancellation should not hold onto the stage. |
| 41 | + BiConsumerAtomicReference<T> whenReference = new BiConsumerAtomicReference<>(); |
| 42 | + CompletionStageHandler<T> handler = new CompletionStageHandler<>(s, whenReference); |
| 43 | + whenReference.lazySet(handler); |
| 44 | + |
| 45 | + s.onSubscribe(handler); |
| 46 | + stage.whenComplete(whenReference); |
| 47 | + } |
| 48 | + |
| 49 | + static final class CompletionStageHandler<T> |
| 50 | + extends DeferredScalarSubscription<T> |
| 51 | + implements BiConsumer<T, Throwable> { |
| 52 | + |
| 53 | + private static final long serialVersionUID = 4665335664328839859L; |
| 54 | + |
| 55 | + final BiConsumerAtomicReference<T> whenReference; |
| 56 | + |
| 57 | + CompletionStageHandler(Subscriber<? super T> downstream, BiConsumerAtomicReference<T> whenReference) { |
| 58 | + super(downstream); |
| 59 | + this.whenReference = whenReference; |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public void accept(T item, Throwable error) { |
| 64 | + if (error != null) { |
| 65 | + downstream.onError(error); |
| 66 | + } |
| 67 | + else if (item != null) { |
| 68 | + complete(item); |
| 69 | + } else { |
| 70 | + downstream.onError(new NullPointerException("The CompletionStage terminated with null.")); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public void cancel() { |
| 76 | + super.cancel(); |
| 77 | + whenReference.set(null); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + static final class BiConsumerAtomicReference<T> extends AtomicReference<BiConsumer<T, Throwable>> |
| 82 | + implements BiConsumer<T, Throwable> { |
| 83 | + |
| 84 | + private static final long serialVersionUID = 45838553147237545L; |
| 85 | + |
| 86 | + @Override |
| 87 | + public void accept(T t, Throwable u) { |
| 88 | + BiConsumer<T, Throwable> biConsumer = get(); |
| 89 | + if (biConsumer != null) { |
| 90 | + biConsumer.accept(t, u); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments