| 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.observers.SerializedObserver; |
| 22 |
|
| 23 |
public final class ObservableSampleWithObservable<T> extends AbstractObservableWithUpstream<T, T> { |
| 24 |
|
| 25 |
final ObservableSource<?> other; |
| 26 |
|
| 27 |
final boolean emitLast; |
| 28 |
|
| 29 |
public ObservableSampleWithObservable(ObservableSource<T> source, ObservableSource<?> other, boolean emitLast) { |
| 30 |
super(source); |
| 31 |
this.other = other; |
| 32 |
this.emitLast = emitLast; |
| 33 |
} |
| 34 |
|
| 35 |
@Override |
| 36 |
public void subscribeActual(Observer<? super T> t) { |
| 37 |
SerializedObserver<T> serial = new SerializedObserver<>(t); |
| 38 |
if (emitLast) { |
| 39 |
source.subscribe(new SampleMainEmitLast<>(serial, other)); |
| 40 |
} else { |
| 41 |
source.subscribe(new SampleMainNoLast<>(serial, other)); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
abstract static class SampleMainObserver<T> extends AtomicReference<T> |
| 46 |
implements Observer<T>, Disposable { |
| 47 |
|
| 48 |
private static final long serialVersionUID = -3517602651313910099L; |
| 49 |
|
| 50 |
final Observer<? super T> downstream; |
| 51 |
final ObservableSource<?> sampler; |
| 52 |
|
| 53 |
final AtomicReference<Disposable> other = new AtomicReference<>(); |
| 54 |
|
| 55 |
Disposable upstream; |
| 56 |
|
| 57 |
SampleMainObserver(Observer<? super T> actual, ObservableSource<?> other) { |
| 58 |
this.downstream = actual; |
| 59 |
this.sampler = other; |
| 60 |
} |
| 61 |
|
| 62 |
@Override |
| 63 |
public void onSubscribe(Disposable d) { |
| 64 |
if (DisposableHelper.validate(this.upstream, d)) { |
| 65 |
this.upstream = d; |
| 66 |
downstream.onSubscribe(this); |
| 67 |
if (other.get() == null) { |
| 68 |
sampler.subscribe(new SamplerObserver<>(this)); |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
@Override |
| 74 |
public void onNext(T t) { |
| 75 |
lazySet(t); |
| 76 |
} |
| 77 |
|
| 78 |
@Override |
| 79 |
public void onError(Throwable t) { |
| 80 |
DisposableHelper.dispose(other); |
| 81 |
downstream.onError(t); |
| 82 |
} |
| 83 |
|
| 84 |
@Override |
| 85 |
public void onComplete() { |
| 86 |
DisposableHelper.dispose(other); |
| 87 |
completion(); |
| 88 |
} |
| 89 |
|
| 90 |
boolean setOther(Disposable o) { |
| 91 |
return DisposableHelper.setOnce(other, o); |
| 92 |
} |
| 93 |
|
| 94 |
@Override |
| 95 |
public void dispose() { |
| 96 |
DisposableHelper.dispose(other); |
| 97 |
upstream.dispose(); |
| 98 |
} |
| 99 |
|
| 100 |
@Override |
| 101 |
public boolean isDisposed() { |
| 102 |
return other.get() == DisposableHelper.DISPOSED; |
| 103 |
} |
| 104 |
|
| 105 |
public void error(Throwable e) { |
| 106 |
upstream.dispose(); |
| 107 |
downstream.onError(e); |
| 108 |
} |
| 109 |
|
| 110 |
public void complete() { |
| 111 |
upstream.dispose(); |
| 112 |
completion(); |
| 113 |
} |
| 114 |
|
| 115 |
void emit() { |
| 116 |
T value = getAndSet(null); |
| 117 |
if (value != null) { |
| 118 |
downstream.onNext(value); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
abstract void completion(); |
| 123 |
|
| 124 |
abstract void run(); |
| 125 |
} |
| 126 |
|
| 127 |
static final class SamplerObserver<T> implements Observer<Object> { |
| 128 |
final SampleMainObserver<T> parent; |
| 129 |
SamplerObserver(SampleMainObserver<T> parent) { |
| 130 |
this.parent = parent; |
| 131 |
|
| 132 |
} |
| 133 |
|
| 134 |
@Override |
| 135 |
public void onSubscribe(Disposable d) { |
| 136 |
parent.setOther(d); |
| 137 |
} |
| 138 |
|
| 139 |
@Override |
| 140 |
public void onNext(Object t) { |
| 141 |
parent.run(); |
| 142 |
} |
| 143 |
|
| 144 |
@Override |
| 145 |
public void onError(Throwable t) { |
| 146 |
parent.error(t); |
| 147 |
} |
| 148 |
|
| 149 |
@Override |
| 150 |
public void onComplete() { |
| 151 |
parent.complete(); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
static final class SampleMainNoLast<T> extends SampleMainObserver<T> { |
| 156 |
|
| 157 |
private static final long serialVersionUID = -3029755663834015785L; |
| 158 |
|
| 159 |
SampleMainNoLast(Observer<? super T> actual, ObservableSource<?> other) { |
| 160 |
super(actual, other); |
| 161 |
} |
| 162 |
|
| 163 |
@Override |
| 164 |
void completion() { |
| 165 |
downstream.onComplete(); |
| 166 |
} |
| 167 |
|
| 168 |
@Override |
| 169 |
void run() { |
| 170 |
emit(); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
static final class SampleMainEmitLast<T> extends SampleMainObserver<T> { |
| 175 |
|
| 176 |
private static final long serialVersionUID = -3029755663834015785L; |
| 177 |
|
| 178 |
final AtomicInteger wip; |
| 179 |
|
| 180 |
volatile boolean done; |
| 181 |
|
| 182 |
SampleMainEmitLast(Observer<? super T> actual, ObservableSource<?> other) { |
| 183 |
super(actual, other); |
| 184 |
this.wip = new AtomicInteger(); |
| 185 |
} |
| 186 |
|
| 187 |
@Override |
| 188 |
void completion() { |
| 189 |
done = true; |
| 190 |
if (wip.getAndIncrement() == 0) { |
| 191 |
emit(); |
| 192 |
downstream.onComplete(); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
@Override |
| 197 |
void run() { |
| 198 |
if (wip.getAndIncrement() == 0) { |
| 199 |
do { |
| 200 |
boolean d = done; |
| 201 |
emit(); |
| 202 |
if (d) { |
| 203 |
downstream.onComplete(); |
| 204 |
return; |
| 205 |
} |
| 206 |
} while (wip.decrementAndGet() != 0); |
| 207 |
} |
| 208 |
} |
| 209 |
} |
| 210 |
} |