Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/observable/ObservableConcatWithCompletable.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.observable;
15
16 import java.util.concurrent.atomic.AtomicReference;
17
18 import io.reactivex.rxjava3.core.*;
19 import io.reactivex.rxjava3.disposables.Disposable;
20 import io.reactivex.rxjava3.internal.disposables.DisposableHelper;
21
22 /**
23 * Subscribe to a main Observable first, then when it completes normally, subscribe to a Single,
24 * signal its success value followed by a completion or signal its error as is.
25 * <p>History: 2.1.10 - experimental
26 * @param <T> the element type of the main source and output type
27 * @since 2.2
28 */
29 public final class ObservableConcatWithCompletable<T> extends AbstractObservableWithUpstream<T, T> {
30
31 final CompletableSource other;
32
33 public ObservableConcatWithCompletable(Observable<T> source, CompletableSource other) {
34 super(source);
35 this.other = other;
36 }
37
38 @Override
39 protected void subscribeActual(Observer<? super T> observer) {
40 source.subscribe(new ConcatWithObserver<>(observer, other));
41 }
42
43 static final class ConcatWithObserver<T>
44 extends AtomicReference<Disposable>
45 implements Observer<T>, CompletableObserver, Disposable {
46
47 private static final long serialVersionUID = -1953724749712440952L;
48
49 final Observer<? super T> downstream;
50
51 CompletableSource other;
52
53 boolean inCompletable;
54
55 ConcatWithObserver(Observer<? super T> actual, CompletableSource other) {
56 this.downstream = actual;
57 this.other = other;
58 }
59
60 @Override
61 public void onSubscribe(Disposable d) {
62 if (DisposableHelper.setOnce(this, d) && !inCompletable) {
63 downstream.onSubscribe(this);
64 }
65 }
66
67 @Override
68 public void onNext(T t) {
69 downstream.onNext(t);
70 }
71
72 @Override
73 public void onError(Throwable e) {
74 downstream.onError(e);
75 }
76
77 @Override
78 public void onComplete() {
79 if (inCompletable) {
80 downstream.onComplete();
81 } else {
82 inCompletable = true;
83 DisposableHelper.replace(this, null);
84 CompletableSource cs = other;
85 other = null;
86 cs.subscribe(this);
87 }
88 }
89
90 @Override
91 public void dispose() {
92 DisposableHelper.dispose(this);
93 }
94
95 @Override
96 public boolean isDisposed() {
97 return DisposableHelper.isDisposed(get());
98 }
99 }
100 }