Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableConcatWithCompletable.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.*;
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.subscriptions.SubscriptionHelper;
24
25 /**
26 * Subscribe to a main Flowable first, then when it completes normally, subscribe to a Completable
27 * and terminate when it terminates.
28 * <p>History: 2.1.10 - experimental
29 * @param <T> the element type of the main source and output type
30 * @since 2.2
31 */
32 public final class FlowableConcatWithCompletable<T> extends AbstractFlowableWithUpstream<T, T> {
33
34 final CompletableSource other;
35
36 public FlowableConcatWithCompletable(Flowable<T> source, CompletableSource other) {
37 super(source);
38 this.other = other;
39 }
40
41 @Override
42 protected void subscribeActual(Subscriber<? super T> s) {
43 source.subscribe(new ConcatWithSubscriber<>(s, other));
44 }
45
46 static final class ConcatWithSubscriber<T>
47 extends AtomicReference<Disposable>
48 implements FlowableSubscriber<T>, CompletableObserver, Subscription {
49
50 private static final long serialVersionUID = -7346385463600070225L;
51
52 final Subscriber<? super T> downstream;
53
54 Subscription upstream;
55
56 CompletableSource other;
57
58 boolean inCompletable;
59
60 ConcatWithSubscriber(Subscriber<? super T> actual, CompletableSource other) {
61 this.downstream = actual;
62 this.other = other;
63 }
64
65 @Override
66 public void onSubscribe(Subscription s) {
67 if (SubscriptionHelper.validate(upstream, s)) {
68 this.upstream = s;
69 downstream.onSubscribe(this);
70 }
71 }
72
73 @Override
74 public void onSubscribe(Disposable d) {
75 DisposableHelper.setOnce(this, d);
76 }
77
78 @Override
79 public void onNext(T t) {
80 downstream.onNext(t);
81 }
82
83 @Override
84 public void onError(Throwable t) {
85 downstream.onError(t);
86 }
87
88 @Override
89 public void onComplete() {
90 if (inCompletable) {
91 downstream.onComplete();
92 } else {
93 inCompletable = true;
94 upstream = SubscriptionHelper.CANCELLED;
95 CompletableSource cs = other;
96 other = null;
97 cs.subscribe(this);
98 }
99 }
100
101 @Override
102 public void request(long n) {
103 upstream.request(n);
104 }
105
106 @Override
107 public void cancel() {
108 upstream.cancel();
109 DisposableHelper.dispose(this);
110 }
111 }
112 }