Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableConcatWithMaybe.java

raw
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.flowable;
15
16 import java.util.concurrent.atomic.AtomicReference;
17
18 import org.reactivestreams.Subscriber;
19
20 import io.reactivex.rxjava3.core.*;
21 import io.reactivex.rxjava3.disposables.Disposable;
22 import io.reactivex.rxjava3.internal.disposables.DisposableHelper;
23 import io.reactivex.rxjava3.internal.subscribers.SinglePostCompleteSubscriber;
24 import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper;
25
26 /**
27 * Subscribe to a main Flowable first, then when it completes normally, subscribe to a Maybe,
28 * signal its success value followed by a completion or signal its error or completion signal as is.
29 * <p>History: 2.1.10 - experimental
30 * @param <T> the element type of the main source and output type
31 * @since 2.2
32 */
33 public final class FlowableConcatWithMaybe<T> extends AbstractFlowableWithUpstream<T, T> {
34
35 final MaybeSource<? extends T> other;
36
37 public FlowableConcatWithMaybe(Flowable<T> source, MaybeSource<? extends T> other) {
38 super(source);
39 this.other = other;
40 }
41
42 @Override
43 protected void subscribeActual(Subscriber<? super T> s) {
44 source.subscribe(new ConcatWithSubscriber<>(s, other));
45 }
46
47 static final class ConcatWithSubscriber<T>
48 extends SinglePostCompleteSubscriber<T, T>
49 implements MaybeObserver<T> {
50
51 private static final long serialVersionUID = -7346385463600070225L;
52
53 final AtomicReference<Disposable> otherDisposable;
54
55 MaybeSource<? extends T> other;
56
57 boolean inMaybe;
58
59 ConcatWithSubscriber(Subscriber<? super T> actual, MaybeSource<? extends T> other) {
60 super(actual);
61 this.other = other;
62 this.otherDisposable = new AtomicReference<>();
63 }
64
65 @Override
66 public void onSubscribe(Disposable d) {
67 DisposableHelper.setOnce(otherDisposable, d);
68 }
69
70 @Override
71 public void onNext(T t) {
72 produced++;
73 downstream.onNext(t);
74 }
75
76 @Override
77 public void onError(Throwable t) {
78 downstream.onError(t);
79 }
80
81 @Override
82 public void onSuccess(T t) {
83 complete(t);
84 }
85
86 @Override
87 public void onComplete() {
88 if (inMaybe) {
89 downstream.onComplete();
90 } else {
91 inMaybe = true;
92 upstream = SubscriptionHelper.CANCELLED;
93 MaybeSource<? extends T> ms = other;
94 other = null;
95 ms.subscribe(this);
96 }
97 }
98
99 @Override
100 public void cancel() {
101 super.cancel();
102 DisposableHelper.dispose(otherDisposable);
103 }
104 }
105 }