Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/observable/ObservableMergeWithCompletable.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.*;
17
18 import io.reactivex.rxjava3.core.*;
19 import io.reactivex.rxjava3.disposables.Disposable;
20 import io.reactivex.rxjava3.internal.disposables.DisposableHelper;
21 import io.reactivex.rxjava3.internal.util.*;
22
23 /**
24 * Merges an Observable and a Completable by emitting the items of the Observable and waiting until
25 * both the Observable and Completable complete normally.
26 * <p>History: 2.1.10 - experimental
27 * @param <T> the element type of the Observable
28 * @since 2.2
29 */
30 public final class ObservableMergeWithCompletable<T> extends AbstractObservableWithUpstream<T, T> {
31
32 final CompletableSource other;
33
34 public ObservableMergeWithCompletable(Observable<T> source, CompletableSource other) {
35 super(source);
36 this.other = other;
37 }
38
39 @Override
40 protected void subscribeActual(Observer<? super T> observer) {
41 MergeWithObserver<T> parent = new MergeWithObserver<>(observer);
42 observer.onSubscribe(parent);
43 source.subscribe(parent);
44 other.subscribe(parent.otherObserver);
45 }
46
47 static final class MergeWithObserver<T> extends AtomicInteger
48 implements Observer<T>, Disposable {
49
50 private static final long serialVersionUID = -4592979584110982903L;
51
52 final Observer<? super T> downstream;
53
54 final AtomicReference<Disposable> mainDisposable;
55
56 final OtherObserver otherObserver;
57
58 final AtomicThrowable errors;
59
60 volatile boolean mainDone;
61
62 volatile boolean otherDone;
63
64 MergeWithObserver(Observer<? super T> downstream) {
65 this.downstream = downstream;
66 this.mainDisposable = new AtomicReference<>();
67 this.otherObserver = new OtherObserver(this);
68 this.errors = new AtomicThrowable();
69 }
70
71 @Override
72 public void onSubscribe(Disposable d) {
73 DisposableHelper.setOnce(mainDisposable, d);
74 }
75
76 @Override
77 public void onNext(T t) {
78 HalfSerializer.onNext(downstream, t, this, errors);
79 }
80
81 @Override
82 public void onError(Throwable ex) {
83 DisposableHelper.dispose(otherObserver);
84 HalfSerializer.onError(downstream, ex, this, errors);
85 }
86
87 @Override
88 public void onComplete() {
89 mainDone = true;
90 if (otherDone) {
91 HalfSerializer.onComplete(downstream, this, errors);
92 }
93 }
94
95 @Override
96 public boolean isDisposed() {
97 return DisposableHelper.isDisposed(mainDisposable.get());
98 }
99
100 @Override
101 public void dispose() {
102 DisposableHelper.dispose(mainDisposable);
103 DisposableHelper.dispose(otherObserver);
104 errors.tryTerminateAndReport();
105 }
106
107 void otherError(Throwable ex) {
108 DisposableHelper.dispose(mainDisposable);
109 HalfSerializer.onError(downstream, ex, this, errors);
110 }
111
112 void otherComplete() {
113 otherDone = true;
114 if (mainDone) {
115 HalfSerializer.onComplete(downstream, this, errors);
116 }
117 }
118
119 static final class OtherObserver extends AtomicReference<Disposable>
120 implements CompletableObserver {
121
122 private static final long serialVersionUID = -2935427570954647017L;
123
124 final MergeWithObserver<?> parent;
125
126 OtherObserver(MergeWithObserver<?> parent) {
127 this.parent = parent;
128 }
129
130 @Override
131 public void onSubscribe(Disposable d) {
132 DisposableHelper.setOnce(this, d);
133 }
134
135 @Override
136 public void onError(Throwable e) {
137 parent.otherError(e);
138 }
139
140 @Override
141 public void onComplete() {
142 parent.otherComplete();
143 }
144 }
145 }
146 }